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