Skip to content

Commit b6674a6

Browse files
authored
feat: add naming for expecations (#30)
1 parent a92b6d0 commit b6674a6

6 files changed

Lines changed: 54 additions & 8 deletions

File tree

README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,26 @@ return an empty response with the status code 200.
190190
Advance
191191
-------
192192

193+
### Naming Expectations
194+
195+
If you have to add many expectations in the same test szenario, it can be difficult to differentiate between that
196+
expectations. For that case it is possible to give each expectation a custom name. By default,
197+
the name is build from method and path.
198+
199+
```php
200+
use Nivseb\PhpMockServerConnector\PhpUnit\UseMockServer;
201+
use Nivseb\PhpMockServerConnector\PhpUnit\MockServerEndpoint;
202+
use PHPUnit\Framework\TestCase;
203+
use GuzzleHttp\Client;
204+
205+
$mockServer = new MockServerEndpoint('/');
206+
$mockServer->allows('GET', '/records/1')->andReturn(200); // result to name `GET /records/1`
207+
$mockServer->allows('GET', '/records/2')->name('Load Record 2')->andReturn(200); // result to name `Load Record 2`
208+
$mockServer->allows('GET', '/records/3')->name('Load Record 3')->andReturn(200); // result to name `Load Record 3`
209+
```
210+
211+
### Duplicate Expectation
212+
193213
In some cases you expect nearly same request twice, but only with a little change in the response or request. In that case,
194214
you can build a new expectation from an existing and add your changes to the new one. this example shows a definition that
195215
expect 2 requests and answer the first one with a 200 response and the second call to with a 304 response.
@@ -205,7 +225,6 @@ expect 2 requests and answer the first one with a 200 response and the second ca
205225
$mockServer->duplicate($firstExpectation)->andReturn(304);
206226
```
207227

208-
209228
Example
210229
-------
211230

src/Exception/UnsuccessfulVerificationException.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ public function __construct(
1414
public ResponseInterface $response,
1515
?Throwable $previous = null
1616
) {
17+
$message .= ' for expectation `';
18+
$message .= $expectation->expectation->getName();
19+
$message .= '`';
20+
1721
parent::__construct($message, previous: $previous);
1822
}
1923
}

src/Expectation/PendingExpectation.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ public function __destruct()
3838

3939
public function __clone(): void
4040
{
41-
$this->expectation = clone $this->expectation;
41+
$this->expectation = clone $this->expectation;
42+
if ($this->expectation->name) {
43+
$this->expectation->name = null;
44+
}
4245
$this->remoteExpectation = null;
4346
}
4447

@@ -57,7 +60,7 @@ public function run(): void
5760
*
5861
* @throws AlreadyExpectedExpectationException
5962
*/
60-
public function andReturn(int $statusCode, null|array|string $responseBody = null, ?array $headers = null): static
63+
public function andReturn(int $statusCode, array|string|null $responseBody = null, ?array $headers = null): static
6164
{
6265
if ($this->remoteExpectation) {
6366
throw new AlreadyExpectedExpectationException($this->remoteExpectation);
@@ -174,4 +177,18 @@ public function once(): static
174177
{
175178
return $this->times(1);
176179
}
180+
181+
/**
182+
* @throws AlreadyExpectedExpectationException
183+
*/
184+
public function name(string $expectationName): static
185+
{
186+
if ($this->remoteExpectation) {
187+
throw new AlreadyExpectedExpectationException($this->remoteExpectation);
188+
}
189+
190+
$this->expectation->name = $expectationName;
191+
192+
return $this;
193+
}
177194
}

src/Server/MockServerEndpoint.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function registerExpectation(RemoteExpectation $expectation): void
3434
}
3535

3636
/**
37-
* @return array<string, MockServerEndpoint>
37+
* @return array<string, RemoteExpectation>
3838
*/
3939
public function getExpectations(): array
4040
{

src/Structs/MockServerExpectation.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,19 @@ public function __construct(
1313
public readonly string $method,
1414
public readonly string $url,
1515
public int $responseStatusCode = 200,
16-
public null|array|string $responseBody = null,
16+
public array|string|null $responseBody = null,
1717
public ?array $responseHeaders = null,
1818
public int $atLeast = 1,
1919
public int $atMost = 1,
2020
public ?array $pathParameters = null,
2121
public ?array $queryParameters = null,
2222
public ?array $requestHeaders = null,
23-
public null|array|string $requestBody = null,
23+
public array|string|null $requestBody = null,
24+
public ?string $name = null,
2425
) {}
26+
27+
public function getName(): string
28+
{
29+
return $this->name ?: $this->method.' '.$this->url;
30+
}
2531
}

tests/Unit/ConnectorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ protected function buildClient(string $mockServerUrl): Client
306306
->toThrow(
307307
function (UnsuccessfulVerificationException $exception) use ($remoteExpectation, $response): void {
308308
expect($exception->getMessage())
309-
->toBe('Request not found exactly 1 times')
309+
->toBe('Request not found exactly 1 times for expectation `METHOD /path`')
310310
->and($exception->expectation)->toBe($remoteExpectation)
311311
->and($exception->response)->toBe($response);
312312
}
@@ -376,7 +376,7 @@ protected function buildClient(string $mockServerUrl): Client
376376
->toThrow(
377377
function (UnsuccessfulVerificationException $exception) use ($remoteExpectation, $response): void {
378378
expect($exception->getMessage())
379-
->toBe('Request not found exactly 1 times')
379+
->toBe('Request not found exactly 1 times for expectation `METHOD /path`')
380380
->and($exception->expectation)->toBe($remoteExpectation)
381381
->and($exception->response)->toBe($response);
382382
}

0 commit comments

Comments
 (0)