Repository navigation
Expand file tree
/
Copy pathSymfony2.php
More file actions
183 lines (160 loc) · 5.88 KB
/
Copy pathSymfony2.php
File metadata and controls
183 lines (160 loc) · 5.88 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
<?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;
/**
* This module uses Symfony2 Crawler and HttpKernel to emulate requests and get response.
*
* It implements common Framework interface.
*
* ## Status
*
* * Maintainer: **davert**
* * Stability: **stable**
* * Contact: codecept@davert.mail.ua
*
* ## Config
*
* * app_path: 'app' - specify custom path to your app dir, where bootstrap cache and kernel interface is located.
*
* ### Example (`functional.suite.yml`)
*
* modules:
* enabled: [Symfony2]
* config:
* Symfony2:
* app_path: 'app/front'
*
* ## Public Properties
*
* * kernel - HttpKernel instance
* * client - current Crawler instance
*
*/
class Symfony2 extends \Codeception\Util\Framework
{
/**
* @var \Symfony\Component\HttpKernel\Kernel
*/
public $kernel;
/**
* @var \Symfony\Component\DependencyInjection\ContainerInterface
*/
public $container;
public $config = array('app_path' => 'app');
/**
* @var
*/
protected $kernelClass;
protected $clientClass = '\Symfony\Component\HttpKernel\Client';
public function _initialize() {
$cache = \Codeception\Configuration::projectDir() . $this->config['app_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('test', true);
}
public function _before(\Codeception\TestCase $test) {
$this->kernel->boot();
$this->container = $this->kernel->getContainer();
if ($this->container->has('test.client')) { // it is Symfony2.2
$this->client = $this->container->get('test.client');
} else {
$this->client = new $this->clientClass($this->kernel);
}
$this->client->followRedirects(true);
}
public function _after(\Codeception\TestCase $test) {
$this->kernel->shutdown();
parent::_after($test);
}
/**
* 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_dir" parameter.');
}
$file = current($results);
$class = $file->getBasename('.php');
require_once $file;
return $class;
}
/**
* Authenticates user for HTTP_AUTH
*
* @param $username
* @param $password
*/
public function amHttpAuthenticated($username, $password) {
$this->client->setServerParameter('PHP_AUTH_USER', $username);
$this->client->setServerParameter('PHP_AUTH_PW', $password);
}
/**
* 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());
if ($profile->hasCollector('db')) $this->debugSection('Db', $profile->getCollector('db')->getQueryCount().' queries');
}
}
}