diff --git a/README.md b/README.md index cf33faf..5af094e 100644 --- a/README.md +++ b/README.md @@ -75,6 +75,29 @@ For those running Behat within Docker, integrating a Wiremock container is strai And wiremock stubs from "dir2" ``` +- **Given wiremock stubs from and should be called exactly times**: This step loads stubs from a specified file or directory and sends them to Wiremock also gives you ability to verify that the stub is called exactly the specified number of times. + + **Example**: + ```gherkin + Given wiremock stubs from "{filename}" and should be called exactly {count} times + And wiremock stubs from "dir2" + ``` + +- **Given wiremock stubs from and should be called minimum times**: This step loads stubs from a specified file or directory and sends them to Wiremock also gives you ability to verify that the stub is called at least the specified number of times. + + **Example**: + ```gherkin + Given wiremock stubs from "{filename}" and should be called minimum {count} times + And wiremock stubs from "dir2" + ``` +- **Given wiremock stubs from and should be called at most times**: This step loads stubs from a specified file or directory and sends them to Wiremock also gives you ability to verify that the stub is not called more than the specified number of times. + + **Example**: + ```gherkin + Given wiremock stubs from "{filename}" and should be called at most {count} times + And wiremock stubs from "dir2" + ``` + ### Managing Wiremock State - **Given clean wiremock**: Resets Wiremock to its initial state. diff --git a/src/WiremockContext.php b/src/WiremockContext.php index af42c1d..b3353a6 100644 --- a/src/WiremockContext.php +++ b/src/WiremockContext.php @@ -285,6 +285,7 @@ private function allStubsMatched(): void ); $requestedStubsIds = []; + $requestedStubsCallCounts = []; foreach ($response['requests'] as $requestData) { if (!isset($requestData['stubMapping'])) { @@ -305,6 +306,12 @@ private function allStubsMatched(): void $mappedRequestStubId = $requestData['stubMapping']['id']; $requestedStubsIds[] = $mappedRequestStubId; + + if (!isset($requestedStubsCallCounts[$mappedRequestStubId])) { + $requestedStubsCallCounts[$mappedRequestStubId] = 0; + } + + $requestedStubsCallCounts[$mappedRequestStubId]++; } $requestedStubsIds = array_unique($requestedStubsIds); @@ -317,15 +324,8 @@ private function allStubsMatched(): void throw new WiremockContextException('Unrequested stub(s) found: ' . json_encode($unrequestedStubs, JSON_PRETTY_PRINT)); } - } - /** - * @throws WiremockContextException - */ - #[AfterScenario] - public function allStubsMatchedAsExpectedForEachScenario(): void - { - $this->allStubsMatchedAsExpected(); + $this->checkRequestedStubsCallCounts($requestedStubsCallCounts); } /** @@ -367,25 +367,8 @@ private function addWiremockStubFromFile( * @param array $requestedStubsCallCounts * @throws WiremockContextException */ - private function allStubsMatchedAsExpected(): void + private function checkRequestedStubsCallCounts(array $requestedStubsCallCounts): void { - $response = $this->sendRequest( - 'GET', - self::PATH_REQUESTS - ); - - $requestedStubsCallCounts = []; - - foreach ($response['requests'] as $requestData) { - $mappedRequestStubId = $requestData['stubMapping']['id']; - - if (!isset($requestedStubsCallCounts[$mappedRequestStubId])) { - $requestedStubsCallCounts[$mappedRequestStubId] = 0; - } - - $requestedStubsCallCounts[$mappedRequestStubId]++; - } - $errors = []; foreach ($requestedStubsCallCounts as $stubId => $actualCount) { diff --git a/tests/WiremockContextTest.php b/tests/WiremockContextTest.php index 8832150..466f671 100644 --- a/tests/WiremockContextTest.php +++ b/tests/WiremockContextTest.php @@ -204,7 +204,7 @@ function ($method, $url) use ($addStubResponse, $matchedStubsResponse) { $this->wiremockContext->addStub($stubBody, 1, 'exact'); $this->expectException(WiremockContextException::class); $this->expectExceptionMessage('Stub with URL "/test" was expected to be called exactly 1 time(s), but was called 2 time(s)'); - $this->wiremockContext->allStubsMatchedAsExpectedForEachScenario(); + $this->wiremockContext->allStubsMatchedStep(); } public function testAllStubsMatchedStepWithExactMatchCountStrategy() @@ -241,7 +241,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->wiremockContext->allStubsMatchedAsExpectedForEachScenario(); + $this->wiremockContext->allStubsMatchedStep(); } public function testAllStubsMatchedStepWithMaxMatchCountStrategyFailed() @@ -289,7 +289,7 @@ function ($method, $url) use ($addStubResponse, $matchedStubsResponse) { $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->wiremockContext->allStubsMatchedAsExpectedForEachScenario(); + $this->wiremockContext->allStubsMatchedStep(); } public function testAllStubsMatchedStepWithMinMatchCountStrategyFailed() @@ -337,7 +337,7 @@ function ($method, $url) use ($addStubResponse, $matchedStubsResponse) { $this->wiremockContext->addStub($stubBody, 3, 'min'); $this->expectException(WiremockContextException::class); $this->expectExceptionMessage('Stub with URL "/test" was expected to be called minimum 3 time(s), but was called 2 time(s)'); - $this->wiremockContext->allStubsMatchedAsExpectedForEachScenario(); + $this->wiremockContext->allStubsMatchedStep(); } public function testAllStubsMatchedStepWithMaxMatchCountStrategy() @@ -374,7 +374,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->wiremockContext->allStubsMatchedAsExpectedForEachScenario(); + $this->wiremockContext->allStubsMatchedStep(); } public function testAllStubsMatchedStepWithMinMatchCountStrategy() @@ -411,6 +411,6 @@ function ($method, $url) use ($addStubResponse, $matchedStubsResponse) { $stubBody = '{"request": {"method": "GET", "url": "/test"}, "response": {"status": 200, "body": "Success"}}'; $this->wiremockContext->addStub($stubBody, 1, 'min'); - $this->wiremockContext->allStubsMatchedAsExpectedForEachScenario(); + $this->wiremockContext->allStubsMatchedStep(); } }