From 180be6f4c774e0d4b8bb3f52ceeffcad87de9e41 Mon Sep 17 00:00:00 2001 From: Jonas Drieghe Date: Fri, 8 Oct 2021 14:23:26 +0200 Subject: [PATCH 01/11] Update dependency of htaccess-api-client to ^2.0 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 727b635..9c95f4d 100644 --- a/composer.json +++ b/composer.json @@ -7,7 +7,7 @@ "symfony/console": "^3.0 || ^4.0 || ^5.0", "http-interop/http-factory-guzzle": "^1.0", "php-http/guzzle7-adapter": "^1.0", - "madewithlove/htaccess-api-client": "^1.6", + "madewithlove/htaccess-api-client": "^2.0", "symfony/yaml": "^3.0 || ^4.0 || ^5.0" }, "bin": [ From fc007e05288ed39ea3e342377f1f4980d0ba074c Mon Sep 17 00:00:00 2001 From: Jonas Drieghe Date: Fri, 8 Oct 2021 14:23:57 +0200 Subject: [PATCH 02/11] Implement breaking changes introduced by htaccess-api-client --- src/HtaccessCommand.php | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/src/HtaccessCommand.php b/src/HtaccessCommand.php index f45af1b..7786167 100644 --- a/src/HtaccessCommand.php +++ b/src/HtaccessCommand.php @@ -125,13 +125,7 @@ private function testSingleUrl(string $url, string $htaccess, InputInterface $in if ($input->getOption('share')) { try { - /** @var ?string $referrer */ - $referrer = $input->getOption('referrer'); - /** @var ?string $serverName */ - $serverName = $input->getOption('server-name'); - - $share = $this->htaccessClient->share($url, $htaccess, $referrer, $serverName); - + $share = $this->htaccessClient->share($url, $htaccess, $this->getServerVariables($input)); $io->text('You can share this test run on ' . $share->getShareUrl()); } catch (HtaccessException $exception) { // when sharing failed, just ignore it @@ -186,12 +180,7 @@ private function validateInput(InputInterface $input): void private function test(string $url, string $htaccess, InputInterface $input, ?SymfonyStyle $io = null): HtaccessResult { - /** @var ?string $referrer */ - $referrer = $input->getOption('referrer'); - /** @var ?string $serverName */ - $serverName = $input->getOption('server-name'); - - $result = $this->htaccessClient->test($url, $htaccess, $referrer, $serverName); + $result = $this->htaccessClient->test($url, $htaccess, $this->getServerVariables($input)); if ($io) { $this->tableRenderer->renderHtaccessResult($result, $io); @@ -199,4 +188,18 @@ private function test(string $url, string $htaccess, InputInterface $input, ?Sym return $result; } + + private function getServerVariables(InputInterface $input): ServerVariables + { + $serverVariables = ServerVariables::default(); + if ($referrer = $input->getOption('referrer')) { + $serverVariables = $serverVariables->with(ServerVariable::HTTP_REFERER, $referrer); + } + + if ($serverName = $input->getOption('server-name')) { + $serverVariables = $serverVariables->with(ServerVariable::SERVER_NAME, $serverName); + } + + return $serverVariables; + } } From 3059614f6c201f49d9fe5d69f9fea7b7333580a3 Mon Sep 17 00:00:00 2001 From: Jonas Drieghe Date: Fri, 8 Oct 2021 14:26:30 +0200 Subject: [PATCH 03/11] Drop support for php 7.x --- .github/workflows/ci.yml | 8 ++++---- composer.json | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5f23429..553bf75 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -16,7 +16,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - php-versions: ['7.3', '7.4', '8.0'] + php-versions: ['8.0', '8.1'] prefer-lowest: ['', '--prefer-lowest'] steps: - uses: actions/checkout@master @@ -26,17 +26,17 @@ jobs: coverage: pcov tools: composer:v2 - name: Install dependencies - if: matrix.php-versions != '8.0' + if: matrix.php-versions != '8.1' run: composer update -n --prefer-dist ${{ matrix.prefer-lowest }} - name: Install dependencies - if: matrix.php-versions == '8.0' + if: matrix.php-versions == '8.1' run: composer install -n --prefer-dist --ignore-platform-req=php - name: Run PHPUnit unit tests run: vendor/bin/phpunit --coverage-clover=build/logs/clover.xml - uses: codecov/codecov-action@v1 with: token: ${{ secrets.CODECOV_TOKEN }} - if: matrix.php-versions == '7.4' + if: matrix.php-versions == '8.0' run: name: Run current version diff --git a/composer.json b/composer.json index 9c95f4d..ddb755d 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,7 @@ "description": "CLI interface for the best htaccess tester in the world.", "type": "package", "require": { - "php": "^7.3|^8.0", + "php": "^8.0", "symfony/console": "^3.0 || ^4.0 || ^5.0", "http-interop/http-factory-guzzle": "^1.0", "php-http/guzzle7-adapter": "^1.0", From 582f18e24e4acf42539e2dfe193cf49f970da956 Mon Sep 17 00:00:00 2001 From: Jonas Drieghe Date: Fri, 8 Oct 2021 14:31:18 +0200 Subject: [PATCH 04/11] Use latest version of shivammathur/setup-php in ci pipeline --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 553bf75..a004ab1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -20,7 +20,7 @@ jobs: prefer-lowest: ['', '--prefer-lowest'] steps: - uses: actions/checkout@master - - uses: shivammathur/setup-php@v1 + - uses: shivammathur/setup-php@v2 with: php-version: ${{ matrix.php-versions }} coverage: pcov From 92ce3a7cf13c39fdc0a2b1d6fb9383419b952797 Mon Sep 17 00:00:00 2001 From: Jonas Drieghe Date: Fri, 8 Oct 2021 14:46:39 +0200 Subject: [PATCH 05/11] Add missing tests for server variables --- tests/HtaccessCommandTest.php | 40 +++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/tests/HtaccessCommandTest.php b/tests/HtaccessCommandTest.php index 912b4b4..700b730 100644 --- a/tests/HtaccessCommandTest.php +++ b/tests/HtaccessCommandTest.php @@ -184,4 +184,44 @@ public function it does mark an unsupported line as potentially invalid( $commandTester->getDisplay() ); } + + /** @test */ + public function it supports http referrer(): void + { + file_put_contents( + getcwd() . '/.htaccess', + "RewriteCond %{HTTP_REFERER} https://example.com\nRewriteRule .* /foo" + ); + + $commandTester = new CommandTester($this->command); + $commandTester->execute([ + 'url' => 'http://localhost', + '--referrer' => 'https://example.com' + ]); + + $this->assertStringContainsString( + 'The output url is "http://localhost/foo"', + $commandTester->getDisplay() + ); + } + + /** @test */ + public function it supports server name(): void + { + file_put_contents( + getcwd() . '/.htaccess', + "RewriteCond %{SERVER_NAME} example.com\nRewriteRule .* /foo" + ); + + $commandTester = new CommandTester($this->command); + $commandTester->execute([ + 'url' => 'http://localhost', + '--server-name' => 'example.com' + ]); + + $this->assertStringContainsString( + 'The output url is "http://localhost/foo"', + $commandTester->getDisplay() + ); + } } From 6dd06991e0945c2bafbb87f9958922488b214ca3 Mon Sep 17 00:00:00 2001 From: Jonas Drieghe Date: Fri, 8 Oct 2021 14:48:38 +0200 Subject: [PATCH 06/11] Use promoted, typed properties --- src/HtaccessCommand.php | 20 ++++---------------- tests/HtaccessCommandTest.php | 5 +---- 2 files changed, 5 insertions(+), 20 deletions(-) diff --git a/src/HtaccessCommand.php b/src/HtaccessCommand.php index 7786167..255f6ff 100644 --- a/src/HtaccessCommand.php +++ b/src/HtaccessCommand.php @@ -3,7 +3,6 @@ namespace Madewithlove; use Madewithlove\Htaccess\TableRenderer; -use Madewithlove\HtaccessResult; use RuntimeException; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Exception\RuntimeException as SymfonyRuntimeException; @@ -16,24 +15,13 @@ final class HtaccessCommand extends Command { - /** - * @var HtaccessClient - */ - private $htaccessClient; - - /** - * @var TableRenderer - */ - private $tableRenderer; - protected static $defaultName = 'htaccess'; - public function __construct(HtaccessClient $htaccessClient, TableRenderer $tableRenderer) - { + public function __construct( + private HtaccessClient $htaccessClient, + private TableRenderer $tableRenderer + ) { parent::__construct(self::$defaultName); - - $this->htaccessClient = $htaccessClient; - $this->tableRenderer = $tableRenderer; } protected function configure(): void diff --git a/tests/HtaccessCommandTest.php b/tests/HtaccessCommandTest.php index 700b730..d18cf85 100644 --- a/tests/HtaccessCommandTest.php +++ b/tests/HtaccessCommandTest.php @@ -12,10 +12,7 @@ final class HtaccessCommandTest extends TestCase { - /** - * @var HtaccessCommand - */ - private $command; + private HtaccessCommand $command; public function setUp(): void { From 0c06ddecc0cfd7e303ffb00dfc28f200409083d9 Mon Sep 17 00:00:00 2001 From: Jonas Drieghe Date: Fri, 8 Oct 2021 14:49:13 +0200 Subject: [PATCH 07/11] Also analyse tests folder --- phpstan.neon | 1 + 1 file changed, 1 insertion(+) diff --git a/phpstan.neon b/phpstan.neon index 5aa0822..555c27e 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -3,3 +3,4 @@ parameters: paths: - src - bin + - tests From 010b27ff808deeb3a521fcb3cbe13319255b960c Mon Sep 17 00:00:00 2001 From: Jonas Drieghe Date: Fri, 8 Oct 2021 14:51:30 +0200 Subject: [PATCH 08/11] Remove unused import --- tests/HtaccessCommandTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/HtaccessCommandTest.php b/tests/HtaccessCommandTest.php index d18cf85..8ca0762 100644 --- a/tests/HtaccessCommandTest.php +++ b/tests/HtaccessCommandTest.php @@ -5,7 +5,6 @@ use Http\Adapter\Guzzle7\Client; use Http\Factory\Guzzle\ServerRequestFactory; use Madewithlove\Htaccess\TableRenderer; -use Madewithlove\HtaccessClient; use PHPUnit\Framework\TestCase; use Symfony\Component\Console\Exception\RuntimeException; use Symfony\Component\Console\Tester\CommandTester; From 2a6412b7fbdde19a10ddd393889e460bbb67d1df Mon Sep 17 00:00:00 2001 From: Jonas Drieghe Date: Fri, 8 Oct 2021 14:53:38 +0200 Subject: [PATCH 09/11] Install dependencies before running static analysis --- .github/workflows/ci.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a004ab1..5140f48 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -7,6 +7,12 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@master + - uses: shivammathur/setup-php@v2 + with: + php-version: '8.0' + tools: composer:v2 + - name: Install dependencies + run: composer update -n --prefer-dist ${{ matrix.prefer-lowest }} - name: PHPStan uses: docker://oskarstark/phpstan-ga with: From 3d1d04b19faa0092f577db9f9a6f1898f225c733 Mon Sep 17 00:00:00 2001 From: Jonas Drieghe Date: Fri, 8 Oct 2021 14:55:59 +0200 Subject: [PATCH 10/11] Run phpstan from dev dependencies --- .github/workflows/ci.yml | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5140f48..1f21998 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -12,11 +12,9 @@ jobs: php-version: '8.0' tools: composer:v2 - name: Install dependencies - run: composer update -n --prefer-dist ${{ matrix.prefer-lowest }} + run: composer update -n --prefer-dist - name: PHPStan - uses: docker://oskarstark/phpstan-ga - with: - args: analyse + run: vendor/bin/phpstan analyse phpunit: runs-on: ubuntu-latest From a6ae7a1e7f3c0e6f9b4df218a6ff5f8690d11252 Mon Sep 17 00:00:00 2001 From: Jonas Drieghe Date: Fri, 8 Oct 2021 14:56:23 +0200 Subject: [PATCH 11/11] Composer v2 is the default --- .github/workflows/ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1f21998..9060620 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -10,7 +10,7 @@ jobs: - uses: shivammathur/setup-php@v2 with: php-version: '8.0' - tools: composer:v2 + tools: composer - name: Install dependencies run: composer update -n --prefer-dist - name: PHPStan @@ -28,7 +28,7 @@ jobs: with: php-version: ${{ matrix.php-versions }} coverage: pcov - tools: composer:v2 + tools: composer - name: Install dependencies if: matrix.php-versions != '8.1' run: composer update -n --prefer-dist ${{ matrix.prefer-lowest }}