-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathSymfony.php
More file actions
510 lines (449 loc) · 17.1 KB
/
Symfony.php
File metadata and controls
510 lines (449 loc) · 17.1 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
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
<?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\CacheTrait;
use Codeception\Module\Symfony\ConsoleAssertionsTrait;
use Codeception\Module\Symfony\DataCollectorName;
use Codeception\Module\Symfony\DoctrineAssertionsTrait;
use Codeception\Module\Symfony\DomCrawlerAssertionsTrait;
use Codeception\Module\Symfony\EventsAssertionsTrait;
use Codeception\Module\Symfony\FormAssertionsTrait;
use Codeception\Module\Symfony\HttpClientAssertionsTrait;
use Codeception\Module\Symfony\HttpKernelAssertionsTrait;
use Codeception\Module\Symfony\LoggerAssertionsTrait;
use Codeception\Module\Symfony\MailerAssertionsTrait;
use Codeception\Module\Symfony\MimeAssertionsTrait;
use Codeception\Module\Symfony\NotifierAssertionsTrait;
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\TranslationAssertionsTrait;
use Codeception\Module\Symfony\TwigAssertionsTrait;
use Codeception\Module\Symfony\ValidatorAssertionsTrait;
use Codeception\TestInterface;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\AssertionFailedError;
use ReflectionException;
use Symfony\Bundle\SecurityBundle\DataCollector\SecurityDataCollector;
use Symfony\Component\BrowserKit\AbstractBrowser;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\Finder\Finder;
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\Notifier\DataCollector\NotificationDataCollector;
use Symfony\Component\VarDumper\Cloner\Data;
use function array_filter;
use function array_map;
use function class_exists;
use function codecept_root_dir;
use function count;
use function extension_loaded;
use function file_exists;
use function implode;
use function ini_get;
use function ini_set;
use function is_object;
use function is_subclass_of;
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, 6.4, 7.4 or 8.0
*
* * `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' - Enable custom authentication system with authenticator (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 CacheTrait;
use ConsoleAssertionsTrait;
use DoctrineAssertionsTrait;
use DomCrawlerAssertionsTrait;
use EventsAssertionsTrait;
use FormAssertionsTrait;
use HttpClientAssertionsTrait;
use HttpKernelAssertionsTrait;
use LoggerAssertionsTrait;
use MailerAssertionsTrait;
use MimeAssertionsTrait;
use NotifierAssertionsTrait;
use ParameterAssertionsTrait;
use RouterAssertionsTrait;
use SecurityAssertionsTrait;
use ServicesAssertionsTrait;
use SessionAssertionsTrait;
use TimeAssertionsTrait;
use TranslationAssertionsTrait;
use TwigAssertionsTrait;
use ValidatorAssertionsTrait;
public Kernel $kernel;
/** @var SymfonyConnector|null */
public ?AbstractBrowser $client = null;
/**
* @var array{
* app_path:string,
* kernel_class:string,
* environment:string,
* debug:bool,
* cache_router:bool,
* em_service:string,
* rebootable_client:bool,
* authenticator:bool,
* bootstrap:bool,
* guard:bool
* }
*/
public array $config = [
'app_path' => 'src',
'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;
/** @return list<string> */
public function _parts(): array
{
return ['services'];
}
public function _initialize(): void
{
$this->kernelClass = $this->getKernelClass();
$this->setXdebugMaxNestingLevel(200);
/** @var class-string<Kernel> $kernelClass */
$kernelClass = $this->kernelClass;
$this->kernel = new $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->state = [];
$this->persistentServices = $this->persistentServices === []
? $this->permanentServices
: [...$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 ($this->permanentServices as $serviceName => $_) {
$service = $this->getService($serviceName);
if (is_object($service)) {
$this->permanentServices[$serviceName] = $service;
} else {
unset($this->permanentServices[$serviceName]);
}
}
$this->persistentServices = [];
$this->cachedResponse = null;
$this->cachedProfile = null;
parent::_after($test);
}
/** @param array<string, string|bool> $settings */
protected function onReconfigure(array $settings = []): void
{
parent::_beforeSuite($settings);
$this->_initialize();
}
/**
* Retrieve the Doctrine EntityManager.
* EntityManager service is retrieved once and then reused.
*/
public function _getEntityManager(): EntityManagerInterface
{
/** @var non-empty-string $emService */
$emService = $this->config['em_service'];
if (!isset($this->permanentServices[$emService])) {
$this->persistPermanentService($emService);
$container = $this->_getContainer();
foreach (['doctrine', 'doctrine.orm.default_entity_manager', 'doctrine.dbal.default_connection'] as $service) {
if ($container->has($service)) {
$this->persistPermanentService($service);
}
}
}
$em = $this->permanentServices[$emService];
if (!$em instanceof EntityManagerInterface) {
Assert::fail(sprintf('Service "%s" is not an instance of EntityManagerInterface.', $emService));
}
return $em;
}
protected function getClient(): SymfonyConnector
{
return $this->client ?? Assert::fail('Client is not initialized');
}
/**
* Find and require the Kernel class file.
*
* @return class-string<Kernel>
* @throws ModuleRequireException|ReflectionException
*/
protected function getKernelClass(): string
{
$kernelClass = $this->config['kernel_class'];
$this->requireAdditionalAutoloader();
if (class_exists($kernelClass) && is_subclass_of($kernelClass, Kernel::class)) {
/** @var class-string<Kernel> $kernelClass */
return $kernelClass;
}
/** @var string $rootDir */
$rootDir = codecept_root_dir();
$path = $rootDir . $this->config['app_path'];
if (!file_exists($path)) {
throw new ModuleRequireException(
self::class,
"Can't load Kernel from {$path}.\nDirectory does not exist. Set `app_path` in your suite configuration to a valid application path."
);
}
$expectedKernelPath = $path . DIRECTORY_SEPARATOR . 'Kernel.php';
if (file_exists($expectedKernelPath)) {
include_once $expectedKernelPath;
} else {
foreach ((new Finder())->name('*Kernel.php')->depth('0')->in($path) as $file) {
include_once $file->getRealPath();
}
}
if (class_exists($kernelClass, false) && is_subclass_of($kernelClass, Kernel::class)) {
/** @var class-string<Kernel> $kernelClass */
return $kernelClass;
}
throw new ModuleRequireException(
self::class,
"Kernel class was not found at {$path}.\nSpecify directory where file with Kernel class for your application is located with `app_path` parameter."
);
}
/**
* @throws AssertionFailedError
*/
protected function getProfile(): ?Profile
{
/** @var Profiler|null $profiler */
$profiler = $this->getService('profiler');
if ($profiler === null) {
return null;
}
try {
$response = $this->getClient()->getResponse();
if ($this->cachedResponse === $response) {
return $this->cachedProfile;
}
$profile = $profiler->loadProfileFromResponse($response);
$this->cachedResponse = $response;
$this->cachedProfile = $profile;
return $profile;
} catch (BadMethodCallException) {
Assert::fail('You must perform a request before using this method.');
}
}
/**
* Set the data that will be displayed when running a test with the `--debug` flag.
*/
protected function debugResponse(mixed $url): void
{
parent::debugResponse($url);
if (!$profile = $this->getProfile()) {
return;
}
$this->debugCollector($profile, DataCollectorName::SECURITY);
$this->debugCollector($profile, DataCollectorName::MAILER);
$this->debugCollector($profile, DataCollectorName::NOTIFIER);
$this->debugCollector($profile, DataCollectorName::TIME);
}
protected function doRebootClientKernel(): void
{
if ($this->client instanceof SymfonyConnector) {
$this->client->rebootKernel();
}
}
/**
* Ensure Xdebug allows deep nesting.
*/
private function setXdebugMaxNestingLevel(int $max): void
{
if (!extension_loaded('xdebug')) {
return;
}
if ((int) ini_get('xdebug.max_nesting_level') < $max) {
ini_set('xdebug.max_nesting_level', (string) $max);
}
}
/**
* Bootstrap environment via tests/bootstrap.php or Dotenv.
*/
private function bootstrapEnvironment(): void
{
$bootstrapFile = $this->kernel->getProjectDir() . '/tests/bootstrap.php';
if (file_exists($bootstrapFile)) {
include_once $bootstrapFile;
return;
}
$_ENV['APP_ENV'] = $this->config['environment'];
(new Dotenv())->bootEnv('.env');
}
private function debugSecurityData(SecurityDataCollector $securityCollector): void
{
if (!$securityCollector->isAuthenticated()) {
$this->debugSection('User', 'Anonymous');
return;
}
$roles = $securityCollector->getRoles();
if ($roles instanceof Data) {
$roles = $roles->getValue(true);
}
$rolesStr = implode(',', array_map('strval', array_filter((array) $roles, 'is_scalar')));
$this->debugSection('User', sprintf('%s [%s]', $securityCollector->getUser(), $rolesStr));
}
private function debugMailerData(MessageDataCollector $messageCollector): void
{
$this->debugSection('Emails', sprintf('%d sent', count($messageCollector->getEvents()->getMessages())));
}
private function debugNotifierData(NotificationDataCollector $notificationCollector): void
{
$this->debugSection('Notifications', sprintf('%d sent', count($notificationCollector->getEvents()->getMessages())));
}
private function debugTimeData(TimeDataCollector $timeCollector): void
{
$this->debugSection('Time', sprintf('%.2f ms', $timeCollector->getDuration()));
}
private function debugCollector(Profile $profile, DataCollectorName $name): void
{
if (!$profile->hasCollector($name->value)) {
return;
}
$collector = $profile->getCollector($name->value);
match (true) {
$collector instanceof SecurityDataCollector => $this->debugSecurityData($collector),
$collector instanceof MessageDataCollector => $this->debugMailerData($collector),
$collector instanceof NotificationDataCollector => $this->debugNotifierData($collector),
$collector instanceof TimeDataCollector => $this->debugTimeData($collector),
default => null,
};
}
/**
* Ensures autoloader loading of additional directories.
* It is only required for CI jobs to run correctly.
*/
private function requireAdditionalAutoloader(): void
{
/** @var string $rootDir */
$rootDir = codecept_root_dir();
$autoload = $rootDir . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
if (file_exists($autoload)) {
include_once $autoload;
}
}
/** @param non-empty-string $name */
protected function updateClientPersistentService(string $name, ?object $service): void
{
if (!$this->client instanceof SymfonyConnector) {
return;
}
if ($service === null) {
unset($this->client->persistentServices[$name]);
} else {
$this->client->persistentServices[$name] = $service;
}
}
}