|
| 1 | +<?php |
| 2 | +namespace Tests\Functional\BehatContext; |
| 3 | + |
| 4 | +use Behat\Behat\Context\Context; |
| 5 | +use Behat\Gherkin\Node\PyStringNode; |
| 6 | +use DemoApp\AbstractKernel; |
| 7 | +use DemoApp\DefaultKernel; |
| 8 | +use DemoApp\KernelWithMethodDocCreatedListener; |
| 9 | +use DemoApp\KernelWithServerDocCreatedListener; |
| 10 | +use PHPUnit\Framework\Assert; |
| 11 | +use Symfony\Component\HttpFoundation\Request; |
| 12 | +use Symfony\Component\HttpFoundation\Response; |
| 13 | +use Yoanm\JsonRpcServer\Domain\JsonRpcMethodInterface; |
| 14 | + |
| 15 | +/** |
| 16 | + * Defines application features from the specific context. |
| 17 | + */ |
| 18 | +class DemoAppContext implements Context |
| 19 | +{ |
| 20 | + /** @var Response|null */ |
| 21 | + private $lastResponse; |
| 22 | + |
| 23 | + /** @var bool */ |
| 24 | + private $useKernelWithMethodDocCreatedListener = false; |
| 25 | + /** @var bool */ |
| 26 | + private $useKernelWithServerDocCreatedListener = false; |
| 27 | + |
| 28 | + /** |
| 29 | + * @Given I will use kernel with MethodDocCreated listener |
| 30 | + */ |
| 31 | + public function givenIWillUseMethodDocCreatedListener() |
| 32 | + { |
| 33 | + $this->useKernelWithMethodDocCreatedListener = true; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * @Given I will use kernel with ServerDocCreated listener |
| 38 | + */ |
| 39 | + public function givenIWillUseServerDocCreatedListener() |
| 40 | + { |
| 41 | + $this->useKernelWithServerDocCreatedListener = true; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @When I send a :httpMethod request on :uri demoApp kernel endpoint |
| 46 | + * @When I send following :httpMethod input on :uri demoApp kernel endpoint: |
| 47 | + */ |
| 48 | + public function whenISendFollowingPayloadToDemoApp($httpMethod, $uri, PyStringNode $payload = null) |
| 49 | + { |
| 50 | + $this->lastResponse = null; |
| 51 | + |
| 52 | + $kernel = $this->getDemoAppKernel(); |
| 53 | + $kernel->boot(); |
| 54 | + $request = Request::create($uri, $httpMethod, [], [], [], [], $payload ? $payload->getRaw() : null); |
| 55 | + $this->lastResponse = $kernel->handle($request); |
| 56 | + $kernel->terminate($request, $this->lastResponse); |
| 57 | + $kernel->shutdown(); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @Then I should have a :httpCode response from demoApp with following content: |
| 62 | + */ |
| 63 | + public function thenIShouldHaveAResponseFromDemoAppWithFollowingContent($httpCode, PyStringNode $payload) |
| 64 | + { |
| 65 | + Assert::assertInstanceOf(Response::class, $this->lastResponse); |
| 66 | + // Decode payload to get ride of indentation, spacing, etc |
| 67 | + Assert::assertEquals( |
| 68 | + json_decode($payload->getRaw(), true), |
| 69 | + json_decode($this->lastResponse->getContent(), true) |
| 70 | + ); |
| 71 | + Assert::assertSame((int) $httpCode, $this->lastResponse->getStatusCode()); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @Then Collector should have :methodClass JSON-RPC method with name :methodName |
| 76 | + */ |
| 77 | + public function thenCollectorShouldHaveAMethodWithName($methodClass, $methodName) |
| 78 | + { |
| 79 | + $kernel = $this->getDemoAppKernel(); |
| 80 | + $kernel->boot(); |
| 81 | + $mappingList = $kernel->getContainer() |
| 82 | + ->get('mapping_aware_service') |
| 83 | + ->getMappingList() |
| 84 | + ; |
| 85 | + $kernel->shutdown(); |
| 86 | + |
| 87 | + if (!isset($mappingList[$methodName])) { |
| 88 | + throw new \Exception(sprintf('No mapping defined to method name "%s"', $methodName)); |
| 89 | + } |
| 90 | + $method = $mappingList[$methodName]; |
| 91 | + |
| 92 | + Assert::assertInstanceOf( |
| 93 | + JsonRpcMethodInterface::class, |
| 94 | + $method, |
| 95 | + 'Method must be a JsonRpcMethodInterface instance' |
| 96 | + ); |
| 97 | + Assert::assertInstanceOf( |
| 98 | + $methodClass, |
| 99 | + $method, |
| 100 | + sprintf('Method "%s" is not an instance of "%s"', $methodName, $methodClass) |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * @return AbstractKernel |
| 106 | + */ |
| 107 | + protected function getDemoAppKernel() |
| 108 | + { |
| 109 | + $env = 'prod'; |
| 110 | + $debug = true; |
| 111 | + |
| 112 | + if (true === $this->useKernelWithMethodDocCreatedListener) { |
| 113 | + $kernelClass = KernelWithMethodDocCreatedListener::class; |
| 114 | + } elseif (true === $this->useKernelWithServerDocCreatedListener) { |
| 115 | + $kernelClass = KernelWithServerDocCreatedListener::class; |
| 116 | + } else { |
| 117 | + $kernelClass = DefaultKernel::class; |
| 118 | + } |
| 119 | + |
| 120 | + return new $kernelClass($env, $debug); |
| 121 | + } |
| 122 | +} |
0 commit comments