1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
namespace Codeception\Module;
use Codeception\Lib\Framework;
use Codeception\TestInterface;
use Codeception\Configuration;
use Codeception\Lib\Connector\ZendExpressive as ZendExpressiveConnector;
use Codeception\Lib\Interfaces\DoctrineProvider;
/**
* This module allows you to run tests inside Zend Expressive.
*
* Uses `config/container.php` file by default.
*
* ## Status
*
* * Maintainer: **Naktibalda**
* * Stability: **alpha**
*
* ## Config
*
* * container: relative path to file which returns Container (default: `config/container.php`)
*
* ## API
*
* * application - instance of `\Zend\Expressive\Application`
* * container - instance of `\Interop\Container\ContainerInterface`
* * client - BrowserKit client
*
*/
class ZendExpressive extends Framework implements DoctrineProvider
{
protected $config = [
'container' => 'config/container.php',
];
/**
* @var \Codeception\Lib\Connector\ZendExpressive
*/
public $client;
/**
* @var \Interop\Container\ContainerInterface
*/
public $container;
/**
* @var \Zend\Expressive\Application
*/
public $application;
protected $responseCollector;
public function _initialize()
{
$cwd = getcwd();
$projectDir = Configuration::projectDir();
chdir($projectDir);
$this->container = require $projectDir . $this->config['container'];
$app = $this->container->get('Zend\Expressive\Application');
$pipelineFile = $projectDir . 'config/pipeline.php';
if (file_exists($pipelineFile)) {
require $pipelineFile;
}
$routesFile = $projectDir . 'config/routes.php';
if (file_exists($routesFile)) {
require $routesFile;
}
chdir($cwd);
$this->application = $app;
$this->initResponseCollector();
}
public function _before(TestInterface $test)
{
$this->client = new ZendExpressiveConnector();
$this->client->setApplication($this->application);
$this->client->setResponseCollector($this->responseCollector);
}
public function _after(TestInterface $test)
{
//Close the session, if any are open
if (session_status() == PHP_SESSION_ACTIVE) {
session_write_close();
}
parent::_after($test);
}
private function initResponseCollector()
{
/**
* @var Zend\Expressive\Emitter\EmitterStack
*/
$emitterStack = $this->application->getEmitter();
while (!$emitterStack->isEmpty()) {
$emitterStack->pop();
}
$this->responseCollector = new ZendExpressiveConnector\ResponseCollector;
$emitterStack->unshift($this->responseCollector);
}
public function _getEntityManager()
{
$service = 'Doctrine\ORM\EntityManager';
if (!$this->container->has($service)) {
throw new \PHPUnit\Framework\AssertionFailedError("Service $service is not available in container");
}
return $this->container->get('Doctrine\ORM\EntityManager');
}
}