|
1 | 1 | <?php |
2 | 2 | namespace Tests\Functional\BehatContext; |
3 | 3 |
|
4 | | -use Behat\Behat\Context\Context; |
5 | 4 | use Behat\Gherkin\Node\PyStringNode; |
6 | 5 | use PHPUnit\Framework\Assert; |
7 | | -use PHPUnit\Framework\Constraint\IsIdentical; |
8 | | -use Symfony\Component\Validator\ValidatorBuilder; |
9 | | -use Yoanm\JsonRpcParamsSymfonyValidator\Infra\JsonRpcParamsValidator; |
10 | | -use Yoanm\JsonRpcServer\Domain\Model\JsonRpcRequest; |
| 6 | +use Tests\Functional\BehatContext\Helper\FakeEndpointCreator; |
11 | 7 |
|
12 | 8 | /** |
13 | 9 | * Defines application features from the specific context. |
14 | 10 | */ |
15 | | -class FeatureContext implements Context |
| 11 | +class FeatureContext extends AbstractContext |
16 | 12 | { |
17 | | - /** @var array */ |
18 | | - private $lastViolationList = []; |
| 13 | + const KEY_JSON_RPC = 'jsonrpc'; |
| 14 | + const KEY_ID = 'id'; |
| 15 | + const KEY_ERROR = 'error'; |
19 | 16 |
|
20 | | - /** |
21 | | - * @When I validate method :methodClass with: |
22 | | - */ |
23 | | - public function whenIValidateMethodWith($methodClass, PyStringNode $payload) |
24 | | - { |
25 | | - $jsonRpcRequest = new JsonRpcRequest('2.0', $methodClass); |
26 | | - $jsonRpcRequest->setParamList(json_decode($payload->getRaw(), true)); |
| 17 | + const SUB_KEY_ERROR_CODE = 'code'; |
| 18 | + const SUB_KEY_ERROR_MESSAGE = 'message'; |
27 | 19 |
|
28 | | - $this->lastViolationList = $this->getValidator()->validate($jsonRpcRequest, new $methodClass); |
29 | | - } |
| 20 | + /** @var string|null */ |
| 21 | + private $lastResponse = null; |
30 | 22 |
|
31 | 23 | /** |
32 | | - * @Then I should have no violation |
| 24 | + * @When I send following payload: |
33 | 25 | */ |
34 | | - public function thenIShouldHaveNoViolation() |
| 26 | + public function whenISendTheFollowingPayload(PyStringNode $payload) |
35 | 27 | { |
36 | | - Assert::assertEmpty($this->lastViolationList); |
| 28 | + $endpoint = (new FakeEndpointCreator())->create(); |
| 29 | + |
| 30 | + $this->lastResponse = $endpoint->index($payload->getRaw()); |
37 | 31 | } |
38 | 32 |
|
39 | 33 | /** |
40 | | - * @Then I should have 1 violation |
41 | | - * @Then I should have :count violations |
| 34 | + * @Then I should have the following response: |
42 | 35 | */ |
43 | | - public function thenIShouldHaveXViolation($count = 1) |
| 36 | + public function thenIShouldHaveTheFollowingResponse(PyStringNode $expectedResult) |
44 | 37 | { |
45 | | - Assert::assertCount((int) $count, $this->lastViolationList); |
| 38 | + // Decode content to get rid of any indentation/spacing/... issues |
| 39 | + Assert::assertEquals( |
| 40 | + $this->jsonDecode($expectedResult->getRaw()), |
| 41 | + $this->getLastResponseDecoded() |
| 42 | + ); |
46 | 43 | } |
47 | 44 |
|
48 | 45 | /** |
49 | | - * @Then I should have the following validation error: |
| 46 | + * @Then I should have an empty response |
50 | 47 | */ |
51 | | - public function thenIShouldHaveTheFollowingViolation(PyStringNode $node) |
| 48 | + public function thenIShouldHaveAnEmptyResponse() |
52 | 49 | { |
53 | | - $found = false; |
54 | | - $decoded = json_decode($node->getRaw(), true); |
55 | | - $constraint = new IsIdentical($decoded); |
56 | | - foreach ($this->lastViolationList as $violation) { |
57 | | - if (true === $constraint->evaluate($violation, '', true)) { |
58 | | - $found = true; |
59 | | - break; |
60 | | - } |
61 | | - } |
62 | | - |
63 | | - if (true !== $found) { |
64 | | - throw new \Exception( |
65 | | - sprintf( |
66 | | - 'Violation "%s" not found in violation list : %s', |
67 | | - json_encode($decoded), |
68 | | - json_encode($this->lastViolationList) |
69 | | - ) |
70 | | - ); |
71 | | - } |
| 50 | + // Decode content to get rid of any indentation/spacing/... issues |
| 51 | + Assert::assertEmpty($this->getLastResponseDecoded()); |
72 | 52 | } |
73 | | - /** |
74 | | - * @return JsonRpcParamsValidator |
75 | | - */ |
76 | | - private function getValidator() : JsonRpcParamsValidator |
| 53 | + |
| 54 | + private function getLastResponseDecoded() |
77 | 55 | { |
78 | | - return new JsonRpcParamsValidator( |
79 | | - (new ValidatorBuilder())->getValidator() |
80 | | - ); |
| 56 | + return $this->jsonDecode($this->lastResponse); |
81 | 57 | } |
82 | 58 | } |
0 commit comments