forked from Codeception/module-mezzio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMezzio.php
More file actions
112 lines (97 loc) · 3.45 KB
/
Mezzio.php
File metadata and controls
112 lines (97 loc) · 3.45 KB
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
<?php
declare(strict_types=1);
namespace Codeception\Module;
use Codeception\Lib\Connector\Mezzio as MezzioConnector;
use Codeception\Lib\Framework;
use Codeception\Lib\Interfaces\DoctrineProvider;
use Codeception\TestInterface;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Container\ContainerInterface;
use Mezzio\Application;
use PHPUnit\Framework\AssertionFailedError;
use Symfony\Component\BrowserKit\AbstractBrowser;
/**
* This module allows you to run tests inside Mezzio.
*
* Uses `config/container.php` file by default.
*
* ## Status
*
* * Maintainer: **Naktibalda**
* * Stability: **alpha**
*
* ## Config
*
* * `container` - (default: `config/container.php`) relative path to file which returns Container
* * `recreateApplicationBetweenTests` - (default: false) whether to recreate the whole application before each test
* * `recreateApplicationBetweenRequests` - (default: false) whether to recreate the whole application before each request
*
* ## Public properties
*
* * application - instance of `\Mezzio\Application`
* * container - instance of `\Psr\Container\ContainerInterface`
* * client - BrowserKit client
*
*/
class Mezzio extends Framework implements DoctrineProvider
{
/**
* @var array<string, mixed>
*/
protected array $config = [
'container' => 'config/container.php',
'recreateApplicationBetweenTests' => true,
'recreateApplicationBetweenRequests' => false,
];
/**
* @var \Codeception\Lib\Connector\Mezzio
*/
public ?AbstractBrowser $client;
/**
* @deprecated Doesn't work as expected if Application is recreated between requests
*/
public ContainerInterface $container;
/**
* @deprecated Doesn't work as expected if Application is recreated between requests
*/
public Application $application;
public function _initialize(): void
{
$this->client = new MezzioConnector();
$this->client->setConfig($this->config);
if ($this->config['recreateApplicationBetweenTests'] == false && $this->config['recreateApplicationBetweenRequests'] == false) {
$this->application = $this->client->initApplication();
$this->container = $this->client->getContainer();
}
}
public function _before(TestInterface $test): void
{
$this->client = new MezzioConnector();
$this->client->setConfig($this->config);
if ($this->config['recreateApplicationBetweenTests'] != false && $this->config['recreateApplicationBetweenRequests'] == false) {
$this->application = $this->client->initApplication();
$this->container = $this->client->getContainer();
} elseif ($this->application !== null) {
$this->client->setApplication($this->application);
}
}
public function _after(TestInterface $test): void
{
//Close the session, if any are open
if (session_status() == PHP_SESSION_ACTIVE) {
session_write_close();
}
if (isset($_SESSION)) {
$_SESSION = [];
}
parent::_after($test);
}
public function _getEntityManager(): EntityManagerInterface
{
$service = 'Doctrine\ORM\EntityManager';
if (!$this->container->has($service)) {
throw new AssertionFailedError("Service $service is not available in container");
}
return $this->container->get('Doctrine\ORM\EntityManager');
}
}