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

Added a new step and updated the documentation #3

Merged
merged 1 commit into from
Nov 21, 2024
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
28 changes: 18 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,35 +67,43 @@ For those running Behat within Docker, integrating a Wiremock container is strai
}
```

- **Given wiremock stubs from**: This step loads stubs from a specified file or directory and sends them to Wiremock.
- **Given wiremock stubs from {file}**: This step loads stubs from a specified file or directory and sends them to Wiremock.

**Example**:
```gherkin
Given wiremock stubs from "dir/awesome-stub.json"
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.
- **Given wiremock stubs from {file} should be called {count} times**: This step loads stubs from a specified file or directory, sends them to WireMock and also allows you to verify that the stub is called 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 "dir/awesome-stub.json" should be called 2 times
And wiremock stubs from "dir2" should be called 2 times
```

- **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.
- **Given wiremock stubs from {file} should be called once**: This step loads stubs from a specified file or directory, sends them to WireMock and also allows you to verify that the stub is called once.

**Example**:
```gherkin
Given wiremock stubs from "{filename}" and should be called minimum {count} times
And wiremock stubs from "dir2"
Given wiremock stubs from "dir/awesome-stub.json" should be called once
And wiremock stubs from "dir2" should be called once
```
- **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.

- **Given wiremock stubs from {file} should be called at least {count} times**: This step loads stubs from a specified file or directory, sends them to WireMock and also allows you 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 at most {count} times
And wiremock stubs from "dir2"
Given wiremock stubs from "dir/awesome-stub.json" should be called at least 2 times
And wiremock stubs from "dir2" should be called at least 2 times
```
- **Given wiremock stubs from {file} should be called at most {count} times**: This step loads stubs from a specified file or directory, sends them to WireMock and also allows you to verify that the stub is not called more than the specified number of times.

**Example**:
```gherkin
Given wiremock stubs from "dir/awesome-stub.json" and should be called at most 2 times
And wiremock stubs from "dir2" and should be called at most 2 times
```

### Managing Wiremock State
Expand Down
23 changes: 16 additions & 7 deletions src/WiremockContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,25 +77,34 @@ public function addWiremockStubFromFileStep(string $path): void
/**
* @throws WiremockContextException
*/
#[Given('/^wiremock stubs from "([^"]+)" and should be called exactly (?P<expectedCallCount>\d+) times$/')]
public function addWiremockStubFromFileShouldBeCalledExactlyStep(string $path, int $expectedCallCount): void
#[Given('/^wiremock stubs from "([^"]+)" should be called (?P<expectedCallCount>\d+) times$/')]
public function addWiremockStubFromFileShouldBeCalledStep(string $path, int $expectedCallCount): void
{
$this->addWiremockStubFromFile($path, $expectedCallCount, self::STUB_MATCH_COUNT_STRATEGY_EXACT);
}

/**
* @throws WiremockContextException
*/
#[Given('/^wiremock stubs from "([^"]+)" and should be called minimal (?P<expectedCallCount>\d+) times$/')]
public function addWiremockStubFromFileShouldBeCalledMinimalStep(string $path, int $expectedCallCount): void
#[Given('/^wiremock stubs from "([^"]+)" should be called once')]
public function addWiremockStubFromFileShouldBeCalledOnceStep(string $path): void
{
$this->addWiremockStubFromFile($path, 1, self::STUB_MATCH_COUNT_STRATEGY_EXACT);
}

/**
* @throws WiremockContextException
*/
#[Given('/^wiremock stubs from "([^"]+)" should be called at least (?P<expectedCallCount>\d+) times$/')]
public function addWiremockStubFromFileShouldBeCalledAtLeastStep(string $path, int $expectedCallCount): void
{
$this->addWiremockStubFromFile($path, $expectedCallCount, self::STUB_MATCH_COUNT_STRATEGY_MIN);
}

/**
* @throws WiremockContextException
*/
#[Given('/^wiremock stubs from "([^"]+)" and should be called at most (?P<expectedCallCount>\d+) times$/')]
#[Given('/^wiremock stubs from "([^"]+)" should be called at most (?P<expectedCallCount>\d+) times$/')]
public function addWiremockStubFromFileShouldBeCalledAtMostStep(string $path, int $expectedCallCount): void
{
$this->addWiremockStubFromFile($path, $expectedCallCount, self::STUB_MATCH_COUNT_STRATEGY_MAX);
Expand Down Expand Up @@ -381,7 +390,7 @@ private function checkRequestedStubsCallCounts(array $requestedStubsCallCounts):
case self::STUB_MATCH_COUNT_STRATEGY_EXACT:
if ($actualCount !== $expectedCount) {
$errors[] = sprintf(
'Stub with URL "%s" was expected to be called exactly %d time(s), but was called %d time(s)',
'Stub with URL "%s" was expected to be called %d time(s), but was called %d time(s)',
$url,
$expectedCount,
$actualCount
Expand All @@ -401,7 +410,7 @@ 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 minimum %d time(s), but was called %d time(s)',
'Stub with URL "%s" was expected to be called at least %d time(s), but was called %d time(s)',
$url,
$expectedCount,
$actualCount
Expand Down
4 changes: 2 additions & 2 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 exactly 1 time(s), but was called 2 time(s)');
$this->expectExceptionMessage('Stub with URL "/test" was expected to be called 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 minimum 3 time(s), but was called 2 time(s)');
$this->expectExceptionMessage('Stub with URL "/test" was expected to be called at least 3 time(s), but was called 2 time(s)');
$this->wiremockContext->allStubsMatchedStep();
}

Expand Down