Skip to content

Commit 73e4cc4

Browse files
committed
Add new verb seeAllRequestWereMatched
1 parent db7a61f commit 73e4cc4

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

src/MockServerHelper.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use GuzzleHttp\Client;
1111
use GuzzleHttp\Psr7\Request;
1212
use PHPUnit\Framework\Assert;
13+
use PHPUnit\Framework\ExpectationFailedException;
1314

1415
class MockServerHelper extends Module
1516
{
@@ -86,6 +87,21 @@ public function seeMockRequestWasNotCalled(string $expectationId): void
8687
$this->seeMockRequestWasCalled($expectationId, 0);
8788
}
8889

90+
public function seeAllRequestWereMatched(): void
91+
{
92+
if (!$this->notMatchedRequest->isEnabled()) {
93+
throw new ExpectationFailedException(
94+
'\'seeAllRequestWereMatched\' can\'t be used without enable notMatchedRequest in config.'
95+
);
96+
}
97+
try {
98+
$this->seeMockRequestWasCalled(self::NOT_MATCHED_REQUEST_ID, 0);
99+
} catch (ExpectationFailedException $exception) {
100+
$message = 'REQUEST NOT MATCHED' . strstr($exception->getMessage(), ' was:');
101+
throw new ExpectationFailedException($message);
102+
}
103+
}
104+
89105
public function createMockRequest(string $json): void
90106
{
91107
$request = new Request(
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace Test\DEVizzent\CodeceptionMockServerHelper\Integration;
4+
5+
use Codeception\Lib\ModuleContainer;
6+
use DEVizzent\CodeceptionMockServerHelper\Config\NotMatchedRequest;
7+
use DEVizzent\CodeceptionMockServerHelper\MockServerHelper;
8+
use GuzzleHttp\Client;
9+
use PHPUnit\Framework\ExpectationFailedException;
10+
use PHPUnit\Framework\TestCase;
11+
12+
class SeeAllRequestWereMatchedTest extends TestCase
13+
{
14+
private MockServerHelper $sot;
15+
private Client $client;
16+
17+
protected function initialize(string $notMatchedRequest): void
18+
{
19+
$moduleContainer = $this->createMock(ModuleContainer::class);
20+
$config = ['url' => 'http://mockserver:1080', 'notMatchedRequest' => $notMatchedRequest];
21+
$this->sot = new MockServerHelper($moduleContainer, $config);
22+
$this->sot->_initialize();
23+
$this->client = new Client(['proxy' => 'http://mockserver:1080', 'verify' => false]);
24+
$this->sot->clearMockServerLogs();
25+
}
26+
27+
public function testAllRequestWereMatchedWhenConfigDisabledThrowException(): void
28+
{
29+
$this->initialize(NotMatchedRequest::DISABLED);
30+
$this->expectException(ExpectationFailedException::class);
31+
$this->expectExceptionMessage(
32+
'\'seeAllRequestWereMatched\' can\'t be used without enable notMatchedRequest in config.'
33+
);
34+
$this->sot->seeAllRequestWereMatched();
35+
}
36+
37+
public function testAllRequestWereMatched(): void
38+
{
39+
$this->initialize(NotMatchedRequest::ENABLED);
40+
$this->client->request('GET', 'https://jsonplaceholder.typicode.com/posts/1');
41+
$this->client->request('GET', 'https://jsonplaceholder.typicode.com/posts/2');
42+
$this->sot->seeAllRequestWereMatched();
43+
}
44+
45+
public function testNotAllRequestWereMatchedThrowException(): void
46+
{
47+
$this->initialize(NotMatchedRequest::ENABLED);
48+
$this->expectException(ExpectationFailedException::class);
49+
$this->expectExceptionMessageMatches('/REQUEST NOT MATCHED was:.*/');
50+
$this->client->request('GET', 'https://jsonplaceholder.typicode.com/posts/1');
51+
$this->client->request(
52+
'GET',
53+
'https://jsonplaceholder.typicode.com/posts/3',
54+
['http_errors' => false]
55+
);
56+
$this->sot->seeAllRequestWereMatched();
57+
}
58+
}

0 commit comments

Comments
 (0)