Skip to content

Commit

Permalink
Merge pull request #2 from LGouttefange/feature/annotations_in_subdir…
Browse files Browse the repository at this point in the history
…ectory

feature: Add the ability to load expectations in subdirectories via annotation
  • Loading branch information
mcustiel authored Oct 6, 2020
2 parents 537c4cd + 224ae2f commit c2a7ff4
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ Retrieves all the requests received by Phiremock server matching the one specifi

### @expectation Annotations

Allows you to to set up an expectation via a json file
Allows you to to set up an expectation via a json file

```php
/**
Expand All @@ -148,6 +148,18 @@ Allows you to to set up an expectation via a json file

That will load by default the file at `tests/_data/phiremock-expectations/get_client_timeout.json`. The path where to place the expectations is configurable.

You may use expectations placed in subdirectories

```php
/**
* @expectation("edge_cases/get_client_timeout")
*/
public function test(FunctionalTester $I)
{
...
}
```

Multiple annotation formats are accepted

```
Expand All @@ -161,4 +173,4 @@ Multiple annotation formats are accepted
## See also:

Phiremock Client: https://github.com/mcustiel/phiremock-client
Examples in tests: https://github.com/mcustiel/phiremock-codeception-module/tree/master/tests/acceptance
Examples in tests: https://github.com/mcustiel/phiremock-codeception-module/tree/master/tests/acceptance
2 changes: 1 addition & 1 deletion src/Util/ExpectationAnnotationParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function getExpectations(TestInterface $test): array
public function parseExpectation(string $expectationAnnotation): string
{
$matches = [];
$expectationRegex = '/\(?\"?(?<filePath>[a-zA-Z0-9_]+)(.json)?\"?\)?/';
$expectationRegex = '/\(?\"?(?<filePath>[a-zA-Z0-9_\\/]+)(.json)?\"?\)?/';
preg_match($expectationRegex, $expectationAnnotation, $matches);

if (empty($matches)) {
Expand Down
15 changes: 15 additions & 0 deletions tests/_data/_unique_expectations/subdirectory/test_first_get.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"request": {
"method": "GET",
"url": {
"isEqualTo" : "/expectation/subdirectory"
}
},
"response": {
"statusCode": 200,
"body": "response",
"headers": {
"Content-Type": "application/json"
}
}
}
13 changes: 13 additions & 0 deletions tests/acceptance/BasicTestCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,19 @@ public function testMultipleAnnotationsAreLoaded(AcceptanceTester $I)
$requests = $I->grabRequestsMadeToRemoteService($requestBuilder);
$I->assertCount(2, $requests);
}

/**
* @param AcceptanceTester $I
* @expectation("subdirectory/test_first_get")
*/
public function testAnnotationInSubdirectoryIsLoaded(AcceptanceTester $I)
{
$conditionsBuilder = A::getRequest();
$requestBuilder = $conditionsBuilder->andMethod(Is::equalTo('GET'))->andUrl(Is::equalTo('/expectation/subdirectory'));
file_get_contents('http://localhost:18080/expectation/subdirectory');
$requests = $I->grabRequestsMadeToRemoteService($requestBuilder);
$I->assertCount(1, $requests);
}

/**
* @param AcceptanceTester $I
Expand Down

0 comments on commit c2a7ff4

Please sign in to comment.