Repository navigation
Expand file tree
/
Copy pathSymfony2.php
More file actions
191 lines (172 loc) · 6.29 KB
/
Copy pathSymfony2.php
File metadata and controls
191 lines (172 loc) · 6.29 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
<?php
namespace Codeception\Module;
use Codeception\Exception\ModuleRequire;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpKernel\Exception\FlattenException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpFoundation\Response;
use Codeception\Lib\Connector\Symfony2 as Symfony2Connector;
/**
* This module uses Symfony2 Crawler and HttpKernel to emulate requests and test response.
*
* ## Demo Project
*
* <https://github.com/DavertMik/SymfonyCodeceptionApp>
*
* ## 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
*
*
* ### Example (`functional.suite.yml`) - Symfony 2.x Directory Structure
*
* modules:
* enabled: [Symfony2]
* config:
* 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
* * debug: true - turn on/off debug mode
*
* ### Example (`functional.suite.yml`) - Symfony 3 Directory Structure
*
* modules:
* enabled: [Symfony2]
* config:
* 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 \Codeception\Lib\Framework
{
/**
* @var \Symfony\Component\HttpKernel\Kernel
*/
public $kernel;
/**
* @var \Symfony\Component\DependencyInjection\ContainerInterface
*/
public $container;
public $config = array('app_path' => 'app', 'var_path' => 'app', 'environment' => 'test', 'debug' => true);
/**
* @var
*/
protected $kernelClass;
public $permanentServices = [];
public function _initialize() {
$cache = \Codeception\Configuration::projectDir() . $this->config['var_path'] . DIRECTORY_SEPARATOR . 'bootstrap.php.cache';
if (!file_exists($cache)) {
throw new ModuleRequire(__CLASS__, 'Symfony2 bootstrap file not found in '.$cache);
}
require_once $cache;
$this->kernelClass = $this->getKernelClass();
$this->kernel = new $this->kernelClass($this->config['environment'], $this->config['debug']);
ini_set('xdebug.max_nesting_level', 200); // Symfony may have very long nesting level
}
public function _before(\Codeception\TestCase $test) {
$this->kernel->boot();
$this->container = $this->kernel->getContainer();
$this->client = new Symfony2Connector($this->kernel);
$this->client->followRedirects(true);
}
/**
* Attempts to guess the kernel location.
*
* When the Kernel is located, the file is required.
*
* @return string The Kernel class name
*/
protected function getKernelClass()
{
$finder = new Finder();
$finder->name('*Kernel.php')->depth('0')->in(\Codeception\Configuration::projectDir() . $this->config['app_path']);
$results = iterator_to_array($finder);
if (!count($results)) {
throw new ModuleRequire(__CLASS__, 'AppKernel was not found. Specify directory where Kernel class for your application is located in "app_path" parameter.');
}
$file = current($results);
$class = $file->getBasename('.php');
require_once $file;
return $class;
}
/**
* Checks if any email were sent by last request
*
* @throws \LogicException
*/
public function seeEmailIsSent() {
$profile = $this->getProfiler();
if (!$profile) \PHPUnit_Framework_Assert::fail('Emails can\'t be tested without Profiler');
if (!$profile->hasCollector('swiftmailer')) \PHPUnit_Framework_Assert::fail('Emails can\'t be tested without SwiftMailer connector');
\PHPUnit_Framework_Assert::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');
return $profiler->loadProfileFromResponse($this->client->getResponse());
}
protected function debugResponse()
{
$this->debugSection('Page', $this->client->getHistory()->current()->getUri());
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());
}
}
}