From d14366b45ce5a0734bf9722d7572ca8525063dd1 Mon Sep 17 00:00:00 2001 From: tienvx Date: Thu, 16 Mar 2023 11:24:21 +0700 Subject: [PATCH 01/13] Test other os --- .github/workflows/main.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 7f23762..3444a8f 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -4,12 +4,13 @@ on: [push, pull_request] jobs: run: - runs-on: ubuntu-latest + runs-on: ${{ matrix.operating-system }} strategy: matrix: + operating-system: [ ubuntu-latest, macos-latest, windows-latest ] php-versions: ['8.0', '8.1', '8.2'] dependency-versions: ['lowest', 'highest'] - name: PHP ${{ matrix.php-versions }} with ${{ matrix.dependency-versions }} versions of Composer dependencies + name: PHP ${{ matrix.php-versions }} on ${{ matrix.operating-system }} with ${{ matrix.dependency-versions }} versions of Composer dependencies steps: - name: Setup PHP uses: shivammathur/setup-php@v2 From fe7e9e36da2927bad9d653d640901268142f664e Mon Sep 17 00:00:00 2001 From: tienvx Date: Thu, 16 Mar 2023 11:24:59 +0700 Subject: [PATCH 02/13] Remove old pacts before test --- .github/workflows/main.yml | 2 +- composer.json | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 3444a8f..7c5b43b 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -36,7 +36,7 @@ jobs: run: phpstan analyse src tests example - name: Test & Generate Code Coverage - run: ./vendor/bin/phpunit + run: composer test env: PACT_LOGLEVEL: ${{ vars.PACT_LOGLEVEL }} diff --git a/composer.json b/composer.json index 7e38a6b..ae8ac2c 100644 --- a/composer.json +++ b/composer.json @@ -47,6 +47,12 @@ "url": "https://github.com/tienvx/pact-php-plugin.git" } ], + "scripts": { + "test": [ + "php -r \"array_map('unlink', glob('./example/consumer/tests/Contract/pacts/*.json'));\"", + "phpunit" + ] + }, "extra": { "downloads": { "pact-csv-plugin-metadata": { From 5c1003b58a3a42f35e816e217e58275452a71a4d Mon Sep 17 00:00:00 2001 From: tienvx Date: Thu, 16 Mar 2023 11:25:16 +0700 Subject: [PATCH 03/13] Update pact in broker --- .../broker/pacts/csvConsumer-csvProvider.json | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/example/broker/pacts/csvConsumer-csvProvider.json b/example/broker/pacts/csvConsumer-csvProvider.json index 15cd01e..ce787b9 100644 --- a/example/broker/pacts/csvConsumer-csvProvider.json +++ b/example/broker/pacts/csvConsumer-csvProvider.json @@ -4,12 +4,12 @@ }, "interactions": [ { - "description": "request for a report", + "description": "request for a report.csv", "interactionMarkup": { "markup": "# Data\n\n|Name|100|2000-01-01|\n", "markupType": "COMMON_MARK" }, - "key": "ad7b43bc4dd005f5", + "key": "a565e88863669aab", "pending": false, "pluginConfiguration": { "csv": { @@ -23,7 +23,7 @@ ], "request": { "headers": { - "Content-Type": [ + "Accept": [ "text/csv" ] }, @@ -72,8 +72,8 @@ "combine": "AND", "matchers": [ { - "match": "timestamp", - "timestamp": "yyyy-MM-dd" + "format": "yyyy-MM-dd", + "match": "datetime" } ] } @@ -81,15 +81,13 @@ }, "status": 200 }, - "transport": "http", "type": "Synchronous/HTTP" } ], "metadata": { "pactRust": { - "ffi": "0.3.14", - "mockserver": "0.9.5", - "models": "1.0.0" + "ffi": "0.4.1", + "models": "1.0.4" }, "pactSpecification": { "version": "4.0" From 26b85a7b549a2e2e6d7ebc73ecd697772c6d0f13 Mon Sep 17 00:00:00 2001 From: tienvx Date: Thu, 16 Mar 2023 11:30:32 +0700 Subject: [PATCH 04/13] Fix rate limit exceeded --- .github/workflows/main.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 7c5b43b..4c44486 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -18,6 +18,8 @@ jobs: php-version: ${{ matrix.php-versions }} tools: phpstan, phpcs, php-cs-fixer:3 coverage: pcov + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - name: Checkout uses: actions/checkout@v2 From 87c435ec99a5065ffc274fd8a90631eb9297434f Mon Sep 17 00:00:00 2001 From: tienvx Date: Thu, 16 Mar 2023 11:46:28 +0700 Subject: [PATCH 05/13] Fix cs --- .../consumer/tests/Contract/CsvHttpClientTest.php | 13 ++++++++----- example/provider/public/index.php | 5 +++-- example/provider/tests/Contract/PactVerifyTest.php | 2 +- src/CsvInteractionBuilder.php | 4 ++-- tests/CsvInteractionBuilderTest.php | 11 +++-------- 5 files changed, 17 insertions(+), 18 deletions(-) diff --git a/example/consumer/tests/Contract/CsvHttpClientTest.php b/example/consumer/tests/Contract/CsvHttpClientTest.php index cf6c077..3721e1a 100644 --- a/example/consumer/tests/Contract/CsvHttpClientTest.php +++ b/example/consumer/tests/Contract/CsvHttpClientTest.php @@ -17,7 +17,8 @@ public function testGetCsvFile() $request ->setMethod('GET') ->setPath('/report.csv') - ->addHeader('Accept', 'text/csv'); + ->addHeader('Accept', 'text/csv') + ; $response = new ProviderResponse(); $response @@ -26,15 +27,16 @@ public function testGetCsvFile() 'csvHeaders' => false, 'column:1' => "matching(type,'Name')", 'column:2' => 'matching(number,100)', - 'column:3' => "matching(datetime, 'yyyy-MM-dd','2000-01-01')" + 'column:3' => "matching(datetime, 'yyyy-MM-dd','2000-01-01')", ]) - ->setContentType('text/csv'); + ->setContentType('text/csv') + ; $config = new MockServerConfig(); $config->setConsumer('csvConsumer'); $config->setProvider('csvProvider'); $config->setPactSpecificationVersion('4.0.0'); - $config->setPactDir(__DIR__ . '/pacts'); + $config->setPactDir(__DIR__.'/pacts'); if ($logLevel = \getenv('PACT_LOGLEVEL')) { $config->setLogLevel($logLevel); } @@ -43,7 +45,8 @@ public function testGetCsvFile() ->given('report.csv file exist') ->uponReceiving('request for a report.csv') ->with($request) - ->willRespondWith($response); + ->willRespondWith($response) + ; $service = new CsvHttpClient($config->getBaseUri()); $columns = $service->getCsvFile(); diff --git a/example/provider/public/index.php b/example/provider/public/index.php index de520d0..cec52ff 100644 --- a/example/provider/public/index.php +++ b/example/provider/public/index.php @@ -1,6 +1,6 @@ getBody()->write('Hello world!'); return $response - ->withHeader('Content-Type', 'text/plain'); + ->withHeader('Content-Type', 'text/plain') + ; }); $app->post('/change-state', function (Request $request, Response $response) { diff --git a/example/provider/tests/Contract/PactVerifyTest.php b/example/provider/tests/Contract/PactVerifyTest.php index dc4ff84..56120cb 100644 --- a/example/provider/tests/Contract/PactVerifyTest.php +++ b/example/provider/tests/Contract/PactVerifyTest.php @@ -41,7 +41,7 @@ public function testPactVerifyConsumer() ; $verifier = new Verifier($config); - $verifier->addDirectory(__DIR__ . '/../../../broker/pacts'); + $verifier->addDirectory(__DIR__.'/../../../broker/pacts'); $this->assertTrue($verifier->verify()); } diff --git a/src/CsvInteractionBuilder.php b/src/CsvInteractionBuilder.php index 6e9638b..c5633c8 100644 --- a/src/CsvInteractionBuilder.php +++ b/src/CsvInteractionBuilder.php @@ -19,7 +19,7 @@ public function __construct(MockServerConfigInterface $config) private function setPluginDir(): void { - $pluginDir = __DIR__ . '/../bin/pact-plugins'; - \putenv("PACT_PLUGIN_DIR=$pluginDir"); + $pluginDir = __DIR__.'/../bin/pact-plugins'; + \putenv("PACT_PLUGIN_DIR={$pluginDir}"); } } diff --git a/tests/CsvInteractionBuilderTest.php b/tests/CsvInteractionBuilderTest.php index 983d860..033256f 100644 --- a/tests/CsvInteractionBuilderTest.php +++ b/tests/CsvInteractionBuilderTest.php @@ -2,14 +2,8 @@ namespace Tienvx\PactPhpCsv\Tests; -use PhpPact\Consumer\InteractionBuilder; -use PhpPact\Consumer\Matcher\Matcher; -use PhpPact\Consumer\Model\ConsumerRequest; -use PhpPact\Consumer\Model\ProviderResponse; -use PhpPact\Standalone\Exception\MissingEnvVariableException; use PhpPact\Standalone\MockService\MockServerConfig; use PhpPact\Standalone\MockService\MockServerConfigInterface; -use PhpPact\Standalone\MockService\MockServerEnvConfig; use PHPUnit\Framework\TestCase; use Tienvx\PactPhpCsv\CsvInteractionBuilder; use Tienvx\PactPhpPlugin\Exception\PluginNotSupportedBySpecificationException; @@ -24,7 +18,8 @@ protected function setUp(): void $this->config ->setConsumer('consumer') ->setProvider('provider') - ->setLogLevel('debug'); + ->setLogLevel('debug') + ; } public function testPluginNotSupportedBySpecification(): void @@ -40,6 +35,6 @@ public function testPluginSupportedBySpecification(): void $this->config->setPactSpecificationVersion('4.0.0'); \putenv('PACT_PLUGIN_DIR=/home'); new CsvInteractionBuilder($this->config); - $this->assertSame(realpath(__DIR__ . '/../bin/pact-plugins'), realpath(\getenv('PACT_PLUGIN_DIR'))); + $this->assertSame(realpath(__DIR__.'/../bin/pact-plugins'), realpath(\getenv('PACT_PLUGIN_DIR'))); } } From 636f0219139621a7bd2e876c0dff750c81a384b7 Mon Sep 17 00:00:00 2001 From: tienvx Date: Thu, 16 Mar 2023 11:48:19 +0700 Subject: [PATCH 06/13] No need to test all combinations --- .github/workflows/main.yml | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 4c44486..0d2f353 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -7,9 +7,16 @@ jobs: runs-on: ${{ matrix.operating-system }} strategy: matrix: - operating-system: [ ubuntu-latest, macos-latest, windows-latest ] + operating-system: [ ubuntu-latest ] php-versions: ['8.0', '8.1', '8.2'] - dependency-versions: ['lowest', 'highest'] + dependency-versions: ['lowest', 'locked'] + include: + - operating-system: windows-latest + php-versions: '8.0' + dependency-versions: 'locked' + - operating-system: macos-latest + php-versions: '8.0' + dependency-versions: 'locked' name: PHP ${{ matrix.php-versions }} on ${{ matrix.operating-system }} with ${{ matrix.dependency-versions }} versions of Composer dependencies steps: - name: Setup PHP From a6632ac2218195f46f9a8095c46510f576cc81c2 Mon Sep 17 00:00:00 2001 From: tienvx Date: Thu, 16 Mar 2023 12:14:50 +0700 Subject: [PATCH 07/13] Only run php cs on ubuntu --- .github/workflows/main.yml | 91 ++++++++++++++++++++++---------------- 1 file changed, 53 insertions(+), 38 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 0d2f353..b9e819f 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -3,8 +3,30 @@ name: main on: [push, pull_request] jobs: - run: + + php-cs: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + name: Checkout repository + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: '8.2' + tools: phpstan, php-cs-fixer:3 + + - name: Run PHP CS Fixer + run: php-cs-fixer fix --diff --dry-run + + - name: Run PHPStan + run: phpstan analyse src tests example + + test: runs-on: ${{ matrix.operating-system }} + needs: + - php-cs strategy: matrix: operating-system: [ ubuntu-latest ] @@ -19,40 +41,33 @@ jobs: dependency-versions: 'locked' name: PHP ${{ matrix.php-versions }} on ${{ matrix.operating-system }} with ${{ matrix.dependency-versions }} versions of Composer dependencies steps: - - name: Setup PHP - uses: shivammathur/setup-php@v2 - with: - php-version: ${{ matrix.php-versions }} - tools: phpstan, phpcs, php-cs-fixer:3 - coverage: pcov - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - - - name: Checkout - uses: actions/checkout@v2 - - - name: Install - uses: "ramsey/composer-install@v2" - with: - dependency-versions: ${{ matrix.dependency-versions }} - env: - COMPOSER_AUTH: '{"github-oauth": {"github.com": "${{ secrets.COMPOSER_AUTH }}"}}' - - - name: Run PHP CS Fixer - run: php-cs-fixer fix --diff --dry-run - - - name: Run PHPStan - run: phpstan analyse src tests example - - - name: Test & Generate Code Coverage - run: composer test - env: - PACT_LOGLEVEL: ${{ vars.PACT_LOGLEVEL }} - - - name: Upload coverage results to Coveralls - env: - COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }} - run: | - composer global require php-coveralls/php-coveralls - php-coveralls --coverage_clover=clover.xml -v - if: matrix.php-versions == '8.0' + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php-versions }} + coverage: pcov + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + - name: Checkout + uses: actions/checkout@v3 + + - name: Install + uses: "ramsey/composer-install@v2" + with: + dependency-versions: ${{ matrix.dependency-versions }} + env: + COMPOSER_AUTH: '{"github-oauth": {"github.com": "${{ secrets.COMPOSER_AUTH }}"}}' + + - name: Test & Generate Code Coverage + run: composer test + env: + PACT_LOGLEVEL: ${{ vars.PACT_LOGLEVEL }} + + - name: Upload coverage results to Coveralls + env: + COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + composer global require php-coveralls/php-coveralls + php-coveralls --coverage_clover=clover.xml -v + if: matrix.php-versions == '8.2' From 325c64e0e1084e766a4ef76c9f42494754c47753 Mon Sep 17 00:00:00 2001 From: tienvx Date: Thu, 16 Mar 2023 12:19:50 +0700 Subject: [PATCH 08/13] Fix phpstan --- .github/workflows/main.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index b9e819f..575a1ec 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -17,6 +17,9 @@ jobs: php-version: '8.2' tools: phpstan, php-cs-fixer:3 + - name: Install + uses: "ramsey/composer-install@v2" + - name: Run PHP CS Fixer run: php-cs-fixer fix --diff --dry-run From 03a1dd0f98e603208102b293c3345fa4cbfd853a Mon Sep 17 00:00:00 2001 From: tienvx Date: Thu, 16 Mar 2023 12:29:58 +0700 Subject: [PATCH 09/13] Fix port already used by macos --- example/provider/tests/Contract/PactVerifyTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/example/provider/tests/Contract/PactVerifyTest.php b/example/provider/tests/Contract/PactVerifyTest.php index 56120cb..6592a8c 100644 --- a/example/provider/tests/Contract/PactVerifyTest.php +++ b/example/provider/tests/Contract/PactVerifyTest.php @@ -14,10 +14,10 @@ class PactVerifyTest extends TestCase protected function setUp(): void { - $this->process = new Process(['php', '-S', 'localhost:8000', '-t', __DIR__.'/../../public/']); + $this->process = new Process(['php', '-S', 'localhost:7000', '-t', __DIR__.'/../../public/']); $this->process->start(); - $this->process->waitUntil(fn () => is_resource(fsockopen('localhost', 8000))); + $this->process->waitUntil(fn () => is_resource(fsockopen('localhost', 7000))); } protected function tearDown(): void @@ -33,8 +33,8 @@ public function testPactVerifyConsumer() ->setProviderVersion('1.0.0') ->setProviderBranch('main') ->setHost('localhost') - ->setPort(8000) - ->setStateChangeUrl(new Uri('http://localhost:8000/change-state')) + ->setPort(7000) + ->setStateChangeUrl(new Uri('http://localhost:7000/change-state')) ->setStateChangeTeardown(true) ->setStateChangeAsBody(true) ->setPublishResults(false) From f328146f7b38801b2345d3282bdd17bf413bb6bb Mon Sep 17 00:00:00 2001 From: tienvx Date: Thu, 16 Mar 2023 12:32:16 +0700 Subject: [PATCH 10/13] Fix 'Could not authenticate against github.com' --- .github/workflows/main.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 575a1ec..89f67c5 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -19,6 +19,8 @@ jobs: - name: Install uses: "ramsey/composer-install@v2" + env: + COMPOSER_AUTH: '{"github-oauth": {"github.com": "${{ secrets.COMPOSER_AUTH }}"}}' - name: Run PHP CS Fixer run: php-cs-fixer fix --diff --dry-run From bdcdd25938296157208d91d1ac9bd6e68141ff05 Mon Sep 17 00:00:00 2001 From: tienvx Date: Thu, 16 Mar 2023 12:40:14 +0700 Subject: [PATCH 11/13] Use dynamic port --- .../provider/tests/Contract/PactVerifyTest.php | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/example/provider/tests/Contract/PactVerifyTest.php b/example/provider/tests/Contract/PactVerifyTest.php index 6592a8c..3902724 100644 --- a/example/provider/tests/Contract/PactVerifyTest.php +++ b/example/provider/tests/Contract/PactVerifyTest.php @@ -11,13 +11,21 @@ class PactVerifyTest extends TestCase { private Process $process; + private int $port; protected function setUp(): void { - $this->process = new Process(['php', '-S', 'localhost:7000', '-t', __DIR__.'/../../public/']); + $this->process = new Process(['php', '-S', 'localhost:0', '-t', __DIR__.'/../../public/']); $this->process->start(); - $this->process->waitUntil(fn () => is_resource(fsockopen('localhost', 7000))); + $this->process->waitUntil(function (string $type, string $output): bool { + $result = preg_match('/Development Server \(http:\/\/localhost:(\d+)\) started/', $output, $matches); + if ($result === 1) { + $this->port = (int)$matches[1]; + } + + return $result; + }); } protected function tearDown(): void @@ -33,8 +41,8 @@ public function testPactVerifyConsumer() ->setProviderVersion('1.0.0') ->setProviderBranch('main') ->setHost('localhost') - ->setPort(7000) - ->setStateChangeUrl(new Uri('http://localhost:7000/change-state')) + ->setPort($this->port) + ->setStateChangeUrl(new Uri("http://localhost:{$this->port}/change-state")) ->setStateChangeTeardown(true) ->setStateChangeAsBody(true) ->setPublishResults(false) From ef9d3decbe9c2c707fd5f01a5daed54186d428d8 Mon Sep 17 00:00:00 2001 From: tienvx Date: Thu, 16 Mar 2023 12:44:52 +0700 Subject: [PATCH 12/13] Install ffi extension --- .github/workflows/main.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 89f67c5..3c68e69 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -49,6 +49,7 @@ jobs: - name: Setup PHP uses: shivammathur/setup-php@v2 with: + extensions: ffi php-version: ${{ matrix.php-versions }} coverage: pcov env: From 3c7b6bcd2eba74c9dae9de5ab161948ef820e576 Mon Sep 17 00:00:00 2001 From: tienvx Date: Thu, 16 Mar 2023 14:51:18 +0700 Subject: [PATCH 13/13] Timeout for windows os --- .github/workflows/main.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 3c68e69..f4533c9 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -30,6 +30,7 @@ jobs: test: runs-on: ${{ matrix.operating-system }} + timeout-minutes: 3 needs: - php-cs strategy: