Skip to content

Commit 70e5e06

Browse files
authored
Simplify the module code (#222)
1 parent f1b6bf1 commit 70e5e06

File tree

5 files changed

+42
-67
lines changed

5 files changed

+42
-67
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"doctrine/orm": "^3.5",
3434
"friendsofphp/php-cs-fixer": "^3.85",
3535
"phpstan/phpstan": "^2.1",
36+
"phpunit/phpunit": "^10.0",
3637
"symfony/browser-kit": "^5.4 | ^6.4 | ^7.3",
3738
"symfony/cache": "^5.4 | ^6.4 | ^7.3",
3839
"symfony/config": "^5.4 | ^6.4 | ^7.3",

src/Codeception/Module/Symfony.php

Lines changed: 28 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,14 @@ protected function getClient(): SymfonyConnector
334334
*/
335335
protected function getKernelClass(): string
336336
{
337+
/** @var class-string<Kernel> $kernelClass */
338+
$kernelClass = $this->config['kernel_class'];
339+
$this->requireAdditionalAutoloader();
340+
341+
if (class_exists($kernelClass)) {
342+
return $kernelClass;
343+
}
344+
337345
/** @var string $rootDir */
338346
$rootDir = codecept_root_dir();
339347
$path = $rootDir . $this->config['app_path'];
@@ -346,40 +354,21 @@ protected function getKernelClass(): string
346354
);
347355
}
348356

349-
$this->requireAdditionalAutoloader();
350-
351-
$finder = new Finder();
352-
$results = iterator_to_array($finder->name('*Kernel.php')->depth('0')->in($path));
353-
354-
if ($results === []) {
355-
throw new ModuleRequireException(
356-
self::class,
357-
"File with Kernel class was not found at {$path}.\n" .
358-
'Specify directory where file with Kernel class for your application is located with `app_path` parameter.'
359-
);
360-
}
361-
362-
$kernelClass = $this->config['kernel_class'];
363-
$filesRealPath = [];
357+
$finder = new Finder();
358+
$finder->name('*Kernel.php')->depth('0')->in($path);
364359

365-
foreach ($results as $file) {
360+
foreach ($finder as $file) {
366361
include_once $file->getRealPath();
367-
$filesRealPath[] = $file->getRealPath();
368362
}
369363

370-
if (class_exists($kernelClass)) {
371-
$ref = new ReflectionClass($kernelClass);
372-
$fileName = $ref->getFileName();
373-
if ($fileName !== false && in_array($fileName, $filesRealPath, true)) {
374-
/** @var class-string<Kernel> $kernelClass */
375-
return $kernelClass;
376-
}
364+
if (class_exists($kernelClass, false)) {
365+
return $kernelClass;
377366
}
378367

379368
throw new ModuleRequireException(
380369
self::class,
381-
"Kernel class was not found.\n" .
382-
'Specify directory where file with Kernel class for your application is located with `kernel_class` parameter.'
370+
"Kernel class was not found at {$path}.\n" .
371+
'Specify directory where file with Kernel class for your application is located with `app_path` parameter.'
383372
);
384373
}
385374

@@ -455,31 +444,19 @@ protected function debugResponse(mixed $url): void
455444
return;
456445
}
457446

