Skip to content

Commit ce671c1

Browse files
authored
Add FT & UT (#6)
1 parent d71ff13 commit ce671c1

30 files changed

+1062
-111
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ vendor
22
build
33
#A library must not provide a composer.lock file
44
composer.lock
5+
6+
features/demo_app/var/

behat.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ default:
22
suites:
33
default:
44
contexts:
5-
- Tests\Functional\BehatContext\FeatureContext: ~
5+
- Tests\Functional\BehatContext\DemoAppContext: ~

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"autoload-dev": {
2323
"psr-4": {
2424
"Tests\\": "tests",
25-
"Tests\\Functional\\BehatContext\\": "features/bootstrap"
25+
"Tests\\Functional\\BehatContext\\": "features/bootstrap",
26+
"DemoApp\\": "features/demo_app/src"
2627
}
2728
},
2829
"require": {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
namespace Tests\Functional\BehatContext;
3+
4+
use Behat\Behat\Context\Context;
5+
6+
class AbstractContext implements Context
7+
{
8+
protected function jsonDecode($encodedData)
9+
{
10+
$decoded = json_decode($encodedData, true);
11+
12+
if (JSON_ERROR_NONE != json_last_error()) {
13+
throw new \Exception(
14+
json_last_error_msg(),
15+
json_last_error()
16+
);
17+
}
18+
19+
return $decoded;
20+
}
21+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
}

features/bootstrap/FeatureContext.php

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)