Skip to content

Commit 45dedf2

Browse files
committed
Add not-matched-request as an error
1 parent 535df0c commit 45dedf2

File tree

4 files changed

+112
-4
lines changed

4 files changed

+112
-4
lines changed

src/MockServerHelper.php

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,27 @@
1111

1212
class MockServerHelper extends Module
1313
{
14+
const CONFIG_NOT_MATCHED_REQUEST = 'not-matched-request';
15+
const NOT_MATCHED_REQUEST_ACTIVATED = 'activated';
16+
const CONFIG_URL = 'url';
1417
private Client $mockserverClient;
1518
public function __construct(ModuleContainer $moduleContainer, ?array $config = null)
1619
{
17-
$this->requiredFields = ['url'];
20+
$this->requiredFields = [self::CONFIG_URL];
1821
$this->config['cleanupBefore'] = 'test';
22+
$this->config[self::CONFIG_NOT_MATCHED_REQUEST] = self::NOT_MATCHED_REQUEST_ACTIVATED;
1923
parent::__construct($moduleContainer, $config);
2024
}
2125

2226

2327
public function _initialize(): void
2428
{
2529
$this->mockserverClient = new Client([
26-
'base_uri' => $this->config['url']
30+
'base_uri' => $this->config[self::CONFIG_URL]
2731
]);
32+
if (self::NOT_MATCHED_REQUEST_ACTIVATED === $this->config[self::CONFIG_NOT_MATCHED_REQUEST]) {
33+
$this->createMockRequest(file_get_contents(__DIR__.'/not-matched-request.json'));
34+
}
2835
}
2936

3037
public function _beforeSuite(array $settings = []): void
@@ -62,6 +69,22 @@ public function seeMockRequestWasNotCalled(string $expectationId): void
6269
$this->seeMockRequestWasCalled($expectationId, 0);
6370
}
6471

72+
public function createMockRequest(string $json): void
73+
{
74+
$request = new Request(
75+
'PUT',
76+
'/mockserver/expectation',
77+
['Content-Type' => 'application/json'],
78+
$json
79+
);
80+
$response = $this->mockserverClient->sendRequest($request);
81+
Assert::assertEquals(
82+
201,
83+
$response->getStatusCode(),
84+
$response->getBody()->getContents()
85+
);
86+
}
87+
6588
public function clearMockServerLogs(): void
6689
{
6790
$request = new Request('PUT', '/mockserver/clear?type=log');
@@ -73,4 +96,19 @@ public function clearMockServerLogs(): void
7396
);
7497

7598
}
99+
100+
public function deactivateNotMatchedRequest(): void
101+
{
102+
$body = json_encode([
103+
'id' => self::CONFIG_NOT_MATCHED_REQUEST
104+
]);
105+
$request = new Request('PUT', '/mockserver/clear?type=expectations', [], $body);
106+
$response = $this->mockserverClient->sendRequest($request);
107+
Assert::assertEquals(
108+
200,
109+
$response->getStatusCode(),
110+
$response->getBody()->getContents()
111+
);
112+
113+
}
76114
}

src/not-matched-request.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"id": "not-matched-request",
3+
"priority": -100,
4+
"httpRequest": {},
5+
"httpResponse": {
6+
"statusCode": 500,
7+
"reasonPhrase": "Internal Server Error",
8+
"body": {
9+
"message": "Request not matched by MockServer"
10+
}
11+
}
12+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace Test\DEVizzent\CodeceptionMockServerHelper\Integration;
4+
5+
use Codeception\Lib\ModuleContainer;
6+
use DEVizzent\CodeceptionMockServerHelper\MockServerHelper;
7+
use GuzzleHttp\Client;
8+
use PHPUnit\Framework\ExpectationFailedException;
9+
use PHPUnit\Framework\TestCase;
10+
11+
class NotMatchedRequestTest extends TestCase
12+
{
13+
const NOT_MATCHED_URI = 'https://jsonplaceholder.typicode.com/posts/3';
14+
private MockServerHelper $sot;
15+
private Client $client;
16+
17+
private function initialize(string $notMatchedRequestConfig): void
18+
{
19+
$moduleContainer = $this->createMock(ModuleContainer::class);
20+
$this->sot = new MockServerHelper(
21+
$moduleContainer,
22+
['url' => 'http://mockserver:1080', 'not-matched-request' => $notMatchedRequestConfig]
23+
);
24+
$this->sot->_initialize();
25+
$this->client = new Client(['proxy' => 'http://mockserver:1080', 'verify' => false]);
26+
$this->sot->clearMockServerLogs();
27+
}
28+
29+
protected function tearDown(): void
30+
{
31+
parent::tearDown();
32+
$this->sot->deactivateNotMatchedRequest();
33+
}
34+
35+
public function testActivateNotMatchedRequestCreateExpectation()
36+
{
37+
$this->initialize('activated');
38+
$this->sot->seeMockRequestWasNotCalled('not-matched-request');
39+
}
40+
41+
public function testActivateNotMatchedRequestWasCreatedAndCalled()
42+
{
43+
$this->initialize('activated');
44+
$this->client->request('GET', self::NOT_MATCHED_URI, ['http_errors' => false]);
45+
$this->sot->seeMockRequestWasCalled('not-matched-request', 1);
46+
}
47+
48+
public function testActivateNotMatchedRequestWasCreatedAndDeactivated()
49+
{
50+
$this->initialize('activated');
51+
$this->sot->deactivateNotMatchedRequest();
52+
$this->client->request('GET', self::NOT_MATCHED_URI, ['http_errors' => false]);
53+
$this->sot->seeMockRequestWasNotCalled('not-matched-request');
54+
}
55+
56+
57+
}

tests/Integration/SeeMockRequestWasNotCalledTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ public function testExpectationWasNotCalled(): void
2929
}
3030

3131
public function testExpectationWasNotCalledButItWasThrowException(): void
32-
{$this->expectException(ExpectationFailedException::class);
32+
{
33+
$this->expectException(ExpectationFailedException::class);
3334
$this->expectExceptionMessageMatches(
3435
'#^Request not found exactly 0 times, expected:((.|\n)*) but was:((.|\n)*)'
3536
. 'Failed asserting that 406 matches expected 202\.$#'
@@ -38,7 +39,7 @@ public function testExpectationWasNotCalledButItWasThrowException(): void
3839
$this->sot->seeMockRequestWasNotCalled('get-post-2');
3940
}
4041

41-
public function testExpectationWasCalledThrowException(): void
42+
public function testExpectationWasCalledNotExistExpectationThrowException(): void
4243
{
4344
$this->expectException(ExpectationFailedException::class);
4445
$this->expectExceptionMessage(

0 commit comments

Comments
 (0)