|
| 1 | +<?php |
| 2 | +namespace Tests\Functional\BehatContext; |
| 3 | + |
| 4 | +use Behat\Gherkin\Node\PyStringNode; |
| 5 | +use DemoApp\AbstractKernel; |
| 6 | +use DemoApp\DefaultKernel; |
| 7 | +use DemoApp\KernelWithDocCreatedListener; |
| 8 | +use PHPUnit\Framework\Assert; |
| 9 | +use Symfony\Component\HttpFoundation\Request; |
| 10 | +use Symfony\Component\HttpFoundation\Response; |
| 11 | + |
| 12 | +/** |
| 13 | + * Defines application features from the specific context. |
| 14 | + */ |
| 15 | +class DemoAppContext extends AbstractContext |
| 16 | +{ |
| 17 | + /** @var Response|null */ |
| 18 | + private $lastResponse; |
| 19 | + |
| 20 | + /** @var bool */ |
| 21 | + private $useKernelWithDocCreatedListener = false; |
| 22 | + |
| 23 | + /** |
| 24 | + * @Given I will use kernel with MethodDocCreated listener |
| 25 | + */ |
| 26 | + public function givenIWillUseMethodDocCreatedListener() |
| 27 | + { |
| 28 | + $this->useKernelWithMethodDocCreatedListener = true; |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * @Given I will use kernel with DocCreated listener |
| 33 | + */ |
| 34 | + public function givenIWillUseKernelWithDocCreatedListener() |
| 35 | + { |
| 36 | + $this->useKernelWithDocCreatedListener = true; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @When I send a :httpMethod request on :uri demoApp kernel endpoint |
| 41 | + * @When I send following :httpMethod input on :uri demoApp kernel endpoint: |
| 42 | + */ |
| 43 | + public function whenISendFollowingPayloadToDemoApp($httpMethod, $uri, PyStringNode $payload = null) |
| 44 | + { |
| 45 | + $this->lastResponse = null; |
| 46 | + |
| 47 | + $kernel = $this->getDemoAppKernel(); |
| 48 | + $kernel->boot(); |
| 49 | + $request = Request::create($uri, $httpMethod, [], [], [], [], $payload ? $payload->getRaw() : null); |
| 50 | + $this->lastResponse = $kernel->handle($request); |
| 51 | + $kernel->terminate($request, $this->lastResponse); |
| 52 | + $kernel->shutdown(); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @Then I should have a :httpCode response from demoApp with following content: |
| 57 | + */ |
| 58 | + public function thenIShouldHaveAResponseFromDemoAppWithFollowingContent($httpCode, PyStringNode $payload) |
| 59 | + { |
| 60 | + Assert::assertInstanceOf(Response::class, $this->lastResponse); |
| 61 | + // Decode payload to get ride of indentation, spacing, etc |
| 62 | + Assert::assertEquals( |
| 63 | + $this->jsonDecode($payload->getRaw()), |
| 64 | + $this->jsonDecode($this->lastResponse->getContent()) |
| 65 | + ); |
| 66 | + Assert::assertSame((int) $httpCode, $this->lastResponse->getStatusCode()); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @return AbstractKernel |
| 71 | + */ |
| 72 | + protected function getDemoAppKernel() |
| 73 | + { |
| 74 | + $env = 'prod'; |
| 75 | + $debug = true; |
| 76 | + |
| 77 | + if (true === $this->useKernelWithDocCreatedListener) { |
| 78 | + $kernelClass = KernelWithDocCreatedListener::class; |
| 79 | + } else { |
| 80 | + $kernelClass = DefaultKernel::class; |
| 81 | + } |
| 82 | + |
| 83 | + return new $kernelClass($env, $debug); |
| 84 | + } |
| 85 | +} |
0 commit comments