From 695b35553e8f76efd4cfbd81c9081643af884ef5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Thu, 7 Jan 2021 17:53:06 +0100 Subject: [PATCH 1/4] Clean up acceptance tests using framework examples --- .github/workflows/ci.yml | 47 +++++++++++++++++++++++++++++++++++++--- README.md | 22 ++++++++++++++++--- tests/acceptance.sh | 34 +++++++---------------------- 3 files changed, 71 insertions(+), 32 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 506578d..50134a3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -6,7 +6,8 @@ on: jobs: PHPUnit: - runs-on: ubuntu-latest + name: PHPUnit (PHP ${{ matrix.php }}) + runs-on: ubuntu-20.04 strategy: matrix: php: @@ -17,8 +18,7 @@ jobs: - 7.1 steps: - uses: actions/checkout@v2 - - name: Setup PHP - uses: shivammathur/setup-php@v2 + - uses: shivammathur/setup-php@v2 with: php-version: ${{ matrix.php }} coverage: xdebug @@ -27,4 +27,45 @@ jobs: if: ${{ matrix.php >= 7.3 }} - run: vendor/bin/phpunit --coverage-text -c phpunit.xml.legacy if: ${{ matrix.php < 7.3 }} + + Built-in-webserver: + name: Built-in webserver (PHP ${{ matrix.php }}) + runs-on: ubuntu-20.04 + strategy: + matrix: + php: + - 8.0 + - 7.4 + - 7.3 + - 7.2 + - 7.1 + steps: + - uses: actions/checkout@v2 + - uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + - run: composer install --no-dev + - run: php examples/index.php & + - run: sleep 0.1 + - run: bash tests/acceptance.sh + + PHP-webserver: + name: PHP webserver (PHP ${{ matrix.php }}) + runs-on: ubuntu-20.04 + strategy: + matrix: + php: + - 8.0 + - 7.4 + - 7.3 + - 7.2 + - 7.1 + steps: + - uses: actions/checkout@v2 + - uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + - run: composer install --no-dev + - run: php -S localhost:8080 examples/index.php & + - run: sleep 0.1 - run: bash tests/acceptance.sh diff --git a/README.md b/README.md index 1f0d114..656be8e 100644 --- a/README.md +++ b/README.md @@ -266,7 +266,7 @@ Runs everywhere: * Built-in webserver * Apache & nginx -* Standalone +* PHP's development webserver […] @@ -325,10 +325,26 @@ Runs everywhere: ## Tests -You can run some simple acceptance tests to verify the frameworks works -as expected by running: +To run the test suite, you first need to clone this repo and then install all +dependencies [through Composer](https://getcomposer.org/): ```bash +$ composer install +``` + +To run the test suite, go to the project root and run: + +```bash +$ php vendor/bin/phpunit +``` + +Additionally, you can run some simple acceptance tests to verify the framework +examples work as expected behind your web server. Use your web server of choice +(see [deployment](#deployment)) and execute the tests with the URL to your +installation like this: + +```bash +$ php examples/index.php $ tests/acceptance.sh http://localhost:8080 ``` diff --git a/tests/acceptance.sh b/tests/acceptance.sh index ac33686..a94abdd 100755 --- a/tests/acceptance.sh +++ b/tests/acceptance.sh @@ -9,31 +9,13 @@ match() { (echo ""; echo "Error in test $n: Unable to \"grep $@\" this output:"; echo "$out"; exit 1) || exit 1 } -killall php 2>/dev/null -php examples/index.php >/dev/null & -sleep 0.2 +out=$(curl -v $base/ 2>&1); match "HTTP/.* 200" +out=$(curl -v $base/test 2>&1); match -i "Location: /" +out=$(curl -v $base/invalid 2>&1); match "HTTP/.* 404" +out=$(curl -v $base/ 2>&1 -X POST); match "HTTP/.* 405" +out=$(curl -v $base/users/foo 2>&1); match "HTTP/.* 200" && match "Hello foo!" +out=$(curl -v $base/users 2>&1); match "HTTP/.* 404" +out=$(curl -v $base/users/ 2>&1); match "HTTP/.* 404" +out=$(curl -v $base/users/a/b 2>&1); match "HTTP/.* 404" -out=$(curl -v $base/ 2>&1) && match "HTTP/.* 200" -out=$(curl -v $base/test 2>&1) && match -i "Location: /" -out=$(curl -v $base/invalid 2>&1) && match "HTTP/.* 404" -out=$(curl -v $base/ 2>&1 -X POST) && match "HTTP/.* 405" -out=$(curl -v $base/users/foo 2>&1) && match "HTTP/.* 200" && match "Hello foo!" -out=$(curl -v $base/users 2>&1) && match "HTTP/.* 404" -out=$(curl -v $base/users/ 2>&1) && match "HTTP/.* 404" -out=$(curl -v $base/users/a/b 2>&1) && match "HTTP/.* 404" - -killall php -php -S localhost:8080 examples/index.php >/dev/null 2>&1 & -sleep 0.2 - -out=$(curl -v $base/ 2>&1) && match "HTTP/.* 200" -out=$(curl -v $base/test 2>&1) && match -i "Location: /" -out=$(curl -v $base/invalid 2>&1) && match "HTTP/.* 404" -out=$(curl -v $base/ 2>&1 -X POST) && match "HTTP/.* 405" -out=$(curl -v $base/users/foo 2>&1) && match "HTTP/.* 200" && match "Hello foo!" -out=$(curl -v $base/users 2>&1) && match "HTTP/.* 404" -out=$(curl -v $base/users/ 2>&1) && match "HTTP/.* 404" -out=$(curl -v $base/users/a/b 2>&1) && match "HTTP/.* 404" - -killall php echo "OK ($n)" From 9144faec9dc10ee83f08bc4826bbe9f8ba83e05b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Tue, 2 Feb 2021 13:09:04 +0100 Subject: [PATCH 2/4] Support running behind Apache --- .github/workflows/ci.yml | 21 +++++++++++++++++++++ examples/apache/.htaccess | 5 +++++ examples/apache/index.php | 3 +++ src/App.php | 7 ++++++- 4 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 examples/apache/.htaccess create mode 100644 examples/apache/index.php diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 50134a3..d74380e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -49,6 +49,27 @@ jobs: - run: sleep 0.1 - run: bash tests/acceptance.sh + Apache-webserver: + name: Apache webserver (PHP ${{ matrix.php }}) + runs-on: ubuntu-latest + strategy: + matrix: + php: + - 8.0 + - 7.4 + - 7.3 + - 7.2 + - 7.1 + steps: + - uses: actions/checkout@v2 + - uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + - run: composer install --no-dev + - run: docker run -d -p 80:80 -v "$PWD":/home/frugal php:${{ matrix.php }}-apache sh -c "rmdir /var/www/html;ln -s /home/frugal/examples/apache /var/www/html;ln -s /etc/apache2/mods-available/rewrite.load /etc/apache2/mods-enabled; apache2-foreground" + - run: sleep 0.1 + - run: bash tests/acceptance.sh http://localhost + PHP-webserver: name: PHP webserver (PHP ${{ matrix.php }}) runs-on: ubuntu-20.04 diff --git a/examples/apache/.htaccess b/examples/apache/.htaccess new file mode 100644 index 0000000..13ffc34 --- /dev/null +++ b/examples/apache/.htaccess @@ -0,0 +1,5 @@ +RewriteEngine On + +RewriteCond %{REQUEST_FILENAME} !-d +RewriteCond %{REQUEST_FILENAME} !-f +RewriteRule .* index.php diff --git a/examples/apache/index.php b/examples/apache/index.php new file mode 100644 index 0000000..bc3f8c8 --- /dev/null +++ b/examples/apache/index.php @@ -0,0 +1,3 @@ +log( ($request->getServerParams()['REMOTE_ADDR'] ?? '-') . ' ' . '"' . $request->getMethod() . ' ' . $request->getUri()->getPath() . ' HTTP/' . $request->getProtocolVersion() . '" ' . @@ -331,7 +336,7 @@ private function log(string $message): void if (\PHP_SAPI === 'cli') { echo $log; } else { - fwrite(fopen('/dev/stderr', 'a'), $log); + fwrite(defined('STDERR') ? STDERR : fopen('php://stderr', 'a'), $log); } } From f5ff15147671fdbf0ac458ffb1a1dd767b7ede23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Wed, 3 Feb 2021 08:13:49 +0100 Subject: [PATCH 3/4] Support running behind nginx with PHP-FPM --- .github/workflows/ci.yml | 22 ++++++++++++++++++++++ README.md | 4 +++- examples/nginx/nginx.conf | 15 +++++++++++++++ examples/nginx/public/index.php | 3 +++ 4 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 examples/nginx/nginx.conf create mode 100644 examples/nginx/public/index.php diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d74380e..84387ce 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -49,6 +49,28 @@ jobs: - run: sleep 0.1 - run: bash tests/acceptance.sh + nginx-webserver: + name: nginx + PHP-FPM (PHP ${{ matrix.php }}) + runs-on: ubuntu-latest + strategy: + matrix: + php: + - 8.0 + - 7.4 + - 7.3 + - 7.2 + - 7.1 + steps: + - uses: actions/checkout@v2 + - uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + - run: composer install --no-dev + - run: docker run -d -v "$PWD":/home/frugal php:${{ matrix.php }}-fpm + - run: docker run -d -p 80:80 --link $(docker ps -qn1):php -v "$PWD":/home/frugal -v "$PWD"/examples/nginx/nginx.conf:/etc/nginx/conf.d/default.conf nginx:stable-alpine + - run: sleep 1 + - run: bash tests/acceptance.sh http://localhost + Apache-webserver: name: Apache webserver (PHP ${{ matrix.php }}) runs-on: ubuntu-latest diff --git a/README.md b/README.md index 656be8e..29c1f08 100644 --- a/README.md +++ b/README.md @@ -265,7 +265,9 @@ chapter, […] Runs everywhere: * Built-in webserver -* Apache & nginx +* nginx with PHP-FPM +* Apache with mod_fcgid and PHP-FPM +* Apache with mod_php * PHP's development webserver […] diff --git a/examples/nginx/nginx.conf b/examples/nginx/nginx.conf new file mode 100644 index 0000000..175e7ee --- /dev/null +++ b/examples/nginx/nginx.conf @@ -0,0 +1,15 @@ +server { + root /home/frugal/examples/nginx/public; + index index.php index.html; + + location / { + try_files $uri $uri/ /index.php$is_args$args; + } + + location ~ \.php$ { + fastcgi_pass php:9000; + fastcgi_split_path_info ^(.+\.php)(/.+)$; + include fastcgi_params; + fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; + } +} diff --git a/examples/nginx/public/index.php b/examples/nginx/public/index.php new file mode 100644 index 0000000..d04099f --- /dev/null +++ b/examples/nginx/public/index.php @@ -0,0 +1,3 @@ + Date: Fri, 5 Feb 2021 18:34:51 +0100 Subject: [PATCH 4/4] Improve tests to await port being open instead of relying on timer --- .github/workflows/ci.yml | 8 ++++---- tests/await.sh | 13 +++++++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) create mode 100755 tests/await.sh diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 84387ce..9d4012a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -46,7 +46,7 @@ jobs: php-version: ${{ matrix.php }} - run: composer install --no-dev - run: php examples/index.php & - - run: sleep 0.1 + - run: bash tests/await.sh - run: bash tests/acceptance.sh nginx-webserver: @@ -68,7 +68,7 @@ jobs: - run: composer install --no-dev - run: docker run -d -v "$PWD":/home/frugal php:${{ matrix.php }}-fpm - run: docker run -d -p 80:80 --link $(docker ps -qn1):php -v "$PWD":/home/frugal -v "$PWD"/examples/nginx/nginx.conf:/etc/nginx/conf.d/default.conf nginx:stable-alpine - - run: sleep 1 + - run: bash tests/await.sh http://localhost - run: bash tests/acceptance.sh http://localhost Apache-webserver: @@ -89,7 +89,7 @@ jobs: php-version: ${{ matrix.php }} - run: composer install --no-dev - run: docker run -d -p 80:80 -v "$PWD":/home/frugal php:${{ matrix.php }}-apache sh -c "rmdir /var/www/html;ln -s /home/frugal/examples/apache /var/www/html;ln -s /etc/apache2/mods-available/rewrite.load /etc/apache2/mods-enabled; apache2-foreground" - - run: sleep 0.1 + - run: bash tests/await.sh http://localhost - run: bash tests/acceptance.sh http://localhost PHP-webserver: @@ -110,5 +110,5 @@ jobs: php-version: ${{ matrix.php }} - run: composer install --no-dev - run: php -S localhost:8080 examples/index.php & - - run: sleep 0.1 + - run: bash tests/await.sh - run: bash tests/acceptance.sh diff --git a/tests/await.sh b/tests/await.sh new file mode 100755 index 0000000..4268d88 --- /dev/null +++ b/tests/await.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +base=${1:-http://localhost:8080} + +for i in {1..10} +do + out=$(curl -v -X PROBE $base/ 2>&1) && exit 0 || echo -n . + sleep 0.1 +done + +echo +echo "$out" +exit 1