Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moved the logic of checking the number of expected calls to the current AllStubsMatchedStep step #2

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
35 changes: 9 additions & 26 deletions src/WiremockContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ private function allStubsMatched(): void
);

$requestedStubsIds = [];
$requestedStubsCallCounts = [];

foreach ($response['requests'] as $requestData) {
if (!isset($requestData['stubMapping'])) {
Expand All @@ -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);
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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) {
Expand Down
12 changes: 6 additions & 6 deletions tests/WiremockContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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();
}
}