diff --git a/src/WiremockContext.php b/src/WiremockContext.php index e991bd9..c84ceb3 100644 --- a/src/WiremockContext.php +++ b/src/WiremockContext.php @@ -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); @@ -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 ); @@ -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 ); @@ -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 ); @@ -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)); } } } diff --git a/tests/WiremockContextTest.php b/tests/WiremockContextTest.php index 52610ca..0cc2b92 100644 --- a/tests/WiremockContextTest.php +++ b/tests/WiremockContextTest.php @@ -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(); } @@ -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(); } @@ -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(); }