Skip to content

Commit

Permalink
Changed format of error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
akryvushenko committed Nov 22, 2024
1 parent 2ba5fca commit 927e93b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 18 deletions.
47 changes: 32 additions & 15 deletions src/WiremockContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,14 +313,18 @@ private function allStubsMatched(): void
));
}

$mappedRequestStubId = $requestData['stubMapping']['id'];
$mappedRequestStubId = $requestData['stubMapping']['id'];
$requestedStubsIds[] = $mappedRequestStubId;

if (!isset($requestedStubsCallCounts[$mappedRequestStubId])) {
$requestedStubsCallCounts[$mappedRequestStubId] = 0;
$requestedStubsCallCounts[$mappedRequestStubId] = [
'count' => 0,
'method' => $requestData["request"]["method"],
'absoluteUrl' => $requestData["request"]["absoluteUrl"],
];
}

$requestedStubsCallCounts[$mappedRequestStubId]++;
$requestedStubsCallCounts[$mappedRequestStubId]['count']++;
}

$requestedStubsIds = array_unique($requestedStubsIds);
Expand Down Expand Up @@ -380,18 +384,22 @@ private function checkRequestedStubsCallCounts(array $requestedStubsCallCounts):
{
$errors = [];

foreach ($requestedStubsCallCounts as $stubId => $actualCount) {
foreach ($requestedStubsCallCounts as $stubId => $requestedStubCallCount) {
$expectedCount = $this->stubs[$stubId]['count'];
$expectedType = $this->stubs[$stubId]['type'];

$url = $this->stubs[$stubId]['response']['request']['urlPath'];
$actualCount = $requestedStubCallCount['count'];
$absoluteUrl = $requestedStubCallCount['absoluteUrl'];

$method = $requestedStubCallCount['method'];

switch ($expectedType) {
case self::STUB_MATCH_COUNT_STRATEGY_EXACT:
if ($actualCount !== $expectedCount) {
$errors[] = sprintf(
'Stub with URL "%s" was expected to be called %d time(s), but was called %d time(s)',
$url,
'%s "%s" was expected to be called %d time(s), but was called %d time(s)',
$method,
$absoluteUrl,
$expectedCount,
$actualCount
);
Expand All @@ -400,8 +408,9 @@ private function checkRequestedStubsCallCounts(array $requestedStubsCallCounts):
case self::STUB_MATCH_COUNT_STRATEGY_MAX:
if ($actualCount > $expectedCount) {
$errors[] = sprintf(
'Stub with URL "%s" was expected to be called at most %d time(s), but was called %d time(s)',
$url,
'%s "%s" was expected to be called at most %d time(s), but was called %d time(s)',
$method,
$absoluteUrl,
$expectedCount,
$actualCount
);
Expand All @@ -410,8 +419,9 @@ private function checkRequestedStubsCallCounts(array $requestedStubsCallCounts):
case self::STUB_MATCH_COUNT_STRATEGY_MIN:
if ($actualCount < $expectedCount) {
$errors[] = sprintf(
'Stub with URL "%s" was expected to be called at least %d time(s), but was called %d time(s)',
$url,
'%s "%s" was expected to be called at least %d time(s), but was called %d time(s)',
$method,
$absoluteUrl,
$expectedCount,
$actualCount
);
Expand All @@ -420,12 +430,19 @@ private function checkRequestedStubsCallCounts(array $requestedStubsCallCounts):
case self::STUB_MATCH_COUNT_STRATEGY_ANY:
break;
default:
throw new WiremockContextException(sprintf('Unknown expectation type %s for URL %s', $expectedType, $url));
throw new WiremockContextException(
sprintf(
'Unknown expectation type %s for %s %s',
$method,
$expectedType,
$absoluteUrl
)
);
}
}

if (!empty($errors)) {
throw new WiremockContextException(implode("\n", $errors));
}
if (!empty($errors)) {
throw new WiremockContextException(implode("\n", $errors));
}
}
}
6 changes: 3 additions & 3 deletions tests/WiremockContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ function ($method, $url) use ($addStubResponse, $matchedStubsResponse) {
$stubBody = '{"request": {"method": "GET", "url": "/test"}, "response": {"status": 200, "body": "Success"}}';
$this->wiremockContext->addStub($stubBody, 1, 'exact');
$this->expectException(WiremockContextException::class);
$this->expectExceptionMessage('Stub with URL "/test" was expected to be called 1 time(s), but was called 2 time(s)');
$this->expectExceptionMessage('GET "http://wiremock:8080/test" was expected to be called 1 time(s), but was called 2 time(s)');
$this->wiremockContext->allStubsMatchedStep();
}

Expand Down Expand Up @@ -288,7 +288,7 @@ function ($method, $url) use ($addStubResponse, $matchedStubsResponse) {
$stubBody = '{"request": {"method": "GET", "url": "/test"}, "response": {"status": 200, "body": "Success"}}';
$this->wiremockContext->addStub($stubBody, 1, 'max');
$this->expectException(WiremockContextException::class);
$this->expectExceptionMessage('Stub with URL "/test" was expected to be called at most 1 time(s), but was called 2 time(s)');
$this->expectExceptionMessage('GET "http://wiremock:8080/test" was expected to be called at most 1 time(s), but was called 2 time(s)');
$this->wiremockContext->allStubsMatchedStep();
}

Expand Down Expand Up @@ -336,7 +336,7 @@ function ($method, $url) use ($addStubResponse, $matchedStubsResponse) {
$stubBody = '{"request": {"method": "GET", "url": "/test"}, "response": {"status": 200, "body": "Success"}}';
$this->wiremockContext->addStub($stubBody, 3, 'min');
$this->expectException(WiremockContextException::class);
$this->expectExceptionMessage('Stub with URL "/test" was expected to be called at least 3 time(s), but was called 2 time(s)');
$this->expectExceptionMessage('GET "http://wiremock:8080/test" was expected to be called at least 3 time(s), but was called 2 time(s)');
$this->wiremockContext->allStubsMatchedStep();
}

Expand Down

0 comments on commit 927e93b

Please sign in to comment.