diff --git a/example/json/consumer/tests/Service/ConsumerServiceMultipleInteractionsTest.php b/example/json/consumer/tests/Service/ConsumerServiceMultipleInteractionsTest.php index 54b29814..c1bf6223 100644 --- a/example/json/consumer/tests/Service/ConsumerServiceMultipleInteractionsTest.php +++ b/example/json/consumer/tests/Service/ConsumerServiceMultipleInteractionsTest.php @@ -48,6 +48,13 @@ public function testGetHelloString() } $builder = new InteractionBuilder($config); $builder + ->given('User exist', [ + 'user' => [ + 'name' => 'Bob', + 'id' => '0c447df2-20ae-469d-ae52-b1258c06dabe', + 'dob' => '1993-06-22', + ], + ]) ->uponReceiving('A get request to /hello/{name}') ->with($request) ->willRespondWith($response, false); // Don't start the mock server yet, because there will me more interactions. diff --git a/example/json/pacts/jsonConsumer-jsonProvider.json b/example/json/pacts/jsonConsumer-jsonProvider.json index 78989191..ec811c63 100644 --- a/example/json/pacts/jsonConsumer-jsonProvider.json +++ b/example/json/pacts/jsonConsumer-jsonProvider.json @@ -54,9 +54,51 @@ } ] } - }, - "header": {}, - "status": {} + } + }, + "status": 200 + } + }, + { + "description": "A get request to /hello/{name}", + "providerStates": [ + { + "name": "User exist", + "params": { + "user": { + "dob": "1993-06-22", + "id": "0c447df2-20ae-469d-ae52-b1258c06dabe", + "name": "Bob" + } + } + } + ], + "request": { + "headers": { + "Content-Type": "application/json" + }, + "method": "GET", + "path": "/hello/Bob" + }, + "response": { + "body": { + "message": "Hello, Bob" + }, + "headers": { + "Content-Type": "application/json" + }, + "matchingRules": { + "body": { + "$.message": { + "combine": "AND", + "matchers": [ + { + "match": "regex", + "regex": "(Hello, )[A-Za-z]+" + } + ] + } + } }, "status": 200 } diff --git a/src/PhpPact/Consumer/AbstractMessageBuilder.php b/src/PhpPact/Consumer/AbstractMessageBuilder.php index 17c4fc36..5fe13e42 100644 --- a/src/PhpPact/Consumer/AbstractMessageBuilder.php +++ b/src/PhpPact/Consumer/AbstractMessageBuilder.php @@ -15,7 +15,7 @@ public function __construct() /** * @param string $name what is given to the request - * @param array $params for that request + * @param array $params for that request * @param bool $overwrite clear pass states completely and start this array */ public function given(string $name, array $params = [], bool $overwrite = false): self diff --git a/src/PhpPact/Consumer/InteractionBuilder.php b/src/PhpPact/Consumer/InteractionBuilder.php index 356df997..1cc9fd08 100644 --- a/src/PhpPact/Consumer/InteractionBuilder.php +++ b/src/PhpPact/Consumer/InteractionBuilder.php @@ -31,7 +31,7 @@ public function newInteraction(): void /** * @param string $providerState what is given to the request - * @param array $params for that request + * @param array $params for that request * @param bool $overwrite clear pass states completely and start this array */ public function given(string $providerState, array $params = [], bool $overwrite = false): self diff --git a/src/PhpPact/Consumer/Model/ProviderState.php b/src/PhpPact/Consumer/Model/ProviderState.php index acfa571d..05ef1104 100644 --- a/src/PhpPact/Consumer/Model/ProviderState.php +++ b/src/PhpPact/Consumer/Model/ProviderState.php @@ -30,12 +30,12 @@ public function getParams(): array } /** - * @param array $params + * @param array $params */ public function setParams(array $params = []): void { foreach ($params as $key => $value) { - $this->addParam($key, $value); + $this->addParam($key, is_string($value) ? $value : json_encode($value, JSON_THROW_ON_ERROR)); } } diff --git a/src/PhpPact/Consumer/Model/ProviderStates.php b/src/PhpPact/Consumer/Model/ProviderStates.php index 09c09ece..61c7040a 100644 --- a/src/PhpPact/Consumer/Model/ProviderStates.php +++ b/src/PhpPact/Consumer/Model/ProviderStates.php @@ -19,7 +19,7 @@ public function getProviderStates(): array /** * @param string $name - * @param array $params + * @param array $params * @param bool $overwrite * * @return array @@ -33,7 +33,7 @@ public function setProviderState(string $name, array $params = [], bool $overwri /** * @param string $name - * @param array $params + * @param array $params * @param bool $overwrite - if true reset the entire state * * @return $this diff --git a/tests/PhpPact/Consumer/Model/InteractionTest.php b/tests/PhpPact/Consumer/Model/InteractionTest.php index 1458a760..1c5c505a 100644 --- a/tests/PhpPact/Consumer/Model/InteractionTest.php +++ b/tests/PhpPact/Consumer/Model/InteractionTest.php @@ -124,4 +124,30 @@ public function testAddProviderState(bool $overwrite): void $this->assertSame(['key 2' => 'value 2'], $providerState->getParams()); } } + + public function testAddProviderStateWithParamMixedValue(): void + { + $this->interaction->addProviderState('provider state 1', ['string value' => 'test']); + $this->interaction->addProviderState('provider state 2', ['number value' => 123]); + $this->interaction->addProviderState('provider state 3', ['array value' => ['value 1', 'value 2', 'value 3']]); + $this->interaction->addProviderState('provider state 4', ['object value' => (object) ['key 1' => 'value 1', 'key 2' => 'value 2']]); + $providerStates = $this->interaction->getProviderStates(); + $this->assertCount(4, $providerStates); + $providerState = reset($providerStates); + $this->assertInstanceOf(ProviderState::class, $providerState); + $this->assertSame('provider state 1', $providerState->getName()); + $this->assertSame(['string value' => 'test'], $providerState->getParams()); + $providerState = next($providerStates); + $this->assertInstanceOf(ProviderState::class, $providerState); + $this->assertSame('provider state 2', $providerState->getName()); + $this->assertSame(['number value' => '123'], $providerState->getParams()); + $providerState = next($providerStates); + $this->assertInstanceOf(ProviderState::class, $providerState); + $this->assertSame('provider state 3', $providerState->getName()); + $this->assertSame(['array value' => '["value 1","value 2","value 3"]'], $providerState->getParams()); + $providerState = next($providerStates); + $this->assertInstanceOf(ProviderState::class, $providerState); + $this->assertSame('provider state 4', $providerState->getName()); + $this->assertSame(['object value' => '{"key 1":"value 1","key 2":"value 2"}'], $providerState->getParams()); + } } diff --git a/tests/PhpPact/Consumer/Model/MessageTest.php b/tests/PhpPact/Consumer/Model/MessageTest.php index dc303362..fb268333 100644 --- a/tests/PhpPact/Consumer/Model/MessageTest.php +++ b/tests/PhpPact/Consumer/Model/MessageTest.php @@ -138,4 +138,30 @@ public function testMultipartContents(): void $multipart = new Multipart([], 'abc123'); $this->message->setContents($multipart); } + + public function testAddProviderStateWithParamMixedValue(): void + { + $this->message->addProviderState('provider state 1', ['string value' => 'test']); + $this->message->addProviderState('provider state 2', ['number value' => 123]); + $this->message->addProviderState('provider state 3', ['array value' => ['value 1', 'value 2', 'value 3']]); + $this->message->addProviderState('provider state 4', ['object value' => (object) ['key 1' => 'value 1', 'key 2' => 'value 2']]); + $providerStates = $this->message->getProviderStates(); + $this->assertCount(4, $providerStates); + $providerState = reset($providerStates); + $this->assertInstanceOf(ProviderState::class, $providerState); + $this->assertSame('provider state 1', $providerState->getName()); + $this->assertSame(['string value' => 'test'], $providerState->getParams()); + $providerState = next($providerStates); + $this->assertInstanceOf(ProviderState::class, $providerState); + $this->assertSame('provider state 2', $providerState->getName()); + $this->assertSame(['number value' => '123'], $providerState->getParams()); + $providerState = next($providerStates); + $this->assertInstanceOf(ProviderState::class, $providerState); + $this->assertSame('provider state 3', $providerState->getName()); + $this->assertSame(['array value' => '["value 1","value 2","value 3"]'], $providerState->getParams()); + $providerState = next($providerStates); + $this->assertInstanceOf(ProviderState::class, $providerState); + $this->assertSame('provider state 4', $providerState->getName()); + $this->assertSame(['object value' => '{"key 1":"value 1","key 2":"value 2"}'], $providerState->getParams()); + } }