Repository navigation
Expand file tree
/
Copy pathSymfony2.php
More file actions
308 lines (282 loc) · 9.68 KB
/
Copy pathSymfony2.php
File metadata and controls
308 lines (282 loc) · 9.68 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<?php
namespace Codeception\Module;
use Codeception\Configuration;
use Codeception\TestCase;
use Codeception\Lib\Framework;
use Codeception\Exception\ModuleRequireException;
use Codeception\Lib\Connector\Symfony2 as Symfony2Connector;
use Codeception\Lib\Interfaces\DoctrineProvider;
use Codeception\Lib\Interfaces\SupportsDomainRouting;
use Symfony\Component\Finder\Finder;
/**
* This module uses Symfony2 Crawler and HttpKernel to emulate requests and test response.
*
* ## Demo Project
*
* <https://github.com/Codeception/symfony-demo>
*
* ## Status
*
* * Maintainer: **davert**
* * Stability: **stable**
* * Contact: codecept@davert.mail.ua
*
* ## Config
*
* ### Symfony 2.x
*
* * app_path: 'app' - specify custom path to your app dir, where bootstrap cache and kernel interface is located.
* * environment: 'local' - environment used for load kernel
* * debug: true - turn on/off debug mode
* * em_service: 'doctrine.orm.entity_manager' - use the stated EntityManager to pair with Doctrine Module.
* *
* ### Example (`functional.suite.yml`) - Symfony 2.x Directory Structure
*
* ```
* modules:
* - Symfony2:
* app_path: 'app/front'
* environment: 'local_test'
* ```
*
* ### Symfony 3.x Directory Structure
*
* * app_path: 'app' - specify custom path to your app dir, where the kernel interface is located.
* * var_path: 'var' - specify custom path to your var dir, where bootstrap cache is located.
* * environment: 'local' - environment used for load kernel
* * em_service: 'doctrine.orm.entity_manager' - use the stated EntityManager to pair with Doctrine Module.
* * debug: true - turn on/off debug mode
*
* ### Example (`functional.suite.yml`) - Symfony 3 Directory Structure
*
* modules:
* enabled:
* - Symfony2:
* app_path: 'app/front'
* var_path: 'var'
* environment: 'local_test'
*
*
* ## Public Properties
*
* * kernel - HttpKernel instance
* * client - current Crawler instance
* * container - dependency injection container instance
*
*/
class Symfony2 extends Framework implements DoctrineProvider, SupportsDomainRouting
{
/**
* @var \Symfony\Component\HttpKernel\Kernel
*/
public $kernel;
/**
* @var \Symfony\Component\DependencyInjection\ContainerInterface
*/
public $container;
public $config = [
'app_path' => 'app',
'var_path' => 'app',
'environment' => 'test',
'debug' => true,
'em_service' => 'doctrine.orm.entity_manager'
];
/**
* @var
*/
protected $kernelClass;
public $permanentServices = [];
public function _initialize()
{
$cache = Configuration::projectDir() . $this->config['var_path'] . DIRECTORY_SEPARATOR . 'bootstrap.php.cache';
if (!file_exists($cache)) {
throw new ModuleRequireException(__CLASS__,
"Symfony2 bootstrap file not found in $cache\n \n" .
"Please specify path to bootstrap file using `var_path` config option\n \n" .
"If you are trying to load bootstrap from a Bundle provide path like:\n \n" .
"modules:\n enabled:\n" .
" - Symfony2:\n" .
" var_path: '../../app'\n" .
" app_path: '../../app'");
}
require_once $cache;
$this->kernelClass = $this->getKernelClass();
ini_set('xdebug.max_nesting_level', 200); // Symfony may have very long nesting level
}
public function _before(\Codeception\TestCase $test)
{
$this->bootKernel();
$this->container = $this->kernel->getContainer();
$this->client = new Symfony2Connector($this->kernel);
$this->client->followRedirects(true);
}
public function _getEntityManager()
{
$this->bootKernel();
if (!$this->kernel->getContainer()->has($this->config['em_service'])) {
return null;
}
$this->client->persistentServices[] = $this->config['em_service'];
$this->client->persistentServices[] = 'doctrine.orm.default_entity_manager';
return $this->kernel->getContainer()->get($this->config['em_service']);
}
protected function bootKernel()
{
if ($this->kernel) {
return;
}
$this->kernel = new $this->kernelClass($this->config['environment'], $this->config['debug']);
$this->kernel->boot();
}
/**
* Attempts to guess the kernel location.
*
* When the Kernel is located, the file is required.
*
* @return string The Kernel class name
*/
protected function getKernelClass()
{
$path = \Codeception\Configuration::projectDir() . $this->config['app_path'];
if (!file_exists(\Codeception\Configuration::projectDir() . $this->config['app_path'])) {
throw new ModuleRequireException(__CLASS__, "Can't load Kernel from $path.\nDirectory does not exists. Use `app_path` parameter to provide valid application path");
}
$finder = new Finder();
$finder->name('*Kernel.php')->depth('0')->in($path);
$results = iterator_to_array($finder);
if (!count($results)) {
throw new ModuleRequireException(__CLASS__, "AppKernel was not found at $path. Specify directory where Kernel class for your application is located with `app_path` parameter.");
}
$file = current($results);
$class = $file->getBasename('.php');
require_once $file;
return $class;
}
/**
* Opens web page using route name and parameters.
*
* ``` php
* <?php
* $I->amOnRoute('posts.create');
* $I->amOnRoute('posts.show', array('id' => 34));
* ?>
* ```
*
* @param $routeName
* @param array $params
*/
public function amOnRoute($routeName, array $params = [])
{
if (!$this->kernel->getContainer()->has('router')) {
$this->fail('Router not found.');
}
$router = $this->kernel->getContainer()->get('router');
$route = $router->getRouteCollection()->get($routeName);
if (!$route) {
$this->fail(sprintf('Route with name "%s" does not exists.', $routeName));
}
$url = $router->generate($routeName, $params);
$this->amOnPage($url);
}
/**
* Checks that current url matches route.
*
* ``` php
* <?php
* $I->seeCurrentRouteIs('posts.index');
* $I->seeCurrentRouteIs('posts.show', array('id' => 8));
* ?>
* ```
*
* @param $routeName
* @param array $params
*/
public function seeCurrentRouteIs($routeName, array $params = [])
{
if (!$this->kernel->getContainer()->has('router')) {
$this->fail('Router not found.');
}
$router = $this->kernel->getContainer()->get('router');
$route = $router->getRouteCollection()->get($routeName);
if (!$route) {
$this->fail(sprintf('Route with name "%s" does not exists.', $routeName));
}
$this->seeCurrentUrlEquals($router->generate($routeName, $params));
}
/**
* Checks if any email were sent by last request
*
* @throws \LogicException
*/
public function seeEmailIsSent()
{
$profile = $this->getProfiler();
if (!$profile) {
$this->fail('Emails can\'t be tested without Profiler');
}
if (!$profile->hasCollector('swiftmailer')) {
$this->fail('Emails can\'t be tested without SwiftMailer connector');
}
$this->assertGreaterThan(0, $profile->getCollector('swiftmailer')->getMessageCount());
}
/**
* Grabs a service from Symfony DIC container.
* Recommended to use for unit testing.
*
* ``` php
* <?php
* $em = $I->grabServiceFromContainer('doctrine');
* ?>
* ```
*
* @param $service
* @return mixed
*/
public function grabServiceFromContainer($service)
{
if (!$this->kernel->getContainer()->has($service)) {
$this->fail("Service $service is not available in container");
}
return $this->kernel->getContainer()->get($service);
}
/**
* @return \Symfony\Component\HttpKernel\Profiler\Profile
*/
protected function getProfiler()
{
if (!$this->kernel->getContainer()->has('profiler')) {
return null;
}
$profiler = $this->kernel->getContainer()->get('profiler');
$response = $this->client->getResponse();
if (null === $response) {
$this->fail("You must perform a request before using this method.");
}
return $profiler->loadProfileFromResponse($response);
}
/**
* @param $url
*/
protected function debugResponse($url)
{
parent::debugResponse($url);
if ($profile = $this->getProfiler()) {
if ($profile->hasCollector('security')) {
if ($profile->getCollector('security')->isAuthenticated()) {
$this->debugSection('User', $profile->getCollector('security')->getUser() . ' [' . implode(',', $profile->getCollector('security')->getRoles()) . ']');
} else {
$this->debugSection('User', 'Anonymous');
}
}
if ($profile->hasCollector('swiftmailer')) {
$messages = $profile->getCollector('swiftmailer')->getMessageCount();
if ($messages) {
$this->debugSection('Emails', $messages . ' sent');
}
}
if ($profile->hasCollector('timer')) {
$this->debugSection('Time', $profile->getCollector('timer')->getTime());
}
}
}
}