Skip to content

Update code standards #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 23 additions & 4 deletions src/Codeception/Lib/Connector/Symfony.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
}
}
Expand All @@ -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;
}
}
66 changes: 35 additions & 31 deletions src/Codeception/Module/Symfony.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand All @@ -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');
Expand All @@ -249,7 +248,7 @@ public function _getEntityManager()
$this->persistPermanentService('doctrine.dbal.backend_connection');
}
}
return $this->permanentServices[$this->config['em_service']];
return $this->permanentServices[$emService];
}

/**
Expand Down Expand Up @@ -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'
);
}
Expand All @@ -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;
Expand All @@ -333,9 +328,6 @@ protected function getKernelClass(): string
);
}

/**
* @return Profile|null
*/
protected function getProfile(): ?Profile
{
/** @var Profiler $profiler */
Expand Down Expand Up @@ -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;
}
}
}
2 changes: 1 addition & 1 deletion src/Codeception/Module/Symfony/ConsoleAssertionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
* <?php
* $result = $I->runSymfonyConsoleCommand('hello:world', ['arg' => 'argValue', 'opt1' => 'optValue'], ['input']);
* ```
Expand Down
12 changes: 6 additions & 6 deletions src/Codeception/Module/Symfony/EventsAssertionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ trait EventsAssertionsTrait
/**
* Make sure events did not fire during the test.
*
* ``` php
* ```php
* <?php
* $I->dontSeeEventTriggered('App\MyEvent');
* $I->dontSeeEventTriggered(new App\Events\MyEvent());
Expand All @@ -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
* <?php
* $I->seeEventTriggered('App\MyEvent');
* $I->seeEventTriggered(new App\Events\MyEvent());
Expand Down Expand Up @@ -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");
}
}

Expand Down
16 changes: 8 additions & 8 deletions src/Codeception/Module/Symfony/FormAssertionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ trait FormAssertionsTrait
/**
* Verifies that there are no errors bound to the submitted form.
*
* ``` php
* ```php
* <?php
* $I->dontSeeFormErrors();
* ```
Expand All @@ -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
* <?php
* $I->seeFormErrorMessage('username');
* $I->seeFormErrorMessage('username', 'Username is empty');
Expand Down Expand Up @@ -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) {
Expand All @@ -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
* <?php
* $I->seeFormErrorMessages(['telephone', 'address']);
* ```
Expand All @@ -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
* <?php
* $I->seeFormErrorMessages([
* 'address' => 'The address is too long'
Expand All @@ -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
* <?php
* $I->seeFormErrorMessages([
* 'telephone' => 'too short',
Expand All @@ -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
* <?php
* $I->seeFormHasErrors();
* ```
Expand Down
18 changes: 9 additions & 9 deletions src/Codeception/Module/Symfony/RouterAssertionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ trait RouterAssertionsTrait
/**
* Opens web page by action name
*
* ``` php
* ```php
* <?php
* $I->amOnAction('PostController::index');
* $I->amOnAction('HomeController');
Expand Down Expand Up @@ -54,10 +54,10 @@ public function amOnAction(string $action, array $params = []): void
/**
* Opens web page using route name and parameters.
*
* ``` php
* ```php
* <?php
* $I->amOnRoute('posts.create');
* $I->amOnRoute('posts.show', array('id' => 34));
* $I->amOnRoute('posts.show', ['id' => 34]);
* ```
*
* @param string $routeName
Expand All @@ -84,7 +84,7 @@ public function invalidateCachedRouter(): void
/**
* Checks that current page matches action
*
* ``` php
* ```php
* <?php
* $I->seeCurrentActionIs('PostController::index');
* $I->seeCurrentActionIs('HomeController');
Expand All @@ -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
* <?php
* $I->seeCurrentRouteIs('posts.index');
* $I->seeCurrentRouteIs('posts.show', array('id' => 8));
* $I->seeCurrentRouteIs('posts.show', ['id' => 8]);
* ```
*
* @param string $routeName
Expand Down Expand Up @@ -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
* <?php
* $I->seeInCurrentRoute('my_blog_pages');
* ```
Expand Down
2 changes: 1 addition & 1 deletion src/Codeception/Module/Symfony/SecurityAssertionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public function seeUserHasRole(string $role): void
/**
* Verifies that the current user has multiple roles
*
* ``` php
* ```php
* <?php
* $I->seeUserHasRoles(['ROLE_USER', 'ROLE_ADMIN']);
* ```
Expand Down
Loading