forked from Codeception/module-symfony
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSymfony.php
453 lines (414 loc) · 15.8 KB
/
Symfony.php
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
<?php
declare(strict_types=1);
namespace Codeception\Module;
use BadMethodCallException;
use Codeception\Exception\ModuleRequireException;
use Codeception\Lib\Connector\Symfony as SymfonyConnector;
use Codeception\Lib\Framework;
use Codeception\Lib\Interfaces\DoctrineProvider;
use Codeception\Lib\Interfaces\PartedModule;
use Codeception\Module\Symfony\BrowserAssertionsTrait;
use Codeception\Module\Symfony\ConsoleAssertionsTrait;
use Codeception\Module\Symfony\DoctrineAssertionsTrait;
use Codeception\Module\Symfony\EventsAssertionsTrait;
use Codeception\Module\Symfony\FormAssertionsTrait;
use Codeception\Module\Symfony\MailerAssertionsTrait;
use Codeception\Module\Symfony\MimeAssertionsTrait;
use Codeception\Module\Symfony\ParameterAssertionsTrait;
use Codeception\Module\Symfony\RouterAssertionsTrait;
use Codeception\Module\Symfony\SecurityAssertionsTrait;
use Codeception\Module\Symfony\ServicesAssertionsTrait;
use Codeception\Module\Symfony\SessionAssertionsTrait;
use Codeception\Module\Symfony\TimeAssertionsTrait;
use Codeception\Module\Symfony\TwigAssertionsTrait;
use Codeception\Module\Symfony\ValidatorAssertionsTrait;
use Codeception\TestInterface;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use LogicException;
use ReflectionClass;
use ReflectionException;
use Symfony\Bundle\SecurityBundle\DataCollector\SecurityDataCollector;
use Symfony\Component\BrowserKit\AbstractBrowser;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface;
use Symfony\Component\HttpKernel\DataCollector\TimeDataCollector;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\HttpKernel\Profiler\Profile;
use Symfony\Component\HttpKernel\Profiler\Profiler;
use Symfony\Component\Mailer\DataCollector\MessageDataCollector;
use Symfony\Component\VarDumper\Cloner\Data;
use function array_keys;
use function array_map;
use function array_unique;
use function class_exists;
use function codecept_root_dir;
use function count;
use function file_exists;
use function implode;
use function ini_get;
use function ini_set;
use function iterator_to_array;
use function number_format;
use function sprintf;
/**
* This module uses [Symfony's DomCrawler](https://symfony.com/doc/current/components/dom_crawler.html)
* and [HttpKernel Component](https://symfony.com/doc/current/components/http_kernel.html) to emulate requests and test response.
*
* * Access Symfony services through the dependency injection container: [`$I->grabService(...)`](#grabService)
* * Use Doctrine to test against the database: `$I->seeInRepository(...)` - see [Doctrine Module](https://codeception.com/docs/modules/Doctrine)
* * Assert that emails would have been sent: [`$I->seeEmailIsSent()`](#seeEmailIsSent)
* * Tests are wrapped into Doctrine transaction to speed them up.
* * Symfony Router can be cached between requests to speed up testing.
*
* ## Demo Project
*
* <https://github.com/Codeception/symfony-module-tests>
*
* ## Config
*
* ### Symfony 5.4 or higher
*
* * `app_path`: 'src' - Specify custom path to your app dir, where the kernel interface is located.
* * `environment`: 'local' - Environment used for load kernel
* * `kernel_class`: 'App\Kernel' - Kernel class name
* * `em_service`: 'doctrine.orm.entity_manager' - Use the stated EntityManager to pair with Doctrine Module.
* * `debug`: true - Turn on/off [debug mode](https://codeception.com/docs/Debugging)
* * `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) (can have an impact on ajax requests sending via '$I->sendAjaxPostRequest()')
* * `rebootable_client`: 'true' - Reboot client's kernel before each request
* * `guard`: 'false' - Enable custom authentication system with guard (only for Symfony 5.4)
* * `bootstrap`: 'false' - Enable the test environment setup with the tests/bootstrap.php file if it exists or with Symfony DotEnv otherwise. If false, it does nothing.
* * `authenticator`: 'false' - Reboot client's kernel before each request (only for Symfony 6.0 or higher)
*
* #### Sample `Functional.suite.yml`
*
* modules:
* enabled:
* - Symfony:
* app_path: 'src'
* environment: 'test'
*
*
* ## Public Properties
*
* * kernel - HttpKernel instance
* * client - current Crawler instance
*
* ## Parts
*
* * `services`: Includes methods related to the Symfony dependency injection container (DIC):
* * grabService
* * persistService
* * persistPermanentService
* * unpersistService
*
* See [WebDriver module](https://codeception.com/docs/modules/WebDriver#Loading-Parts-from-other-Modules)
* for general information on how to load parts of a framework module.
*
* Usage example:
*
* ```yaml
* actor: AcceptanceTester
* modules:
* enabled:
* - Symfony:
* part: services
* - Doctrine:
* depends: Symfony
* - WebDriver:
* url: http://example.com
* browser: firefox
* ```
*
* If you're using Symfony with Eloquent ORM (instead of Doctrine), you can load the [`ORM` part of Laravel module](https://codeception.com/docs/modules/Laravel#Parts)
* in addition to Symfony module.
*
*/
class Symfony extends Framework implements DoctrineProvider, PartedModule
{
use BrowserAssertionsTrait;
use ConsoleAssertionsTrait;
use DoctrineAssertionsTrait;
use EventsAssertionsTrait;
use FormAssertionsTrait;
use MailerAssertionsTrait;
use MimeAssertionsTrait;
use ParameterAssertionsTrait;
use RouterAssertionsTrait;
use SecurityAssertionsTrait;
use ServicesAssertionsTrait;
use SessionAssertionsTrait;
use TimeAssertionsTrait;
use TwigAssertionsTrait;
use ValidatorAssertionsTrait;
public Kernel $kernel;
/**
* @var SymfonyConnector
*/
public ?AbstractBrowser $client = null;
/**
* @var array<string, mixed>
*/
public array $config = [
'app_path' => 'app',
'kernel_class' => 'App\Kernel',
'environment' => 'test',
'debug' => true,
'cache_router' => false,
'em_service' => 'doctrine.orm.entity_manager',
'rebootable_client' => true,
'authenticator' => false,
'bootstrap' => false,
'guard' => false
];
protected ?string $kernelClass = null;
/**
* Services that should be persistent permanently for all tests
*/
protected array $permanentServices = [];
/**
* Services that should be persistent during test execution between kernel reboots
*/
protected array $persistentServices = [];
/**
* @return string[]
*/
public function _parts(): array
{
return ['services'];
}
public function _initialize(): void
{
$this->kernelClass = $this->getKernelClass();
$this->setXdebugMaxNestingLevel(200);
$this->kernel = new $this->kernelClass($this->config['environment'], $this->config['debug']);
if ($this->config['bootstrap']) {
$this->bootstrapEnvironment();
}
$this->kernel->boot();
if ($this->config['cache_router']) {
$this->persistPermanentService('router');
}
}
/**
* Initialize new client instance before each test
*/
public function _before(TestInterface $test): void
{
$this->persistentServices = array_merge($this->persistentServices, $this->permanentServices);
$this->client = new SymfonyConnector($this->kernel, $this->persistentServices, $this->config['rebootable_client']);
}
/**
* Update permanent services after each test
*/
public function _after(TestInterface $test): void
{
foreach (array_keys($this->permanentServices) as $serviceName) {
$this->permanentServices[$serviceName] = $this->grabService($serviceName);
}
parent::_after($test);
}
protected function onReconfigure(array $settings = []): void
{
parent::_beforeSuite($settings);
$this->_initialize();
}
/**
* Retrieve Entity Manager.
*
* EM service is retrieved once and then that instance returned on each call
*/
public function _getEntityManager(): EntityManagerInterface
{
if ($this->kernel === null) {
$this->fail('Symfony module is not loaded');
}
$emService = $this->config['em_service'];
if (!isset($this->permanentServices[$emService])) {
$this->persistPermanentService($emService);
$container = $this->_getContainer();
$services = ['doctrine', 'doctrine.orm.default_entity_manager', 'doctrine.dbal.default_connection'];
foreach ($services as $service) {
if ($container->has($service)) {
$this->persistPermanentService($service);
}
}
}
return $this->permanentServices[$emService];
}
public function _getContainer(): ContainerInterface
{
$container = $this->kernel->getContainer();
return $container->has('test.service_container') ? $container->get('test.service_container') : $container;
}
protected function getClient(): SymfonyConnector
{
return $this->client ?: $this->fail('Client is not initialized');
}
/**
* Attempts to guess the kernel location.
* When the Kernel is located, the file is required.
*
* @return string The Kernel class name
* @throws ModuleRequireException|ReflectionException
*/
protected function getKernelClass(): string
{
$path = codecept_root_dir() . $this->config['app_path'];
if (!file_exists($path)) {
throw new ModuleRequireException(
self::class,
"Can't load Kernel from {$path}.\n"
. 'Directory does not exist. Set `app_path` in your suite configuration to a valid application path.'
);
}
$this->requireAdditionalAutoloader();
$finder = new Finder();
$results = iterator_to_array($finder->name('*Kernel.php')->depth('0')->in($path));
if ($results === []) {
throw new ModuleRequireException(
self::class,
"File with Kernel class was not found at {$path}.\n"
. 'Specify directory where file with Kernel class for your application is located with `app_path` parameter.'
);
}
$kernelClass = $this->config['kernel_class'];
$filesRealPath = array_map(static function ($file) {
require_once $file;
return $file->getRealPath();
}, $results);
if (class_exists($kernelClass)) {
$reflectionClass = new ReflectionClass($kernelClass);
if (in_array($reflectionClass->getFileName(), $filesRealPath, true)) {
return $kernelClass;
}
}
throw new ModuleRequireException(
self::class,
"Kernel class was not found.\n"
. 'Specify directory where file with Kernel class for your application is located with `app_path` parameter.'
);
}
protected function getProfile(): ?Profile
{
/** @var Profiler $profiler */
$profiler = $this->getService('profiler');
try {
return $profiler?->loadProfileFromResponse($this->getClient()->getResponse());
} catch (BadMethodCallException) {
$this->fail('You must perform a request before using this method.');
} catch (Exception $e) {
$this->fail($e->getMessage());
}
return null;
}
/**
* Grabs a Symfony Data Collector
*/
protected function grabCollector(string $collector, string $function, ?string $message = null): DataCollectorInterface
{
$profile = $this->getProfile();
if ($profile === null) {
$this->fail(sprintf("The Profile is needed to use the '%s' function.", $function));
}
if (!$profile->hasCollector($collector)) {
$this->fail($message ?: "The '{$collector}' collector is needed to use the '{$function}' function.");
}
return $profile->getCollector($collector);
}
/**
* Set the data that will be displayed when running a test with the `--debug` flag
*
* @param mixed $url
*/
protected function debugResponse($url): void
{
parent::debugResponse($url);
if ($profile = $this->getProfile()) {
$collectors = [
'security' => 'debugSecurityData',
'mailer' => 'debugMailerData',
'time' => 'debugTimeData',
];
foreach ($collectors as $collector => $method) {
if ($profile->hasCollector($collector)) {
$this->$method($profile->getCollector($collector));
}
}
}
}
/**
* Returns a list of recognized domain names.
*/
protected function getInternalDomains(): array
{
$internalDomains = [];
$router = $this->grabRouterService();
$routes = $router->getRouteCollection();
foreach ($routes as $route) {
if ($route->getHost() !== null) {
$compiledRoute = $route->compile();
if ($compiledRoute->getHostRegex() !== null) {
$internalDomains[] = $compiledRoute->getHostRegex();
}
}
}
return array_unique($internalDomains);
}
private function setXdebugMaxNestingLevel(int $maxNestingLevel): void
{
if (ini_get('xdebug.max_nesting_level') < $maxNestingLevel) {
ini_set('xdebug.max_nesting_level', (string)$maxNestingLevel);
}
}
private function bootstrapEnvironment(): void
{
$bootstrapFile = $this->kernel->getProjectDir() . '/tests/bootstrap.php';
if (file_exists($bootstrapFile)) {
require_once $bootstrapFile;
} else {
if (!method_exists(Dotenv::class, 'bootEnv')) {
throw new LogicException(
"Symfony DotEnv is missing. Try running 'composer require symfony/dotenv'\n" .
"If you can't install DotEnv add your env files to the 'params' key in codeception.yml\n" .
"or update your symfony/framework-bundle recipe by running:\n" .
'composer recipes:install symfony/framework-bundle --force'
);
}
$_ENV['APP_ENV'] = $this->config['environment'];
(new Dotenv())->bootEnv('.env');
}
}
private function debugSecurityData(SecurityDataCollector $security): void
{
if ($security->isAuthenticated()) {
$roles = $security->getRoles();
$rolesString = implode(',', $roles instanceof Data ? $roles->getValue() : $roles);
$userInfo = $security->getUser() . ' [' . $rolesString . ']';
} else {
$userInfo = 'Anonymous';
}
$this->debugSection('User', $userInfo);
}
private function debugMailerData(MessageDataCollector $mailerCollector): void
{
$this->debugSection('Emails', count($mailerCollector->getEvents()->getMessages()) . ' sent');
}
private function debugTimeData(TimeDataCollector $timeCollector): void
{
$this->debugSection('Time', number_format($timeCollector->getDuration(), 2) . ' ms');
}
/**
* Ensures autoloader loading of additional directories.
* It is only required for CI jobs to run correctly.
*/
private function requireAdditionalAutoloader(): void
{
$autoLoader = codecept_root_dir() . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
if (file_exists($autoLoader)) {
require_once $autoLoader;
}
}
}