Skip to content

Commit

Permalink
feat: Provider state > allow param mixed value
Browse files Browse the repository at this point in the history
  • Loading branch information
tienvx committed Oct 31, 2024
1 parent 0e38dd2 commit e2ebe2d
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
48 changes: 45 additions & 3 deletions example/json/pacts/jsonConsumer-jsonProvider.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion src/PhpPact/Consumer/AbstractMessageBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function __construct()

/**
* @param string $name what is given to the request
* @param array<string, string> $params for that request
* @param array<string, mixed> $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
Expand Down
2 changes: 1 addition & 1 deletion src/PhpPact/Consumer/InteractionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function newInteraction(): void

/**
* @param string $providerState what is given to the request
* @param array<string, string> $params for that request
* @param array<string, mixed> $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
Expand Down
4 changes: 2 additions & 2 deletions src/PhpPact/Consumer/Model/ProviderState.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ public function getParams(): array
}

/**
* @param array<string, string> $params
* @param array<string, mixed> $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));

Check failure on line 38 in src/PhpPact/Consumer/Model/ProviderState.php

View workflow job for this annotation

GitHub Actions / php-cs

Parameter #2 $value of method PhpPact\Consumer\Model\ProviderState::addParam() expects string, string|false given.
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/PhpPact/Consumer/Model/ProviderStates.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function getProviderStates(): array

/**
* @param string $name
* @param array<string, string> $params
* @param array<string, mixed> $params
* @param bool $overwrite
*
* @return array<int, ProviderState>
Expand All @@ -33,7 +33,7 @@ public function setProviderState(string $name, array $params = [], bool $overwri

/**
* @param string $name
* @param array<string, string> $params
* @param array<string, mixed> $params
* @param bool $overwrite - if true reset the entire state
*
* @return $this
Expand Down
26 changes: 26 additions & 0 deletions tests/PhpPact/Consumer/Model/InteractionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
26 changes: 26 additions & 0 deletions tests/PhpPact/Consumer/Model/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit e2ebe2d

Please sign in to comment.