Skip to content

Commit 281886d

Browse files
committed
Add seeMockRequestWasNotCalled verb and some tests
1 parent fadef65 commit 281886d

File tree

4 files changed

+144
-2
lines changed

4 files changed

+144
-2
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
{
2+
"id": "get-post-2",
3+
"httpRequest": {
4+
"method": "GET",
5+
"path": "/posts/2",
6+
"headers": {
7+
"content-length": [
8+
"0"
9+
],
10+
"User-Agent": [
11+
"GuzzleHttp/7"
12+
],
13+
"Host": [
14+
"jsonplaceholder.typicode.com"
15+
]
16+
},
17+
"keepAlive": true,
18+
"secure": true,
19+
"protocol": "HTTP_1_1",
20+
"localAddress": "localhost/127.0.0.1:1080",
21+
"remoteAddress": "127.0.0.1:35156"
22+
},
23+
"httpResponse": {
24+
"statusCode": 200,
25+
"reasonPhrase": "OK",
26+
"headers": {
27+
"alt-svc": [
28+
"h3=\":443\"; ma=86400, h3-29=\":443\"; ma=86400"
29+
],
30+
"X-Ratelimit-Reset": [
31+
"1663541001"
32+
],
33+
"X-Ratelimit-Remaining": [
34+
"999"
35+
],
36+
"X-Ratelimit-Limit": [
37+
"1000"
38+
],
39+
"X-Powered-By": [
40+
"Express"
41+
],
42+
"X-Content-Type-Options": [
43+
"nosniff"
44+
],
45+
"Via": [
46+
"1.1 vegur"
47+
],
48+
"Vary": [
49+
"Origin, Accept-Encoding"
50+
],
51+
"Server-Timing": [
52+
"cf-q-config;dur=3.999999989901e-06"
53+
],
54+
"Server": [
55+
"cloudflare"
56+
],
57+
"Report-To": [
58+
"{\"endpoints\":[{\"url\":\"https:\\/\\/a.nel.cloudflare.com\\/report\\/v3?s=8bXL92eq%2BaEEjrC7gOV1Q4BBYz%2F18b9cuz%2Bm59VP4iUzDEjH9G2uxRK%2BxKIr6xoYfxmrmTqybLrsyYpmEZVf0IzSMfA7XizRycbnUCkM67ezH0PH6jGfb5%2FEMv8NX%2F6AusKf%2FqhFab1HWvcD5ZrD\"}],\"group\":\"cf-nel\",\"max_age\":604800}"
59+
],
60+
"Pragma": [
61+
"no-cache"
62+
],
63+
"NEL": [
64+
"{\"success_fraction\":0,\"report_to\":\"cf-nel\",\"max_age\":604800}"
65+
],
66+
"Expires": [
67+
"-1"
68+
],
69+
"Etag": [
70+
"W/\"124-yiKdLzqO5gfBrJFrcdJ8Yq0LGnU\""
71+
],
72+
"Date": [
73+
"Sat, 04 Feb 2023 07:40:35 GMT"
74+
],
75+
"Content-Type": [
76+
"application/json; charset=utf-8"
77+
],
78+
"Cache-Control": [
79+
"max-age=43200"
80+
],
81+
"CF-RAY": [
82+
"7941c252df112f93-MAD"
83+
],
84+
"CF-Cache-Status": [
85+
"HIT"
86+
],
87+
"Age": [
88+
"25601"
89+
],
90+
"Access-Control-Allow-Credentials": [
91+
"true"
92+
]
93+
},
94+
"body": {
95+
"userId": 1,
96+
"id": 2,
97+
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
98+
"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
99+
}
100+
}
101+
}

src/MockServerHelper.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,9 @@ public function seeMockRequestWasCalled(string $expectationId, ?int $times = nul
3131
);
3232
}
3333

34+
public function seeMockRequestWasNotCalled(string $expectationId): void
35+
{
36+
$this->seeMockRequestWasCalled($expectationId, 0);
37+
}
38+
3439
}

tests/Integration/SeeMockRequestWasCalledTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ protected function setUp(): void
1919
$this->client = new Client(['proxy' => 'http://mockserver:1080', 'verify' => false]);
2020
}
2121

22-
public function testExpectationWasCalledNotThrowException(): void
22+
public function testExpectationWasCalled(): void
2323
{
24-
$this->client->request('GET', 'https://jsonplaceholder.typicode.com/posts/1', []);
24+
$this->client->request('GET', 'https://jsonplaceholder.typicode.com/posts/1');
2525
$this->sot->seeMockRequestWasCalled('get-post-1');
2626
}
2727

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Integration;
4+
5+
use DEVizzent\CodeceptionMockServerHelper\MockServerHelper;
6+
use GuzzleHttp\Client;
7+
use PHPUnit\Framework\ExpectationFailedException;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class SeeMockRequestWasNotCalledTest extends TestCase
11+
{
12+
private MockServerHelper $sot;
13+
private Client $client;
14+
15+
protected function setUp(): void
16+
{
17+
parent::setUp();
18+
$this->sot = new MockServerHelper();
19+
$this->client = new Client(['proxy' => 'http://mockserver:1080', 'verify' => false]);
20+
}
21+
22+
public function testExpectationWasNotCalled(): void
23+
{
24+
$this->sot->seeMockRequestWasNotCalled('get-post-2');
25+
}
26+
27+
public function testExpectationWasCalledThrowException(): void
28+
{
29+
$this->expectException(ExpectationFailedException::class);
30+
$this->expectExceptionMessage(
31+
'No expectation found with id not-existing-expectation' . PHP_EOL
32+
. 'Failed asserting that 400 matches expected 202.'
33+
);
34+
$this->sot->seeMockRequestWasNotCalled('not-existing-expectation');
35+
}
36+
}

0 commit comments

Comments
 (0)