From d33f3b15a08dbea6c23bd2c66d8a733dcd71f04d Mon Sep 17 00:00:00 2001 From: Tavo Nieves J Date: Wed, 27 Jan 2021 14:39:41 -0500 Subject: [PATCH] Update code standards --- readme.md | 2 +- src/Codeception/Lib/Connector/Symfony.php | 27 ++++++-- src/Codeception/Module/Symfony.php | 66 ++++++++++--------- .../Module/Symfony/ConsoleAssertionsTrait.php | 2 +- .../Module/Symfony/EventsAssertionsTrait.php | 12 ++-- .../Module/Symfony/FormAssertionsTrait.php | 16 ++--- .../Module/Symfony/RouterAssertionsTrait.php | 18 ++--- .../Symfony/SecurityAssertionsTrait.php | 2 +- .../Symfony/ServicesAssertionsTrait.php | 7 +- .../Module/Symfony/SessionAssertionsTrait.php | 4 +- .../Module/Symfony/TwigAssertionsTrait.php | 1 + 11 files changed, 91 insertions(+), 66 deletions(-) diff --git a/readme.md b/readme.md index c3f2b1be..cd32b1d6 100644 --- a/readme.md +++ b/readme.md @@ -9,7 +9,7 @@ A Codeception module for Symfony framework. ## Requirements -* `Symfony 3.4` or higher. +* `Symfony 4.4` or higher. * `PHP 7.3` or higher. ## Installation diff --git a/src/Codeception/Lib/Connector/Symfony.php b/src/Codeception/Lib/Connector/Symfony.php index af6f3334..a50d1214 100644 --- a/src/Codeception/Lib/Connector/Symfony.php +++ b/src/Codeception/Lib/Connector/Symfony.php @@ -10,6 +10,7 @@ use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\HttpKernelBrowser; use Symfony\Component\HttpKernel\Kernel; +use Symfony\Component\HttpKernel\Profiler\Profiler; use function array_keys; use function codecept_debug; @@ -79,8 +80,8 @@ public function rebootKernel(): void { if ($this->container) { foreach (array_keys($this->persistentServices) as $serviceName) { - if ($this->container->has($serviceName)) { - $this->persistentServices[$serviceName] = $this->container->get($serviceName); + if ($service = $this->getService($serviceName)) { + $this->persistentServices[$serviceName] = $service; } } } @@ -95,12 +96,30 @@ public function rebootKernel(): void $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()); + codecept_debug("[Symfony] Can't set persistent service {$serviceName}: " . $e->getMessage()); } } + if ($profiler = $this->getProfiler()) { + $profiler->enable(); + } + } + + private function getProfiler(): ?Profiler + { if ($this->container->has('profiler')) { - $this->container->get('profiler')->enable(); + /** @var Profiler $profiler */ + $profiler = $this->container->get('profiler'); + return $profiler; + } + return null; + } + + private function getService(string $serviceName): ?object + { + if ($this->container->has($serviceName)) { + return $this->container->get($serviceName); } + return null; } } diff --git a/src/Codeception/Module/Symfony.php b/src/Codeception/Module/Symfony.php index f1327e22..9d0269ac 100644 --- a/src/Codeception/Module/Symfony.php +++ b/src/Codeception/Module/Symfony.php @@ -120,20 +120,18 @@ */ class Symfony extends Framework implements DoctrineProvider, PartedModule { - use - BrowserAssertionsTrait, - ConsoleAssertionsTrait, - DoctrineAssertionsTrait, - EventsAssertionsTrait, - FormAssertionsTrait, - MailerAssertionsTrait, - ParameterAssertionsTrait, - RouterAssertionsTrait, - SecurityAssertionsTrait, - ServicesAssertionsTrait, - SessionAssertionsTrait, - TwigAssertionsTrait - ; + use BrowserAssertionsTrait; + use ConsoleAssertionsTrait; + use DoctrineAssertionsTrait; + use EventsAssertionsTrait; + use FormAssertionsTrait; + use MailerAssertionsTrait; + use ParameterAssertionsTrait; + use RouterAssertionsTrait; + use SecurityAssertionsTrait; + use ServicesAssertionsTrait; + use SessionAssertionsTrait; + use TwigAssertionsTrait; /** * @var Kernel @@ -219,7 +217,7 @@ public function _after(TestInterface $test): void parent::_after($test); } - protected function onReconfigure($settings = []): void + protected function onReconfigure(array $settings = []): void { parent::_beforeSuite($settings); $this->_initialize(); @@ -235,9 +233,10 @@ public function _getEntityManager() if ($this->kernel === null) { $this->fail('Symfony module is not loaded'); } - if (!isset($this->permanentServices[$this->config['em_service']])) { - // try to persist configured EM - $this->persistPermanentService($this->config['em_service']); + $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'); @@ -249,7 +248,7 @@ public function _getEntityManager() $this->persistPermanentService('doctrine.dbal.backend_connection'); } } - return $this->permanentServices[$this->config['em_service']]; + return $this->permanentServices[$emService]; } /** @@ -277,19 +276,18 @@ protected function getTestContainer(): ?object /** * Attempts to guess the kernel location. - * * When the Kernel is located, the file is required. * * @return string The Kernel class name - * @throws ModuleRequireException|ReflectionException|ModuleException + * @throws ModuleRequireException|ReflectionException */ protected function getKernelClass(): string { $path = codecept_root_dir() . $this->config['app_path']; - if (!file_exists(codecept_root_dir() . $this->config['app_path'])) { + if (!file_exists($path)) { throw new ModuleRequireException( self::class, - "Can't load Kernel from $path.\n" + "Can't load Kernel from {$path}.\n" . 'Directory does not exists. Use `app_path` parameter to provide valid application path' ); } @@ -300,15 +298,12 @@ protected function getKernelClass(): string if ($results === []) { throw new ModuleRequireException( self::class, - "File with Kernel class was not found at $path. " + "File with Kernel class was not found at {$path}.\n" . 'Specify directory where file with Kernel class for your application is located with `app_path` parameter.' ); } - if (file_exists(codecept_root_dir() . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php')) { - // ensure autoloader from this dir is loaded - require_once codecept_root_dir() . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php'; - } + $this->requireAdditionalAutoloader(); $filesRealPath = array_map(function ($file) { require_once $file; @@ -333,9 +328,6 @@ protected function getKernelClass(): string ); } - /** - * @return Profile|null - */ protected function getProfile(): ?Profile { /** @var Profiler $profiler */ @@ -451,4 +443,16 @@ protected function getInternalDomains(): array return array_unique($internalDomains); } + + /** + * Ensures autoloader loading of additional directories. + * It is only required for CI jobs to run correctly. + */ + private function requireAdditionalAutoloader(): void + { + $autoLoader = codecept_root_dir() . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php'; + if (file_exists($autoLoader)) { + require_once $autoLoader; + } + } } diff --git a/src/Codeception/Module/Symfony/ConsoleAssertionsTrait.php b/src/Codeception/Module/Symfony/ConsoleAssertionsTrait.php index c95d7fd9..de61419d 100644 --- a/src/Codeception/Module/Symfony/ConsoleAssertionsTrait.php +++ b/src/Codeception/Module/Symfony/ConsoleAssertionsTrait.php @@ -14,7 +14,7 @@ trait ConsoleAssertionsTrait * Run Symfony console command, grab response and return as string. * Recommended to use for integration or functional testing. * - * ``` php + * ```php * runSymfonyConsoleCommand('hello:world', ['arg' => 'argValue', 'opt1' => 'optValue'], ['input']); * ``` diff --git a/src/Codeception/Module/Symfony/EventsAssertionsTrait.php b/src/Codeception/Module/Symfony/EventsAssertionsTrait.php index 8a8e9e3e..272b607c 100644 --- a/src/Codeception/Module/Symfony/EventsAssertionsTrait.php +++ b/src/Codeception/Module/Symfony/EventsAssertionsTrait.php @@ -16,7 +16,7 @@ trait EventsAssertionsTrait /** * Make sure events did not fire during the test. * - * ``` php + * ```php * dontSeeEventTriggered('App\MyEvent'); * $I->dontSeeEventTriggered(new App\Events\MyEvent()); @@ -40,18 +40,18 @@ public function dontSeeEventTriggered($expected): void $expectedEvent = is_object($expectedEvent) ? get_class($expectedEvent) : $expectedEvent; foreach ($actual as $actualEvent) { - if (strpos($actualEvent['pretty'], $expectedEvent) === 0) { + if (strpos($actualEvent['pretty'], (string) $expectedEvent) === 0) { $notTriggered = true; } } - $this->assertTrue($notTriggered, "The '$expectedEvent' event triggered"); + $this->assertTrue($notTriggered, "The '{$expectedEvent}' event triggered"); } } /** * Make sure events fired during the test. * - * ``` php + * ```php * seeEventTriggered('App\MyEvent'); * $I->seeEventTriggered(new App\Events\MyEvent()); @@ -79,11 +79,11 @@ public function seeEventTriggered($expected): void $expectedEvent = is_object($expectedEvent) ? get_class($expectedEvent) : $expectedEvent; foreach ($actual as $actualEvent) { - if (strpos($actualEvent['pretty'], $expectedEvent) === 0) { + if (strpos($actualEvent['pretty'], (string) $expectedEvent) === 0) { $triggered = true; } } - $this->assertTrue($triggered, "The '$expectedEvent' event did not trigger"); + $this->assertTrue($triggered, "The '{$expectedEvent}' event did not trigger"); } } diff --git a/src/Codeception/Module/Symfony/FormAssertionsTrait.php b/src/Codeception/Module/Symfony/FormAssertionsTrait.php index a00b6e28..1ce10023 100644 --- a/src/Codeception/Module/Symfony/FormAssertionsTrait.php +++ b/src/Codeception/Module/Symfony/FormAssertionsTrait.php @@ -15,7 +15,7 @@ trait FormAssertionsTrait /** * Verifies that there are no errors bound to the submitted form. * - * ``` php + * ```php * dontSeeFormErrors(); * ``` @@ -35,7 +35,7 @@ public function dontSeeFormErrors(): void * Verifies that a form field has an error. * You can specify the expected error message as second parameter. * - * ``` php + * ```php * seeFormErrorMessage('username'); * $I->seeFormErrorMessage('username', 'Username is empty'); @@ -70,11 +70,11 @@ public function seeFormErrorMessage(string $field, ?string $message = null): voi } if (!in_array($field, $fields)) { - $this->fail("the field '$field' does not exist in the form."); + $this->fail("the field '{$field}' does not exist in the form."); } if (!array_key_exists($field, $errors)) { - $this->fail("No form error message for field '$field'."); + $this->fail("No form error message for field '{$field}'."); } if (!$message) { @@ -97,7 +97,7 @@ public function seeFormErrorMessage(string $field, ?string $message = null): voi * If you only specify the name of the fields, this method will * verify that the field contains at least one error of any type: * - * ``` php + * ```php * seeFormErrorMessages(['telephone', 'address']); * ``` @@ -110,7 +110,7 @@ public function seeFormErrorMessage(string $field, ?string $message = null): voi * is contained in the actual error message, that is, * you can specify either the entire error message or just a part of it: * - * ``` php + * ```php * seeFormErrorMessages([ * 'address' => 'The address is too long' @@ -123,7 +123,7 @@ public function seeFormErrorMessage(string $field, ?string $message = null): voi * or you can directly omit the value of that field. If that is the case, * it will be validated that that field has at least one error of any type: * - * ``` php + * ```php * seeFormErrorMessages([ * 'telephone' => 'too short', @@ -148,7 +148,7 @@ public function seeFormErrorMessages(array $expectedErrors): void /** * Verifies that there are one or more errors bound to the submitted form. * - * ``` php + * ```php * seeFormHasErrors(); * ``` diff --git a/src/Codeception/Module/Symfony/RouterAssertionsTrait.php b/src/Codeception/Module/Symfony/RouterAssertionsTrait.php index 41d9d920..dafbd87f 100644 --- a/src/Codeception/Module/Symfony/RouterAssertionsTrait.php +++ b/src/Codeception/Module/Symfony/RouterAssertionsTrait.php @@ -20,7 +20,7 @@ trait RouterAssertionsTrait /** * Opens web page by action name * - * ``` php + * ```php * amOnAction('PostController::index'); * $I->amOnAction('HomeController'); @@ -54,10 +54,10 @@ public function amOnAction(string $action, array $params = []): void /** * Opens web page using route name and parameters. * - * ``` php + * ```php * amOnRoute('posts.create'); - * $I->amOnRoute('posts.show', array('id' => 34)); + * $I->amOnRoute('posts.show', ['id' => 34]); * ``` * * @param string $routeName @@ -84,7 +84,7 @@ public function invalidateCachedRouter(): void /** * Checks that current page matches action * - * ``` php + * ```php * seeCurrentActionIs('PostController::index'); * $I->seeCurrentActionIs('HomeController'); @@ -105,20 +105,20 @@ public function seeCurrentActionIs(string $action): void $request = $this->client->getRequest(); $currentActionFqcn = $request->attributes->get('_controller'); - $this->assertStringEndsWith($action, $currentActionFqcn, "Current action is '$currentActionFqcn'."); + $this->assertStringEndsWith($action, $currentActionFqcn, "Current action is '{$currentActionFqcn}'."); return; } } - $this->fail("Action '$action' does not exist"); + $this->fail("Action '{$action}' does not exist"); } /** * Checks that current url matches route. * - * ``` php + * ```php * seeCurrentRouteIs('posts.index'); - * $I->seeCurrentRouteIs('posts.show', array('id' => 8)); + * $I->seeCurrentRouteIs('posts.show', ['id' => 8]); * ``` * * @param string $routeName @@ -147,7 +147,7 @@ public function seeCurrentRouteIs(string $routeName, array $params = []): void * Checks that current url matches route. * Unlike seeCurrentRouteIs, this can matches without exact route parameters * - * ``` php + * ```php * seeInCurrentRoute('my_blog_pages'); * ``` diff --git a/src/Codeception/Module/Symfony/SecurityAssertionsTrait.php b/src/Codeception/Module/Symfony/SecurityAssertionsTrait.php index c1b7febf..8ae73d00 100644 --- a/src/Codeception/Module/Symfony/SecurityAssertionsTrait.php +++ b/src/Codeception/Module/Symfony/SecurityAssertionsTrait.php @@ -137,7 +137,7 @@ public function seeUserHasRole(string $role): void /** * Verifies that the current user has multiple roles * - * ``` php + * ```php * seeUserHasRoles(['ROLE_USER', 'ROLE_ADMIN']); * ``` diff --git a/src/Codeception/Module/Symfony/ServicesAssertionsTrait.php b/src/Codeception/Module/Symfony/ServicesAssertionsTrait.php index 484c0f82..8d172c3b 100644 --- a/src/Codeception/Module/Symfony/ServicesAssertionsTrait.php +++ b/src/Codeception/Module/Symfony/ServicesAssertionsTrait.php @@ -10,10 +10,11 @@ trait ServicesAssertionsTrait { /** * Grabs a service from the Symfony dependency injection container (DIC). - * In "test" environment, Symfony uses a special `test.service_container`, see https://symfony.com/doc/current/testing.html#accessing-the-container + * In "test" environment, Symfony uses a special `test.service_container`. + * See the "[Accessing the Container](https://symfony.com/doc/current/testing.html#accessing-the-container)" documentation. * Services that aren't injected somewhere into your app, need to be defined as `public` to be accessible by Codeception. * - * ``` php + * ```php * grabService('doctrine'); * ``` @@ -25,7 +26,7 @@ trait ServicesAssertionsTrait public function grabService(string $serviceId): object { if (!$service = $this->getService($serviceId)) { - $this->fail("Service $serviceId is not available in container. + $this->fail("Service {$serviceId} is not available in container. If the service isn't injected anywhere in your app, you need to set it to `public` in your `config/services_test.php`/`.yaml`, see https://symfony.com/doc/current/testing.html#accessing-the-container"); } diff --git a/src/Codeception/Module/Symfony/SessionAssertionsTrait.php b/src/Codeception/Module/Symfony/SessionAssertionsTrait.php index e7f47ba8..7af446ec 100644 --- a/src/Codeception/Module/Symfony/SessionAssertionsTrait.php +++ b/src/Codeception/Module/Symfony/SessionAssertionsTrait.php @@ -72,7 +72,7 @@ public function dontSeeInSession(string $attribute, $value = null): void if (null === $value) { if ($session->has($attribute)) { - $this->fail("Session attribute with name '$attribute' does exist"); + $this->fail("Session attribute with name '{$attribute}' does exist"); } } else { @@ -129,7 +129,7 @@ public function seeInSession(string $attribute, $value = null): void $session = $this->grabSessionService(); if (!$session->has($attribute)) { - $this->fail("No session attribute with name '$attribute'"); + $this->fail("No session attribute with name '{$attribute}'"); } if (null !== $value) { diff --git a/src/Codeception/Module/Symfony/TwigAssertionsTrait.php b/src/Codeception/Module/Symfony/TwigAssertionsTrait.php index 80ac4c29..56eaeb94 100644 --- a/src/Codeception/Module/Symfony/TwigAssertionsTrait.php +++ b/src/Codeception/Module/Symfony/TwigAssertionsTrait.php @@ -5,6 +5,7 @@ namespace Codeception\Module\Symfony; use Symfony\Bridge\Twig\DataCollector\TwigDataCollector; +use function array_key_first; trait TwigAssertionsTrait {