Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@
/vendor/
/composer.lock
/framework-tests
/.php-cs-fixer.cache
/.php-cs-fixer.cache
tests/_output/
tests/_support/_generated/
tests/_support/FunctionalTester.php
var/

7 changes: 7 additions & 0 deletions codeception.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace: Tests
actor_suffix: Tester
paths:
tests: tests
output: tests/_output
data: tests/_data
support: tests/_support
17 changes: 16 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@
"require": {
"php": "^8.2",
"ext-json": "*",
"codeception/codeception": "^5.3",
"codeception/lib-innerbrowser": "^3.1 | ^4.0"
},
"require-dev": {
"codeception/codeception": "^5.3",
"codeception/module-asserts": "^3.0",
"codeception/module-doctrine": "^3.1",
"doctrine/orm": "^3.5",
"friendsofphp/php-cs-fixer": "^3.85",
"phpstan/phpstan": "^2.1",
"phpunit/phpunit": "^10.5",
"symfony/browser-kit": "^5.4 | ^6.4 | ^7.3",
"symfony/cache": "^5.4 | ^6.4 | ^7.3",
"symfony/config": "^5.4 | ^6.4 | ^7.3",
Expand Down Expand Up @@ -72,6 +73,20 @@
"Codeception\\": "src/Codeception/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/",
"Tests\\_app\\": "tests/_app/"
},
"files": [
"tests/_app/TestKernel.php",
"tests/_app/ExampleCommand.php",
"tests/_app/DoctrineFixturesLoadCommand.php",
"tests/_app/HelloCommand.php",
"tests/_app/TestUser.php",
"tests/_app/ValidEntity.php"
]
},
"config": {
"sort-packages": true
},
Expand Down
4 changes: 4 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Codeception Module Symfony

A Codeception module for Symfony framework.
It can be used with Codeception or as a standalone Symfony BrowserKit client.

