Skip to content

Commit cf090b6

Browse files
committed
I-5 Fix codestyle and phpstan
1 parent 4a06474 commit cf090b6

File tree

3 files changed

+37
-11
lines changed

3 files changed

+37
-11
lines changed

src/Client/MockServer.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@ public function verify(string $expectationId, ?int $times = null): void
3535
);
3636
}
3737

38+
/**
39+
* @return array<int, array{
40+
* method: string,
41+
* path: string,
42+
* headers:array<string, string>,
43+
* keepAlive: bool,
44+
* secure: bool,
45+
* protocol: string,
46+
* body?: string
47+
* }>
48+
* @throws \Psr\Http\Client\ClientExceptionInterface
49+
*/
3850
public function getNotMatchedRequests(): array
3951
{
4052
$notMatchedRequests = [];
@@ -49,6 +61,7 @@ public function getNotMatchedRequests(): array
4961
}
5062
return $notMatchedRequests;
5163
}
64+
5265
public function create(string $json): void
5366
{
5467
$request = new Request(
@@ -64,6 +77,7 @@ public function create(string $json): void
6477
$response->getBody()->getContents()
6578
);
6679
}
80+
6781
public function removeById(string $mockRequestId): void
6882
{
6983
$body = json_encode([
@@ -78,6 +92,7 @@ public function removeById(string $mockRequestId): void
7892
$response->getBody()->getContents()
7993
);
8094
}
95+
8196
public function removeAllExpectations(): void
8297
{
8398
$request = new Request('PUT', '/mockserver/clear?type=expectations');

src/MockServerHelper.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,13 @@ public function seeMockRequestWasCalled(string $expectationId, ?int $times = nul
9191
} catch (AssertionFailedError $exception) {
9292
//throw $exception;
9393
preg_match('#(.|\n)* expected:<(?<expected>\{(.|\n)*\})> but was:<(.|\n)*>#', $exception->getMessage(), $matches);
94-
if (empty($matches)) {
94+
if (!isset($matches['expected'])) {
9595
throw $exception;
9696
}
9797
$expected = json_decode($matches['expected'], true);
9898
$notMatchedRequests = $this->mockserver->getNotMatchedRequests();
9999
$currentSimilityRatio = 0;
100+
$bestDiff = '';
100101
$expectedFormatted = $this->formatMockServerRequest($expected);
101102
foreach ($notMatchedRequests as $notMatchedRequest) {
102103
$diff = DiffHelper::calculate($expectedFormatted, $this->formatMockServerRequest($notMatchedRequest));
@@ -107,7 +108,7 @@ public function seeMockRequestWasCalled(string $expectationId, ?int $times = nul
107108
$bestDiff = $diff;
108109
}
109110
}
110-
throw new AssertionFailedError('Impossible match request: '. PHP_EOL . $bestDiff);
111+
throw new AssertionFailedError('Impossible match request: ' . PHP_EOL . $bestDiff);
111112
}
112113
}
113114

@@ -163,9 +164,11 @@ public function createMockRequestFromJsonFile(string $expectationFile): void
163164
$this->createMockRequest($expectationJson);
164165
}
165166

167+
/** @param array<string, mixed> $mockServerRequest */
166168
public function formatMockServerRequest(array $mockServerRequest): string
167169
{
168170
ksort($mockServerRequest);
169-
return json_encode($mockServerRequest, JSON_PRETTY_PRINT);
171+
$requestFormatted = json_encode($mockServerRequest, JSON_PRETTY_PRINT);
172+
return is_string($requestFormatted) ? $requestFormatted : '';
170173
}
171174
}

tests/Integration/SeeMockRequestWasCalledTest.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,14 @@ public function testExpectationNotWasCalledThrowException2(): void
6060

6161
public function testExpectationWasCalledButWasNotWithGoodRecomendation(): void
6262
{
63-
$this->client->request('GET', 'https://jsonplaceholder.typicode.com/posts/2', ['http_errors'=>false]);
64-
$this->client->request('GET', 'https://jsonplaceholder.typicode.com/posts/2', ['http_errors'=>false]);
65-
$this->client->request('GET', 'https://jsonplaceholder.typicode.com/posts/5', ['http_errors'=>false, 'headers' => ['randomHeader' => 'value']]);
66-
$this->client->request('GET', 'https://jsonplaceholder.typicode.com/users/3/albums', ['http_errors'=>false]);
63+
$this->client->request('GET', 'https://jsonplaceholder.typicode.com/posts/2', ['http_errors' => false]);
64+
$this->client->request('GET', 'https://jsonplaceholder.typicode.com/posts/2', ['http_errors' => false]);
65+
$this->client->request(
66+
'GET',
67+
'https://jsonplaceholder.typicode.com/posts/5',
68+
['http_errors' => false, 'headers' => ['randomHeader' => 'value']]
69+
);
70+
$this->client->request('GET', 'https://jsonplaceholder.typicode.com/users/3/albums', ['http_errors' => false]);
6771

6872
$this->expectException(AssertionFailedError::class);
6973
$this->expectExceptionMessageMatches('#.*"path": "\\\/users\\\/3\\\/albums".*#');
@@ -72,10 +76,14 @@ public function testExpectationWasCalledButWasNotWithGoodRecomendation(): void
7276

7377
public function testExpectationWasCalledButWasNotWithGoodRecomendation2(): void
7478
{
75-
$this->client->request('GET', 'https://jsonplaceholder.typicode.com/posts/2', ['http_errors'=>false]);
76-
$this->client->request('GET', 'https://jsonplaceholder.typicode.com/posts/2', ['http_errors'=>false]);
77-
$this->client->request('GET', 'https://jsonplaceholder.typicode.com/posts/5', ['http_errors'=>false]);
78-
$this->client->request('GET', 'https://jsonplaceholder.typicode.com/users/3/albums', ['http_errors'=>false, 'headers' => ['randomHeader' => 'value']]);
79+
$this->client->request('GET', 'https://jsonplaceholder.typicode.com/posts/2', ['http_errors' => false]);
80+
$this->client->request('GET', 'https://jsonplaceholder.typicode.com/posts/2', ['http_errors' => false]);
81+
$this->client->request('GET', 'https://jsonplaceholder.typicode.com/posts/5', ['http_errors' => false]);
82+
$this->client->request(
83+
'GET',
84+
'https://jsonplaceholder.typicode.com/users/3/albums',
85+
['http_errors' => false, 'headers' => ['randomHeader' => 'value']]
86+
);
7987

8088
$this->expectException(AssertionFailedError::class);
8189
$this->expectExceptionMessageMatches('#.*"path": "\\\/posts\\\/5".*#');

0 commit comments

Comments
 (0)