diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 506578d..9d4012a 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,88 @@ 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: bash tests/await.sh + - 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: bash tests/await.sh http://localhost + - run: bash tests/acceptance.sh http://localhost + + 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: bash tests/await.sh http://localhost + - run: bash tests/acceptance.sh http://localhost + + 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: bash tests/await.sh - run: bash tests/acceptance.sh diff --git a/README.md b/README.md index 1f0d114..29c1f08 100644 --- a/README.md +++ b/README.md @@ -265,8 +265,10 @@ chapter, […] Runs everywhere: * Built-in webserver -* Apache & nginx -* Standalone +* nginx with PHP-FPM +* Apache with mod_fcgid and PHP-FPM +* Apache with mod_php +* PHP's development webserver […] @@ -325,10 +327,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/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); } } 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)" 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