diff --git a/.cirrus.yml b/.cirrus.yml index dde4e49c..80abe231 100644 --- a/.cirrus.yml +++ b/.cirrus.yml @@ -27,7 +27,7 @@ linux_arm64_task: container: image: php:$VERSION pre_req_script: - - apt update --yes && apt install --yes zip unzip git libffi-dev + - apt update --yes && apt install --yes zip unzip git libffi-dev shared-mime-info - curl -sS https://getcomposer.org/installer -o /tmp/composer-setup.php - php /tmp/composer-setup.php --install-dir=/usr/local/bin --filename=composer - docker-php-ext-install sockets @@ -46,7 +46,7 @@ macos_arm64_task: macos_instance: image: ghcr.io/cirruslabs/macos-ventura-base:latest pre_req_script: - - brew install php@$VERSION composer + - brew install php@$VERSION composer shared-mime-info version_check_script: - php --version << : *BUILD_TEST_TASK_TEMPLATE diff --git a/composer.json b/composer.json index 373592e1..e558f038 100644 --- a/composer.json +++ b/composer.json @@ -42,21 +42,18 @@ "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", + "BinaryConsumer\\": "example/binary/consumer/src", + "BinaryConsumer\\Tests\\": "example/binary/consumer/tests", + "BinaryProvider\\": "example/binary/provider/src", + "BinaryProvider\\Tests\\": "example/binary/provider/tests" } }, "scripts": { @@ -65,8 +62,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/binary/consumer/phpunit.xml b/example/binary/consumer/phpunit.xml new file mode 100644 index 00000000..62a9eb00 --- /dev/null +++ b/example/binary/consumer/phpunit.xml @@ -0,0 +1,11 @@ + + + + + ./tests + + + + + + diff --git a/example/binary/consumer/src/Service/HttpClientService.php b/example/binary/consumer/src/Service/HttpClientService.php new file mode 100644 index 00000000..3f8d22a7 --- /dev/null +++ b/example/binary/consumer/src/Service/HttpClientService.php @@ -0,0 +1,31 @@ +httpClient = new Client(); + $this->baseUri = $baseUri; + } + + public function getImageContent(): string + { + $response = $this->httpClient->get(new Uri("{$this->baseUri}/image.jpg"), [ + 'headers' => ['Accept' => 'image/jpeg'] + ]); + + return $response->getBody(); + } +} diff --git a/example/binary/consumer/tests/Service/HttpClientServiceTest.php b/example/binary/consumer/tests/Service/HttpClientServiceTest.php new file mode 100644 index 00000000..42fe6b82 --- /dev/null +++ b/example/binary/consumer/tests/Service/HttpClientServiceTest.php @@ -0,0 +1,53 @@ +setMethod('GET') + ->setPath('/image.jpg') + ->addHeader('Accept', 'image/jpeg'); + + $response = new ProviderResponse(); + $response + ->setStatus(200) + ->addHeader('Content-Type', 'image/jpeg') + ->setBody(new Binary($imageContent, in_array(php_uname('m'), ['AMD64', 'arm64']) ? 'application/octet-stream' : 'image/jpeg')); + + $config = new MockServerConfig(); + $config + ->setConsumer('binaryConsumer') + ->setProvider('binaryProvider') + ->setPactDir(__DIR__.'/../../../pacts'); + if ($logLevel = \getenv('PACT_LOGLEVEL')) { + $config->setLogLevel($logLevel); + } + $builder = new InteractionBuilder($config); + $builder + ->given('Image file image.jpg exists') + ->uponReceiving('A get request to /image.jpg') + ->with($request) + ->willRespondWith($response); + + $service = new HttpClientService($config->getBaseUri()); + $imageContentResult = $service->getImageContent(); + $verifyResult = $builder->verify(); + + $this->assertTrue($verifyResult); + $this->assertEquals($imageContent, $imageContentResult); + } +} diff --git a/example/binary/consumer/tests/_resource/image.jpg b/example/binary/consumer/tests/_resource/image.jpg new file mode 100644 index 00000000..6e93db1c Binary files /dev/null and b/example/binary/consumer/tests/_resource/image.jpg differ diff --git a/example/binary/pacts/binaryConsumer-binaryProvider.json b/example/binary/pacts/binaryConsumer-binaryProvider.json new file mode 100644 index 00000000..7ecf427f --- /dev/null +++ b/example/binary/pacts/binaryConsumer-binaryProvider.json @@ -0,0 +1,56 @@ +{ + "consumer": { + "name": "binaryConsumer" + }, + "interactions": [ + { + "description": "A get request to /image.jpg", + "providerStates": [ + { + "name": "Image file image.jpg exists" + } + ], + "request": { + "headers": { + "Accept": "image/jpeg" + }, + "method": "GET", + "path": "/image.jpg" + }, + "response": { + "body": "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBgUGCQgKCgkICQkKDA8MCgsOCwkJDRENDg8QEBEQCgwSExIQEw8QEBD/wAALCAAPAA8BAREA/8QAFgABAQEAAAAAAAAAAAAAAAAAAgME/8QAIBAAAgIDAAIDAQAAAAAAAAAAAQIDBAUREhMhACIxUf/aAAgBAQAAPwBNlclNW6S0s7wbvv4K6iNI2HJcp9Rrnk+T0ByNofwZIjdnyYx0stWrYWAP5y03SBjt+GQ/YnmHbaUkfoA107Br40Wa+DrRLNXhjchECDwyjjlXPpdFSo3Gw5CbDFdmUVOrHcfEU4JYb2PWSJCzbVPUEmh7PQaOZHOyD0x/hHz/2Q==", + "headers": { + "Content-Type": "image/jpeg" + }, + "matchingRules": { + "body": { + "$": { + "combine": "AND", + "matchers": [ + { + "match": "contentType", + "value": "image/jpeg" + } + ] + } + }, + "header": {} + }, + "status": 200 + } + } + ], + "metadata": { + "pactRust": { + "ffi": "0.4.7", + "mockserver": "1.2.3", + "models": "1.1.9" + }, + "pactSpecification": { + "version": "3.0.0" + } + }, + "provider": { + "name": "binaryProvider" + } +} \ No newline at end of file diff --git a/example/binary/provider/phpunit.xml b/example/binary/provider/phpunit.xml new file mode 100644 index 00000000..62a9eb00 --- /dev/null +++ b/example/binary/provider/phpunit.xml @@ -0,0 +1,11 @@ + + + + + ./tests + + + + + + diff --git a/example/binary/provider/public/image.jpg b/example/binary/provider/public/image.jpg new file mode 100644 index 00000000..5477a6da Binary files /dev/null and b/example/binary/provider/public/image.jpg differ diff --git a/example/binary/provider/tests/PactVerifyTest.php b/example/binary/provider/tests/PactVerifyTest.php new file mode 100644 index 00000000..cc30e108 --- /dev/null +++ b/example/binary/provider/tests/PactVerifyTest.php @@ -0,0 +1,62 @@ +process = new Process(['php', '-S', '127.0.0.1:7202', '-t', __DIR__ . '/../public/']); + + $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('binaryProvider') // Providers name to fetch. + ->setHost('localhost') + ->setPort(7202); + if ($level = \getenv('PACT_LOGLEVEL')) { + $config->setLogLevel($level); + } + + $verifier = new Verifier($config); + $verifier->addFile(__DIR__ . '/../../pacts/binaryConsumer-binaryProvider.json'); + + $verifyResult = $verifier->verify(); + + $this->assertTrue($verifyResult); + } +} 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..408dade1 --- /dev/null +++ b/example/json/provider/tests/PactVerifyTest.php @@ -0,0 +1,66 @@ +process = new Process(['php', '-S', '127.0.0.1:7202', '-t', __DIR__ . '/../public/']); + + $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..3c2344a0 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -10,8 +10,26 @@ - - ./tests/PhpPact + + ./tests + + + ./example/json/consumer/tests + + + ./example/json/provider/tests + + + ./example/binary/consumer/tests + + + ./example/binary/provider/tests + + + ./example/message/consumer/tests + + + ./example/message/provider/tests diff --git a/src/PhpPact/Consumer/AbstractMessageBuilder.php b/src/PhpPact/Consumer/AbstractMessageBuilder.php index d25a743c..71a23275 100644 --- a/src/PhpPact/Consumer/AbstractMessageBuilder.php +++ b/src/PhpPact/Consumer/AbstractMessageBuilder.php @@ -56,11 +56,4 @@ public function withContent(mixed $contents): self return $this; } - - public function withContentType(?string $contentType): self - { - $this->message->setContentType($contentType); - - return $this; - } } diff --git a/src/PhpPact/Consumer/Exception/MessageContentsNotAddedException.php b/src/PhpPact/Consumer/Exception/MessageContentsNotAddedException.php new file mode 100644 index 00000000..4cd40881 --- /dev/null +++ b/src/PhpPact/Consumer/Exception/MessageContentsNotAddedException.php @@ -0,0 +1,9 @@ +setContents(StringData::createFrom($contents, false)); + $this->setContentType($contentType); + } + + public function getContents(): StringData + { + return $this->contents; + } + + public function setContents(StringData $contents): self + { + $this->contents = $contents; + + return $this; + } +} diff --git a/src/PhpPact/Consumer/Model/Body/ContentTypeTrait.php b/src/PhpPact/Consumer/Model/Body/ContentTypeTrait.php new file mode 100644 index 00000000..41acc2cf --- /dev/null +++ b/src/PhpPact/Consumer/Model/Body/ContentTypeTrait.php @@ -0,0 +1,20 @@ +contentType; + } + + public function setContentType(string $contentType): self + { + $this->contentType = $contentType; + + return $this; + } +} diff --git a/src/PhpPact/Consumer/Model/Body/Text.php b/src/PhpPact/Consumer/Model/Body/Text.php new file mode 100644 index 00000000..1d2e54f7 --- /dev/null +++ b/src/PhpPact/Consumer/Model/Body/Text.php @@ -0,0 +1,28 @@ +setContents($contents); + $this->setContentType($contentType); + } + + public function getContents(): string + { + return $this->contents; + } + + public function setContents(string $contents): self + { + $this->contents = $contents; + + return $this; + } +} diff --git a/src/PhpPact/Consumer/Model/ConsumerRequest.php b/src/PhpPact/Consumer/Model/ConsumerRequest.php index 58d98740..1c0b4c08 100644 --- a/src/PhpPact/Consumer/Model/ConsumerRequest.php +++ b/src/PhpPact/Consumer/Model/ConsumerRequest.php @@ -3,7 +3,6 @@ namespace PhpPact\Consumer\Model; use PhpPact\Consumer\Model\Interaction\BodyTrait; -use PhpPact\Consumer\Model\Interaction\ContentTypeTrait; use PhpPact\Consumer\Model\Interaction\HeadersTrait; use PhpPact\Consumer\Model\Interaction\MethodTrait; use PhpPact\Consumer\Model\Interaction\PathTrait; @@ -16,7 +15,6 @@ class ConsumerRequest { use HeadersTrait; use BodyTrait; - use ContentTypeTrait; use MethodTrait; use PathTrait; use QueryTrait; diff --git a/src/PhpPact/Consumer/Model/Interaction/BodyTrait.php b/src/PhpPact/Consumer/Model/Interaction/BodyTrait.php index 3a7c6b37..3cb31e7f 100644 --- a/src/PhpPact/Consumer/Model/Interaction/BodyTrait.php +++ b/src/PhpPact/Consumer/Model/Interaction/BodyTrait.php @@ -3,32 +3,29 @@ namespace PhpPact\Consumer\Model\Interaction; use JsonException; +use PhpPact\Consumer\Model\Body\Binary; +use PhpPact\Consumer\Model\Body\Text; trait BodyTrait { - use ContentTypeTrait; + private Text|Binary|null $body = null; - private ?string $body = null; - - public function getBody(): ?string + public function getBody(): Text|Binary|null { return $this->body; } /** - * @param array|string|null $body - * * @throws JsonException */ - public function setBody(array|string|null $body): self + public function setBody(mixed $body): self { - if (\is_string($body) || \is_null($body)) { + if (\is_string($body)) { + $this->body = new Text($body, 'text/plain'); + } elseif (\is_null($body) || $body instanceof Text || $body instanceof Binary) { $this->body = $body; } else { - $this->body = \json_encode($body, JSON_THROW_ON_ERROR); - if (!isset($this->contentType)) { - $this->setContentType('application/json'); - } + $this->body = new Text(\json_encode($body, JSON_THROW_ON_ERROR), 'application/json'); } return $this; diff --git a/src/PhpPact/Consumer/Model/Interaction/ContentTypeTrait.php b/src/PhpPact/Consumer/Model/Interaction/ContentTypeTrait.php deleted file mode 100644 index 257f6cbe..00000000 --- a/src/PhpPact/Consumer/Model/Interaction/ContentTypeTrait.php +++ /dev/null @@ -1,20 +0,0 @@ -contentType; - } - - public function setContentType(?string $contentType): self - { - $this->contentType = $contentType; - - return $this; - } -} diff --git a/src/PhpPact/Consumer/Model/Message.php b/src/PhpPact/Consumer/Model/Message.php index 796a683f..8d2d2a4e 100644 --- a/src/PhpPact/Consumer/Model/Message.php +++ b/src/PhpPact/Consumer/Model/Message.php @@ -3,7 +3,8 @@ namespace PhpPact\Consumer\Model; use JsonException; -use PhpPact\Consumer\Model\Interaction\ContentTypeTrait; +use PhpPact\Consumer\Model\Body\Binary; +use PhpPact\Consumer\Model\Body\Text; /** * Message metadata and contents to be posted to the Mock Server for PACT tests. @@ -11,7 +12,6 @@ class Message { use ProviderStates; - use ContentTypeTrait; private string $description; @@ -20,7 +20,7 @@ class Message */ private array $metadata = []; - private ?string $contents = null; + private Text|Binary|null $contents = null; public function getDescription(): string { @@ -60,7 +60,7 @@ private function setMetadataValue(string $key, string $value): void $this->metadata[$key] = $value; } - public function getContents(): ?string + public function getContents(): Text|Binary|null { return $this->contents; } @@ -70,13 +70,12 @@ public function getContents(): ?string */ public function setContents(mixed $contents): self { - if (\is_string($contents) || \is_null($contents)) { + if (\is_string($contents)) { + $this->contents = new Text($contents, 'text/plain'); + } elseif (\is_null($contents) || $contents instanceof Text || $contents instanceof Binary) { $this->contents = $contents; } else { - $this->contents = \json_encode($contents, JSON_THROW_ON_ERROR); - if (!isset($this->contentType)) { - $this->setContentType('application/json'); - } + $this->contents = new Text(\json_encode($contents, JSON_THROW_ON_ERROR), 'application/json'); } return $this; diff --git a/src/PhpPact/Consumer/Model/ProviderResponse.php b/src/PhpPact/Consumer/Model/ProviderResponse.php index 3201811f..2abbe24b 100644 --- a/src/PhpPact/Consumer/Model/ProviderResponse.php +++ b/src/PhpPact/Consumer/Model/ProviderResponse.php @@ -3,7 +3,6 @@ namespace PhpPact\Consumer\Model; use PhpPact\Consumer\Model\Interaction\BodyTrait; -use PhpPact\Consumer\Model\Interaction\ContentTypeTrait; use PhpPact\Consumer\Model\Interaction\HeadersTrait; use PhpPact\Consumer\Model\Interaction\StatusTrait; @@ -14,6 +13,5 @@ class ProviderResponse { use HeadersTrait; use BodyTrait; - use ContentTypeTrait; use StatusTrait; } diff --git a/src/PhpPact/Consumer/Registry/Interaction/Body/AbstractBodyRegistry.php b/src/PhpPact/Consumer/Registry/Interaction/Body/AbstractBodyRegistry.php new file mode 100644 index 00000000..aa3760d9 --- /dev/null +++ b/src/PhpPact/Consumer/Registry/Interaction/Body/AbstractBodyRegistry.php @@ -0,0 +1,32 @@ +client->call('pactffi_with_binary_file', $this->interactionRegistry->getId(), $this->getPart(), $body->getContentType(), $body->getContents()->getValue(), $body->getContents()->getSize()); + } else { + $success = $this->client->call('pactffi_with_body', $this->interactionRegistry->getId(), $this->getPart(), $body->getContentType(), $body->getContents()); + } + if (!$success) { + throw new InteractionBodyNotAddedException(); + } + } + + abstract protected function getPart(): int; +} diff --git a/src/PhpPact/Consumer/Registry/Interaction/Body/BodyRegistryInterface.php b/src/PhpPact/Consumer/Registry/Interaction/Body/BodyRegistryInterface.php new file mode 100644 index 00000000..d7f69d3e --- /dev/null +++ b/src/PhpPact/Consumer/Registry/Interaction/Body/BodyRegistryInterface.php @@ -0,0 +1,11 @@ +client->call('pactffi_with_binary_file', $this->messageRegistry->getId(), $this->getPart(), $body->getContentType(), $body->getContents()->getValue(), $body->getContents()->getSize()); + } else { + $success = $this->client->call('pactffi_with_body', $this->messageRegistry->getId(), $this->getPart(), $body->getContentType(), $body->getContents()); + } + if (!$success) { + throw new MessageContentsNotAddedException(); + } + } +} diff --git a/src/PhpPact/Consumer/Registry/Interaction/Contents/RequestBodyRegistry.php b/src/PhpPact/Consumer/Registry/Interaction/Body/RequestBodyRegistry.php similarity index 73% rename from src/PhpPact/Consumer/Registry/Interaction/Contents/RequestBodyRegistry.php rename to src/PhpPact/Consumer/Registry/Interaction/Body/RequestBodyRegistry.php index 1a4ed86e..f1266404 100644 --- a/src/PhpPact/Consumer/Registry/Interaction/Contents/RequestBodyRegistry.php +++ b/src/PhpPact/Consumer/Registry/Interaction/Body/RequestBodyRegistry.php @@ -1,6 +1,6 @@ client->call('pactffi_with_body', $this->interactionRegistry->getId(), $this->getPart(), $contentType, $body); - if (!$success) { - throw new InteractionBodyNotAddedException(); - } - } - - abstract protected function getPart(): int; -} diff --git a/src/PhpPact/Consumer/Registry/Interaction/Contents/ContentsRegistryInterface.php b/src/PhpPact/Consumer/Registry/Interaction/Contents/ContentsRegistryInterface.php deleted file mode 100644 index 3e3394ff..00000000 --- a/src/PhpPact/Consumer/Registry/Interaction/Contents/ContentsRegistryInterface.php +++ /dev/null @@ -1,8 +0,0 @@ -client->call('pactffi_message_with_contents', $this->messageRegistry->getId(), $contentType, $data->getValue(), $data->getSize()); - } -} diff --git a/src/PhpPact/Consumer/Registry/Interaction/InteractionRegistry.php b/src/PhpPact/Consumer/Registry/Interaction/InteractionRegistry.php index 7a917416..70a4b083 100644 --- a/src/PhpPact/Consumer/Registry/Interaction/InteractionRegistry.php +++ b/src/PhpPact/Consumer/Registry/Interaction/InteractionRegistry.php @@ -76,7 +76,7 @@ private function with(ConsumerRequest $request): self ->withRequest($request->getMethod(), $request->getPath()) ->withQueryParameters($request->getQuery()) ->withHeaders($request->getHeaders()) - ->withBody($request->getContentType(), $request->getBody()); + ->withBody($request->getBody()); return $this; } @@ -86,7 +86,7 @@ private function willRespondWith(ProviderResponse $response): self $this->responseRegistry ->withResponse($response->getStatus()) ->withHeaders($response->getHeaders()) - ->withBody($response->getContentType(), $response->getBody()); + ->withBody($response->getBody()); return $this; } diff --git a/src/PhpPact/Consumer/Registry/Interaction/MessageRegistry.php b/src/PhpPact/Consumer/Registry/Interaction/MessageRegistry.php index 2da4e9b5..607b9bdf 100644 --- a/src/PhpPact/Consumer/Registry/Interaction/MessageRegistry.php +++ b/src/PhpPact/Consumer/Registry/Interaction/MessageRegistry.php @@ -2,21 +2,23 @@ namespace PhpPact\Consumer\Registry\Interaction; +use PhpPact\Consumer\Model\Body\Binary; +use PhpPact\Consumer\Model\Body\Text; use PhpPact\Consumer\Model\Message; use PhpPact\Consumer\Model\ProviderState; -use PhpPact\Consumer\Registry\Interaction\Contents\ContentsRegistryInterface; -use PhpPact\Consumer\Registry\Interaction\Contents\MessageContentsRegistry; +use PhpPact\Consumer\Registry\Interaction\Body\BodyRegistryInterface; +use PhpPact\Consumer\Registry\Interaction\Body\MessageContentsRegistry; use PhpPact\Consumer\Registry\Pact\PactRegistryInterface; use PhpPact\FFI\ClientInterface; class MessageRegistry extends AbstractRegistry implements MessageRegistryInterface { - private ContentsRegistryInterface $messageContentsRegistry; + private BodyRegistryInterface $messageContentsRegistry; public function __construct( ClientInterface $client, PactRegistryInterface $pactRegistry, - ?ContentsRegistryInterface $messageContentsRegistry = null + ?BodyRegistryInterface $messageContentsRegistry = null ) { parent::__construct($client, $pactRegistry); $this->messageContentsRegistry = $messageContentsRegistry ?? new MessageContentsRegistry($client, $this); @@ -30,7 +32,7 @@ public function registerMessage(Message $message): void ->given($message->getProviderStates()) ->expectsToReceive($message->getDescription()) ->withMetadata($message->getMetadata()) - ->withContents($message->getContentType(), $message->getContents()); + ->withContents($message->getContents()); } protected function newInteraction(string $description): self @@ -40,9 +42,11 @@ protected function newInteraction(string $description): self return $this; } - private function withContents(?string $contentType = null, ?string $contents = null): self + private function withContents(Text|Binary|null $contents): self { - $this->messageContentsRegistry->withContents($contentType, $contents); + if ($contents) { + $this->messageContentsRegistry->withBody($contents); + } return $this; } diff --git a/src/PhpPact/Consumer/Registry/Interaction/Part/AbstractPartRegistry.php b/src/PhpPact/Consumer/Registry/Interaction/Part/AbstractPartRegistry.php index 9531a07c..787d71dc 100644 --- a/src/PhpPact/Consumer/Registry/Interaction/Part/AbstractPartRegistry.php +++ b/src/PhpPact/Consumer/Registry/Interaction/Part/AbstractPartRegistry.php @@ -2,7 +2,9 @@ namespace PhpPact\Consumer\Registry\Interaction\Part; -use PhpPact\Consumer\Registry\Interaction\Contents\ContentsRegistryInterface; +use PhpPact\Consumer\Model\Body\Binary; +use PhpPact\Consumer\Model\Body\Text; +use PhpPact\Consumer\Registry\Interaction\Body\BodyRegistryInterface; use PhpPact\Consumer\Registry\Interaction\InteractionRegistryInterface; use PhpPact\FFI\ClientInterface; @@ -11,13 +13,15 @@ abstract class AbstractPartRegistry implements PartRegistryInterface public function __construct( protected ClientInterface $client, protected InteractionRegistryInterface $interactionRegistry, - private ContentsRegistryInterface $contentsRegistry + private BodyRegistryInterface $bodyRegistry ) { } - public function withBody(?string $contentType = null, ?string $body = null): self + public function withBody(Text|Binary|null $body): self { - $this->contentsRegistry->withContents($contentType, $body); + if ($body) { + $this->bodyRegistry->withBody($body); + } return $this; } diff --git a/src/PhpPact/Consumer/Registry/Interaction/Part/PartRegistryInterface.php b/src/PhpPact/Consumer/Registry/Interaction/Part/PartRegistryInterface.php index 90a6516b..5f7f59f8 100644 --- a/src/PhpPact/Consumer/Registry/Interaction/Part/PartRegistryInterface.php +++ b/src/PhpPact/Consumer/Registry/Interaction/Part/PartRegistryInterface.php @@ -2,9 +2,12 @@ namespace PhpPact\Consumer\Registry\Interaction\Part; +use PhpPact\Consumer\Model\Body\Binary; +use PhpPact\Consumer\Model\Body\Text; + interface PartRegistryInterface { - public function withBody(?string $contentType = null, ?string $body = null): self; + public function withBody(Text|Binary|null $body): self; /** * @param array $headers diff --git a/src/PhpPact/Consumer/Registry/Interaction/Part/RequestRegistry.php b/src/PhpPact/Consumer/Registry/Interaction/Part/RequestRegistry.php index 026dc57c..890ad9b7 100644 --- a/src/PhpPact/Consumer/Registry/Interaction/Part/RequestRegistry.php +++ b/src/PhpPact/Consumer/Registry/Interaction/Part/RequestRegistry.php @@ -2,8 +2,8 @@ namespace PhpPact\Consumer\Registry\Interaction\Part; -use PhpPact\Consumer\Registry\Interaction\Contents\ContentsRegistryInterface; -use PhpPact\Consumer\Registry\Interaction\Contents\RequestBodyRegistry; +use PhpPact\Consumer\Registry\Interaction\Body\BodyRegistryInterface; +use PhpPact\Consumer\Registry\Interaction\Body\RequestBodyRegistry; use PhpPact\Consumer\Registry\Interaction\InteractionRegistryInterface; use PhpPact\FFI\ClientInterface; @@ -14,7 +14,7 @@ class RequestRegistry extends AbstractPartRegistry implements RequestRegistryInt public function __construct( ClientInterface $client, InteractionRegistryInterface $interactionRegistry, - ?ContentsRegistryInterface $requestBodyRegistry = null + ?BodyRegistryInterface $requestBodyRegistry = null ) { parent::__construct($client, $interactionRegistry, $requestBodyRegistry ?? new RequestBodyRegistry($client, $interactionRegistry)); } diff --git a/src/PhpPact/Consumer/Registry/Interaction/Part/ResponseRegistry.php b/src/PhpPact/Consumer/Registry/Interaction/Part/ResponseRegistry.php index 3649a408..b74c0078 100644 --- a/src/PhpPact/Consumer/Registry/Interaction/Part/ResponseRegistry.php +++ b/src/PhpPact/Consumer/Registry/Interaction/Part/ResponseRegistry.php @@ -2,8 +2,8 @@ namespace PhpPact\Consumer\Registry\Interaction\Part; -use PhpPact\Consumer\Registry\Interaction\Contents\ContentsRegistryInterface; -use PhpPact\Consumer\Registry\Interaction\Contents\ResponseBodyRegistry; +use PhpPact\Consumer\Registry\Interaction\Body\BodyRegistryInterface; +use PhpPact\Consumer\Registry\Interaction\Body\ResponseBodyRegistry; use PhpPact\Consumer\Registry\Interaction\InteractionRegistryInterface; use PhpPact\FFI\ClientInterface; @@ -14,7 +14,7 @@ class ResponseRegistry extends AbstractPartRegistry implements ResponseRegistryI public function __construct( ClientInterface $client, InteractionRegistryInterface $interactionRegistry, - ?ContentsRegistryInterface $responseBodyRegistry = null + ?BodyRegistryInterface $responseBodyRegistry = null ) { parent::__construct($client, $interactionRegistry, $responseBodyRegistry ?? new ResponseBodyRegistry($client, $interactionRegistry)); } 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 } } diff --git a/src/PhpPact/FFI/Model/StringData.php b/src/PhpPact/FFI/Model/StringData.php index 622f457a..82e8fe57 100644 --- a/src/PhpPact/FFI/Model/StringData.php +++ b/src/PhpPact/FFI/Model/StringData.php @@ -23,10 +23,10 @@ public function getSize(): int return $this->size; } - public static function createFrom(string $value): ?self + public static function createFrom(string $value, bool $nullTerminated = true): self { $length = \strlen($value); - $size = $length + 1; + $size = $length + ($nullTerminated ? 1 : 0); $cData = FFI::new("uint8_t[{$size}]"); FFI::memcpy($cData, $value, $length); diff --git a/tests/PhpPact/Consumer/Model/ConsumerRequestTest.php b/tests/PhpPact/Consumer/Model/ConsumerRequestTest.php index f30aeca7..6ddcb9d7 100644 --- a/tests/PhpPact/Consumer/Model/ConsumerRequestTest.php +++ b/tests/PhpPact/Consumer/Model/ConsumerRequestTest.php @@ -3,6 +3,7 @@ namespace PhpPactTest\Consumer\Model; use PhpPact\Consumer\Matcher\Matcher; +use PhpPact\Consumer\Model\Body\Text; use PhpPact\Consumer\Model\ConsumerRequest; use PHPUnit\Framework\TestCase; @@ -24,7 +25,11 @@ public function testSerializing() $this->assertEquals(['Content-Type' => ['application/json']], $model->getHeaders()); $this->assertEquals(['fruit' => ['apple', 'banana']], $model->getQuery()); $this->assertEquals('/somepath', $model->getPath()); - $this->assertEquals('{"currentCity":"Austin"}', $model->getBody()); + + $body = $model->getBody(); + $this->assertInstanceOf(Text::class, $body); + $this->assertEquals('{"currentCity":"Austin"}', $body->getContents()); + $this->assertEquals('application/json', $body->getContentType()); } public function testSerializingWhenPathUsingMatcher() @@ -45,6 +50,10 @@ public function testSerializingWhenPathUsingMatcher() $this->assertEquals(['Content-Type' => ['application/json']], $model->getHeaders()); $this->assertEquals(['food' => ['milk']], $model->getQuery()); $this->assertEquals('{"value":"\/somepath\/474d610b-c6e3-45bd-9f70-529e7ad21df0\/status","regex":"\\\\\\/somepath\\\\\\/[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}\\\\\\/status","pact:matcher:type":"regex"}', $model->getPath()); - $this->assertEquals('{"status":"finished"}', $model->getBody()); + + $body = $model->getBody(); + $this->assertInstanceOf(Text::class, $body); + $this->assertEquals('{"status":"finished"}', $body->getContents()); + $this->assertEquals('application/json', $body->getContentType()); } } diff --git a/tests/PhpPact/Consumer/Model/MessageTest.php b/tests/PhpPact/Consumer/Model/MessageTest.php index 165ed8a8..8182706d 100644 --- a/tests/PhpPact/Consumer/Model/MessageTest.php +++ b/tests/PhpPact/Consumer/Model/MessageTest.php @@ -2,6 +2,7 @@ namespace PhpPactTest\Consumer\Model; +use PhpPact\Consumer\Model\Body\Text; use PhpPact\Consumer\Model\Message; use PhpPact\Consumer\Model\ProviderState; use PHPUnit\Framework\TestCase; @@ -29,6 +30,10 @@ public function testSetters() static::assertEquals($providerStateName, $providerStates[0]->getName()); static::assertEquals($providerStateParams, $providerStates[0]->getParams()); static::assertSame($metadata, $subject->getMetadata()); - static::assertSame($contents, $subject->getContents()); + + $messageContents = $subject->getContents(); + $this->assertInstanceOf(Text::class, $messageContents); + $this->assertEquals($contents, $messageContents->getContents()); + $this->assertEquals('text/plain', $messageContents->getContentType()); } } diff --git a/tests/PhpPact/Consumer/Model/ProviderResponseTest.php b/tests/PhpPact/Consumer/Model/ProviderResponseTest.php index 2da664be..315322e7 100644 --- a/tests/PhpPact/Consumer/Model/ProviderResponseTest.php +++ b/tests/PhpPact/Consumer/Model/ProviderResponseTest.php @@ -2,6 +2,7 @@ namespace PhpPactTest\Consumer\Model; +use PhpPact\Consumer\Model\Body\Text; use PhpPact\Consumer\Model\ProviderResponse; use PHPUnit\Framework\TestCase; @@ -19,6 +20,10 @@ public function testSerializing() $this->assertEquals(200, $model->getStatus()); $this->assertEquals(['Content-Type' => ['application/json']], $model->getHeaders()); - $this->assertEquals('{"currentCity":"Austin"}', $model->getBody()); + + $body = $model->getBody(); + $this->assertInstanceOf(Text::class, $body); + $this->assertEquals('{"currentCity":"Austin"}', $body->getContents()); + $this->assertEquals('application/json', $body->getContentType()); } } diff --git a/tests/PhpPact/FFI/Model/StringDataTest.php b/tests/PhpPact/FFI/Model/StringDataTest.php new file mode 100644 index 00000000..075399b6 --- /dev/null +++ b/tests/PhpPact/FFI/Model/StringDataTest.php @@ -0,0 +1,43 @@ +getValue(); + $size = \strlen($value) + 1; + + $this->assertSame($size, FFI::sizeof($cData)); + $this->assertSame($value, $this->cDataToString($cData, $size - 1)); // ignore null + } + + public function testCreateBinaryString() + { + $value = file_get_contents(__DIR__ . '/../../../_resources/image.jpg'); + $stringData = StringData::createFrom($value, false); + $cData = $stringData->getValue(); + $size = \strlen($value); + + $this->assertSame($size, FFI::sizeof($cData)); + $this->assertEquals($value, $this->cDataToString($cData, $size)); + } + + private function cDataToString(CData $cData, int $size): string + { + $result = ''; + for ($index = 0; $index < $size; $index++) { + $result .= chr($cData[$index]); // @phpstan-ignore-line + } + + return $result; + } +} diff --git a/tests/PhpPact/Standalone/ProviderVerifier/VerifierTest.php b/tests/PhpPact/Standalone/ProviderVerifier/VerifierTest.php index 245d60a5..cf302a7c 100644 --- a/tests/PhpPact/Standalone/ProviderVerifier/VerifierTest.php +++ b/tests/PhpPact/Standalone/ProviderVerifier/VerifierTest.php @@ -55,7 +55,7 @@ public function testVerify(): void } $verifier = new Verifier($config); - $verifier->addDirectory(__DIR__ . '/../../../_resources'); + $verifier->addFile(__DIR__ . '/../../../_resources/someconsumer-someprovider.json'); $verifyResult = $verifier->verify(); diff --git a/tests/PhpPact/Standalone/StubServer/StubServerConfigTest.php b/tests/PhpPact/Standalone/StubServer/StubServerConfigTest.php index 6ae7c400..1e0e77f3 100644 --- a/tests/PhpPact/Standalone/StubServer/StubServerConfigTest.php +++ b/tests/PhpPact/Standalone/StubServer/StubServerConfigTest.php @@ -18,7 +18,7 @@ public function testSetters() $providerStateHeaderName = 'header'; $token = 'token'; $user = 'user:password'; - $dirs = [__DIR__ . '/../../../_resources']; + $dirs = ['/path/to/pacts']; $files = ['/path/to/pact.json']; $urls = ['http://example.com/path/to/file.json']; $consumerNames = ['consumer-1', 'consumer-2']; diff --git a/tests/_resources/image.jpg b/tests/_resources/image.jpg new file mode 100644 index 00000000..8c1702a1 Binary files /dev/null and b/tests/_resources/image.jpg differ