|
2 | 2 | namespace Tests\Functional\BehatContext; |
3 | 3 |
|
4 | 4 | use Behat\Behat\Context\Context; |
| 5 | +use Behat\Behat\Context\Environment\InitializedContextEnvironment; |
| 6 | +use Behat\Behat\Hook\Scope\BeforeScenarioScope; |
| 7 | +use Behat\Gherkin\Node\PyStringNode; |
| 8 | +use PHPUnit\Framework\Assert; |
| 9 | +use PHPUnit\Framework\Constraint\IsIdentical; |
| 10 | +use Prophecy\Argument; |
| 11 | +use Symfony\Component\Validator\ValidatorBuilder; |
| 12 | +use Tests\Functional\BehatContext\App\FakeEndpointCreator; |
| 13 | +use Yoanm\JsonRpcParamsSymfonyValidator\Infra\JsonRpcParamsValidator; |
| 14 | +use Yoanm\JsonRpcServer\Domain\Model\JsonRpcRequest; |
5 | 15 |
|
6 | 16 | /** |
7 | 17 | * Defines application features from the specific context. |
8 | 18 | */ |
9 | 19 | class FeatureContext implements Context |
10 | 20 | { |
| 21 | + /** @var array */ |
| 22 | + private $lastViolationList = []; |
11 | 23 |
|
| 24 | + /** |
| 25 | + * @When I validate method :methodClass with: |
| 26 | + */ |
| 27 | + public function whenIValidateMethodWith($methodClass, PyStringNode $payload) |
| 28 | + { |
| 29 | + $jsonRpcRequest = new JsonRpcRequest('2.0', $methodClass); |
| 30 | + $jsonRpcRequest->setParamList(json_decode($payload->getRaw(), true)); |
| 31 | + |
| 32 | + $this->lastViolationList = $this->getValidator()->validate($jsonRpcRequest, new $methodClass); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * @Then I should have no violation |
| 37 | + */ |
| 38 | + public function thenIShouldHaveNoViolation() |
| 39 | + { |
| 40 | + Assert::assertEmpty($this->lastViolationList); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * @Then I should have 1 violation |
| 45 | + * @Then I should have :count violations |
| 46 | + */ |
| 47 | + public function thenIShouldHaveXViolation($count = 1) |
| 48 | + { |
| 49 | + Assert::assertCount((int) $count, $this->lastViolationList); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * @Then I should have the following validation error: |
| 54 | + * @param PyStringNode $node |
| 55 | + */ |
| 56 | + public function thenIShouldHaveTheFollowingViolation(PyStringNode $node) |
| 57 | + { |
| 58 | + $found = false; |
| 59 | + $decoded = json_decode($node->getRaw(), true); |
| 60 | + $constraint = new IsIdentical($decoded); |
| 61 | + foreach ($this->lastViolationList as $violation) { |
| 62 | + if (true === $constraint->evaluate($violation, '', true)) { |
| 63 | + $found = true; |
| 64 | + break; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + if (true !== $found) { |
| 69 | + throw new \Exception( |
| 70 | + sprintf( |
| 71 | + 'Violation "%s" not found in violation list : %s', |
| 72 | + json_encode($decoded), |
| 73 | + json_encode($this->lastViolationList) |
| 74 | + ) |
| 75 | + ); |
| 76 | + } |
| 77 | + } |
| 78 | + /** |
| 79 | + * @return JsonRpcParamsValidator |
| 80 | + */ |
| 81 | + private function getValidator() : JsonRpcParamsValidator |
| 82 | + { |
| 83 | + return new JsonRpcParamsValidator( |
| 84 | + (new ValidatorBuilder())->getValidator() |
| 85 | + ); |
| 86 | + } |
12 | 87 | } |
0 commit comments