diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 44477a5b..85b0d49d 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -1,18 +1,16 @@ name: CI - on: [push, pull_request] jobs: tests: runs-on: ubuntu-latest - strategy: matrix: - php: [8.1, 8.2, 8.3] - symfony: ["5.4.*", "6.4.*", "7.0.*"] + php: [8.1, 8.2, 8.3, 8.4] + symfony: ["5.4.*", "6.4.*", "7.2.*"] exclude: - php: 8.1 - symfony: "7.0.*" + symfony: "7.2.*" steps: - name: Checkout code @@ -26,40 +24,28 @@ jobs: extensions: ctype, iconv, intl, json, mbstring, pdo, pdo_sqlite coverage: none - - name: Checkout Symfony 5.4 Sample - if: "matrix.symfony == '5.4.*'" - uses: actions/checkout@v4 - with: - repository: Codeception/symfony-module-tests - path: framework-tests - ref: "5.4" - - - name: Checkout Symfony 6.4 Sample - if: "matrix.symfony == '6.4.*'" - uses: actions/checkout@v4 - with: - repository: Codeception/symfony-module-tests - path: framework-tests - ref: "6.4" + - name: Set Symfony version reference + run: echo "SF_REF=${MATRIX_SYMFONY%.*}" >> $GITHUB_ENV + env: + MATRIX_SYMFONY: ${{ matrix.symfony }} - - name: Checkout Symfony 7.0 Sample - if: "matrix.symfony == '7.0.*'" + - name: Checkout Symfony ${{ env.SF_REF }} Sample uses: actions/checkout@v4 with: repository: Codeception/symfony-module-tests path: framework-tests - ref: "7.0" + ref: ${{ env.SF_REF }} - name: Get composer cache directory id: composer-cache - run: echo "::set-output name=dir::$(composer config cache-files-dir)" + run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT - - name: Cache composer dependencies + - name: Cache Composer dependencies uses: actions/cache@v3 with: path: ${{ steps.composer-cache.outputs.dir }} - key: ${{ runner.os }}-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }} - restore-keys: ${{ runner.os }}-${{ matrix.php }}-composer- + key: ${{ runner.os }}-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json', 'composer.lock') }} + restore-keys: ${{ runner.os }}-php-${{ matrix.php }}-composer- - name: Install PHPUnit 10 run: composer require --dev --no-update "phpunit/phpunit=^10.0" @@ -78,27 +64,27 @@ jobs: composer require codeception/module-doctrine="3.*" --no-update composer update --prefer-dist --no-progress --no-dev - - name: Validate composer.json and composer.lock - run: composer validate + - name: Validate Composer files + run: composer validate --strict working-directory: framework-tests - - name: Install PHPUnit 10 in framework-tests + - name: Install PHPUnit in framework-tests run: composer require --dev --no-update "phpunit/phpunit=^10.0" working-directory: framework-tests - - name: Install Symfony Sample + - name: Prepare Symfony sample run: | composer remove codeception/codeception codeception/module-asserts codeception/module-doctrine codeception/lib-innerbrowser codeception/module-symfony --dev --no-update composer update --no-progress working-directory: framework-tests - - name: Prepare the test environment + - name: Setup Database run: | php bin/console doctrine:schema:update --force php bin/console doctrine:fixtures:load --quiet working-directory: framework-tests - - name: Run test suite + - name: Run tests run: | php vendor/bin/codecept build -c framework-tests php vendor/bin/codecept run Functional -c framework-tests diff --git a/composer.json b/composer.json index 1421734d..747a5941 100644 --- a/composer.json +++ b/composer.json @@ -5,6 +5,7 @@ "type": "library", "keywords": [ "codeception", + "functional testing", "symfony" ], "authors": [ @@ -27,29 +28,34 @@ "codeception/module-asserts": "^3.0", "codeception/module-doctrine": "^3.1", "doctrine/orm": "^2.10", - "symfony/browser-kit": "^5.4 | ^6.4 | ^7.0", - "symfony/cache": "^5.4 | ^6.4 | ^7.0", - "symfony/config": "^5.4 | ^6.4 | ^7.0", - "symfony/dependency-injection": "^5.4 | ^6.4 | ^7.0", - "symfony/dom-crawler": "^5.4 | ^6.4 | ^7.0", - "symfony/error-handler": "^5.4 | ^6.4 | ^7.0", - "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-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/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", - "symfony/routing": "^5.4 | ^6.4 | ^7.0", - "symfony/security-bundle": "^5.4 | ^6.4 | ^7.0", - "symfony/security-core": "^5.4 | ^6.4 | ^7.0", - "symfony/security-csrf": "^5.4 | ^6.4 | ^7.0", - "symfony/security-http": "^5.4 | ^6.4 | ^7.0", - "symfony/twig-bundle": "^5.4 | ^6.4 | ^7.0", - "symfony/var-exporter": "^5.4 | ^6.4 | ^7.0", + "symfony/browser-kit": "^5.4 | ^6.4 | ^7.2", + "symfony/cache": "^5.4 | ^6.4 | ^7.2", + "symfony/config": "^5.4 | ^6.4 | ^7.2", + "symfony/dependency-injection": "^5.4 | ^6.4 | ^7.2", + "symfony/dom-crawler": "^5.4 | ^6.4 | ^7.2", + "symfony/dotenv": "^5.4 | ^6.4 | ^7.2", + "symfony/error-handler": "^5.4 | ^6.4 | ^7.2", + "symfony/filesystem": "^5.4 | ^6.4 | ^7.2", + "symfony/form": "^5.4 | ^6.4 | ^7.2", + "symfony/framework-bundle": "^5.4 | ^6.4 | ^7.2", + "symfony/http-client": "^5.4 | ^6.4 | ^7.2", + "symfony/http-foundation": "^5.4 | ^6.4 | ^7.2", + "symfony/http-kernel": "^5.4 | ^6.4 | ^7.2", + "symfony/mailer": "^5.4 | ^6.4 | ^7.2", + "symfony/mime": "^5.4 | ^6.4 | ^7.2", + "symfony/notifier": "^5.4 | ^6.4 | ^7.2", + "symfony/options-resolver": "^5.4 | ^6.4 | ^7.2", + "symfony/property-access": "^5.4 | ^6.4 | ^7.2", + "symfony/property-info": "^5.4 | ^6.4 | ^7.2", + "symfony/routing": "^5.4 | ^6.4 | ^7.2", + "symfony/security-bundle": "^5.4 | ^6.4 | ^7.2", + "symfony/security-core": "^5.4 | ^6.4 | ^7.2", + "symfony/security-csrf": "^5.4 | ^6.4 | ^7.2", + "symfony/security-http": "^5.4 | ^6.4 | ^7.2", + "symfony/translation": "^5.4 | ^6.4 | ^7.2", + "symfony/twig-bundle": "^5.4 | ^6.4 | ^7.2", + "symfony/validator": "^5.4 | ^6.4 | ^7.2", + "symfony/var-exporter": "^5.4 | ^6.4 | ^7.2", "vlucas/phpdotenv": "^4.2 | ^5.4" }, "suggest": { diff --git a/readme.md b/readme.md index 06d2d614..c5bbcb98 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.2.x` or higher, as per the [Symfony supported versions](https://symfony.com/releases). * `PHP 8.1` or higher. ## Installation 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 13a9f6df..3ac2bc79 100644 --- a/src/Codeception/Module/Symfony.php +++ b/src/Codeception/Module/Symfony.php @@ -13,8 +13,11 @@ 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\LoggerAssertionsTrait; use Codeception\Module\Symfony\MailerAssertionsTrait; use Codeception\Module\Symfony\MimeAssertionsTrait; use Codeception\Module\Symfony\ParameterAssertionsTrait; @@ -23,15 +26,19 @@ use Codeception\Module\Symfony\ServicesAssertionsTrait; use Codeception\Module\Symfony\SessionAssertionsTrait; use Codeception\Module\Symfony\TimeAssertionsTrait; +use Codeception\Module\Symfony\TranslationAssertionsTrait; use Codeception\Module\Symfony\TwigAssertionsTrait; +use Codeception\Module\Symfony\ValidatorAssertionsTrait; use Codeception\TestInterface; use Doctrine\ORM\EntityManagerInterface; use Exception; +use LogicException; use ReflectionClass; use ReflectionException; use Symfony\Bundle\SecurityBundle\DataCollector\SecurityDataCollector; use Symfony\Component\BrowserKit\AbstractBrowser; use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\Dotenv\Dotenv; use Symfony\Component\Finder\Finder; use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface; use Symfony\Component\HttpKernel\DataCollector\TimeDataCollector; @@ -39,11 +46,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; @@ -52,7 +57,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; @@ -80,9 +84,10 @@ * * `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. * * `authenticator`: 'false' - Reboot client's kernel before each request (only for Symfony 6.0 or higher) * * #### Sample `Functional.suite.yml` @@ -134,8 +139,11 @@ class Symfony extends Framework implements DoctrineProvider, PartedModule use BrowserAssertionsTrait; use ConsoleAssertionsTrait; use DoctrineAssertionsTrait; + use DomCrawlerAssertionsTrait; use EventsAssertionsTrait; use FormAssertionsTrait; + use HttpClientAssertionsTrait; + use LoggerAssertionsTrait; use MailerAssertionsTrait; use MimeAssertionsTrait; use ParameterAssertionsTrait; @@ -143,8 +151,10 @@ class Symfony extends Framework implements DoctrineProvider, PartedModule use SecurityAssertionsTrait; use ServicesAssertionsTrait; use SessionAssertionsTrait; + use TranslationAssertionsTrait; use TimeAssertionsTrait; use TwigAssertionsTrait; + use ValidatorAssertionsTrait; public Kernel $kernel; @@ -165,46 +175,38 @@ class Symfony extends Framework implements DoctrineProvider, PartedModule 'em_service' => 'doctrine.orm.entity_manager', 'rebootable_client' => true, 'authenticator' => false, + 'bootstrap' => false, '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']) { + $this->bootstrapEnvironment(); + } $this->kernel->boot(); - - if ($this->config['cache_router'] === true) { + if ($this->config['cache_router']) { $this->persistPermanentService('router'); } } @@ -214,7 +216,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']); } @@ -226,7 +228,6 @@ public function _after(TestInterface $test): void foreach (array_keys($this->permanentServices) as $serviceName) { $this->permanentServices[$serviceName] = $this->grabService($serviceName); } - parent::_after($test); } @@ -249,40 +250,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 @@ -308,9 +293,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, @@ -319,41 +305,32 @@ 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( self::class, "Kernel class was not found.\n" - . 'Specify directory where file with Kernel class for your application is located with `app_path` parameter.' + . 'Specify directory where file with Kernel class for your application is located with `kernel_class` parameter.' ); } 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) { @@ -366,22 +343,14 @@ 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 { - 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); @@ -390,49 +359,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); - } } /** @@ -441,15 +384,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(); } } } @@ -457,6 +399,54 @@ 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 { + if (!method_exists(Dotenv::class, 'bootEnv')) { + throw new LogicException( + "Symfony DotEnv is missing. Try running 'composer require symfony/dotenv'\n" . + "If you can't install DotEnv add your env files to the 'params' key in codeception.yml\n" . + "or update your symfony/framework-bundle recipe by running:\n" . + 'composer recipes:install symfony/framework-bundle --force' + ); + } + $_ENV['APP_ENV'] = $this->config['environment']; + (new Dotenv())->bootEnv('.env'); + } + } + + private function debugSecurityData(SecurityDataCollector $security): void + { + if ($security->isAuthenticated()) { + $roles = $security->getRoles(); + $rolesString = implode(',', $roles instanceof Data ? $roles->getValue() : $roles); + $userInfo = $security->getUser() . ' [' . $rolesString . ']'; + } else { + $userInfo = 'Anonymous'; + } + $this->debugSection('User', $userInfo); + } + + private function debugMailerData(MessageDataCollector $mailerCollector): void + { + $this->debugSection('Emails', count($mailerCollector->getEvents()->getMessages()) . ' sent'); + } + + private function debugTimeData(TimeDataCollector $timeCollector): void + { + $this->debugSection('Time', number_format($timeCollector->getDuration(), 2) . ' ms'); + } + /** * Ensures autoloader loading of additional directories. * It is only required for CI jobs to run correctly. diff --git a/src/Codeception/Module/Symfony/BrowserAssertionsTrait.php b/src/Codeception/Module/Symfony/BrowserAssertionsTrait.php index 001e7ca2..fbd8a075 100644 --- a/src/Codeception/Module/Symfony/BrowserAssertionsTrait.php +++ b/src/Codeception/Module/Symfony/BrowserAssertionsTrait.php @@ -4,14 +4,273 @@ namespace Codeception\Module\Symfony; +use PHPUnit\Framework\Constraint\Constraint; +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 { /** - * Reboot client's kernel. - * Can be used to manually reboot kernel when 'rebootable_client' => false + * Asserts that the given cookie in the test client is set to the expected value. + * + * ```php + * assertBrowserCookieValueSame('cookie_name', 'expected_value'); + * ``` + */ + public function assertBrowserCookieValueSame(string $name, string $expectedValue, bool $raw = false, string $path = '/', ?string $domain = null, string $message = ''): void + { + $this->assertThatForClient(new BrowserHasCookie($name, $path, $domain), $message); + $this->assertThatForClient(new BrowserCookieValueSame($name, $expectedValue, $raw, $path, $domain), $message); + } + + /** + * Asserts that the test client has the specified cookie set. + * This indicates that the cookie was set by any response during the test. + * + * ``` + * assertBrowserHasCookie('cookie_name'); + * ``` + */ + 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 specified cookie set. + * This indicates that the cookie was not set by any response during the test. + * + * ```php + * assertBrowserNotHasCookie('cookie_name'); + * ``` + */ + public function assertBrowserNotHasCookie(string $name, string $path = '/', ?string $domain = null, string $message = ''): void + { + $this->assertThatForClient(new LogicalNot(new BrowserHasCookie($name, $path, $domain)), $message); + } + + /** + * Asserts that the specified request attribute matches the expected value. + * + * ```php + * assertRequestAttributeValueSame('attribute_name', 'expected_value'); + * ``` + */ + public function assertRequestAttributeValueSame(string $name, string $expectedValue, string $message = ''): void + { + $this->assertThat($this->getClient()->getRequest(), new RequestAttributeValueSame($name, $expectedValue), $message); + } + + /** + * Asserts that the specified response cookie is present and matches the expected value. + * + * ```php + * assertResponseCookieValueSame('cookie_name', 'expected_value'); + * ``` + */ + public function assertResponseCookieValueSame(string $name, string $expectedValue, string $path = '/', ?string $domain = null, string $message = ''): void + { + $this->assertThatForResponse(new ResponseHasCookie($name, $path, $domain), $message); + $this->assertThatForResponse(new ResponseCookieValueSame($name, $expectedValue, $path, $domain), $message); + } + + /** + * Asserts that the response format matches the expected format. This checks the format returned by the `Response::getFormat()` method. + * + * ```php + * assertResponseFormatSame('json'); + * ``` + */ + public function assertResponseFormatSame(?string $expectedFormat, string $message = ''): void + { + $this->assertThatForResponse(new ResponseFormatSame($this->getClient()->getRequest(), $expectedFormat), $message); + } + + /** + * Asserts that the specified cookie is present in the response. Optionally, it can check for a specific cookie path or domain. + * + * ```php + * assertResponseHasCookie('cookie_name'); + * ``` + */ + public function assertResponseHasCookie(string $name, string $path = '/', ?string $domain = null, string $message = ''): void + { + $this->assertThatForResponse(new ResponseHasCookie($name, $path, $domain), $message); + } + + /** + * Asserts that the specified header is available in the response. + * For example, use `assertResponseHasHeader('content-type');`. + * + * ```php + * assertResponseHasHeader('content-type'); + * ``` + */ + public function assertResponseHasHeader(string $headerName, string $message = ''): void + { + $this->assertThatForResponse(new ResponseHasHeader($headerName), $message); + } + + /** + * Asserts that the specified header does not contain the expected value in the response. + * For example, use `assertResponseHeaderNotSame('content-type', 'application/octet-stream');`. + * + * ```php + * assertResponseHeaderNotSame('content-type', 'application/json'); + * ``` + */ + public function assertResponseHeaderNotSame(string $headerName, string $expectedValue, string $message = ''): void + { + $this->assertThatForResponse(new LogicalNot(new ResponseHeaderSame($headerName, $expectedValue)), $message); + } + + /** + * Asserts that the specified header contains the expected value in the response. + * For example, use `assertResponseHeaderSame('content-type', 'application/octet-stream');`. + * + * ```php + * assertResponseHeaderSame('content-type', 'application/json'); + * ``` + */ + 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 code is in the 2xx range). + * + * ```php + * assertResponseIsSuccessful(); + * ``` + */ + public function assertResponseIsSuccessful(string $message = '', bool $verbose = true): void + { + $this->assertThatForResponse(new ResponseIsSuccessful($verbose), $message); + } + + /** + * Asserts that the response is unprocessable (HTTP status code is 422). + * + * ```php + * assertResponseIsUnprocessable(); + * ``` + */ + public function assertResponseIsUnprocessable(string $message = '', bool $verbose = true): void + { + $this->assertThatForResponse(new ResponseIsUnprocessable($verbose), $message); + } + + /** + * Asserts that the specified cookie is not present in the response. Optionally, it can check for a specific cookie path or domain. + * + * ```php + * assertResponseNotHasCookie('cookie_name'); + * ``` + */ + public function assertResponseNotHasCookie(string $name, string $path = '/', ?string $domain = null, string $message = ''): void + { + $this->assertThatForResponse(new LogicalNot(new ResponseHasCookie($name, $path, $domain)), $message); + } + + /** + * Asserts that the specified header is not available in the response. + * + * ```php + * assertResponseNotHasHeader('content-type'); + * ``` + */ + public function assertResponseNotHasHeader(string $headerName, string $message = ''): void + { + $this->assertThatForResponse(new LogicalNot(new ResponseHasHeader($headerName)), $message); + } + + /** + * 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. + * + * ```php + * assertResponseRedirects('/login', 302); + * ``` + */ + public function assertResponseRedirects(?string $expectedLocation = null, ?int $expectedCode = null, string $message = '', bool $verbose = true): void + { + $this->assertThatForResponse(new ResponseIsRedirected($verbose), $message); + + if ($expectedLocation) { + $constraint = class_exists(ResponseHeaderLocationSame::class) + ? new ResponseHeaderLocationSame($this->getClient()->getRequest(), $expectedLocation) + : new ResponseHeaderSame('Location', $expectedLocation); + $this->assertThatForResponse($constraint, $message); + } + + if ($expectedCode) { + $this->assertThatForResponse(new ResponseStatusCodeSame($expectedCode), $message); + } + } + + /** + * Asserts that the response status code matches the expected code. + * + * ```php + * assertResponseStatusCodeSame(200); + * ``` + */ + 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. + * + * ```php + * assertRouteSame('profile', ['id' => 123]); + * ``` + */ + 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) { + $this->assertThat($request, new RequestAttributeValueSame($key, $value), $message); + } + } + + /** + * Reboots the client's kernel. + * Can be used to manually reboot the kernel when 'rebootable_client' is set to false. * * ```php * 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 + public function seePageIsAvailable(?string $url = null): void { if ($url !== null) { $this->amOnPage($url); $this->seeInCurrentUrl($url); } - $this->assertThat($this->getClient()->getResponse(), new ResponseIsSuccessful()); + $this->assertResponseIsSuccessful(); } /** - * Goes to a page and check that it redirects to another. + * Navigates to a page and verifies that it redirects to another page. * * ```php * 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 * ` (you cannot use an array as selector here) - * @param string[] $fields + * @param string $name The `name` attribute of the `
`. You cannot use an array as a selector here. + * @param array $fields The form fields to submit. */ public function submitSymfonyForm(string $name, array $fields): void { @@ -104,4 +366,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..8786be4c --- /dev/null +++ b/src/Codeception/Module/Symfony/DomCrawlerAssertionsTrait.php @@ -0,0 +1,175 @@ +assertCheckboxChecked('agree_terms'); + * ``` + */ + public function assertCheckboxChecked(string $fieldName, string $message = ''): void + { + $this->assertThatCrawler(new CrawlerSelectorExists("input[name=\"$fieldName\"]:checked"), $message); + } + + /** + * Asserts that the checkbox with the given name is not checked. + * + * ```php + * assertCheckboxNotChecked('subscribe'); + * ``` + */ + public function assertCheckboxNotChecked(string $fieldName, string $message = ''): void + { + $this->assertThatCrawler(new LogicalNot( + new CrawlerSelectorExists("input[name=\"$fieldName\"]:checked") + ), $message); + } + + /** + * Asserts that the value of the form input with the given name does not equal the expected value. + * + * ```php + * assertInputValueNotSame('username', 'admin'); + * ``` + */ + public function assertInputValueNotSame(string $fieldName, string $expectedValue, string $message = ''): void + { + $this->assertThatCrawler(new CrawlerSelectorExists("input[name=\"$fieldName\"]"), $message); + $this->assertThatCrawler(new LogicalNot( + new CrawlerSelectorAttributeValueSame("input[name=\"$fieldName\"]", 'value', $expectedValue) + ), $message); + } + + /** + * Asserts that the value of the form input with the given name equals the expected value. + * + * ```php + * assertInputValueSame('username', 'johndoe'); + * ``` + */ + public function assertInputValueSame(string $fieldName, string $expectedValue, string $message = ''): void + { + $this->assertThatCrawler(new CrawlerSelectorExists("input[name=\"$fieldName\"]"), $message); + $this->assertThatCrawler( + new CrawlerSelectorAttributeValueSame("input[name=\"$fieldName\"]", 'value', $expectedValue), + $message + ); + } + + /** + * Asserts that the `` element contains the given title. + * + * ```php + * <?php + * $I->assertPageTitleContains('Welcome'); + * ``` + */ + public function assertPageTitleContains(string $expectedTitle, string $message = ''): void + { + $this->assertSelectorTextContains('title', $expectedTitle, $message); + } + + /** + * Asserts that the `<title>` element equals the given title. + * + * ```php + * <?php + * $I->assertPageTitleSame('Home Page'); + * ``` + */ + public function assertPageTitleSame(string $expectedTitle, string $message = ''): void + { + $this->assertSelectorTextSame('title', $expectedTitle, $message); + } + + /** + * Asserts that the given selector matches at least one element in the response. + * + * ```php + * <?php + * $I->assertSelectorExists('.main-content'); + * ``` + */ + public function assertSelectorExists(string $selector, string $message = ''): void + { + $this->assertThatCrawler(new CrawlerSelectorExists($selector), $message); + } + + /** + * Asserts that the given selector does not match at least one element in the response. + * + * ```php + * <?php + * $I->assertSelectorNotExists('.error'); + * ``` + */ + public function assertSelectorNotExists(string $selector, string $message = ''): void + { + $this->assertThatCrawler(new LogicalNot(new CrawlerSelectorExists($selector)), $message); + } + + /** + * Asserts that the first element matching the given selector contains the expected text. + * + * ```php + * <?php + * $I->assertSelectorTextContains('h1', 'Dashboard'); + * ``` + */ + public function assertSelectorTextContains(string $selector, string $text, string $message = ''): void + { + $this->assertThatCrawler(new CrawlerSelectorExists($selector), $message); + $this->assertThatCrawler(new CrawlerSelectorTextContains($selector, $text), $message); + } + + /** + * Asserts that the first element matching the given selector does not contain the expected text. + * + * ```php + * <?php + * $I->assertSelectorTextNotContains('p', 'error'); + * ``` + */ + public function assertSelectorTextNotContains(string $selector, string $text, string $message = ''): void + { + $this->assertThatCrawler(new CrawlerSelectorExists($selector), $message); + $this->assertThatCrawler(new LogicalNot(new CrawlerSelectorTextContains($selector, $text)), $message); + } + + /** + * Asserts that the text of the first element matching the given selector equals the expected text. + * + * ```php + * <?php + * $I->assertSelectorTextSame('h1', 'Dashboard'); + * ``` + */ + public function assertSelectorTextSame(string $selector, string $text, string $message = ''): void + { + $this->assertThatCrawler(new CrawlerSelectorExists($selector), $message); + $this->assertThatCrawler(new CrawlerSelectorTextSame($selector, $text), $message); + } + + protected function assertThatCrawler(Constraint $constraint, string $message): void + { + $this->assertThat($this->getClient()->getCrawler(), $constraint, $message); + } +} 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..f77403bd 100644 --- a/src/Codeception/Module/Symfony/FormAssertionsTrait.php +++ b/src/Codeception/Module/Symfony/FormAssertionsTrait.php @@ -12,6 +12,39 @@ trait FormAssertionsTrait { + /** + * Asserts that value of the field of the first form matching the given selector does equal the expected value. + * + * ```php + * <?php + * $I->assertFormValue('#loginForm', 'username', 'john_doe'); + * ``` + */ + public function assertFormValue(string $formSelector, string $fieldName, string $value, string $message = ''): void + { + $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)); + $this->assertSame($value, $values[$fieldName]); + } + + /** + * Asserts that the field of the first form matching the given selector does not have a value. + * + * ```php + * <?php + * $I->assertNoFormValue('#registrationForm', 'middle_name'); + * ``` + */ + public function assertNoFormValue(string $formSelector, string $fieldName, string $message = ''): void + { + $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)); + } + /** * Verifies that there are no errors bound to the submitted form. * @@ -42,10 +75,8 @@ 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 + public function seeFormErrorMessage(string $field, ?string $message = null): void { $formCollector = $this->grabFormCollector(__FUNCTION__); @@ -107,7 +138,6 @@ public function seeFormErrorMessage(string $field, string $message = null): void * If you want to specify the error messages, you can do so * by sending an associative array instead, with the key being * the name of the field and the error message the value. - * * This method will validate that the expected error message * is contained in the actual error message, that is, * you can specify either the entire error message or just a part of it: @@ -115,7 +145,7 @@ public function seeFormErrorMessage(string $field, string $message = null): void * ```php * <?php * $I->seeFormErrorMessages([ - * 'address' => 'The address is too long' + * 'address' => 'The address is too long', * 'telephone' => 'too short', // the full error message is 'The telephone is too short' * ]); * ``` @@ -170,4 +200,4 @@ protected function grabFormCollector(string $function): FormDataCollector { return $this->grabCollector('form', $function); } -} \ No newline at end of file +} diff --git a/src/Codeception/Module/Symfony/HttpClientAssertionsTrait.php b/src/Codeception/Module/Symfony/HttpClientAssertionsTrait.php new file mode 100644 index 00000000..f6f322eb --- /dev/null +++ b/src/Codeception/Module/Symfony/HttpClientAssertionsTrait.php @@ -0,0 +1,138 @@ +<?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.) + * + * ```php + * <?php + * $I->assertHttpClientRequest( + * 'https://example.com/api', + * 'POST', + * '{"data": "value"}', + * ['Authorization' => 'Bearer token'] + * ); + * ``` + */ + 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. + * + * ```php + * <?php + * $I->assertHttpClientRequestCount(3); + * ``` + */ + 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. + * + * ```php + * <?php + * $I->assertNotHttpClientRequest('https://example.com/unexpected', 'GET'); + * ``` + */ + 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/LoggerAssertionsTrait.php b/src/Codeception/Module/Symfony/LoggerAssertionsTrait.php new file mode 100644 index 00000000..4cd0266a --- /dev/null +++ b/src/Codeception/Module/Symfony/LoggerAssertionsTrait.php @@ -0,0 +1,60 @@ +<?php + +declare(strict_types=1); + +namespace Codeception\Module\Symfony; + +use Symfony\Component\HttpKernel\DataCollector\LoggerDataCollector; +use Symfony\Component\VarDumper\Cloner\Data; +use function sprintf; + +trait LoggerAssertionsTrait +{ + /** + * Asserts that there are no deprecation messages in Symfony's log. + * + * ```php + * <?php + * $I->amOnPage('/home'); + * $I->dontSeeDeprecations(); + * ``` + * + * @param string $message Optional custom failure message. + */ + public function dontSeeDeprecations(string $message = ''): void + { + $loggerCollector = $this->grabLoggerCollector(__FUNCTION__); + $logs = $loggerCollector->getProcessedLogs(); + + $foundDeprecations = []; + + foreach ($logs as $log) { + if (isset($log['type']) && $log['type'] === 'deprecation') { + $msg = $log['message']; + if ($msg instanceof Data) { + $msg = $msg->getValue(true); + } + if (!is_string($msg)) { + $msg = (string)$msg; + } + $foundDeprecations[] = $msg; + } + } + + $errorMessage = $message ?: sprintf( + "Found %d deprecation message%s in the log:\n%s", + count($foundDeprecations), + count($foundDeprecations) > 1 ? 's' : '', + implode("\n", array_map(static function ($msg) { + return " - " . $msg; + }, $foundDeprecations)) + ); + + $this->assertEmpty($foundDeprecations, $errorMessage); + } + + protected function grabLoggerCollector(string $function): LoggerDataCollector + { + return $this->grabCollector('logger', $function); + } +} diff --git a/src/Codeception/Module/Symfony/MailerAssertionsTrait.php b/src/Codeception/Module/Symfony/MailerAssertionsTrait.php index f8bb9772..5a31e6d8 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,36 +14,80 @@ 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. + * + * ```php + * <?php + * $I->assertEmailCount(2, 'smtp'); + * ``` */ - 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). - * 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. + * Asserts that the given mailer event is not queued. + * Use `getMailerEvent(int $index = 0, ?string $transport = null)` to retrieve a mailer event by index. * * ```php * <?php - * $I->seeEmailIsSent(2); + * $event = $I->getMailerEvent(); + * $I->assertEmailIsNotQueued($event); * ``` + */ + 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. * - * @param int $expectedCount The expected number of emails sent + * ```php + * <?php + * $event = $I->getMailerEvent(); + * $I->assertEmailIsQueued($event); + * ``` */ - public function seeEmailIsSent(int $expectedCount = 1): void + public function assertEmailIsQueued(MessageEvent $event, string $message = ''): void { - $this->assertThat($this->getMessageMailerEvents(), new MailerConstraint\EmailCount($expectedCount)); + $this->assertThat($event, new MailerConstraint\EmailIsQueued(), $message); + } + + /** + * Asserts that the expected number of emails was queued (e.g. using the Messenger component). + * + * ```php + * <?php + * $I->assertQueuedEmailCount(1, 'smtp'); + * ``` + */ + 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, you need to suppress it using [stopFollowingRedirects()](#stopFollowingRedirects) first; otherwise this check will *always* pass. + * + * ```php + * <?php + * $I->dontSeeEmailIsSent(); + * ``` + */ + public function dontSeeEmailIsSent(): void + { + $this->assertThat($this->getMessageMailerEvents(), new MailerConstraint\EmailCount(0)); } /** * Returns the last sent email. * The function 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. + * If your app performs an HTTP redirect after sending the email, you need to suppress it using [stopFollowingRedirects()](#stopFollowingRedirects) first. * See also: [grabSentEmails()](https://codeception.com/docs/modules/Symfony#grabSentEmails) * * ```php @@ -63,7 +109,7 @@ public function grabLastSentEmail(): ?Email /** * Returns an array of all sent emails. * The function 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. + * If your app performs an HTTP redirect after sending the email, you need to suppress it using [stopFollowingRedirects()](#stopFollowingRedirects) first. * See also: [grabLastSentEmail()](https://codeception.com/docs/modules/Symfony#grabLastSentEmail) * * ```php @@ -78,18 +124,53 @@ 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()](#stopFollowingRedirects) first. + * + * Limitation: + * If your mail is sent in a Symfony console command and you start that command in your test with [$I->runShellCommand()](https://codeception.com/docs/modules/Cli#runShellCommand), + * Codeception will not notice it. + * As a more professional alternative, we recommend Mailpit (see [Addons](https://codeception.com/addons)), which also lets you test the content of the mail. + * + * ```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)); + } + + /** + * Returns the mailer event at the specified index. + * + * ```php + * <?php + * $event = $I->getMailerEvent(); + * ``` + */ + public function getMailerEvent(int $index = 0, ?string $transport = null): ?MessageEvent + { + $mailerEvents = $this->getMessageMailerEvents(); + $events = $mailerEvents->getEvents($transport); + return $events[$index] ?? null; + } + protected function getMessageMailerEvents(): MessageEvents { - if ($messageLogger = $this->getService('mailer.message_logger_listener')) { - /** @var MessageLoggerListener $messageLogger */ - return $messageLogger->getEvents(); + if ($mailer = $this->getService('mailer.message_logger_listener')) { + /** @var MessageLoggerListener $mailer */ + return $mailer->getEvents(); } - - if ($messageLogger = $this->getService('mailer.logger_message_listener')) { - /** @var MessageLoggerListener $messageLogger */ - return $messageLogger->getEvents(); + if ($mailer = $this->getService('mailer.logger_message_listener')) { + /** @var MessageLoggerListener $mailer */ + return $mailer->getEvents(); } - $this->fail("Emails can't be tested without Symfony Mailer service."); } } diff --git a/src/Codeception/Module/Symfony/MimeAssertionsTrait.php b/src/Codeception/Module/Symfony/MimeAssertionsTrait.php index 12e73cd8..ba2ee9ac 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 @@ -20,7 +21,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 +36,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 +51,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 +67,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 +83,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 +98,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 +113,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 +128,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 +143,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 +158,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/ParameterAssertionsTrait.php b/src/Codeception/Module/Symfony/ParameterAssertionsTrait.php index 61c98ddd..ecbbbbc7 100644 --- a/src/Codeception/Module/Symfony/ParameterAssertionsTrait.php +++ b/src/Codeception/Module/Symfony/ParameterAssertionsTrait.php @@ -16,6 +16,7 @@ trait ParameterAssertionsTrait * <?php * $I->grabParameter('app.business_name'); * ``` + * This only works for explicitly set parameters (just using `bind` for Symfony's dependency injection is not enough). */ public function grabParameter(string $parameterName): array|bool|string|int|float|UnitEnum|null { 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 7d26314d..aa7ac9e9 100644 --- a/src/Codeception/Module/Symfony/SessionAssertionsTrait.php +++ b/src/Codeception/Module/Symfony/SessionAssertionsTrait.php @@ -7,9 +7,14 @@ use Symfony\Component\BrowserKit\Cookie; use Symfony\Component\HttpFoundation\Session\SessionInterface; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; +use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; use Symfony\Component\Security\Core\User\UserInterface; use Symfony\Component\Security\Guard\Token\PostAuthenticationGuardToken; +use Symfony\Component\Security\Guard\Token\GuardTokenInterface; +use Symfony\Component\Security\Http\Authenticator\AuthenticatorInterface; +use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge; +use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport; use Symfony\Component\Security\Http\Authenticator\Token\PostAuthenticationToken; use Symfony\Component\Security\Http\Logout\LogoutUrlGenerator; use function is_int; @@ -30,14 +35,22 @@ 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 { - $session = $this->getCurrentSession(); - $roles = $user->getRoles(); + $token = $this->createAuthenticationToken($user, $firewallName); + $this->loginWithToken($token, $firewallName, $firewallContext); + } - $token = $this->createAuthenticationToken($user, $firewallName, $roles); + public function amLoggedInWithToken(TokenInterface $token, string $firewallName = 'main', ?string $firewallContext = null): void + { + $this->loginWithToken($token, $firewallName, $firewallContext); + } + + protected function loginWithToken(TokenInterface $token, string $firewallName, ?string $firewallContext): void + { $this->getTokenStorage()->setToken($token); + $session = $this->getCurrentSession(); $sessionKey = $firewallContext ? "_security_{$firewallContext}" : "_security_{$firewallName}"; $session->set($sessionKey, serialize($token)); $session->save(); @@ -174,6 +187,11 @@ protected function getLogoutUrlGenerator(): ?LogoutUrlGenerator return $this->getService('security.logout_url_generator'); } + protected function getAuthenticator(): ?AuthenticatorInterface + { + return $this->getService(AuthenticatorInterface::class); + } + protected function getCurrentSession(): SessionInterface { $container = $this->_getContainer(); @@ -194,18 +212,24 @@ protected function getSymfonyMajorVersion(): int } /** - * @return UsernamePasswordToken|PostAuthenticationGuardToken|PostAuthenticationToken + * @return TokenInterface|GuardTokenInterface */ - protected function createAuthenticationToken(UserInterface $user, string $firewallName, array $roles) + protected function createAuthenticationToken(UserInterface $user, string $firewallName) { + $roles = $user->getRoles(); if ($this->getSymfonyMajorVersion() < 6) { return $this->config['guard'] ? new PostAuthenticationGuardToken($user, $firewallName, $roles) : new UsernamePasswordToken($user, null, $firewallName, $roles); } - return $this->config['authenticator'] - ? new PostAuthenticationToken($user, $firewallName, $roles) - : new UsernamePasswordToken($user, $firewallName, $roles); + if ($this->config['authenticator']) { + if ($authenticator = $this->getAuthenticator()) { + $passport = new SelfValidatingPassport(new UserBadge($user->getUserIdentifier(), fn () => $user)); + return $authenticator->createToken($passport, $firewallName); + } + return new PostAuthenticationToken($user, $firewallName, $roles); + } + return new UsernamePasswordToken($user, $firewallName, $roles); } } diff --git a/src/Codeception/Module/Symfony/TranslationAssertionsTrait.php b/src/Codeception/Module/Symfony/TranslationAssertionsTrait.php new file mode 100644 index 00000000..7c4c385a --- /dev/null +++ b/src/Codeception/Module/Symfony/TranslationAssertionsTrait.php @@ -0,0 +1,178 @@ +<?php + +declare(strict_types=1); + +namespace Codeception\Module\Symfony; + +use Symfony\Component\Translation\DataCollector\TranslationDataCollector; +use Symfony\Component\VarDumper\Cloner\Data; + +trait TranslationAssertionsTrait +{ + /** + * Asserts that no fallback translations were found. + * + * ```php + * <?php + * $I->dontSeeFallbackTranslations(); + * ``` + */ + public function dontSeeFallbackTranslations(): void + { + $translationCollector = $this->grabTranslationCollector(__FUNCTION__); + $fallbacks = $translationCollector->getCountFallbacks(); + + $this->assertSame( + $fallbacks, + 0, + "Expected no fallback translations, but found {$fallbacks}." + ); + } + + /** + * Asserts that no missing translations were found. + * + * ```php + * <?php + * $I->dontSeeMissingTranslations(); + * ``` + */ + public function dontSeeMissingTranslations(): void + { + $translationCollector = $this->grabTranslationCollector(__FUNCTION__); + $missings = $translationCollector->getCountMissings(); + + $this->assertSame( + $missings, + 0, + "Expected no missing translations, but found {$missings}." + ); + } + + /** + * Grabs the count of defined translations. + * + * ```php + * <?php + * $count = $I->grabDefinedTranslations(); + * ``` + * + * @return int The count of defined translations. + */ + public function grabDefinedTranslationsCount(): int + { + $translationCollector = $this->grabTranslationCollector(__FUNCTION__); + return $translationCollector->getCountDefines(); + } + + /** + * Asserts that there are no missing translations and no fallback translations. + * + * ```php + * <?php + * $I->seeAllTranslationsDefined(); + * ``` + */ + public function seeAllTranslationsDefined(): void + { + $this->dontSeeMissingTranslations(); + $this->dontSeeFallbackTranslations(); + } + + /** + * Asserts that the default locale is the expected one. + * + * ```php + * <?php + * $I->seeDefaultLocaleIs('en'); + * ``` + * + * @param string $expectedLocale The expected default locale + */ + public function seeDefaultLocaleIs(string $expectedLocale): void + { + $translationCollector = $this->grabTranslationCollector(__FUNCTION__); + $locale = $translationCollector->getLocale(); + + $this->assertSame( + $expectedLocale, + $locale, + "Expected default locale '{$expectedLocale}', but found '{$locale}'." + ); + } + + /** + * Asserts that the fallback locales match the expected ones. + * + * ```php + * <?php + * $I->seeFallbackLocalesAre(['es', 'fr']); + * ``` + * + * @param array $expectedLocales The expected fallback locales + */ + public function seeFallbackLocalesAre(array $expectedLocales): void + { + $translationCollector = $this->grabTranslationCollector(__FUNCTION__); + $fallbackLocales = $translationCollector->getFallbackLocales(); + + if ($fallbackLocales instanceof Data) { + $fallbackLocales = $fallbackLocales->getValue(true); + } + + $this->assertSame( + $expectedLocales, + $fallbackLocales, + "Fallback locales do not match expected." + ); + } + + /** + * Asserts that the count of fallback translations is less than the given limit. + * + * ```php + * <?php + * $I->seeFallbackTranslationsCountLessThan(10); + * ``` + * + * @param int $limit Maximum count of fallback translations + */ + public function seeFallbackTranslationsCountLessThan(int $limit): void + { + $translationCollector = $this->grabTranslationCollector(__FUNCTION__); + $fallbacks = $translationCollector->getCountFallbacks(); + + $this->assertLessThan( + $limit, + $fallbacks, + "Expected fewer than {$limit} fallback translations, but found {$fallbacks}." + ); + } + + /** + * Asserts that the count of missing translations is less than the given limit. + * + * ```php + * <?php + * $I->seeMissingTranslationsCountLessThan(5); + * ``` + * + * @param int $limit Maximum count of missing translations + */ + public function seeMissingTranslationsCountLessThan(int $limit): void + { + $translationCollector = $this->grabTranslationCollector(__FUNCTION__); + $missings = $translationCollector->getCountMissings(); + + $this->assertLessThan( + $limit, + $missings, + "Expected fewer than {$limit} missing translations, but found {$missings}." + ); + } + + protected function grabTranslationCollector(string $function): TranslationDataCollector + { + return $this->grabCollector('translation', $function); + } +} diff --git a/src/Codeception/Module/Symfony/ValidatorAssertionsTrait.php b/src/Codeception/Module/Symfony/ValidatorAssertionsTrait.php new file mode 100644 index 00000000..ca82e196 --- /dev/null +++ b/src/Codeception/Module/Symfony/ValidatorAssertionsTrait.php @@ -0,0 +1,106 @@ +<?php + +declare(strict_types=1); + +namespace Codeception\Module\Symfony; + +use Symfony\Component\Validator\ConstraintViolationInterface; +use Symfony\Component\Validator\Validator\ValidatorInterface; + +trait ValidatorAssertionsTrait +{ + /** + * Asserts that the given subject fails validation. + * This assertion does not concern the exact number of violations. + * + * ```php + * <?php + * $I->dontSeeViolatedConstraint($subject); + * $I->dontSeeViolatedConstraint($subject, 'propertyName'); + * $I->dontSeeViolatedConstraint($subject, 'propertyName', 'Symfony\Validator\ConstraintClass'); + * ``` + */ + public function dontSeeViolatedConstraint(mixed $subject, ?string $propertyPath = null, ?string $constraint = null): void + { + $violations = $this->getViolationsForSubject($subject, $propertyPath, $constraint); + $this->assertCount(0, $violations, 'Constraint violations found.'); + } + + /** + * Asserts that the given subject passes validation. + * This assertion does not concern the exact number of violations. + * + * ```php + * <?php + * $I->seeViolatedConstraint($subject); + * $I->seeViolatedConstraint($subject, 'propertyName'); + * $I->seeViolatedConstraint($subject, 'propertyName', 'Symfony\Validator\ConstraintClass'); + * ``` + */ + public function seeViolatedConstraint(mixed $subject, ?string $propertyPath = null, ?string $constraint = null): void + { + $violations = $this->getViolationsForSubject($subject, $propertyPath, $constraint); + $this->assertNotCount(0, $violations, 'No constraint violations found.'); + } + + /** + * Asserts the exact number of violations for the given subject. + * + * ```php + * <?php + * $I->seeViolatedConstraintsCount(3, $subject); + * $I->seeViolatedConstraintsCount(2, $subject, 'propertyName'); + * ``` + */ + public function seeViolatedConstraintsCount(int $expected, mixed $subject, ?string $propertyPath = null, ?string $constraint = null): void + { + $violations = $this->getViolationsForSubject($subject, $propertyPath, $constraint); + $this->assertCount($expected, $violations); + } + + /** + * Asserts that a constraint violation message or a part of it is present in the subject's violations. + * + * ```php + * <?php + * $I->seeViolatedConstraintMessage('too short', $user, 'address'); + * ``` + */ + public function seeViolatedConstraintMessage(string $expected, mixed $subject, string $propertyPath): void + { + $violations = $this->getViolationsForSubject($subject, $propertyPath); + $containsExpected = false; + foreach ($violations as $violation) { + if ($violation->getPropertyPath() === $propertyPath && str_contains($violation->getMessage(), $expected)) { + $containsExpected = true; + break; + } + } + + $this->assertTrue($containsExpected, 'The violation messages do not contain: ' . $expected); + } + + /** @return ConstraintViolationInterface[] */ + protected function getViolationsForSubject(mixed $subject, ?string $propertyPath = null, ?string $constraint = null): array + { + $validator = $this->getValidatorService(); + $violations = $propertyPath ? $validator->validateProperty($subject, $propertyPath) : $validator->validate($subject); + + $violations = iterator_to_array($violations); + + if ($constraint !== null) { + return array_filter( + $violations, + static fn($violation): bool => $violation->getConstraint()::class === $constraint && + ($propertyPath === null || $violation->getPropertyPath() === $propertyPath) + ); + } + + return $violations; + } + + protected function getValidatorService(): ValidatorInterface + { + return $this->grabService(ValidatorInterface::class); + } +}