Repository navigation
Expand file tree
/
Copy pathSymfony2.php
More file actions
417 lines (379 loc) · 12.7 KB
/
Copy pathSymfony2.php
File metadata and controls
417 lines (379 loc) · 12.7 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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
<?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\PartedModule;
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: **raistlin**
* * Stability: **stable**
*
* ## 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.
* * cache_router: 'false' - enable router caching between tests in order to [increase performance](http://lakion.com/blog/how-did-we-speed-up-sylius-behat-suite-with-blackfire)
*
* ### 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
* * cache_router: 'false' - enable router caching between tests in order to [increase performance](http://lakion.com/blog/how-did-we-speed-up-sylius-behat-suite-with-blackfire)
*
* ### 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
*
* ## Parts
*
* * services - allows to use Symfony2 DIC only with WebDriver or PhpBrowser modules.
*
* Usage example:
*
* ```yaml
* class_name: AcceptanceTester
* modules:
* enabled:
* - Symfony2:
* part: SERVICES
* - Doctrine2:
* depends: Symfony2
* - WebDriver:
* url: http://your-url.com
* browser: phantomjs
* ```
*/
class Symfony2 extends Framework implements DoctrineProvider, PartedModule
{
/**
* @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,
'cache_router' => false,
'em_service' => 'doctrine.orm.entity_manager'
];
/**
* @return array
*/
public function _parts()
{
return ['services'];
}
/**
* @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();
$maxNestingLevel = 200; // Symfony may have very long nesting level
$xdebugMaxLevelKey = 'xdebug.max_nesting_level';
if (ini_get($xdebugMaxLevelKey) < $maxNestingLevel) {
ini_set($xdebugMaxLevelKey, $maxNestingLevel);
}
$this->bootKernel();
$this->container = $this->kernel->getContainer();
}
public function _before(\Codeception\TestCase $test)
{
$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();
if ($this->config['cache_router'] === true) {
if (isset($this->permanentServices['router'])) {
$this->kernel->getContainer()->set('router', $this->permanentServices['router']);
} else {
$this->permanentServices['router'] = $this->getRouter();
}
}
}
/**
* 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;
}
/**
* Get router from container.
*
* @return object
*/
private function getRouter()
{
if (!$this->kernel->getContainer()->has('router')) {
$this->fail('Router not found.');
}
return $this->kernel->getContainer()->get('router');
}
/**
* Invalidate previously cached routes.
*/
public function invalidateCachedRouter()
{
$this->permanentServices['router'] = null;
}
/**
* 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 = [])
{
$router = $this->getRouter();
$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 = [])
{
$router = $this->getRouter();
$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 that current url matches route.
* Unlike seeCurrentRouteIs, this can matches without exact route parameters
*
* ``` php
* <?php
* $I->seeCurrentRouteMatches('my_blog_pages');
* ?>
* ```
*
* @param $routeName
*/
public function seeInCurrentRoute($routeName)
{
$router = $this->grabServiceFromContainer('router');
$route = $router->getRouteCollection()->get($routeName);
if (!$route) {
$this->fail(sprintf('Route with name "%s" does not exists.', $routeName));
}
try {
$matchedRouteName = $router->match($this->grabFromCurrentUrl())['_route'];
} catch (\Exception\ResourceNotFoundException $e) {
$this->fail(sprintf('The "%s" url does not match with any route', $routeName));
}
$this->assertEquals($matchedRouteName, $routeName);
}
/**
* 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
* @part services
*/
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());
}
}
}
/**
* Returns a list of recognized domain names.
*
* @return array
*/
protected function getInternalDomains()
{
$internalDomains = [];
/* @var \Symfony\Component\Routing\Route $route */
foreach ($this->getRouter()->getRouteCollection() as $route) {
if (!is_null($route->getHost())) {
$compiled = $route->compile();
if (!is_null($compiled->getHostRegex())) {
$internalDomains[] = $compiled->getHostRegex();
}
}
}
return array_unique($internalDomains);
}
}