From 39293eaad322a2a25bafec42a2dc6384ad37aed9 Mon Sep 17 00:00:00 2001 From: Rostyslav Date: Wed, 19 Jun 2024 02:20:14 +0300 Subject: [PATCH 1/6] cache_router_doc_enhancement: fixed (#195) --- src/Codeception/Module/Symfony.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Codeception/Module/Symfony.php b/src/Codeception/Module/Symfony.php index 6323e523..45b798dc 100644 --- a/src/Codeception/Module/Symfony.php +++ b/src/Codeception/Module/Symfony.php @@ -83,7 +83,7 @@ * * `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) + * * `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. From df4c02cb255b079c38561d0fcb898956dea69853 Mon Sep 17 00:00:00 2001 From: Aaron Gustavo Nieves <64917965+TavoNiievez@users.noreply.github.com> Date: Thu, 27 Jun 2024 23:14:16 -0500 Subject: [PATCH 2/6] Simplify module logic (#196) --- src/Codeception/Lib/Connector/Symfony.php | 35 +--- src/Codeception/Module/Symfony.php | 197 ++++++++-------------- 2 files changed, 84 insertions(+), 148 deletions(-) diff --git a/src/Codeception/Lib/Connector/Symfony.php b/src/Codeception/Lib/Connector/Symfony.php index 684add44..44d7595a 100644 --- a/src/Codeception/Lib/Connector/Symfony.php +++ b/src/Codeception/Lib/Connector/Symfony.php @@ -21,15 +21,8 @@ class Symfony extends HttpKernelBrowser { private bool $hasPerformedRequest = false; - private ?ContainerInterface $container; - /** - * Constructor. - * - * @param Kernel $kernel A booted HttpKernel instance - * @param array $persistentServices An injected services - */ public function __construct( Kernel $kernel, public array $persistentServices = [], @@ -74,14 +67,12 @@ public function rebootKernel(): void $this->persistDoctrineConnections(); $this->kernel->reboot(null); - $this->container = $this->getContainer(); foreach ($this->persistentServices as $serviceName => $service) { try { $this->container->set($serviceName, $service); } catch (InvalidArgumentException $e) { - //Private services can't be set in Symfony 4 codecept_debug("[Symfony] Can't set persistent service {$serviceName}: " . $e->getMessage()); } } @@ -95,31 +86,23 @@ private function getContainer(): ?ContainerInterface { /** @var ContainerInterface $container */ $container = $this->kernel->getContainer(); - if ($container->has('test.service_container')) { - $container = $container->get('test.service_container'); - } - - return $container; + return $container->has('test.service_container') + ? $container->get('test.service_container') + : $container; } private function getProfiler(): ?Profiler { - if ($this->container->has('profiler')) { - /** @var Profiler $profiler */ - $profiler = $this->container->get('profiler'); - return $profiler; - } - - return null; + return $this->container->has('profiler') + ? $this->container->get('profiler') + : null; } private function getService(string $serviceName): ?object { - if ($this->container->has($serviceName)) { - return $this->container->get($serviceName); - } - - return null; + return $this->container->has($serviceName) + ? $this->container->get($serviceName) + : null; } private function persistDoctrineConnections(): void diff --git a/src/Codeception/Module/Symfony.php b/src/Codeception/Module/Symfony.php index 45b798dc..b677c039 100644 --- a/src/Codeception/Module/Symfony.php +++ b/src/Codeception/Module/Symfony.php @@ -42,11 +42,9 @@ use Symfony\Component\HttpKernel\Profiler\Profile; use Symfony\Component\HttpKernel\Profiler\Profiler; use Symfony\Component\Mailer\DataCollector\MessageDataCollector; -use Symfony\Component\Routing\Route; use Symfony\Component\VarDumper\Cloner\Data; use function array_keys; use function array_map; -use function array_search; use function array_unique; use function class_exists; use function codecept_root_dir; @@ -55,7 +53,6 @@ use function implode; use function ini_get; use function ini_set; -use function is_null; use function iterator_to_array; use function number_format; use function sprintf; @@ -174,46 +171,34 @@ class Symfony extends Framework implements DoctrineProvider, PartedModule 'guard' => false ]; - /** - * @return string[] - */ - public function _parts(): array - { - return ['services']; - } - protected ?string $kernelClass = null; - /** * Services that should be persistent permanently for all tests - * - * @var array */ - protected $permanentServices = []; - + protected array $permanentServices = []; /** * Services that should be persistent during test execution between kernel reboots - * - * @var array */ - protected $persistentServices = []; + protected array $persistentServices = []; + + /** + * @return string[] + */ + public function _parts(): array + { + return ['services']; + } public function _initialize(): void { $this->kernelClass = $this->getKernelClass(); - $maxNestingLevel = 200; // Symfony may have very long nesting level - $xdebugMaxLevelKey = 'xdebug.max_nesting_level'; - if (ini_get($xdebugMaxLevelKey) < $maxNestingLevel) { - ini_set($xdebugMaxLevelKey, (string)$maxNestingLevel); - } - + $this->setXdebugMaxNestingLevel(200); $this->kernel = new $this->kernelClass($this->config['environment'], $this->config['debug']); - if($this->config['bootstrap']) { + if ($this->config['bootstrap']) { $this->bootstrapEnvironment(); } $this->kernel->boot(); - - if ($this->config['cache_router'] === true) { + if ($this->config['cache_router']) { $this->persistPermanentService('router'); } } @@ -223,7 +208,7 @@ public function _initialize(): void */ public function _before(TestInterface $test): void { - $this->persistentServices = [...$this->persistentServices, ...$this->permanentServices]; + $this->persistentServices = array_merge($this->persistentServices, $this->permanentServices); $this->client = new SymfonyConnector($this->kernel, $this->persistentServices, $this->config['rebootable_client']); } @@ -235,7 +220,6 @@ public function _after(TestInterface $test): void foreach (array_keys($this->permanentServices) as $serviceName) { $this->permanentServices[$serviceName] = $this->grabService($serviceName); } - parent::_after($test); } @@ -258,40 +242,24 @@ public function _getEntityManager(): EntityManagerInterface $emService = $this->config['em_service']; if (!isset($this->permanentServices[$emService])) { - // Try to persist configured entity manager $this->persistPermanentService($emService); $container = $this->_getContainer(); - if ($container->has('doctrine')) { - $this->persistPermanentService('doctrine'); - } - - if ($container->has('doctrine.orm.default_entity_manager')) { - $this->persistPermanentService('doctrine.orm.default_entity_manager'); - } - - if ($container->has('doctrine.dbal.default_connection')) { - $this->persistPermanentService('doctrine.dbal.default_connection'); + $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]; } - /** - * Return container. - */ public function _getContainer(): ContainerInterface { $container = $this->kernel->getContainer(); - if (!$container instanceof ContainerInterface) { - $this->fail('Could not get Symfony container'); - } - if ($container->has('test.service_container')) { - $container = $container->get('test.service_container'); - } - - return $container; + return $container->has('test.service_container') ? $container->get('test.service_container') : $container; } protected function getClient(): SymfonyConnector @@ -317,9 +285,10 @@ protected function getKernelClass(): string ); } + $this->requireAdditionalAutoloader(); + $finder = new Finder(); - $finder->name('*Kernel.php')->depth('0')->in($path); - $results = iterator_to_array($finder); + $results = iterator_to_array($finder->name('*Kernel.php')->depth('0')->in($path)); if ($results === []) { throw new ModuleRequireException( self::class, @@ -328,22 +297,17 @@ protected function getKernelClass(): string ); } - $this->requireAdditionalAutoloader(); - + $kernelClass = $this->config['kernel_class']; $filesRealPath = array_map(static function ($file) { require_once $file; return $file->getRealPath(); }, $results); - $kernelClass = $this->config['kernel_class']; - if (class_exists($kernelClass)) { $reflectionClass = new ReflectionClass($kernelClass); - if ($file = array_search($reflectionClass->getFileName(), $filesRealPath, true)) { + if (in_array($reflectionClass->getFileName(), $filesRealPath, true)) { return $kernelClass; } - - throw new ModuleRequireException(self::class, "Kernel class was not found in {$file}."); } throw new ModuleRequireException( @@ -356,13 +320,9 @@ protected function getKernelClass(): string protected function getProfile(): ?Profile { /** @var Profiler $profiler */ - if (!$profiler = $this->getService('profiler')) { - return null; - } - + $profiler = $this->getService('profiler'); try { - $response = $this->getClient()->getResponse(); - return $profiler->loadProfileFromResponse($response); + return $profiler?->loadProfileFromResponse($this->getClient()->getResponse()); } catch (BadMethodCallException) { $this->fail('You must perform a request before using this method.'); } catch (Exception $e) { @@ -377,20 +337,12 @@ protected function getProfile(): ?Profile */ protected function grabCollector(string $collector, string $function, string $message = null): DataCollectorInterface { - if (($profile = $this->getProfile()) === null) { - $this->fail( - sprintf("The Profile is needed to use the '%s' function.", $function) - ); + $profile = $this->getProfile(); + if ($profile === null) { + $this->fail(sprintf("The Profile is needed to use the '%s' function.", $function)); } - if (!$profile->hasCollector($collector)) { - if ($message) { - $this->fail($message); - } - - $this->fail( - sprintf("The '%s' collector is needed to use the '%s' function.", $collector, $function) - ); + $this->fail($message ?: "The '{$collector}' collector is needed to use the '{$function}' function."); } return $profile->getCollector($collector); @@ -399,49 +351,23 @@ protected function grabCollector(string $collector, string $function, string $me /** * Set the data that will be displayed when running a test with the `--debug` flag * - * @param $url + * @param mixed $url */ protected function debugResponse($url): void { parent::debugResponse($url); - - if (($profile = $this->getProfile()) === null) { - return; - } - - if ($profile->hasCollector('security')) { - /** @var SecurityDataCollector $security */ - $security = $profile->getCollector('security'); - if ($security->isAuthenticated()) { - $roles = $security->getRoles(); - - if ($roles instanceof Data) { - $roles = $roles->getValue(); + 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)); } - - $this->debugSection( - 'User', - $security->getUser() - . ' [' . implode(',', $roles) . ']' - ); - } else { - $this->debugSection('User', 'Anonymous'); } } - - if ($profile->hasCollector('mailer')) { - /** @var MessageDataCollector $mailerCollector */ - $mailerCollector = $profile->getCollector('mailer'); - $emails = count($mailerCollector->getEvents()->getMessages()); - $this->debugSection('Emails', $emails . ' sent'); - } - - if ($profile->hasCollector('time')) { - /** @var TimeDataCollector $timeCollector */ - $timeCollector = $profile->getCollector('time'); - $duration = number_format($timeCollector->getDuration(), 2) . ' ms'; - $this->debugSection('Time', $duration); - } } /** @@ -450,15 +376,14 @@ protected function debugResponse($url): void protected function getInternalDomains(): array { $internalDomains = []; - $router = $this->grabRouterService(); $routes = $router->getRouteCollection(); - /* @var Route $route */ + foreach ($routes as $route) { - if (!is_null($route->getHost())) { - $compiled = $route->compile(); - if (!is_null($compiled->getHostRegex())) { - $internalDomains[] = $compiled->getHostRegex(); + if ($route->getHost() !== null) { + $compiledRoute = $route->compile(); + if ($compiledRoute->getHostRegex() !== null) { + $internalDomains[] = $compiledRoute->getHostRegex(); } } } @@ -466,10 +391,16 @@ protected function getInternalDomains(): array 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 { @@ -486,6 +417,28 @@ private function bootstrapEnvironment(): void } } + 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. From 9b77251bc2c2a3d4d84eee84da1d971127c1ef4c Mon Sep 17 00:00:00 2001 From: Dieter Beck Date: Fri, 26 Jul 2024 17:22:48 +0200 Subject: [PATCH 3/6] Declare nullable parameter types explicitly for PHP 8.4 compatibility (#197) --- src/Codeception/Module/Symfony.php | 2 +- .../Module/Symfony/BrowserAssertionsTrait.php | 2 +- .../Module/Symfony/EventsAssertionsTrait.php | 4 ++-- .../Module/Symfony/FormAssertionsTrait.php | 2 +- .../Module/Symfony/MimeAssertionsTrait.php | 20 +++++++++---------- .../Symfony/SecurityAssertionsTrait.php | 2 +- .../Module/Symfony/SessionAssertionsTrait.php | 4 ++-- 7 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/Codeception/Module/Symfony.php b/src/Codeception/Module/Symfony.php index b677c039..c508d5e3 100644 --- a/src/Codeception/Module/Symfony.php +++ b/src/Codeception/Module/Symfony.php @@ -335,7 +335,7 @@ protected function getProfile(): ?Profile /** * Grabs a Symfony Data Collector */ - protected function grabCollector(string $collector, string $function, string $message = null): DataCollectorInterface + protected function grabCollector(string $collector, string $function, ?string $message = null): DataCollectorInterface { $profile = $this->getProfile(); if ($profile === null) { diff --git a/src/Codeception/Module/Symfony/BrowserAssertionsTrait.php b/src/Codeception/Module/Symfony/BrowserAssertionsTrait.php index 001e7ca2..9eb56364 100644 --- a/src/Codeception/Module/Symfony/BrowserAssertionsTrait.php +++ b/src/Codeception/Module/Symfony/BrowserAssertionsTrait.php @@ -43,7 +43,7 @@ public function rebootClientKernel(): void * * @param string|null $url */ - public function seePageIsAvailable(string $url = null): void + public function seePageIsAvailable(?string $url = null): void { if ($url !== null) { $this->amOnPage($url); diff --git a/src/Codeception/Module/Symfony/EventsAssertionsTrait.php b/src/Codeception/Module/Symfony/EventsAssertionsTrait.php index 8ee296b9..f761499b 100644 --- a/src/Codeception/Module/Symfony/EventsAssertionsTrait.php +++ b/src/Codeception/Module/Symfony/EventsAssertionsTrait.php @@ -23,7 +23,7 @@ trait EventsAssertionsTrait * * @param string|string[]|null $expected */ - public function dontSeeEvent(array|string $expected = null): void + public function dontSeeEvent(array|string|null $expected = null): void { $actualEvents = [...array_column($this->getCalledListeners(), 'event')]; $actual = [$this->getOrphanedEvents(), $actualEvents]; @@ -87,7 +87,7 @@ public function dontSeeEventTriggered(array|object|string $expected): void * * @param string|string[] $expected */ - public function dontSeeOrphanEvent(array|string $expected = null): void + public function dontSeeOrphanEvent(array|string|null $expected = null): void { $actual = [$this->getOrphanedEvents()]; $this->assertEventTriggered(false, $expected, $actual); diff --git a/src/Codeception/Module/Symfony/FormAssertionsTrait.php b/src/Codeception/Module/Symfony/FormAssertionsTrait.php index c6fba53d..930969c1 100644 --- a/src/Codeception/Module/Symfony/FormAssertionsTrait.php +++ b/src/Codeception/Module/Symfony/FormAssertionsTrait.php @@ -45,7 +45,7 @@ public function dontSeeFormErrors(): void * * @param string|null $message */ - public function seeFormErrorMessage(string $field, string $message = null): void + public function seeFormErrorMessage(string $field, ?string $message = null): void { $formCollector = $this->grabFormCollector(__FUNCTION__); diff --git a/src/Codeception/Module/Symfony/MimeAssertionsTrait.php b/src/Codeception/Module/Symfony/MimeAssertionsTrait.php index 12e73cd8..d20ea306 100644 --- a/src/Codeception/Module/Symfony/MimeAssertionsTrait.php +++ b/src/Codeception/Module/Symfony/MimeAssertionsTrait.php @@ -20,7 +20,7 @@ trait MimeAssertionsTrait * $I->assertEmailAddressContains('To', 'jane_doe@example.com'); * ``` */ - public function assertEmailAddressContains(string $headerName, string $expectedValue, Email $email = null): void + public function assertEmailAddressContains(string $headerName, string $expectedValue, ?Email $email = null): void { $email = $this->verifyEmailObject($email, __FUNCTION__); $this->assertThat($email, new MimeConstraint\EmailAddressContains($headerName, $expectedValue)); @@ -35,7 +35,7 @@ public function assertEmailAddressContains(string $headerName, string $expectedV * $I->assertEmailAttachmentCount(1); * ``` */ - public function assertEmailAttachmentCount(int $count, Email $email = null): void + public function assertEmailAttachmentCount(int $count, ?Email $email = null): void { $email = $this->verifyEmailObject($email, __FUNCTION__); $this->assertThat($email, new MimeConstraint\EmailAttachmentCount($count)); @@ -50,7 +50,7 @@ public function assertEmailAttachmentCount(int $count, Email $email = null): voi * $I->assertEmailHasHeader('Bcc'); * ``` */ - public function assertEmailHasHeader(string $headerName, Email $email = null): void + public function assertEmailHasHeader(string $headerName, ?Email $email = null): void { $email = $this->verifyEmailObject($email, __FUNCTION__); $this->assertThat($email, new MimeConstraint\EmailHasHeader($headerName)); @@ -66,7 +66,7 @@ public function assertEmailHasHeader(string $headerName, Email $email = null): v * $I->assertEmailHeaderNotSame('To', 'john_doe@gmail.com'); * ``` */ - public function assertEmailHeaderNotSame(string $headerName, string $expectedValue, Email $email = null): void + public function assertEmailHeaderNotSame(string $headerName, string $expectedValue, ?Email $email = null): void { $email = $this->verifyEmailObject($email, __FUNCTION__); $this->assertThat($email, new LogicalNot(new MimeConstraint\EmailHeaderSame($headerName, $expectedValue))); @@ -82,7 +82,7 @@ public function assertEmailHeaderNotSame(string $headerName, string $expectedVal * $I->assertEmailHeaderSame('To', 'jane_doe@gmail.com'); * ``` */ - public function assertEmailHeaderSame(string $headerName, string $expectedValue, Email $email = null): void + public function assertEmailHeaderSame(string $headerName, string $expectedValue, ?Email $email = null): void { $email = $this->verifyEmailObject($email, __FUNCTION__); $this->assertThat($email, new MimeConstraint\EmailHeaderSame($headerName, $expectedValue)); @@ -97,7 +97,7 @@ public function assertEmailHeaderSame(string $headerName, string $expectedValue, * $I->assertEmailHtmlBodyContains('Successful registration'); * ``` */ - public function assertEmailHtmlBodyContains(string $text, Email $email = null): void + public function assertEmailHtmlBodyContains(string $text, ?Email $email = null): void { $email = $this->verifyEmailObject($email, __FUNCTION__); $this->assertThat($email, new MimeConstraint\EmailHtmlBodyContains($text)); @@ -112,7 +112,7 @@ public function assertEmailHtmlBodyContains(string $text, Email $email = null): * $I->assertEmailHtmlBodyNotContains('userpassword'); * ``` */ - public function assertEmailHtmlBodyNotContains(string $text, Email $email = null): void + public function assertEmailHtmlBodyNotContains(string $text, ?Email $email = null): void { $email = $this->verifyEmailObject($email, __FUNCTION__); $this->assertThat($email, new LogicalNot(new MimeConstraint\EmailHtmlBodyContains($text))); @@ -127,7 +127,7 @@ public function assertEmailHtmlBodyNotContains(string $text, Email $email = null * $I->assertEmailNotHasHeader('Bcc'); * ``` */ - public function assertEmailNotHasHeader(string $headerName, Email $email = null): void + public function assertEmailNotHasHeader(string $headerName, ?Email $email = null): void { $email = $this->verifyEmailObject($email, __FUNCTION__); $this->assertThat($email, new LogicalNot(new MimeConstraint\EmailHasHeader($headerName))); @@ -142,7 +142,7 @@ public function assertEmailNotHasHeader(string $headerName, Email $email = null) * $I->assertEmailTextBodyContains('Example text body'); * ``` */ - public function assertEmailTextBodyContains(string $text, Email $email = null): void + public function assertEmailTextBodyContains(string $text, ?Email $email = null): void { $email = $this->verifyEmailObject($email, __FUNCTION__); $this->assertThat($email, new MimeConstraint\EmailTextBodyContains($text)); @@ -157,7 +157,7 @@ public function assertEmailTextBodyContains(string $text, Email $email = null): * $I->assertEmailTextBodyNotContains('My secret text body'); * ``` */ - public function assertEmailTextBodyNotContains(string $text, Email $email = null): void + public function assertEmailTextBodyNotContains(string $text, ?Email $email = null): void { $email = $this->verifyEmailObject($email, __FUNCTION__); $this->assertThat($email, new LogicalNot(new MimeConstraint\EmailTextBodyContains($text))); diff --git a/src/Codeception/Module/Symfony/SecurityAssertionsTrait.php b/src/Codeception/Module/Symfony/SecurityAssertionsTrait.php index afd160bf..81559730 100644 --- a/src/Codeception/Module/Symfony/SecurityAssertionsTrait.php +++ b/src/Codeception/Module/Symfony/SecurityAssertionsTrait.php @@ -163,7 +163,7 @@ public function seeUserHasRoles(array $roles): void * * @param UserInterface|null $user */ - public function seeUserPasswordDoesNotNeedRehash(UserInterface $user = null): void + public function seeUserPasswordDoesNotNeedRehash(?UserInterface $user = null): void { if ($user === null) { $security = $this->grabSecurityService(); diff --git a/src/Codeception/Module/Symfony/SessionAssertionsTrait.php b/src/Codeception/Module/Symfony/SessionAssertionsTrait.php index 47e40fc3..aa7ac9e9 100644 --- a/src/Codeception/Module/Symfony/SessionAssertionsTrait.php +++ b/src/Codeception/Module/Symfony/SessionAssertionsTrait.php @@ -35,13 +35,13 @@ trait SessionAssertionsTrait * $I->amLoggedInAs($user); * ``` */ - public function amLoggedInAs(UserInterface $user, string $firewallName = 'main', string $firewallContext = null): void + public function amLoggedInAs(UserInterface $user, string $firewallName = 'main', ?string $firewallContext = null): void { $token = $this->createAuthenticationToken($user, $firewallName); $this->loginWithToken($token, $firewallName, $firewallContext); } - public function amLoggedInWithToken(TokenInterface $token, string $firewallName = 'main', string $firewallContext = null): void + public function amLoggedInWithToken(TokenInterface $token, string $firewallName = 'main', ?string $firewallContext = null): void { $this->loginWithToken($token, $firewallName, $firewallContext); } From 72cf4d1ba74627b870d6b38ed4e0807cc85819d3 Mon Sep 17 00:00:00 2001 From: Aaron Gustavo Nieves <64917965+TavoNiievez@users.noreply.github.com> Date: Sat, 17 Aug 2024 21:25:18 -0500 Subject: [PATCH 4/6] Inherit symfony pre-built assertions (#198) --- composer.json | 3 + src/Codeception/Module/Symfony.php | 6 + .../Module/Symfony/BrowserAssertionsTrait.php | 195 +++++++++++++++++- .../Symfony/DomCrawlerAssertionsTrait.php | 176 ++++++++++++++++ .../Module/Symfony/FormAssertionsTrait.php | 25 ++- .../Symfony/HttpClientAssertionsTrait.php | 117 +++++++++++ .../Module/Symfony/MailerAssertionsTrait.php | 68 ++++-- .../Module/Symfony/MimeAssertionsTrait.php | 17 ++ .../Symfony/NotificationAssertionsTrait.php | 91 ++++++++ 9 files changed, 679 insertions(+), 19 deletions(-) create mode 100644 src/Codeception/Module/Symfony/DomCrawlerAssertionsTrait.php create mode 100644 src/Codeception/Module/Symfony/HttpClientAssertionsTrait.php create mode 100644 src/Codeception/Module/Symfony/NotificationAssertionsTrait.php diff --git a/composer.json b/composer.json index 684c23c1..0bdc3bd6 100644 --- a/composer.json +++ b/composer.json @@ -5,6 +5,7 @@ "type": "library", "keywords": [ "codeception", + "functional testing", "symfony" ], "authors": [ @@ -37,10 +38,12 @@ "symfony/filesystem": "^5.4 | ^6.4 | ^7.0", "symfony/form": "^5.4 | ^6.4 | ^7.0", "symfony/framework-bundle": "^5.4 | ^6.4 | ^7.0", + "symfony/http-client": "^5.4 | ^6.4 | ^7.0", "symfony/http-foundation": "^5.4 | ^6.4 | ^7.0", "symfony/http-kernel": "^5.4 | ^6.4 | ^7.0", "symfony/mailer": "^5.4 | ^6.4 | ^7.0", "symfony/mime": "^5.4 | ^6.4 | ^7.0", + "symfony/notifier": "5.4 | ^6.4 | ^7.0", "symfony/options-resolver": "^5.4 | ^6.4 | ^7.0", "symfony/property-access": "^5.4 | ^6.4 | ^7.0", "symfony/property-info": "^5.4 | ^6.4 | ^7.0", diff --git a/src/Codeception/Module/Symfony.php b/src/Codeception/Module/Symfony.php index c508d5e3..34694898 100644 --- a/src/Codeception/Module/Symfony.php +++ b/src/Codeception/Module/Symfony.php @@ -13,10 +13,13 @@ use Codeception\Module\Symfony\BrowserAssertionsTrait; use Codeception\Module\Symfony\ConsoleAssertionsTrait; 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\MailerAssertionsTrait; use Codeception\Module\Symfony\MimeAssertionsTrait; +use Codeception\Module\Symfony\NotificationAssertionsTrait; use Codeception\Module\Symfony\ParameterAssertionsTrait; use Codeception\Module\Symfony\RouterAssertionsTrait; use Codeception\Module\Symfony\SecurityAssertionsTrait; @@ -135,10 +138,13 @@ class Symfony extends Framework implements DoctrineProvider, PartedModule use BrowserAssertionsTrait; use ConsoleAssertionsTrait; use DoctrineAssertionsTrait; + use DomCrawlerAssertionsTrait; use EventsAssertionsTrait; use FormAssertionsTrait; + use HttpClientAssertionsTrait; use MailerAssertionsTrait; use MimeAssertionsTrait; + use NotificationAssertionsTrait; use ParameterAssertionsTrait; use RouterAssertionsTrait; use SecurityAssertionsTrait; diff --git a/src/Codeception/Module/Symfony/BrowserAssertionsTrait.php b/src/Codeception/Module/Symfony/BrowserAssertionsTrait.php index 9eb56364..cc8bfb54 100644 --- a/src/Codeception/Module/Symfony/BrowserAssertionsTrait.php +++ b/src/Codeception/Module/Symfony/BrowserAssertionsTrait.php @@ -4,11 +4,194 @@ namespace Codeception\Module\Symfony; +use PHPUnit\Framework\Constraint\Constraint; +use PHPUnit\Framework\Constraint\LogicalAnd; +use PHPUnit\Framework\Constraint\LogicalNot; +use Symfony\Component\BrowserKit\Test\Constraint\BrowserCookieValueSame; +use Symfony\Component\BrowserKit\Test\Constraint\BrowserHasCookie; +use Symfony\Component\HttpFoundation\Test\Constraint\RequestAttributeValueSame; +use Symfony\Component\HttpFoundation\Test\Constraint\ResponseCookieValueSame; +use Symfony\Component\HttpFoundation\Test\Constraint\ResponseFormatSame; +use Symfony\Component\HttpFoundation\Test\Constraint\ResponseHasCookie; +use Symfony\Component\HttpFoundation\Test\Constraint\ResponseHasHeader; +use Symfony\Component\HttpFoundation\Test\Constraint\ResponseHeaderLocationSame; +use Symfony\Component\HttpFoundation\Test\Constraint\ResponseHeaderSame; +use Symfony\Component\HttpFoundation\Test\Constraint\ResponseIsRedirected; use Symfony\Component\HttpFoundation\Test\Constraint\ResponseIsSuccessful; +use Symfony\Component\HttpFoundation\Test\Constraint\ResponseIsUnprocessable; +use Symfony\Component\HttpFoundation\Test\Constraint\ResponseStatusCodeSame; use function sprintf; trait BrowserAssertionsTrait { + /** + * Asserts the given cookie in the test Client is set to the expected value. + */ + public function assertBrowserCookieValueSame(string $name, string $expectedValue, bool $raw = false, string $path = '/', ?string $domain = null, string $message = ''): void + { + $this->assertThatForClient(LogicalAnd::fromConstraints( + new BrowserHasCookie($name, $path, $domain), + new BrowserCookieValueSame($name, $expectedValue, $raw, $path, $domain) + ), $message); + } + + /** + * Asserts that the test Client does have the given cookie set (meaning, the cookie was set by any response in the test). + */ + public function assertBrowserHasCookie(string $name, string $path = '/', ?string $domain = null, string $message = ''): void + { + $this->assertThatForClient(new BrowserHasCookie($name, $path, $domain), $message); + } + + /** + * Asserts that the test Client does not have the given cookie set (meaning, the cookie was set by any response in the test). + */ + public function assertBrowserNotHasCookie(string $name, string $path = '/', ?string $domain = null, string $message = ''): void + { + $this->assertThatForClient(new LogicalNot(new BrowserHasCookie($name, $path, $domain)), $message); + } + + /** + * Asserts the given request attribute is set to the expected value. + */ + public function assertRequestAttributeValueSame(string $name, string $expectedValue, string $message = ''): void + { + $this->assertThat($this->getClient()->getRequest(), new RequestAttributeValueSame($name, $expectedValue), $message); + } + + /** + * Asserts the given cookie is present and set to the expected value. + */ + public function assertResponseCookieValueSame(string $name, string $expectedValue, string $path = '/', ?string $domain = null, string $message = ''): void + { + $this->assertThatForResponse(LogicalAnd::fromConstraints( + new ResponseHasCookie($name, $path, $domain), + new ResponseCookieValueSame($name, $expectedValue, $path, $domain) + ), $message); + } + + /** + * Asserts the response format returned by the `Response::getFormat()` method is the same as the expected value. + */ + public function assertResponseFormatSame(?string $expectedFormat, string $message = ''): void + { + $this->assertThatForResponse(new ResponseFormatSame($this->getClient()->getRequest(), $expectedFormat), $message); + } + + /** + * Asserts the given cookie is present in the response (optionally checking for a specific cookie path or domain). + */ + public function assertResponseHasCookie(string $name, string $path = '/', ?string $domain = null, string $message = ''): void + { + $this->assertThatForResponse(new ResponseHasCookie($name, $path, $domain), $message); + } + + /** + * Asserts the given header is available on the response, e.g. assertResponseHasHeader('content-type');. + */ + public function assertResponseHasHeader(string $headerName, string $message = ''): void + { + $this->assertThatForResponse(new ResponseHasHeader($headerName), $message); + } + + /** + * Asserts the given header does not contain the expected value on the response, + * e.g. assertResponseHeaderNotSame('content-type', 'application/octet-stream');. + */ + public function assertResponseHeaderNotSame(string $headerName, string $expectedValue, string $message = ''): void + { + $this->assertThatForResponse(new LogicalNot(new ResponseHeaderSame($headerName, $expectedValue)), $message); + } + + /** + * Asserts the given header does contain the expected value on the response, + * e.g. assertResponseHeaderSame('content-type', 'application/octet-stream');. + */ + public function assertResponseHeaderSame(string $headerName, string $expectedValue, string $message = ''): void + { + $this->assertThatForResponse(new ResponseHeaderSame($headerName, $expectedValue), $message); + } + + /** + * Asserts that the response was successful (HTTP status is 2xx). + */ + public function assertResponseIsSuccessful(string $message = '', bool $verbose = true): void + { + $this->assertThatForResponse(new ResponseIsSuccessful($verbose), $message); + } + + /** + * Asserts the response is unprocessable (HTTP status is 422) + */ + public function assertResponseIsUnprocessable(string $message = '', bool $verbose = true): void + { + $this->assertThatForResponse(new ResponseIsUnprocessable($verbose), $message); + } + + /** + * Asserts the given cookie is not present in the response (optionally checking for a specific cookie path or domain). + */ + public function assertResponseNotHasCookie(string $name, string $path = '/', ?string $domain = null, string $message = ''): void + { + $this->assertThatForResponse(new LogicalNot(new ResponseHasCookie($name, $path, $domain)), $message); + } + + /** + * Asserts the given header is not available on the response, e.g. assertResponseNotHasHeader('content-type');. + */ + public function assertResponseNotHasHeader(string $headerName, string $message = ''): void + { + $this->assertThatForResponse(new LogicalNot(new ResponseHasHeader($headerName)), $message); + } + + /** + * Asserts the response is a redirect response (optionally, you can check the target location and status code). + * The excepted location can be either an absolute or a relative path. + */ + public function assertResponseRedirects(?string $expectedLocation = null, ?int $expectedCode = null, string $message = '', bool $verbose = true): void + { + $constraint = new ResponseIsRedirected($verbose); + if ($expectedLocation) { + if (class_exists(ResponseHeaderLocationSame::class)) { + $locationConstraint = new ResponseHeaderLocationSame($this->getClient()->getRequest(), $expectedLocation); + } else { + $locationConstraint = new ResponseHeaderSame('Location', $expectedLocation); + } + + $constraint = LogicalAnd::fromConstraints($constraint, $locationConstraint); + } + if ($expectedCode) { + $constraint = LogicalAnd::fromConstraints($constraint, new ResponseStatusCodeSame($expectedCode)); + } + + $this->assertThatForResponse($constraint, $message); + } + + /** + * Asserts a specific HTTP status code. + */ + public function assertResponseStatusCodeSame(int $expectedCode, string $message = '', bool $verbose = true): void + { + $this->assertThatForResponse(new ResponseStatusCodeSame($expectedCode, $verbose), $message); + } + + /** + * Asserts the request matches the given route and optionally route parameters. + */ + public function assertRouteSame(string $expectedRoute, array $parameters = [], string $message = ''): void + { + $constraint = new RequestAttributeValueSame('_route', $expectedRoute); + $constraints = []; + foreach ($parameters as $key => $value) { + $constraints[] = new RequestAttributeValueSame($key, $value); + } + if ($constraints) { + $constraint = LogicalAnd::fromConstraints($constraint, ...$constraints); + } + + $this->assertThat($this->getClient()->getRequest(), $constraint, $message); + } + /** * Reboot client's kernel. * Can be used to manually reboot kernel when 'rebootable_client' => false @@ -50,7 +233,7 @@ public function seePageIsAvailable(?string $url = null): void $this->seeInCurrentUrl($url); } - $this->assertThat($this->getClient()->getResponse(), new ResponseIsSuccessful()); + $this->assertResponseIsSuccessful(); } /** @@ -104,4 +287,14 @@ public function submitSymfonyForm(string $name, array $fields): void $this->submitForm($selector, $params, $button); } + + protected function assertThatForClient(Constraint $constraint, string $message = ''): void + { + $this->assertThat($this->getClient(), $constraint, $message); + } + + protected function assertThatForResponse(Constraint $constraint, string $message = ''): void + { + $this->assertThat($this->getClient()->getResponse(), $constraint, $message); + } } diff --git a/src/Codeception/Module/Symfony/DomCrawlerAssertionsTrait.php b/src/Codeception/Module/Symfony/DomCrawlerAssertionsTrait.php new file mode 100644 index 00000000..0d5bca59 --- /dev/null +++ b/src/Codeception/Module/Symfony/DomCrawlerAssertionsTrait.php @@ -0,0 +1,176 @@ +assertThat($this->getCrawler(), LogicalAnd::fromConstraints( + new CrawlerSelectorExists($selector), + new CrawlerAnySelectorTextContains($selector, $text) + ), $message); + } + + /** + * Asserts that any element matching the given selector does not contain the expected text. + */ + public function assertAnySelectorTextNotContains(string $selector, string $text, string $message = ''): void + { + $this->assertThat($this->getCrawler(), LogicalAnd::fromConstraints( + new CrawlerSelectorExists($selector), + new LogicalNot(new CrawlerAnySelectorTextContains($selector, $text)) + ), $message); + } + + /** + * Asserts that any element matching the given selector does equal the expected text. + */ + public function assertAnySelectorTextSame(string $selector, string $text, string $message = ''): void + { + $this->assertThat($this->getCrawler(), LogicalAnd::fromConstraints( + new CrawlerSelectorExists($selector), + new CrawlerAnySelectorTextSame($selector, $text) + ), $message); + } + + /** + * Asserts that the checkbox with the given name is checked. + */ + public function assertCheckboxChecked(string $fieldName, string $message = ''): void + { + $this->assertThat( + $this->getCrawler(), + new CrawlerSelectorExists("input[name=\"$fieldName\"]:checked"), + $message + ); + } + + /** + * Asserts that the checkbox with the given name is not checked. + */ + public function assertCheckboxNotChecked(string $fieldName, string $message = ''): void + { + $this->assertThat( + $this->getCrawler(), + new LogicalNot(new CrawlerSelectorExists("input[name=\"$fieldName\"]:checked")), + $message + ); + } + + /** + * Asserts that value of the form input with the given name does not equal the expected value. + */ + public function assertInputValueNotSame(string $fieldName, string $expectedValue, string $message = ''): void + { + $this->assertThat($this->getCrawler(), LogicalAnd::fromConstraints( + new CrawlerSelectorExists("input[name=\"$fieldName\"]"), + new LogicalNot(new CrawlerSelectorAttributeValueSame("input[name=\"$fieldName\"]", 'value', $expectedValue)) + ), $message); + } + + /** + * Asserts that value of the form input with the given name does equal the expected value. + */ + public function assertInputValueSame(string $fieldName, string $expectedValue, string $message = ''): void + { + $this->assertThat($this->getCrawler(), LogicalAnd::fromConstraints( + new CrawlerSelectorExists("input[name=\"$fieldName\"]"), + new CrawlerSelectorAttributeValueSame("input[name=\"$fieldName\"]", 'value', $expectedValue) + ), $message); + } + + /** + * Asserts that the `` element contains the given title. + */ + public function assertPageTitleContains(string $expectedTitle, string $message = ''): void + { + $this->assertSelectorTextContains('title', $expectedTitle, $message); + } + + /** + * Asserts that the `<title>` element is equal to the given title. + */ + public function assertPageTitleSame(string $expectedTitle, string $message = ''): void + { + $this->assertSelectorTextSame('title', $expectedTitle, $message); + } + + /** + * Asserts that the expected number of selector elements are in the response. + */ + public function assertSelectorCount(int $expectedCount, string $selector, string $message = ''): void + { + $this->assertThat($this->getCrawler(), new CrawlerSelectorCount($expectedCount, $selector), $message); + } + + /** + * Asserts that the given selector does match at least one element in the response. + */ + public function assertSelectorExists(string $selector, string $message = ''): void + { + $this->assertThat($this->getCrawler(), new CrawlerSelectorExists($selector), $message); + } + + /** + * Asserts that the given selector does not match at least one element in the response. + */ + public function assertSelectorNotExists(string $selector, string $message = ''): void + { + $this->assertThat($this->getCrawler(), new LogicalNot(new CrawlerSelectorExists($selector)), $message); + } + + /** + * Asserts that the first element matching the given selector does contain the expected text. + */ + public function assertSelectorTextContains(string $selector, string $text, string $message = ''): void + { + $this->assertThat($this->getCrawler(), LogicalAnd::fromConstraints( + new CrawlerSelectorExists($selector), + new CrawlerSelectorTextContains($selector, $text) + ), $message); + } + + /** + * Asserts that the first element matching the given selector does not contain the expected text. + */ + public function assertSelectorTextNotContains(string $selector, string $text, string $message = ''): void + { + $this->assertThat($this->getCrawler(), LogicalAnd::fromConstraints( + new CrawlerSelectorExists($selector), + new LogicalNot(new CrawlerSelectorTextContains($selector, $text)) + ), $message); + } + + /** + * Asserts that the contents of the first element matching the given selector does equal the expected text. + */ + public function assertSelectorTextSame(string $selector, string $text, string $message = ''): void + { + $this->assertThat($this->getCrawler(), LogicalAnd::fromConstraints( + new CrawlerSelectorExists($selector), + new CrawlerSelectorTextSame($selector, $text) + ), $message); + } + + protected function getCrawler(): Crawler + { + return $this->client->getCrawler(); + } +} diff --git a/src/Codeception/Module/Symfony/FormAssertionsTrait.php b/src/Codeception/Module/Symfony/FormAssertionsTrait.php index 930969c1..0c8736f0 100644 --- a/src/Codeception/Module/Symfony/FormAssertionsTrait.php +++ b/src/Codeception/Module/Symfony/FormAssertionsTrait.php @@ -12,6 +12,29 @@ trait FormAssertionsTrait { + /** + * Asserts that value of the field of the first form matching the given selector does equal the expected value. + */ + public function assertFormValue(string $formSelector, string $fieldName, string $value, string $message = ''): void + { + $node = $this->getCrawler()->filter($formSelector); + $this->assertNotEmpty($node, sprintf('Form "%s" not found.', $formSelector)); + $values = $node->form()->getValues(); + $this->assertArrayHasKey($fieldName, $values, $message ?: sprintf('Field "%s" not found in form "%s".', $fieldName, $formSelector)); + $this->assertSame($value, $values[$fieldName]); + } + + /** + * Asserts that value of the field of the first form matching the given selector does equal the expected value. + */ + public function assertNoFormValue(string $formSelector, string $fieldName, string $message = ''): void + { + $node = $this->getCrawler()->filter($formSelector); + $this->assertNotEmpty($node, sprintf('Form "%s" not found.', $formSelector)); + $values = $node->form()->getValues(); + $this->assertArrayNotHasKey($fieldName, $values, $message ?: sprintf('Field "%s" has a value in form "%s".', $fieldName, $formSelector)); + } + /** * Verifies that there are no errors bound to the submitted form. * @@ -42,8 +65,6 @@ public function dontSeeFormErrors(): void * $I->seeFormErrorMessage('username'); * $I->seeFormErrorMessage('username', 'Username is empty'); * ``` - * - * @param string|null $message */ public function seeFormErrorMessage(string $field, ?string $message = null): void { diff --git a/src/Codeception/Module/Symfony/HttpClientAssertionsTrait.php b/src/Codeception/Module/Symfony/HttpClientAssertionsTrait.php new file mode 100644 index 00000000..9ac3a6e4 --- /dev/null +++ b/src/Codeception/Module/Symfony/HttpClientAssertionsTrait.php @@ -0,0 +1,117 @@ +<?php + +declare(strict_types=1); + +namespace Codeception\Module\Symfony; + +use Symfony\Component\HttpClient\DataCollector\HttpClientDataCollector; +use function array_key_exists; +use function is_string; + +trait HttpClientAssertionsTrait +{ + /** + * Asserts that the given URL has been called using, if specified, the given method body and headers. + * By default, it will check on the HttpClient, but you can also pass a specific HttpClient ID. (It will succeed if the request has been called multiple times.) + */ + public function assertHttpClientRequest(string $expectedUrl, string $expectedMethod = 'GET', string|array|null $expectedBody = null, array $expectedHeaders = [], string $httpClientId = 'http_client'): void + { + $httpClientCollector = $this->grabHttpClientCollector(__FUNCTION__); + $expectedRequestHasBeenFound = false; + + if (!array_key_exists($httpClientId, $httpClientCollector->getClients())) { + $this->fail(sprintf('HttpClient "%s" is not registered.', $httpClientId)); + } + + foreach ($httpClientCollector->getClients()[$httpClientId]['traces'] as $trace) { + if (($expectedUrl !== $trace['info']['url'] && $expectedUrl !== $trace['url']) + || $expectedMethod !== $trace['method'] + ) { + continue; + } + + if (null !== $expectedBody) { + $actualBody = null; + + if (null !== $trace['options']['body'] && null === $trace['options']['json']) { + $actualBody = is_string($trace['options']['body']) ? $trace['options']['body'] : $trace['options']['body']->getValue(true); + } + + if (null === $trace['options']['body'] && null !== $trace['options']['json']) { + $actualBody = $trace['options']['json']->getValue(true); + } + + if (!$actualBody) { + continue; + } + + if ($expectedBody === $actualBody) { + $expectedRequestHasBeenFound = true; + + if (!$expectedHeaders) { + break; + } + } + } + + if ($expectedHeaders) { + $actualHeaders = $trace['options']['headers'] ?? []; + + foreach ($actualHeaders as $headerKey => $actualHeader) { + if (array_key_exists($headerKey, $expectedHeaders) + && $expectedHeaders[$headerKey] === $actualHeader->getValue(true) + ) { + $expectedRequestHasBeenFound = true; + break 2; + } + } + } + + $expectedRequestHasBeenFound = true; + break; + } + + $this->assertTrue($expectedRequestHasBeenFound, 'The expected request has not been called: "' . $expectedMethod . '" - "' . $expectedUrl . '"'); + } + + /** + * Asserts that the given number of requests has been made on the HttpClient. + * By default, it will check on the HttpClient, but you can also pass a specific HttpClient ID. + */ + public function assertHttpClientRequestCount(int $count, string $httpClientId = 'http_client'): void + { + $httpClientCollector = $this->grabHttpClientCollector(__FUNCTION__); + + $this->assertCount($count, $httpClientCollector->getClients()[$httpClientId]['traces']); + } + + /** + * Asserts that the given URL has not been called using GET or the specified method. + * By default, it will check on the HttpClient, but a HttpClient id can be specified. + */ + public function assertNotHttpClientRequest(string $unexpectedUrl, string $expectedMethod = 'GET', string $httpClientId = 'http_client'): void + { + $httpClientCollector = $this->grabHttpClientCollector(__FUNCTION__); + $unexpectedUrlHasBeenFound = false; + + if (!array_key_exists($httpClientId, $httpClientCollector->getClients())) { + $this->fail(sprintf('HttpClient "%s" is not registered.', $httpClientId)); + } + + foreach ($httpClientCollector->getClients()[$httpClientId]['traces'] as $trace) { + if (($unexpectedUrl === $trace['info']['url'] || $unexpectedUrl === $trace['url']) + && $expectedMethod === $trace['method'] + ) { + $unexpectedUrlHasBeenFound = true; + break; + } + } + + $this->assertFalse($unexpectedUrlHasBeenFound, sprintf('Unexpected URL called: "%s" - "%s"', $expectedMethod, $unexpectedUrl)); + } + + protected function grabHttpClientCollector(string $function): HttpClientDataCollector + { + return $this->grabCollector('http_client', $function); + } +} diff --git a/src/Codeception/Module/Symfony/MailerAssertionsTrait.php b/src/Codeception/Module/Symfony/MailerAssertionsTrait.php index f8bb9772..df2fd0f1 100644 --- a/src/Codeception/Module/Symfony/MailerAssertionsTrait.php +++ b/src/Codeception/Module/Symfony/MailerAssertionsTrait.php @@ -4,6 +4,8 @@ namespace Codeception\Module\Symfony; +use PHPUnit\Framework\Constraint\LogicalNot; +use Symfony\Component\Mailer\Event\MessageEvent; use Symfony\Component\Mailer\Event\MessageEvents; use Symfony\Component\Mailer\EventListener\MessageLoggerListener; use Symfony\Component\Mailer\Test\Constraint as MailerConstraint; @@ -12,30 +14,47 @@ trait MailerAssertionsTrait { /** - * Checks that no email was sent. - * The check is based on `\Symfony\Component\Mailer\EventListener\MessageLoggerListener`, which means: - * If your app performs an HTTP redirect, you need to suppress it using [stopFollowingRedirects()](https://codeception.com/docs/modules/Symfony#stopFollowingRedirects) first; otherwise this check will *always* pass. + * Asserts that the expected number of emails was sent. */ - public function dontSeeEmailIsSent(): void + public function assertEmailCount(int $count, ?string $transport = null, string $message = ''): void { - $this->assertThat($this->getMessageMailerEvents(), new MailerConstraint\EmailCount(0)); + $this->assertThat($this->getMessageMailerEvents(), new MailerConstraint\EmailCount($count, $transport), $message); } /** - * Checks if the given number of emails was sent (default `$expectedCount`: 1). + * Asserts that the given mailer event is not queued. + * Use `getMailerEvent(int $index = 0, ?string $transport = null)` to retrieve a mailer event by index. + */ + public function assertEmailIsNotQueued(MessageEvent $event, string $message = ''): void + { + $this->assertThat($event, new LogicalNot(new MailerConstraint\EmailIsQueued()), $message); + } + + /** + * Asserts that the given mailer event is queued. + * Use `getMailerEvent(int $index = 0, ?string $transport = null)` to retrieve a mailer event by index. + */ + public function assertEmailIsQueued(MessageEvent $event, string $message = ''): void + { + $this->assertThat($event, new MailerConstraint\EmailIsQueued(), $message); + } + + /** + * Asserts that the expected number of emails was queued (e.g. using the Messenger component). + */ + public function assertQueuedEmailCount(int $count, ?string $transport = null, string $message = ''): void + { + $this->assertThat($this->getMessageMailerEvents(), new MailerConstraint\EmailCount($count, $transport, true), $message); + } + + /** + * Checks that no email was sent. * The check is based on `\Symfony\Component\Mailer\EventListener\MessageLoggerListener`, which means: - * If your app performs an HTTP redirect after sending the email, you need to suppress it using [stopFollowingRedirects()](https://codeception.com/docs/modules/Symfony#stopFollowingRedirects) first. - * - * ```php - * <?php - * $I->seeEmailIsSent(2); - * ``` - * - * @param int $expectedCount The expected number of emails sent + * If your app performs an HTTP redirect, you need to suppress it using [stopFollowingRedirects()](https://codeception.com/docs/modules/Symfony#stopFollowingRedirects) first; otherwise this check will *always* pass. */ - public function seeEmailIsSent(int $expectedCount = 1): void + public function dontSeeEmailIsSent(): void { - $this->assertThat($this->getMessageMailerEvents(), new MailerConstraint\EmailCount($expectedCount)); + $this->assertThat($this->getMessageMailerEvents(), new MailerConstraint\EmailCount(0)); } /** @@ -78,6 +97,23 @@ public function grabSentEmails(): array return $this->getMessageMailerEvents()->getMessages(); } + /** + * Checks if the given number of emails was sent (default `$expectedCount`: 1). + * The check is based on `\Symfony\Component\Mailer\EventListener\MessageLoggerListener`, which means: + * If your app performs an HTTP redirect after sending the email, you need to suppress it using [stopFollowingRedirects()](https://codeception.com/docs/modules/Symfony#stopFollowingRedirects) first. + * + * ```php + * <?php + * $I->seeEmailIsSent(2); + * ``` + * + * @param int $expectedCount The expected number of emails sent + */ + public function seeEmailIsSent(int $expectedCount = 1): void + { + $this->assertThat($this->getMessageMailerEvents(), new MailerConstraint\EmailCount($expectedCount)); + } + protected function getMessageMailerEvents(): MessageEvents { if ($messageLogger = $this->getService('mailer.message_logger_listener')) { diff --git a/src/Codeception/Module/Symfony/MimeAssertionsTrait.php b/src/Codeception/Module/Symfony/MimeAssertionsTrait.php index d20ea306..a55b13ba 100644 --- a/src/Codeception/Module/Symfony/MimeAssertionsTrait.php +++ b/src/Codeception/Module/Symfony/MimeAssertionsTrait.php @@ -6,6 +6,7 @@ use PHPUnit\Framework\Constraint\LogicalNot; use Symfony\Component\Mime\Email; +use Symfony\Component\Mime\RawMessage; use Symfony\Component\Mime\Test\Constraint as MimeConstraint; trait MimeAssertionsTrait @@ -133,6 +134,22 @@ public function assertEmailNotHasHeader(string $headerName, ?Email $email = null $this->assertThat($email, new LogicalNot(new MimeConstraint\EmailHasHeader($headerName))); } + /** + * Asserts that the subject of the given email does contain the expected subject. + */ + public function assertEmailSubjectContains(RawMessage $email, string $expectedValue, string $message = ''): void + { + $this->assertThat($email, new MimeConstraint\EmailSubjectContains($expectedValue), $message); + } + + /** + * Asserts that the subject of the given email does not contain the expected subject. + */ + public function assertEmailSubjectNotContains(RawMessage $email, string $expectedValue, string $message = ''): void + { + $this->assertThat($email, new LogicalNot(new MimeConstraint\EmailSubjectContains($expectedValue)), $message); + } + /** * Verify the text body of an email contains a `$text`. * If the Email object is not specified, the last email sent is used instead. diff --git a/src/Codeception/Module/Symfony/NotificationAssertionsTrait.php b/src/Codeception/Module/Symfony/NotificationAssertionsTrait.php new file mode 100644 index 00000000..c8b0f74c --- /dev/null +++ b/src/Codeception/Module/Symfony/NotificationAssertionsTrait.php @@ -0,0 +1,91 @@ +<?php + +declare(strict_types=1); + +namespace Codeception\Module\Symfony; + +use PHPUnit\Framework\Constraint\LogicalNot; +use Symfony\Component\Notifier\Event\MessageEvent; +use Symfony\Component\Notifier\Event\NotificationEvents; +use Symfony\Component\Notifier\Message\MessageInterface; +use Symfony\Component\Notifier\Test\Constraint\NotificationCount; +use Symfony\Component\Notifier\Test\Constraint\NotificationIsQueued; +use Symfony\Component\Notifier\Test\Constraint\NotificationSubjectContains; +use Symfony\Component\Notifier\Test\Constraint\NotificationTransportIsEqual; + +trait NotificationAssertionsTrait +{ + /** + * Asserts that the given number of notifications has been created (in total or for the given transport). + */ + public function assertNotificationCount(int $count, ?string $transportName = null, string $message = ''): void + { + $this->assertThat($this->getNotificationEvents(), new NotificationCount($count, $transportName), $message); + } + + /** + * Asserts that the given notification is not queued. + */ + public function assertNotificationIsNotQueued(MessageEvent $event, string $message = ''): void + { + $this->assertThat($event, new LogicalNot(new NotificationIsQueued()), $message); + } + + /** + * Asserts that the given notification is queued. + */ + public function assertNotificationIsQueued(MessageEvent $event, string $message = ''): void + { + $this->assertThat($event, new NotificationIsQueued(), $message); + } + + /** + * Asserts that the given text is included in the subject of the given notification. + */ + public function assertNotificationSubjectContains(MessageInterface $notification, string $text, string $message = ''): void + { + $this->assertThat($notification, new NotificationSubjectContains($text), $message); + } + + /** + * Asserts that the given text is not included in the subject of the given notification. + */ + public function assertNotificationSubjectNotContains(MessageInterface $notification, string $text, string $message = ''): void + { + $this->assertThat($notification, new LogicalNot(new NotificationSubjectContains($text)), $message); + } + + /** + * Asserts that the name of the transport for the given notification is the same as the given text. + */ + public function assertNotificationTransportIsEqual(MessageInterface $notification, ?string $transportName = null, string $message = ''): void + { + $this->assertThat($notification, new NotificationTransportIsEqual($transportName), $message); + } + + /** + * Asserts that the name of the transport for the given notification is not the same as the given text. + */ + public function assertNotificationTransportIsNotEqual(MessageInterface $notification, ?string $transportName = null, string $message = ''): void + { + $this->assertThat($notification, new LogicalNot(new NotificationTransportIsEqual($transportName)), $message); + } + + /** + * Asserts that the given number of notifications are queued (in total or for the given transport). + */ + public function assertQueuedNotificationCount(int $count, ?string $transportName = null, string $message = ''): void + { + $this->assertThat($this->getNotificationEvents(), new NotificationCount($count, $transportName, true), $message); + } + + protected function getNotificationEvents(): NotificationEvents + { + $notificationLogger = $this->getService('notifier.notification_logger_listener'); + if ($notificationLogger) { + return $notificationLogger->getEvents(); + } + + $this->fail('A client must have Notifier enabled to make notifications assertions. Did you forget to require symfony/notifier?'); + } +} From 3f1a2b81da2e38356243204431d4d61d93baf2ed Mon Sep 17 00:00:00 2001 From: Dieter Beck <beck.worma@gmail.com> Date: Mon, 11 Nov 2024 08:12:47 +0100 Subject: [PATCH 5/6] Test against PHP 8.4 (#201) --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index db3651cb..f7f897c8 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -8,7 +8,7 @@ jobs: strategy: matrix: - php: [8.1, 8.2, 8.3] + php: [8.1, 8.2, 8.3, 8.4] symfony: ["5.4.*", "6.4.*", "7.1.*"] exclude: - php: 8.1 From cad3c7a46da68f591dd363bc114ec9fde4f1dd63 Mon Sep 17 00:00:00 2001 From: Aaron Gustavo Nieves <64917965+TavoNiievez@users.noreply.github.com> Date: Wed, 13 Nov 2024 23:45:04 -0500 Subject: [PATCH 6/6] Symfony assertion refinement (#200) --- composer.json | 2 +- readme.md | 2 +- src/Codeception/Module/Symfony.php | 2 - .../Module/Symfony/BrowserAssertionsTrait.php | 118 +++++++++--------- .../Symfony/DomCrawlerAssertionsTrait.php | 114 +++++------------ .../Module/Symfony/FormAssertionsTrait.php | 4 +- .../Module/Symfony/MimeAssertionsTrait.php | 16 --- .../Symfony/NotificationAssertionsTrait.php | 91 -------------- 8 files changed, 89 insertions(+), 260 deletions(-) delete mode 100644 src/Codeception/Module/Symfony/NotificationAssertionsTrait.php diff --git a/composer.json b/composer.json index 0bdc3bd6..65238b1d 100644 --- a/composer.json +++ b/composer.json @@ -43,7 +43,7 @@ "symfony/http-kernel": "^5.4 | ^6.4 | ^7.0", "symfony/mailer": "^5.4 | ^6.4 | ^7.0", "symfony/mime": "^5.4 | ^6.4 | ^7.0", - "symfony/notifier": "5.4 | ^6.4 | ^7.0", + "symfony/notifier": "^5.4 | ^6.4 | ^7.0", "symfony/options-resolver": "^5.4 | ^6.4 | ^7.0", "symfony/property-access": "^5.4 | ^6.4 | ^7.0", "symfony/property-info": "^5.4 | ^6.4 | ^7.0", diff --git a/readme.md b/readme.md index 06d2d614..ccedd850 100644 --- a/readme.md +++ b/readme.md @@ -9,7 +9,7 @@ A Codeception module for Symfony framework. ## Requirements -* `Symfony` `5.4.x`, `6.4.x`, `7.0.x` or higher, as per the [Symfony supported versions](https://symfony.com/releases). +* `Symfony` `5.4.x`, `6.4.x`, `7.1.x` or higher, as per the [Symfony supported versions](https://symfony.com/releases). * `PHP 8.1` or higher. ## Installation diff --git a/src/Codeception/Module/Symfony.php b/src/Codeception/Module/Symfony.php index 34694898..3f7917bb 100644 --- a/src/Codeception/Module/Symfony.php +++ b/src/Codeception/Module/Symfony.php @@ -19,7 +19,6 @@ use Codeception\Module\Symfony\HttpClientAssertionsTrait; use Codeception\Module\Symfony\MailerAssertionsTrait; use Codeception\Module\Symfony\MimeAssertionsTrait; -use Codeception\Module\Symfony\NotificationAssertionsTrait; use Codeception\Module\Symfony\ParameterAssertionsTrait; use Codeception\Module\Symfony\RouterAssertionsTrait; use Codeception\Module\Symfony\SecurityAssertionsTrait; @@ -144,7 +143,6 @@ class Symfony extends Framework implements DoctrineProvider, PartedModule use HttpClientAssertionsTrait; use MailerAssertionsTrait; use MimeAssertionsTrait; - use NotificationAssertionsTrait; use ParameterAssertionsTrait; use RouterAssertionsTrait; use SecurityAssertionsTrait; diff --git a/src/Codeception/Module/Symfony/BrowserAssertionsTrait.php b/src/Codeception/Module/Symfony/BrowserAssertionsTrait.php index cc8bfb54..488e1976 100644 --- a/src/Codeception/Module/Symfony/BrowserAssertionsTrait.php +++ b/src/Codeception/Module/Symfony/BrowserAssertionsTrait.php @@ -5,7 +5,6 @@ namespace Codeception\Module\Symfony; use PHPUnit\Framework\Constraint\Constraint; -use PHPUnit\Framework\Constraint\LogicalAnd; use PHPUnit\Framework\Constraint\LogicalNot; use Symfony\Component\BrowserKit\Test\Constraint\BrowserCookieValueSame; use Symfony\Component\BrowserKit\Test\Constraint\BrowserHasCookie; @@ -25,18 +24,17 @@ trait BrowserAssertionsTrait { /** - * Asserts the given cookie in the test Client is set to the expected value. + * Asserts that the given cookie in the test client is set to the expected value. */ public function assertBrowserCookieValueSame(string $name, string $expectedValue, bool $raw = false, string $path = '/', ?string $domain = null, string $message = ''): void { - $this->assertThatForClient(LogicalAnd::fromConstraints( - new BrowserHasCookie($name, $path, $domain), - new BrowserCookieValueSame($name, $expectedValue, $raw, $path, $domain) - ), $message); + $this->assertThatForClient(new BrowserHasCookie($name, $path, $domain), $message); + $this->assertThatForClient(new BrowserCookieValueSame($name, $expectedValue, $raw, $path, $domain), $message); } /** - * Asserts that the test Client does have the given cookie set (meaning, the cookie was set by any response in the test). + * Asserts that the test client has the specified cookie set. + * This indicates that the cookie was set by any response during the test. */ public function assertBrowserHasCookie(string $name, string $path = '/', ?string $domain = null, string $message = ''): void { @@ -44,7 +42,8 @@ public function assertBrowserHasCookie(string $name, string $path = '/', ?string } /** - * Asserts that the test Client does not have the given cookie set (meaning, the cookie was set by any response in the test). + * Asserts that the test client does not have the specified cookie set. + * This indicates that the cookie was not set by any response during the test. */ public function assertBrowserNotHasCookie(string $name, string $path = '/', ?string $domain = null, string $message = ''): void { @@ -52,7 +51,7 @@ public function assertBrowserNotHasCookie(string $name, string $path = '/', ?str } /** - * Asserts the given request attribute is set to the expected value. + * Asserts that the specified request attribute matches the expected value. */ public function assertRequestAttributeValueSame(string $name, string $expectedValue, string $message = ''): void { @@ -60,18 +59,16 @@ public function assertRequestAttributeValueSame(string $name, string $expectedVa } /** - * Asserts the given cookie is present and set to the expected value. + * Asserts that the specified response cookie is present and matches the expected value. */ public function assertResponseCookieValueSame(string $name, string $expectedValue, string $path = '/', ?string $domain = null, string $message = ''): void { - $this->assertThatForResponse(LogicalAnd::fromConstraints( - new ResponseHasCookie($name, $path, $domain), - new ResponseCookieValueSame($name, $expectedValue, $path, $domain) - ), $message); + $this->assertThatForResponse(new ResponseHasCookie($name, $path, $domain), $message); + $this->assertThatForResponse(new ResponseCookieValueSame($name, $expectedValue, $path, $domain), $message); } /** - * Asserts the response format returned by the `Response::getFormat()` method is the same as the expected value. + * Asserts that the response format matches the expected format. This checks the format returned by the `Response::getFormat()` method. */ public function assertResponseFormatSame(?string $expectedFormat, string $message = ''): void { @@ -79,7 +76,7 @@ public function assertResponseFormatSame(?string $expectedFormat, string $messag } /** - * Asserts the given cookie is present in the response (optionally checking for a specific cookie path or domain). + * Asserts that the specified cookie is present in the response. Optionally, it can check for a specific cookie path or domain. */ public function assertResponseHasCookie(string $name, string $path = '/', ?string $domain = null, string $message = ''): void { @@ -87,7 +84,8 @@ public function assertResponseHasCookie(string $name, string $path = '/', ?strin } /** - * Asserts the given header is available on the response, e.g. assertResponseHasHeader('content-type');. + * Asserts that the specified header is available in the response. + * For example, use `assertResponseHasHeader('content-type');`. */ public function assertResponseHasHeader(string $headerName, string $message = ''): void { @@ -95,8 +93,8 @@ public function assertResponseHasHeader(string $headerName, string $message = '' } /** - * Asserts the given header does not contain the expected value on the response, - * e.g. assertResponseHeaderNotSame('content-type', 'application/octet-stream');. + * Asserts that the specified header does not contain the expected value in the response. + * For example, use `assertResponseHeaderNotSame('content-type', 'application/octet-stream');`. */ public function assertResponseHeaderNotSame(string $headerName, string $expectedValue, string $message = ''): void { @@ -104,8 +102,8 @@ public function assertResponseHeaderNotSame(string $headerName, string $expected } /** - * Asserts the given header does contain the expected value on the response, - * e.g. assertResponseHeaderSame('content-type', 'application/octet-stream');. + * Asserts that the specified header contains the expected value in the response. + * For example, use `assertResponseHeaderSame('content-type', 'application/octet-stream');`. */ public function assertResponseHeaderSame(string $headerName, string $expectedValue, string $message = ''): void { @@ -113,7 +111,7 @@ public function assertResponseHeaderSame(string $headerName, string $expectedVal } /** - * Asserts that the response was successful (HTTP status is 2xx). + * Asserts that the response was successful (HTTP status code is in the 2xx range). */ public function assertResponseIsSuccessful(string $message = '', bool $verbose = true): void { @@ -121,7 +119,7 @@ public function assertResponseIsSuccessful(string $message = '', bool $verbose = } /** - * Asserts the response is unprocessable (HTTP status is 422) + * Asserts that the response is unprocessable (HTTP status code is 422). */ public function assertResponseIsUnprocessable(string $message = '', bool $verbose = true): void { @@ -129,7 +127,7 @@ public function assertResponseIsUnprocessable(string $message = '', bool $verbos } /** - * Asserts the given cookie is not present in the response (optionally checking for a specific cookie path or domain). + * Asserts that the specified cookie is not present in the response. Optionally, it can check for a specific cookie path or domain. */ public function assertResponseNotHasCookie(string $name, string $path = '/', ?string $domain = null, string $message = ''): void { @@ -137,7 +135,8 @@ public function assertResponseNotHasCookie(string $name, string $path = '/', ?st } /** - * Asserts the given header is not available on the response, e.g. assertResponseNotHasHeader('content-type');. + * Asserts that the specified header is not available in the response. + * For example, use `assertResponseNotHasHeader('content-type');`. */ public function assertResponseNotHasHeader(string $headerName, string $message = ''): void { @@ -145,30 +144,27 @@ public function assertResponseNotHasHeader(string $headerName, string $message = } /** - * Asserts the response is a redirect response (optionally, you can check the target location and status code). - * The excepted location can be either an absolute or a relative path. + * Asserts that the response is a redirect. Optionally, you can check the target location and status code. + * The expected location can be either an absolute or a relative path. */ public function assertResponseRedirects(?string $expectedLocation = null, ?int $expectedCode = null, string $message = '', bool $verbose = true): void { - $constraint = new ResponseIsRedirected($verbose); - if ($expectedLocation) { - if (class_exists(ResponseHeaderLocationSame::class)) { - $locationConstraint = new ResponseHeaderLocationSame($this->getClient()->getRequest(), $expectedLocation); - } else { - $locationConstraint = new ResponseHeaderSame('Location', $expectedLocation); - } + $this->assertThatForResponse(new ResponseIsRedirected($verbose), $message); - $constraint = LogicalAnd::fromConstraints($constraint, $locationConstraint); + if ($expectedLocation) { + $constraint = class_exists(ResponseHeaderLocationSame::class) + ? new ResponseHeaderLocationSame($this->getClient()->getRequest(), $expectedLocation) + : new ResponseHeaderSame('Location', $expectedLocation); + $this->assertThatForResponse($constraint, $message); } + if ($expectedCode) { - $constraint = LogicalAnd::fromConstraints($constraint, new ResponseStatusCodeSame($expectedCode)); + $this->assertThatForResponse(new ResponseStatusCodeSame($expectedCode), $message); } - - $this->assertThatForResponse($constraint, $message); } /** - * Asserts a specific HTTP status code. + * Asserts that the response status code matches the expected code. */ public function assertResponseStatusCodeSame(int $expectedCode, string $message = '', bool $verbose = true): void { @@ -178,23 +174,18 @@ public function assertResponseStatusCodeSame(int $expectedCode, string $message /** * Asserts the request matches the given route and optionally route parameters. */ - public function assertRouteSame(string $expectedRoute, array $parameters = [], string $message = ''): void - { - $constraint = new RequestAttributeValueSame('_route', $expectedRoute); - $constraints = []; + public function assertRouteSame(string $expectedRoute, array $parameters = [], string $message = ''): void { + $request = $this->getClient()->getRequest(); + $this->assertThat($request, new RequestAttributeValueSame('_route', $expectedRoute)); + foreach ($parameters as $key => $value) { - $constraints[] = new RequestAttributeValueSame($key, $value); - } - if ($constraints) { - $constraint = LogicalAnd::fromConstraints($constraint, ...$constraints); + $this->assertThat($request, new RequestAttributeValueSame($key, $value), $message); } - - $this->assertThat($this->getClient()->getRequest(), $constraint, $message); } /** - * Reboot client's kernel. - * Can be used to manually reboot kernel when 'rebootable_client' => false + * Reboots the client's kernel. + * Can be used to manually reboot the kernel when 'rebootable_client' is set to false. * * ```php * <?php @@ -214,7 +205,7 @@ public function rebootClientKernel(): void /** * Verifies that a page is available. - * By default, it checks the current page, specify the `$url` parameter to change it. + * By default, it checks the current page. Specify the `$url` parameter to change the page being checked. * * ```php * <?php @@ -224,7 +215,7 @@ public function rebootClientKernel(): void * $I->seePageIsAvailable('/dashboard'); // Same as above * ``` * - * @param string|null $url + * @param string|null $url The URL of the page to check. If null, the current page is checked. */ public function seePageIsAvailable(?string $url = null): void { @@ -237,7 +228,7 @@ public function seePageIsAvailable(?string $url = null): void } /** - * Goes to a page and check that it redirects to another. + * Navigates to a page and verifies that it redirects to another page. * * ```php * <?php @@ -246,21 +237,24 @@ public function seePageIsAvailable(?string $url = null): void */ public function seePageRedirectsTo(string $page, string $redirectsTo): void { - $this->getClient()->followRedirects(false); + $client = $this->getClient(); + $client->followRedirects(false); $this->amOnPage($page); - $response = $this->getClient()->getResponse(); + $this->assertTrue( - $response->isRedirection() + $client->getResponse()->isRedirection(), + 'The response is not a redirection.' ); - $this->getClient()->followRedirect(); + + $client->followRedirect(); $this->seeInCurrentUrl($redirectsTo); } /** - * Submit a form specifying the form name only once. + * Submits a form by specifying the form name only once. * * Use this function instead of [`$I->submitForm()`](#submitForm) to avoid repeating the form name in the field selectors. - * If you customized the names of the field selectors use `$I->submitForm()` for full control. + * If you have customized the names of the field selectors, use `$I->submitForm()` for full control. * * ```php * <?php @@ -270,8 +264,8 @@ public function seePageRedirectsTo(string $page, string $redirectsTo): void * ]); * ``` * - * @param string $name The `name` attribute of the `<form>` (you cannot use an array as selector here) - * @param string[] $fields + * @param string $name The `name` attribute of the `<form>`. You cannot use an array as a selector here. + * @param array<string, mixed> $fields The form fields to submit. */ public function submitSymfonyForm(string $name, array $fields): void { diff --git a/src/Codeception/Module/Symfony/DomCrawlerAssertionsTrait.php b/src/Codeception/Module/Symfony/DomCrawlerAssertionsTrait.php index 0d5bca59..643e3fd1 100644 --- a/src/Codeception/Module/Symfony/DomCrawlerAssertionsTrait.php +++ b/src/Codeception/Module/Symfony/DomCrawlerAssertionsTrait.php @@ -4,62 +4,21 @@ namespace Codeception\Module\Symfony; -use PHPUnit\Framework\Constraint\LogicalAnd; +use PHPUnit\Framework\Constraint\Constraint; use PHPUnit\Framework\Constraint\LogicalNot; -use Symfony\Component\DomCrawler\Crawler; -use Symfony\Component\DomCrawler\Test\Constraint\CrawlerAnySelectorTextContains; -use Symfony\Component\DomCrawler\Test\Constraint\CrawlerAnySelectorTextSame; use Symfony\Component\DomCrawler\Test\Constraint\CrawlerSelectorAttributeValueSame; -use Symfony\Component\DomCrawler\Test\Constraint\CrawlerSelectorCount; use Symfony\Component\DomCrawler\Test\Constraint\CrawlerSelectorExists; use Symfony\Component\DomCrawler\Test\Constraint\CrawlerSelectorTextContains; use Symfony\Component\DomCrawler\Test\Constraint\CrawlerSelectorTextSame; trait DomCrawlerAssertionsTrait { - /** - * Asserts that any element matching the given selector does contain the expected text. - */ - public function assertAnySelectorTextContains(string $selector, string $text, string $message = ''): void - { - $this->assertThat($this->getCrawler(), LogicalAnd::fromConstraints( - new CrawlerSelectorExists($selector), - new CrawlerAnySelectorTextContains($selector, $text) - ), $message); - } - - /** - * Asserts that any element matching the given selector does not contain the expected text. - */ - public function assertAnySelectorTextNotContains(string $selector, string $text, string $message = ''): void - { - $this->assertThat($this->getCrawler(), LogicalAnd::fromConstraints( - new CrawlerSelectorExists($selector), - new LogicalNot(new CrawlerAnySelectorTextContains($selector, $text)) - ), $message); - } - - /** - * Asserts that any element matching the given selector does equal the expected text. - */ - public function assertAnySelectorTextSame(string $selector, string $text, string $message = ''): void - { - $this->assertThat($this->getCrawler(), LogicalAnd::fromConstraints( - new CrawlerSelectorExists($selector), - new CrawlerAnySelectorTextSame($selector, $text) - ), $message); - } - /** * Asserts that the checkbox with the given name is checked. */ public function assertCheckboxChecked(string $fieldName, string $message = ''): void { - $this->assertThat( - $this->getCrawler(), - new CrawlerSelectorExists("input[name=\"$fieldName\"]:checked"), - $message - ); + $this->assertThatCrawler(new CrawlerSelectorExists("input[name=\"$fieldName\"]:checked"), $message); } /** @@ -67,33 +26,32 @@ public function assertCheckboxChecked(string $fieldName, string $message = ''): */ public function assertCheckboxNotChecked(string $fieldName, string $message = ''): void { - $this->assertThat( - $this->getCrawler(), - new LogicalNot(new CrawlerSelectorExists("input[name=\"$fieldName\"]:checked")), - $message - ); + $this->assertThatCrawler(new LogicalNot( + new CrawlerSelectorExists("input[name=\"$fieldName\"]:checked") + ), $message); } /** - * Asserts that value of the form input with the given name does not equal the expected value. + * Asserts that the value of the form input with the given name does not equal the expected value. */ public function assertInputValueNotSame(string $fieldName, string $expectedValue, string $message = ''): void { - $this->assertThat($this->getCrawler(), LogicalAnd::fromConstraints( - new CrawlerSelectorExists("input[name=\"$fieldName\"]"), - new LogicalNot(new CrawlerSelectorAttributeValueSame("input[name=\"$fieldName\"]", 'value', $expectedValue)) + $this->assertThatCrawler(new CrawlerSelectorExists("input[name=\"$fieldName\"]"), $message); + $this->assertThatCrawler(new LogicalNot( + new CrawlerSelectorAttributeValueSame("input[name=\"$fieldName\"]", 'value', $expectedValue) ), $message); } /** - * Asserts that value of the form input with the given name does equal the expected value. + * Asserts that the value of the form input with the given name equals the expected value. */ public function assertInputValueSame(string $fieldName, string $expectedValue, string $message = ''): void { - $this->assertThat($this->getCrawler(), LogicalAnd::fromConstraints( - new CrawlerSelectorExists("input[name=\"$fieldName\"]"), - new CrawlerSelectorAttributeValueSame("input[name=\"$fieldName\"]", 'value', $expectedValue) - ), $message); + $this->assertThatCrawler(new CrawlerSelectorExists("input[name=\"$fieldName\"]"), $message); + $this->assertThatCrawler( + new CrawlerSelectorAttributeValueSame("input[name=\"$fieldName\"]", 'value', $expectedValue), + $message + ); } /** @@ -105,7 +63,7 @@ public function assertPageTitleContains(string $expectedTitle, string $message = } /** - * Asserts that the `<title>` element is equal to the given title. + * Asserts that the `<title>` element equals the given title. */ public function assertPageTitleSame(string $expectedTitle, string $message = ''): void { @@ -113,19 +71,11 @@ public function assertPageTitleSame(string $expectedTitle, string $message = '') } /** - * Asserts that the expected number of selector elements are in the response. - */ - public function assertSelectorCount(int $expectedCount, string $selector, string $message = ''): void - { - $this->assertThat($this->getCrawler(), new CrawlerSelectorCount($expectedCount, $selector), $message); - } - - /** - * Asserts that the given selector does match at least one element in the response. + * Asserts that the given selector matches at least one element in the response. */ public function assertSelectorExists(string $selector, string $message = ''): void { - $this->assertThat($this->getCrawler(), new CrawlerSelectorExists($selector), $message); + $this->assertThatCrawler(new CrawlerSelectorExists($selector), $message); } /** @@ -133,18 +83,16 @@ public function assertSelectorExists(string $selector, string $message = ''): vo */ public function assertSelectorNotExists(string $selector, string $message = ''): void { - $this->assertThat($this->getCrawler(), new LogicalNot(new CrawlerSelectorExists($selector)), $message); + $this->assertThatCrawler(new LogicalNot(new CrawlerSelectorExists($selector)), $message); } /** - * Asserts that the first element matching the given selector does contain the expected text. + * Asserts that the first element matching the given selector contains the expected text. */ public function assertSelectorTextContains(string $selector, string $text, string $message = ''): void { - $this->assertThat($this->getCrawler(), LogicalAnd::fromConstraints( - new CrawlerSelectorExists($selector), - new CrawlerSelectorTextContains($selector, $text) - ), $message); + $this->assertThatCrawler(new CrawlerSelectorExists($selector), $message); + $this->assertThatCrawler(new CrawlerSelectorTextContains($selector, $text), $message); } /** @@ -152,25 +100,21 @@ public function assertSelectorTextContains(string $selector, string $text, strin */ public function assertSelectorTextNotContains(string $selector, string $text, string $message = ''): void { - $this->assertThat($this->getCrawler(), LogicalAnd::fromConstraints( - new CrawlerSelectorExists($selector), - new LogicalNot(new CrawlerSelectorTextContains($selector, $text)) - ), $message); + $this->assertThatCrawler(new CrawlerSelectorExists($selector), $message); + $this->assertThatCrawler(new LogicalNot(new CrawlerSelectorTextContains($selector, $text)), $message); } /** - * Asserts that the contents of the first element matching the given selector does equal the expected text. + * Asserts that the text of the first element matching the given selector equals the expected text. */ public function assertSelectorTextSame(string $selector, string $text, string $message = ''): void { - $this->assertThat($this->getCrawler(), LogicalAnd::fromConstraints( - new CrawlerSelectorExists($selector), - new CrawlerSelectorTextSame($selector, $text) - ), $message); + $this->assertThatCrawler(new CrawlerSelectorExists($selector), $message); + $this->assertThatCrawler(new CrawlerSelectorTextSame($selector, $text), $message); } - protected function getCrawler(): Crawler + protected function assertThatCrawler(Constraint $constraint, string $message): void { - return $this->client->getCrawler(); + $this->assertThat($this->getClient()->getCrawler(), $constraint, $message); } } diff --git a/src/Codeception/Module/Symfony/FormAssertionsTrait.php b/src/Codeception/Module/Symfony/FormAssertionsTrait.php index 0c8736f0..cdebbb64 100644 --- a/src/Codeception/Module/Symfony/FormAssertionsTrait.php +++ b/src/Codeception/Module/Symfony/FormAssertionsTrait.php @@ -17,7 +17,7 @@ trait FormAssertionsTrait */ public function assertFormValue(string $formSelector, string $fieldName, string $value, string $message = ''): void { - $node = $this->getCrawler()->filter($formSelector); + $node = $this->getCLient()->getCrawler()->filter($formSelector); $this->assertNotEmpty($node, sprintf('Form "%s" not found.', $formSelector)); $values = $node->form()->getValues(); $this->assertArrayHasKey($fieldName, $values, $message ?: sprintf('Field "%s" not found in form "%s".', $fieldName, $formSelector)); @@ -29,7 +29,7 @@ public function assertFormValue(string $formSelector, string $fieldName, string */ public function assertNoFormValue(string $formSelector, string $fieldName, string $message = ''): void { - $node = $this->getCrawler()->filter($formSelector); + $node = $this->getCLient()->getCrawler()->filter($formSelector); $this->assertNotEmpty($node, sprintf('Form "%s" not found.', $formSelector)); $values = $node->form()->getValues(); $this->assertArrayNotHasKey($fieldName, $values, $message ?: sprintf('Field "%s" has a value in form "%s".', $fieldName, $formSelector)); diff --git a/src/Codeception/Module/Symfony/MimeAssertionsTrait.php b/src/Codeception/Module/Symfony/MimeAssertionsTrait.php index a55b13ba..ba2ee9ac 100644 --- a/src/Codeception/Module/Symfony/MimeAssertionsTrait.php +++ b/src/Codeception/Module/Symfony/MimeAssertionsTrait.php @@ -134,22 +134,6 @@ public function assertEmailNotHasHeader(string $headerName, ?Email $email = null $this->assertThat($email, new LogicalNot(new MimeConstraint\EmailHasHeader($headerName))); } - /** - * Asserts that the subject of the given email does contain the expected subject. - */ - public function assertEmailSubjectContains(RawMessage $email, string $expectedValue, string $message = ''): void - { - $this->assertThat($email, new MimeConstraint\EmailSubjectContains($expectedValue), $message); - } - - /** - * Asserts that the subject of the given email does not contain the expected subject. - */ - public function assertEmailSubjectNotContains(RawMessage $email, string $expectedValue, string $message = ''): void - { - $this->assertThat($email, new LogicalNot(new MimeConstraint\EmailSubjectContains($expectedValue)), $message); - } - /** * Verify the text body of an email contains a `$text`. * If the Email object is not specified, the last email sent is used instead. diff --git a/src/Codeception/Module/Symfony/NotificationAssertionsTrait.php b/src/Codeception/Module/Symfony/NotificationAssertionsTrait.php deleted file mode 100644 index c8b0f74c..00000000 --- a/src/Codeception/Module/Symfony/NotificationAssertionsTrait.php +++ /dev/null @@ -1,91 +0,0 @@ -<?php - -declare(strict_types=1); - -namespace Codeception\Module\Symfony; - -use PHPUnit\Framework\Constraint\LogicalNot; -use Symfony\Component\Notifier\Event\MessageEvent; -use Symfony\Component\Notifier\Event\NotificationEvents; -use Symfony\Component\Notifier\Message\MessageInterface; -use Symfony\Component\Notifier\Test\Constraint\NotificationCount; -use Symfony\Component\Notifier\Test\Constraint\NotificationIsQueued; -use Symfony\Component\Notifier\Test\Constraint\NotificationSubjectContains; -use Symfony\Component\Notifier\Test\Constraint\NotificationTransportIsEqual; - -trait NotificationAssertionsTrait -{ - /** - * Asserts that the given number of notifications has been created (in total or for the given transport). - */ - public function assertNotificationCount(int $count, ?string $transportName = null, string $message = ''): void - { - $this->assertThat($this->getNotificationEvents(), new NotificationCount($count, $transportName), $message); - } - - /** - * Asserts that the given notification is not queued. - */ - public function assertNotificationIsNotQueued(MessageEvent $event, string $message = ''): void - { - $this->assertThat($event, new LogicalNot(new NotificationIsQueued()), $message); - } - - /** - * Asserts that the given notification is queued. - */ - public function assertNotificationIsQueued(MessageEvent $event, string $message = ''): void - { - $this->assertThat($event, new NotificationIsQueued(), $message); - } - - /** - * Asserts that the given text is included in the subject of the given notification. - */ - public function assertNotificationSubjectContains(MessageInterface $notification, string $text, string $message = ''): void - { - $this->assertThat($notification, new NotificationSubjectContains($text), $message); - } - - /** - * Asserts that the given text is not included in the subject of the given notification. - */ - public function assertNotificationSubjectNotContains(MessageInterface $notification, string $text, string $message = ''): void - { - $this->assertThat($notification, new LogicalNot(new NotificationSubjectContains($text)), $message); - } - - /** - * Asserts that the name of the transport for the given notification is the same as the given text. - */ - public function assertNotificationTransportIsEqual(MessageInterface $notification, ?string $transportName = null, string $message = ''): void - { - $this->assertThat($notification, new NotificationTransportIsEqual($transportName), $message); - } - - /** - * Asserts that the name of the transport for the given notification is not the same as the given text. - */ - public function assertNotificationTransportIsNotEqual(MessageInterface $notification, ?string $transportName = null, string $message = ''): void - { - $this->assertThat($notification, new LogicalNot(new NotificationTransportIsEqual($transportName)), $message); - } - - /** - * Asserts that the given number of notifications are queued (in total or for the given transport). - */ - public function assertQueuedNotificationCount(int $count, ?string $transportName = null, string $message = ''): void - { - $this->assertThat($this->getNotificationEvents(), new NotificationCount($count, $transportName, true), $message); - } - - protected function getNotificationEvents(): NotificationEvents - { - $notificationLogger = $this->getService('notifier.notification_logger_listener'); - if ($notificationLogger) { - return $notificationLogger->getEvents(); - } - - $this->fail('A client must have Notifier enabled to make notifications assertions. Did you forget to require symfony/notifier?'); - } -} <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'> <html xmlns='http://www.w3.org/1999/xhtml'> <head> <title>pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy