From 3d8a05526f2d25d20f8515125524edad2b89a18e Mon Sep 17 00:00:00 2001 From: tienvx Date: Sat, 19 Aug 2023 01:05:21 +0700 Subject: [PATCH 1/2] chore: reorganize examples (create separate directory for each example) --- composer.json | 27 +++----- example/README.md | 24 ------- example/json/consumer/phpunit.xml | 11 +++ .../src}/Service/HttpClientService.php | 2 +- .../Service/ConsumerServiceGoodbyeTest.php | 20 +++--- .../Service/ConsumerServiceHelloTest.php | 14 +++- .../pacts/jsonConsumer-jsonProvider.json} | 10 +-- example/json/provider/phpunit.xml | 11 +++ .../provider}/public/index.php | 16 +---- example/json/provider/src/ExampleProvider.php | 27 ++++++++ .../json/provider/tests/PactVerifyTest.php | 68 +++++++++++++++++++ example/message/consumer/phpunit.xml | 11 +++ .../consumer/src}/ExampleMessageConsumer.php | 0 .../consumer/src}/receive.php | 2 +- .../tests}/ExampleMessageConsumerTest.php | 19 +++--- .../messageConsumer-messageProvider.json} | 8 +-- example/message/provider/phpunit.xml | 11 +++ example/message/provider/public/index.php | 36 ++++++++++ .../provider/src}/ExampleMessageProvider.php | 0 .../provider/src}/ExampleProvider.php | 12 +--- .../provider/src}/send.php | 2 +- .../provider/tests}/PactVerifyTest.php | 15 ++-- example/pacts/README.md | 11 --- example/phpunit.all.xml | 22 ------ example/phpunit.consumer.xml | 13 ---- example/phpunit.core.xml | 13 ---- example/phpunit.message.consumer.xml | 8 --- example/phpunit.provider.xml | 8 --- phpunit.xml | 16 ++++- src/PhpPact/FFI/Model/ArrayData.php | 2 +- 30 files changed, 251 insertions(+), 188 deletions(-) delete mode 100644 example/README.md create mode 100644 example/json/consumer/phpunit.xml rename example/{src/Consumer => json/consumer/src}/Service/HttpClientService.php (97%) rename example/{tests/Consumer => json/consumer/tests}/Service/ConsumerServiceGoodbyeTest.php (70%) rename example/{tests/Consumer => json/consumer/tests}/Service/ConsumerServiceHelloTest.php (81%) rename example/{pacts/someconsumer-someprovider.json => json/pacts/jsonConsumer-jsonProvider.json} (91%) create mode 100644 example/json/provider/phpunit.xml rename example/{src/Provider => json/provider}/public/index.php (66%) create mode 100644 example/json/provider/src/ExampleProvider.php create mode 100644 example/json/provider/tests/PactVerifyTest.php create mode 100644 example/message/consumer/phpunit.xml rename example/{src/MessageConsumer => message/consumer/src}/ExampleMessageConsumer.php (100%) rename example/{src/MessageConsumer => message/consumer/src}/receive.php (93%) rename example/{tests/MessageConsumer => message/consumer/tests}/ExampleMessageConsumerTest.php (77%) rename example/{pacts/test_consumer-test_provider.json => message/pacts/messageConsumer-messageProvider.json} (90%) create mode 100644 example/message/provider/phpunit.xml create mode 100644 example/message/provider/public/index.php rename example/{src/MessageProvider => message/provider/src}/ExampleMessageProvider.php (100%) rename example/{src/Provider => message/provider/src}/ExampleProvider.php (87%) rename example/{src/MessageProvider => message/provider/src}/send.php (94%) rename example/{tests/Provider => message/provider/tests}/PactVerifyTest.php (75%) delete mode 100644 example/pacts/README.md delete mode 100644 example/phpunit.all.xml delete mode 100644 example/phpunit.consumer.xml delete mode 100644 example/phpunit.core.xml delete mode 100644 example/phpunit.message.consumer.xml delete mode 100644 example/phpunit.provider.xml diff --git a/composer.json b/composer.json index 373592e1..f506240c 100644 --- a/composer.json +++ b/composer.json @@ -42,21 +42,14 @@ "autoload-dev": { "psr-4": { "PhpPactTest\\": "tests/PhpPact", - "Consumer\\": [ - "example/src/Consumer", - "example/tests/Consumer" - ], - "MessageConsumer\\": [ - "example/src/MessageConsumer", - "example/tests/MessageConsumer" - ], - "MessageProvider\\": [ - "example/src/MessageProvider", - "example/tests/MessageProvider" - ], - "Provider\\": [ - "example/src/Provider" - ] + "JsonConsumer\\": "example/json/consumer/src", + "JsonConsumer\\Tests\\": "example/json/consumer/tests", + "JsonProvider\\": "example/json/provider/src", + "JsonProvider\\Tests\\": "example/json/provider/tests", + "MessageConsumer\\": "example/message/consumer/src", + "MessageConsumer\\Tests\\": "example/message/consumer/tests", + "MessageProvider\\": "example/message/provider/src", + "MessageProvider\\Tests\\": "example/message/provider/tests" } }, "scripts": { @@ -65,8 +58,8 @@ "lint": "php-cs-fixer fix --dry-run", "fix": "php-cs-fixer fix", "test": [ - "php -r \"array_map('unlink', glob('./example/output/*.json'));\"", - "phpunit --debug -c example/phpunit.all.xml" + "php -r \"array_map('unlink', glob('./example/*/pacts/*.json'));\"", + "phpunit --debug" ] }, "extra": { diff --git a/example/README.md b/example/README.md deleted file mode 100644 index ab66a43b..00000000 --- a/example/README.md +++ /dev/null @@ -1,24 +0,0 @@ -# Pact PHP Usage examples - -This folder contains some integration tests which demonstrate the functionality of `pact-php`. -All examples could be run within tests. - -## Consumer Tests - - docker-compose up -d - vendor/bin/phpunit -c example/phpunit.consumer.xml - docker-compose down - -## Provider Verification Tests - - vendor/bin/phpunit -c example/phpunit.provider.xml - -## Consumer Tests for Message Processing - - vendor/bin/phpunit -c example/phpunit.message.consumer.xml - -## All tests together - - docker-compose up -d - vendor/bin/phpunit -c example/phpunit.all.xml - docker-compose down diff --git a/example/json/consumer/phpunit.xml b/example/json/consumer/phpunit.xml new file mode 100644 index 00000000..62a9eb00 --- /dev/null +++ b/example/json/consumer/phpunit.xml @@ -0,0 +1,11 @@ + + + + + ./tests + + + + + + diff --git a/example/src/Consumer/Service/HttpClientService.php b/example/json/consumer/src/Service/HttpClientService.php similarity index 97% rename from example/src/Consumer/Service/HttpClientService.php rename to example/json/consumer/src/Service/HttpClientService.php index 85ab9c53..93831973 100644 --- a/example/src/Consumer/Service/HttpClientService.php +++ b/example/json/consumer/src/Service/HttpClientService.php @@ -1,6 +1,6 @@ 'Goodbye, Bob' ]); - $config = new MockServerEnvConfig(); - $builder = new InteractionBuilder($config); + $config = new MockServerConfig(); + $config + ->setConsumer('jsonConsumer') + ->setProvider('jsonProvider') + ->setPactDir(__DIR__.'/../../../pacts'); + if ($logLevel = \getenv('PACT_LOGLEVEL')) { + $config->setLogLevel($logLevel); + } + $builder = new InteractionBuilder($config); $builder ->given('Get Goodbye') ->uponReceiving('A get request to /goodbye/{name}') diff --git a/example/tests/Consumer/Service/ConsumerServiceHelloTest.php b/example/json/consumer/tests/Service/ConsumerServiceHelloTest.php similarity index 81% rename from example/tests/Consumer/Service/ConsumerServiceHelloTest.php rename to example/json/consumer/tests/Service/ConsumerServiceHelloTest.php index d10782ff..605a3fb6 100644 --- a/example/tests/Consumer/Service/ConsumerServiceHelloTest.php +++ b/example/json/consumer/tests/Service/ConsumerServiceHelloTest.php @@ -1,12 +1,13 @@ setConsumer('jsonConsumer') + ->setProvider('jsonProvider') + ->setPactDir(__DIR__.'/../../../pacts'); + if ($logLevel = \getenv('PACT_LOGLEVEL')) { + $config->setLogLevel($logLevel); + } $builder = new InteractionBuilder($config); $builder ->uponReceiving('A get request to /hello/{name}') diff --git a/example/pacts/someconsumer-someprovider.json b/example/json/pacts/jsonConsumer-jsonProvider.json similarity index 91% rename from example/pacts/someconsumer-someprovider.json rename to example/json/pacts/jsonConsumer-jsonProvider.json index 2d917286..709eefa4 100644 --- a/example/pacts/someconsumer-someprovider.json +++ b/example/json/pacts/jsonConsumer-jsonProvider.json @@ -1,6 +1,6 @@ { "consumer": { - "name": "someConsumer" + "name": "jsonConsumer" }, "interactions": [ { @@ -63,15 +63,15 @@ ], "metadata": { "pactRust": { - "ffi": "0.4.4", - "mockserver": "1.0.3", - "models": "1.0.13" + "ffi": "0.4.7", + "mockserver": "1.2.3", + "models": "1.1.9" }, "pactSpecification": { "version": "3.0.0" } }, "provider": { - "name": "someProvider" + "name": "jsonProvider" } } \ No newline at end of file diff --git a/example/json/provider/phpunit.xml b/example/json/provider/phpunit.xml new file mode 100644 index 00000000..62a9eb00 --- /dev/null +++ b/example/json/provider/phpunit.xml @@ -0,0 +1,11 @@ + + + + + ./tests + + + + + + diff --git a/example/src/Provider/public/index.php b/example/json/provider/public/index.php similarity index 66% rename from example/src/Provider/public/index.php rename to example/json/provider/public/index.php index 48e8a188..d4c57891 100644 --- a/example/src/Provider/public/index.php +++ b/example/json/provider/public/index.php @@ -1,6 +1,6 @@ withHeader('Content-Type', 'application/json'); }); -$app->post('/pact-messages', function (Request $request, Response $response) use ($provider) { - $body = $request->getParsedBody(); - $message = $provider->dispatchMessage($body['description'], $body['providerStates']); - if ($message) { - $response->getBody()->write(\json_encode($message->getContents())); - - return $response - ->withHeader('Content-Type', 'application/json') - ->withHeader('Pact-Message-Metadata', \base64_encode(\json_encode($message->getMetadata()))); - } - - return $response; -}); - $app->post('/pact-change-state', function (Request $request, Response $response) use ($provider) { $body = $request->getParsedBody(); $provider->changeSate($body['action'], $body['state'], $body['params']); diff --git a/example/json/provider/src/ExampleProvider.php b/example/json/provider/src/ExampleProvider.php new file mode 100644 index 00000000..70daec33 --- /dev/null +++ b/example/json/provider/src/ExampleProvider.php @@ -0,0 +1,27 @@ +currentState = [ + 'action' => $action, + 'state' => $state, + 'params' => $params, + ]; + } +} diff --git a/example/json/provider/tests/PactVerifyTest.php b/example/json/provider/tests/PactVerifyTest.php new file mode 100644 index 00000000..73dd84c9 --- /dev/null +++ b/example/json/provider/tests/PactVerifyTest.php @@ -0,0 +1,68 @@ +process = new Process(['php', '-S', '127.0.0.1:7202', '-t', $publicPath]); + + $this->process->start(); + $this->process->waitUntil(function (): bool { + $fp = @fsockopen('127.0.0.1', 7202); + $isOpen = is_resource($fp); + if ($isOpen) { + fclose($fp); + } + + return $isOpen; + }); + } + + /** + * Stop the web server process once complete. + */ + protected function tearDown(): void + { + $this->process->stop(); + } + + /** + * This test will run after the web server is started. + */ + public function testPactVerifyConsumer() + { + $config = new VerifierConfig(); + $config->getProviderInfo() + ->setName('jsonProvider') // Providers name to fetch. + ->setHost('localhost') + ->setPort(7202); + $config->getProviderState() + ->setStateChangeUrl(new Uri('http://localhost:7202/pact-change-state')) + ; + if ($level = \getenv('PACT_LOGLEVEL')) { + $config->setLogLevel($level); + } + + $verifier = new Verifier($config); + $verifier->addFile(__DIR__ . '/../../pacts/jsonConsumer-jsonProvider.json'); + + $verifyResult = $verifier->verify(); + + $this->assertTrue($verifyResult); + } +} diff --git a/example/message/consumer/phpunit.xml b/example/message/consumer/phpunit.xml new file mode 100644 index 00000000..62a9eb00 --- /dev/null +++ b/example/message/consumer/phpunit.xml @@ -0,0 +1,11 @@ + + + + + ./tests + + + + + + diff --git a/example/src/MessageConsumer/ExampleMessageConsumer.php b/example/message/consumer/src/ExampleMessageConsumer.php similarity index 100% rename from example/src/MessageConsumer/ExampleMessageConsumer.php rename to example/message/consumer/src/ExampleMessageConsumer.php diff --git a/example/src/MessageConsumer/receive.php b/example/message/consumer/src/receive.php similarity index 93% rename from example/src/MessageConsumer/receive.php rename to example/message/consumer/src/receive.php index 66c12a5c..48996111 100644 --- a/example/src/MessageConsumer/receive.php +++ b/example/message/consumer/src/receive.php @@ -1,6 +1,6 @@ setConsumer('test_consumer') - ->setProvider('test_provider') - ->setPactDir(__DIR__ . '/../../output/'); + ->setConsumer('messageConsumer') + ->setProvider('messageProvider') + ->setPactDir(__DIR__.'/../../pacts'); + if ($logLevel = \getenv('PACT_LOGLEVEL')) { + self::$config->setLogLevel($logLevel); + } } /** @@ -36,7 +37,7 @@ public function testProcessText() $contents = new stdClass(); $contents->text = 'Hello Mary'; - $metadata = ['queue'=>'wind cries', 'routing_key'=>'wind cries']; + $metadata = ['queue' => 'wind cries', 'routing_key' => 'wind cries']; $builder ->given('a message', ['foo' => 'bar']) @@ -64,7 +65,7 @@ public function testProcessSong() $contents = new stdClass(); $contents->song = 'And the wind whispers Mary'; - $metadata = ['queue'=>'And the clowns have all gone to bed', 'routing_key'=>'And the clowns have all gone to bed']; + $metadata = ['queue' => 'And the clowns have all gone to bed', 'routing_key' => 'And the clowns have all gone to bed']; $builder ->given('You can hear happiness staggering on down the street') diff --git a/example/pacts/test_consumer-test_provider.json b/example/message/pacts/messageConsumer-messageProvider.json similarity index 90% rename from example/pacts/test_consumer-test_provider.json rename to example/message/pacts/messageConsumer-messageProvider.json index 13f684cd..95669646 100644 --- a/example/pacts/test_consumer-test_provider.json +++ b/example/message/pacts/messageConsumer-messageProvider.json @@ -1,6 +1,6 @@ { "consumer": { - "name": "test_consumer" + "name": "messageConsumer" }, "messages": [ { @@ -41,14 +41,14 @@ ], "metadata": { "pactRust": { - "ffi": "0.4.4", - "models": "1.0.13" + "ffi": "0.4.7", + "models": "1.1.9" }, "pactSpecification": { "version": "3.0.0" } }, "provider": { - "name": "test_provider" + "name": "messageProvider" } } \ No newline at end of file diff --git a/example/message/provider/phpunit.xml b/example/message/provider/phpunit.xml new file mode 100644 index 00000000..62a9eb00 --- /dev/null +++ b/example/message/provider/phpunit.xml @@ -0,0 +1,11 @@ + + + + + ./tests + + + + + + diff --git a/example/message/provider/public/index.php b/example/message/provider/public/index.php new file mode 100644 index 00000000..9f4ae661 --- /dev/null +++ b/example/message/provider/public/index.php @@ -0,0 +1,36 @@ +addBodyParsingMiddleware(); + +$provider = new ExampleProvider(); + +$app->post('/pact-messages', function (Request $request, Response $response) use ($provider) { + $body = $request->getParsedBody(); + $message = $provider->dispatchMessage($body['description'], $body['providerStates']); + if ($message) { + $response->getBody()->write(\json_encode($message->getContents())); + + return $response + ->withHeader('Content-Type', 'application/json') + ->withHeader('Pact-Message-Metadata', \base64_encode(\json_encode($message->getMetadata()))); + } + + return $response; +}); + +$app->post('/pact-change-state', function (Request $request, Response $response) use ($provider) { + $body = $request->getParsedBody(); + $provider->changeSate($body['action'], $body['state'], $body['params']); + + return $response; +}); + +$app->run(); diff --git a/example/src/MessageProvider/ExampleMessageProvider.php b/example/message/provider/src/ExampleMessageProvider.php similarity index 100% rename from example/src/MessageProvider/ExampleMessageProvider.php rename to example/message/provider/src/ExampleMessageProvider.php diff --git a/example/src/Provider/ExampleProvider.php b/example/message/provider/src/ExampleProvider.php similarity index 87% rename from example/src/Provider/ExampleProvider.php rename to example/message/provider/src/ExampleProvider.php index c7fddb72..2def3fa5 100644 --- a/example/src/Provider/ExampleProvider.php +++ b/example/message/provider/src/ExampleProvider.php @@ -1,6 +1,6 @@ messages[$description])) { diff --git a/example/src/MessageProvider/send.php b/example/message/provider/src/send.php similarity index 94% rename from example/src/MessageProvider/send.php rename to example/message/provider/src/send.php index e1dbb74d..125db85e 100644 --- a/example/src/MessageProvider/send.php +++ b/example/message/provider/src/send.php @@ -9,7 +9,7 @@ $connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest'); // build the message with appropriate metadata -$providerMessage = new \MessageProvider\ExampleMessageProvider(['queue'=>'myKey', 'routing_key'=>'myKey']); +$providerMessage = new \MessageProvider\ExampleMessageProvider(['queue' => 'myKey', 'routing_key' => 'myKey']); $content = new \stdClass(); $content->text = 'Hello Mary'; $providerMessage->setContents($content); diff --git a/example/tests/Provider/PactVerifyTest.php b/example/message/provider/tests/PactVerifyTest.php similarity index 75% rename from example/tests/Provider/PactVerifyTest.php rename to example/message/provider/tests/PactVerifyTest.php index e36e0b75..f0c459a1 100644 --- a/example/tests/Provider/PactVerifyTest.php +++ b/example/message/provider/tests/PactVerifyTest.php @@ -1,6 +1,6 @@ process = new Process(['php', '-S', '127.0.0.1:7202', '-t', $publicPath]); @@ -54,7 +49,7 @@ public function testPactVerifyConsumer() { $config = new VerifierConfig(); $config->getProviderInfo() - ->setName('someProvider') // Providers name to fetch. + ->setName('messageProvider') // Providers name to fetch. ->setHost('localhost') ->setPort(7202); $config->getProviderState() @@ -71,10 +66,8 @@ public function testPactVerifyConsumer() $config->setLogLevel($level); } - // Verify that the Consumer 'someConsumer' that is tagged with 'master' is valid. $verifier = new Verifier($config); - $verifier->addFile(__DIR__ . '/../../pacts/someconsumer-someprovider.json'); - $verifier->addFile(__DIR__ . '/../../pacts/test_consumer-test_provider.json'); + $verifier->addFile(__DIR__ . '/../../pacts/messageConsumer-messageProvider.json'); $verifyResult = $verifier->verify(); diff --git a/example/pacts/README.md b/example/pacts/README.md deleted file mode 100644 index 0d5d2a0b..00000000 --- a/example/pacts/README.md +++ /dev/null @@ -1,11 +0,0 @@ -# Example Pacts - -The json files in this folder are explicitly here for an easy-to-read output of the test examples. These are *not* the actual test results from running all these tests of this project. By default, the pact files of this project's examples are written to example/output. The tests themselves need to generate the appropropriate files as part of the tests. - -To run the tests locally, try `composer test` - - - - - - diff --git a/example/phpunit.all.xml b/example/phpunit.all.xml deleted file mode 100644 index d3f50c70..00000000 --- a/example/phpunit.all.xml +++ /dev/null @@ -1,22 +0,0 @@ - - - - - ../tests - - - ./tests/Consumer - - - ./tests/Provider - - - ./tests/MessageConsumer - - - - - - - - diff --git a/example/phpunit.consumer.xml b/example/phpunit.consumer.xml deleted file mode 100644 index 6236e64d..00000000 --- a/example/phpunit.consumer.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - ./tests/Consumer - - - - - - - - diff --git a/example/phpunit.core.xml b/example/phpunit.core.xml deleted file mode 100644 index 6cc7fc8b..00000000 --- a/example/phpunit.core.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - ../tests - - - - - - - - diff --git a/example/phpunit.message.consumer.xml b/example/phpunit.message.consumer.xml deleted file mode 100644 index 7170d67e..00000000 --- a/example/phpunit.message.consumer.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - ./tests/MessageConsumer - - - diff --git a/example/phpunit.provider.xml b/example/phpunit.provider.xml deleted file mode 100644 index 8f143865..00000000 --- a/example/phpunit.provider.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - ./tests/Provider - - - diff --git a/phpunit.xml b/phpunit.xml index 0a7db8ac..ff6fe126 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -10,8 +10,20 @@ - - ./tests/PhpPact + + ./tests + + + ./example/json/consumer/tests + + + ./example/json/provider/tests + + + ./example/message/consumer/tests + + + ./example/message/provider/tests diff --git a/src/PhpPact/FFI/Model/ArrayData.php b/src/PhpPact/FFI/Model/ArrayData.php index cec3fb75..8945b2d1 100644 --- a/src/PhpPact/FFI/Model/ArrayData.php +++ b/src/PhpPact/FFI/Model/ArrayData.php @@ -54,7 +54,7 @@ public static function createFrom(array $values): ?self public function __destruct() { - for ($i=0; $i < $this->size; $i++) { + for ($i = 0; $i < $this->size; $i++) { FFI::free($this->items[$i]); // @phpstan-ignore-line } } From 2f5baac119fcd9efa746ab8380cf1cfb4b5b2cf5 Mon Sep 17 00:00:00 2001 From: "tien.xuan.vo" Date: Tue, 12 Sep 2023 09:50:30 +0700 Subject: [PATCH 2/2] fix: Update link to examples in README --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index cef1868c..046667c2 100644 --- a/README.md +++ b/README.md @@ -102,7 +102,7 @@ All of the following code will be used exclusively for the Consumer. Create a standard PHPUnit test case class and function. -[Click here](/example/tests/Consumer/Service/ConsumerServiceHelloTest.php) to see the full sample file. +[Click here](/example/json/consumer/tests/Service/ConsumerServiceHelloTest.php) to see the full sample file. ### Create Mock Request @@ -381,7 +381,7 @@ to processing class. Aside from changing default ports, this should be transpa Both the provider and consumer side make heavy use of lambda functions. ### Consumer Side Message Processing -The examples provided are pretty basic. See examples\tests\MessageConsumer. +The examples provided are pretty basic. See [example](/example/message/consumer/tests/ExampleMessageConsumerTest.php). 1. Create the content and metadata (array) 1. Annotate the MessageBuilder appropriate content and states 1. Given = Provider State @@ -425,7 +425,7 @@ Handle these requests on your provider: 1. Return message's content in body 2. Return message's metadata in header `PACT-MESSAGE-METADATA` -[Click here](/example/src/Provider/public/index.php) to see the full sample file. +[Click here](/example/message/provider/public/index.php) to see the full sample file. ## Usage for the optional `pact-stub-service`