458-
if ($profile->hasCollector(DataCollectorName::SECURITY->value)) {
459-
$securityCollector = $profile->getCollector(DataCollectorName::SECURITY->value);
460-
if ($securityCollector instanceof SecurityDataCollector) {
461-
$this->debugSecurityData($securityCollector);
462-
}
463-
}
464-
465-
if ($profile->hasCollector(DataCollectorName::MAILER->value)) {
466-
$mailerCollector = $profile->getCollector(DataCollectorName::MAILER->value);
467-
if ($mailerCollector instanceof MessageDataCollector) {
468-
$this->debugMailerData($mailerCollector);
469-
}
470-
}
471-
472-
if ($profile->hasCollector(DataCollectorName::NOTIFIER->value)) {
473-
$notifierCollector = $profile->getCollector(DataCollectorName::NOTIFIER->value);
474-
if ($notifierCollector instanceof NotificationDataCollector) {
475-
$this->debugNotifierData($notifierCollector);
476-
}
477-
}
478-
479-
if ($profile->hasCollector(DataCollectorName::TIME->value)) {
480-
$timeCollector = $profile->getCollector(DataCollectorName::TIME->value);
481-
if ($timeCollector instanceof TimeDataCollector) {
482-
$this->debugTimeData($timeCollector);
447+
$collectors = [
448+
DataCollectorName::SECURITY->value => [$this->debugSecurityData(...), SecurityDataCollector::class],
449+
DataCollectorName::MAILER->value => [$this->debugMailerData(...), MessageDataCollector::class],
450+
DataCollectorName::NOTIFIER->value => [$this->debugNotifierData(...), NotificationDataCollector::class],
451+
DataCollectorName::TIME->value => [$this->debugTimeData(...), TimeDataCollector::class],
452+
];
453+
454+
foreach ($collectors as $name => [$callback, $expectedClass]) {
455+
if ($profile->hasCollector($name)) {
456+
$collector = $profile->getCollector($name);
457+
if ($collector instanceof $expectedClass) {
458+
$callback($collector);
459+
}
483460
}
484461
}
485462
}

src/Codeception/Module/Symfony/BrowserAssertionsTrait.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,8 +359,7 @@ public function submitSymfonyForm(string $name, array $fields): void
359359

360360
$params = [];
361361
foreach ($fields as $key => $value) {
362-
$fixedKey = sprintf('%s%s', $name, $key);
363-
$params[$fixedKey] = $value;
362+
$params[$name . $key] = $value;
364363
}
365364

366365
$button = sprintf('%s_submit', $name);

src/Codeception/Module/Symfony/MailerAssertionsTrait.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -164,13 +164,12 @@ public function getMailerEvent(int $index = 0, ?string $transport = null): ?Mess
164164

165165
protected function getMessageMailerEvents(): MessageEvents
166166
{
167-
$mailer = $this->getService('mailer.message_logger_listener');
168-
if ($mailer instanceof MessageLoggerListener) {
169-
return $mailer->getEvents();
170-
}
171-
$mailer = $this->getService('mailer.logger_message_listener');
172-
if ($mailer instanceof MessageLoggerListener) {
173-
return $mailer->getEvents();
167+
$services = ['mailer.message_logger_listener', 'mailer.logger_message_listener'];
168+
foreach ($services as $serviceId) {
169+
$mailer = $this->getService($serviceId);
170+
if ($mailer instanceof MessageLoggerListener) {
171+
return $mailer->getEvents();
172+
}
174173
}
175174
Assert::fail("Emails can't be tested without Symfony Mailer service.");
176175
}

src/Codeception/Module/Symfony/NotifierAssertionsTrait.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -251,13 +251,12 @@ protected function getNotificationEvents(): NotificationEvents
251251
Assert::fail('Notifier assertions require Symfony 6.2 or higher.');
252252
}
253253

254-
$notifier = $this->getService('notifier.notification_logger_listener');
255-
if ($notifier instanceof NotificationLoggerListener) {
256-
return $notifier->getEvents();
257-
}
258-
$notifier = $this->getService('notifier.logger_notification_listener');
259-
if ($notifier instanceof NotificationLoggerListener) {
260-
return $notifier->getEvents();
254+
$services = ['notifier.notification_logger_listener', 'notifier.logger_notification_listener'];
255+
foreach ($services as $serviceId) {
256+
$notifier = $this->getService($serviceId);
257+
if ($notifier instanceof NotificationLoggerListener) {
258+
return $notifier->getEvents();
259+
}
261260
}
262261
Assert::fail("Notifications can't be tested without Symfony Notifier service.");
263262
}

0 commit comments

Comments
 (0)