[![Actions Status](https://github.com/Codeception/module-symfony/workflows/CI/badge.svg)](https://github.com/Codeception/module-symfony/actions)
[![Latest Stable Version](https://poser.pugx.org/codeception/module-symfony/v/stable)](https://github.com/Codeception/module-symfony/releases)
Expand All @@ -18,6 +19,9 @@ A Codeception module for Symfony framework.
composer require "codeception/module-symfony" --dev
```

To use the connector without Codeception, require the package and instantiate
`Codeception\\Lib\\Connector\\Symfony` with your kernel.

## Documentation

See [the module documentation](https://codeception.com/docs/modules/Symfony).
Expand Down
6 changes: 4 additions & 2 deletions src/Codeception/Lib/Connector/Symfony.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\HttpKernel\Profiler\Profiler;

use function codecept_debug;
use function function_exists;

/**
* @property KernelInterface $kernel
Expand Down Expand Up @@ -73,7 +73,9 @@ public function rebootKernel(): void
try {
$this->container->set($name, $service);
} catch (InvalidArgumentException $e) {
codecept_debug("[Symfony] Can't set persistent service {$name}: {$e->getMessage()}");
if (function_exists('codecept_debug')) {
codecept_debug("[Symfony] Can't set persistent service {$name}: {$e->getMessage()}");
}
}
}

Expand Down
79 changes: 28 additions & 51 deletions src/Codeception/Module/Symfony.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,14 @@ protected function getClient(): SymfonyConnector
*/
protected function getKernelClass(): string
{
/** @var class-string<Kernel> $kernelClass */
$kernelClass = $this->config['kernel_class'];
$this->requireAdditionalAutoloader();

if (class_exists($kernelClass)) {
return $kernelClass;
}

/** @var string $rootDir */
$rootDir = codecept_root_dir();
$path = $rootDir . $this->config['app_path'];
Expand All @@ -346,40 +354,21 @@ protected function getKernelClass(): string
);
}

$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 = [];
$finder = new Finder();
$finder->name('*Kernel.php')->depth('0')->in($path);

foreach ($results as $file) {
foreach ($finder as $file) {
include_once $file->getRealPath();
$filesRealPath[] = $file->getRealPath();
}

if (class_exists($kernelClass)) {
$ref = new ReflectionClass($kernelClass);
$fileName = $ref->getFileName();
if ($fileName !== false && in_array($fileName, $filesRealPath, true)) {
/** @var class-string<Kernel> $kernelClass */
return $kernelClass;
}
if (class_exists($kernelClass, false)) {
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 `kernel_class` parameter.'
"Kernel class was not found at {$path}.\n" .
'Specify directory where file with Kernel class for your application is located with `app_path` parameter.'
);
}

Expand Down Expand Up @@ -455,31 +444,19 @@ protected function debugResponse(mixed $url): void
return;
}

if ($profile->hasCollector(DataCollectorName::SECURITY->value)) {
$securityCollector = $profile->getCollector(DataCollectorName::SECURITY->value);
if ($securityCollector instanceof SecurityDataCollector) {
$this->debugSecurityData($securityCollector);
}
}

if ($profile->hasCollector(DataCollectorName::MAILER->value)) {
$mailerCollector = $profile->getCollector(DataCollectorName::MAILER->value);
if ($mailerCollector instanceof MessageDataCollector) {
$this->debugMailerData($mailerCollector);
}
}

if ($profile->hasCollector(DataCollectorName::NOTIFIER->value)) {
$notifierCollector = $profile->getCollector(DataCollectorName::NOTIFIER->value);
if ($notifierCollector instanceof NotificationDataCollector) {
$this->debugNotifierData($notifierCollector);
}
}

if ($profile->hasCollector(DataCollectorName::TIME->value)) {
$timeCollector = $profile->getCollector(DataCollectorName::TIME->value);
if ($timeCollector instanceof TimeDataCollector) {
$this->debugTimeData($timeCollector);
$collectors = [
DataCollectorName::SECURITY->value => [$this->debugSecurityData(...), SecurityDataCollector::class],
DataCollectorName::MAILER->value => [$this->debugMailerData(...), MessageDataCollector::class],
DataCollectorName::NOTIFIER->value => [$this->debugNotifierData(...), NotificationDataCollector::class],
DataCollectorName::TIME->value => [$this->debugTimeData(...), TimeDataCollector::class],
];

foreach ($collectors as $name => [$callback, $expectedClass]) {
if ($profile->hasCollector($name)) {
$collector = $profile->getCollector($name);
if ($collector instanceof $expectedClass) {
$callback($collector);
}
}
}
}
Expand Down
22 changes: 14 additions & 8 deletions src/Codeception/Module/Symfony/BrowserAssertionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,8 @@ public function rebootClientKernel(): void
public function seePageIsAvailable(?string $url = null): void
{
if ($url !== null) {
$this->amOnPage($url);
$this->seeInCurrentUrl($url);
$this->getClient()->request('GET', $url);
$this->assertStringContainsString($url, $this->getClient()->getRequest()->getRequestUri());
}

$this->assertResponseIsSuccessful();
Expand All @@ -328,12 +328,12 @@ public function seePageRedirectsTo(string $page, string $redirectsTo): void
{
$client = $this->getClient();
$client->followRedirects(false);
$this->amOnPage($page);
$client->request('GET', $page);

$this->assertThatForResponse(new ResponseIsRedirected(), 'The response is not a redirection.');

$client->followRedirect();
$this->seeInCurrentUrl($redirectsTo);
$this->assertStringContainsString($redirectsTo, $client->getRequest()->getRequestUri());
}

/**
Expand All @@ -359,13 +359,19 @@ public function submitSymfonyForm(string $name, array $fields): void

$params = [];
foreach ($fields as $key => $value) {
$fixedKey = sprintf('%s%s', $name, $key);
$params[$fixedKey] = $value;
$params[$name . $key] = $value;
}

$button = sprintf('%s_submit', $name);
if (method_exists($this, 'submitForm')) { // @phpstan-ignore-line
$button = sprintf('%s_submit', $name);
$this->submitForm($selector, $params, $button);
return;
}

$this->submitForm($selector, $params, $button);
$node = $this->getClient()->getCrawler()->filter($selector);
$this->assertNotEmpty($node, sprintf('Form "%s" not found.', $selector));
$form = $node->form();
$this->getClient()->submit($form, $params);
}

protected function assertThatForClient(Constraint $constraint, string $message = ''): void
Expand Down
13 changes: 6 additions & 7 deletions src/Codeception/Module/Symfony/MailerAssertionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,12 @@ public function getMailerEvent(int $index = 0, ?string $transport = null): ?Mess

protected function getMessageMailerEvents(): MessageEvents
{
$mailer = $this->getService('mailer.message_logger_listener');
if ($mailer instanceof MessageLoggerListener) {
return $mailer->getEvents();
}
$mailer = $this->getService('mailer.logger_message_listener');
if ($mailer instanceof MessageLoggerListener) {
return $mailer->getEvents();
$services = ['mailer.message_logger_listener', 'mailer.logger_message_listener'];
foreach ($services as $serviceId) {
$mailer = $this->getService($serviceId);
if ($mailer instanceof MessageLoggerListener) {
return $mailer->getEvents();
}
}
Assert::fail("Emails can't be tested without Symfony Mailer service.");
}
Expand Down
13 changes: 6 additions & 7 deletions src/Codeception/Module/Symfony/NotifierAssertionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,13 +251,12 @@ protected function getNotificationEvents(): NotificationEvents
Assert::fail('Notifier assertions require Symfony 6.2 or higher.');
}

$notifier = $this->getService('notifier.notification_logger_listener');
if ($notifier instanceof NotificationLoggerListener) {
return $notifier->getEvents();
}
$notifier = $this->getService('notifier.logger_notification_listener');
if ($notifier instanceof NotificationLoggerListener) {
return $notifier->getEvents();
$services = ['notifier.notification_logger_listener', 'notifier.logger_notification_listener'];
foreach ($services as $serviceId) {
$notifier = $this->getService($serviceId);
if ($notifier instanceof NotificationLoggerListener) {
return $notifier->getEvents();
}
}
Assert::fail("Notifications can't be tested without Symfony Notifier service.");
}
Expand Down
5 changes: 2 additions & 3 deletions src/Codeception/Module/Symfony/RouterAssertionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ private function getCurrentRouteMatch(string $routeName): array
{
$this->assertRouteExists($routeName);

$url = $this->grabFromCurrentUrl();
Assert::assertIsString($url, 'Unable to obtain current URL.');
$url = $this->getClient()->getRequest()->getRequestUri();
$path = (string) parse_url($url, PHP_URL_PATH);

/** @var array<string, mixed> $match */
Expand Down Expand Up @@ -143,7 +142,7 @@ private function assertRouteExists(string $routeName): void
/** @param array<string, mixed> $params */
private function openRoute(string $routeName, array $params = []): void
{
$this->amOnPage($this->grabRouterService()->generate($routeName, $params));
$this->getClient()->request('GET', $this->grabRouterService()->generate($routeName, $params));
}

protected function grabRouterService(): RouterInterface
Expand Down
2 changes: 1 addition & 1 deletion src/Codeception/Module/Symfony/SessionAssertionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function dontSeeInSession(string $attribute, mixed $value = null): void
*/
public function goToLogoutPath(): void
{
$this->amOnPage($this->getLogoutUrlGenerator()->getLogoutPath());
$this->getClient()->request('GET', $this->getLogoutUrlGenerator()->getLogoutPath());
}

/**
Expand Down
Loading
Loading