Skip to content

Commit 7c41835

Browse files
authored
Improve (#38)
1 parent 906d2f9 commit 7c41835

File tree

2 files changed

+65
-26
lines changed

2 files changed

+65
-26
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
namespace Tests\Functional\BehatContext;
3+
4+
use Behat\Behat\Context\Context;
5+
use DemoApp\AbstractKernel;
6+
use DemoApp\DefaultKernel;
7+
use Symfony\Component\HttpFoundation\Request;
8+
use Symfony\Component\HttpFoundation\Response;
9+
10+
class AbstractContext implements Context
11+
{
12+
public function jsonDecode($encodedData)
13+
{
14+
$decoded = json_decode($encodedData, true);
15+
16+
if (JSON_ERROR_NONE != json_last_error()) {
17+
throw new \Exception(
18+
json_last_error_msg(),
19+
json_last_error()
20+
);
21+
}
22+
23+
return $decoded;
24+
}
25+
26+
/**
27+
* @param string $uri
28+
* @param string $httpMethod
29+
* @param string $content
30+
*
31+
* @return Response
32+
*
33+
* @throws \Exception
34+
*/
35+
public function sendHttpContentTo(string $uri, string $httpMethod, string $content): Response
36+
{
37+
$kernel = $this->getDemoAppKernel();
38+
$kernel->boot();
39+
$request = Request::create($uri, $httpMethod, [], [], [], [], $content);
40+
$response = $kernel->handle($request);
41+
$kernel->terminate($request, $response);
42+
$kernel->shutdown();
43+
44+
return $response;
45+
}
46+
47+
/**
48+
* @return AbstractKernel
49+
*/
50+
public function getDemoAppKernel()
51+
{
52+
$env = 'prod';
53+
$debug = true;
54+
55+
return new DefaultKernel($env, $debug);
56+
}
57+
}

features/bootstrap/DemoAppContext.php

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
<?php
22
namespace Tests\Functional\BehatContext;
33

4-
use Behat\Behat\Context\Context;
54
use Behat\Gherkin\Node\PyStringNode;
6-
use DemoApp\AbstractKernel;
7-
use DemoApp\DefaultKernel;
85
use PHPUnit\Framework\Assert;
9-
use Symfony\Component\HttpFoundation\Request;
106
use Symfony\Component\HttpFoundation\Response;
117
use Yoanm\JsonRpcServer\Domain\JsonRpcMethodInterface;
128

139
/**
1410
* Defines application features from the specific context.
1511
*/
16-
class DemoAppContext implements Context
12+
class DemoAppContext extends AbstractContext
1713
{
1814
/** @var Response|null */
1915
private $lastResponse;
@@ -23,14 +19,11 @@ class DemoAppContext implements Context
2319
*/
2420
public function whenISendFollowingPayloadToDemoApp($httpMethod, $uri, PyStringNode $payload)
2521
{
26-
$this->lastResponse = null;
27-
28-
$kernel = $this->getDemoAppKernel();
29-
$kernel->boot();
30-
$request = Request::create($uri, $httpMethod, [], [], [], [], $payload->getRaw());
31-
$this->lastResponse = $kernel->handle($request);
32-
$kernel->terminate($request, $this->lastResponse);
33-
$kernel->shutdown();
22+
$this->lastResponse = $this->sendHttpContentTo(
23+
$uri,
24+
$httpMethod,
25+
$payload->getRaw()
26+
);
3427
}
3528

3629
/**
@@ -41,8 +34,8 @@ public function thenIShouldHaveAResponseFromDemoAppWithFollowingContent($httpCod
4134
Assert::assertInstanceOf(Response::class, $this->lastResponse);
4235
// Decode payload to get ride of indentation, spacing, etc
4336
Assert::assertEquals(
44-
json_decode($payload->getRaw(), true),
45-
json_decode($this->lastResponse->getContent(), true)
37+
$this->jsonDecode($payload->getRaw()),
38+
$this->jsonDecode($this->lastResponse->getContent())
4639
);
4740
Assert::assertSame((int) $httpCode, $this->lastResponse->getStatusCode());
4841
}
@@ -76,15 +69,4 @@ public function thenCollectorShouldHaveAMethodWithName($methodClass, $methodName
7669
sprintf('Method "%s" is not an instance of "%s"', $methodName, $methodClass)
7770
);
7871
}
79-
80-
/**
81-
* @return AbstractKernel
82-
*/
83-
protected function getDemoAppKernel()
84-
{
85-
$env = 'prod';
86-
$debug = true;
87-
88-
return new DefaultKernel($env, $debug);
89-
}
9072
}

0 commit comments

Comments
 (0)