Skip to content

Commit 27ea512

Browse files
authored
Update code standards (Codeception#106)
1 parent 70abb83 commit 27ea512

11 files changed

+91
-66
lines changed

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ A Codeception module for Symfony framework.
99

1010
## Requirements
1111

12-
* `Symfony 3.4` or higher.
12+
* `Symfony 4.4` or higher.
1313
* `PHP 7.3` or higher.
1414

1515
## Installation

src/Codeception/Lib/Connector/Symfony.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Symfony\Component\HttpFoundation\Response;
1111
use Symfony\Component\HttpKernel\HttpKernelBrowser;
1212
use Symfony\Component\HttpKernel\Kernel;
13+
use Symfony\Component\HttpKernel\Profiler\Profiler;
1314
use function array_keys;
1415
use function codecept_debug;
1516

@@ -79,8 +80,8 @@ public function rebootKernel(): void
7980
{
8081
if ($this->container) {
8182
foreach (array_keys($this->persistentServices) as $serviceName) {
82-
if ($this->container->has($serviceName)) {
83-
$this->persistentServices[$serviceName] = $this->container->get($serviceName);
83+
if ($service = $this->getService($serviceName)) {
84+
$this->persistentServices[$serviceName] = $service;
8485
}
8586
}
8687
}
@@ -95,12 +96,30 @@ public function rebootKernel(): void
9596
$this->container->set($serviceName, $service);
9697
} catch (InvalidArgumentException $e) {
9798
//Private services can't be set in Symfony 4
98-
codecept_debug("[Symfony] Can't set persistent service $serviceName: " . $e->getMessage());
99+
codecept_debug("[Symfony] Can't set persistent service {$serviceName}: " . $e->getMessage());
99100
}
100101
}
101102

103+
if ($profiler = $this->getProfiler()) {
104+
$profiler->enable();
105+
}
106+
}
107+
108+
private function getProfiler(): ?Profiler
109+
{
102110
if ($this->container->has('profiler')) {
103-
$this->container->get('profiler')->enable();
111+
/** @var Profiler $profiler */
112+
$profiler = $this->container->get('profiler');
113+
return $profiler;
114+
}
115+
return null;
116+
}
117+
118+
private function getService(string $serviceName): ?object
119+
{
120+
if ($this->container->has($serviceName)) {
121+
return $this->container->get($serviceName);
104122
}
123+
return null;
105124
}
106125
}

src/Codeception/Module/Symfony.php

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -120,20 +120,18 @@
120120
*/
121121
class Symfony extends Framework implements DoctrineProvider, PartedModule
122122
{
123-
use
124-
BrowserAssertionsTrait,
125-
ConsoleAssertionsTrait,
126-
DoctrineAssertionsTrait,
127-
EventsAssertionsTrait,
128-
FormAssertionsTrait,
129-
MailerAssertionsTrait,
130-
ParameterAssertionsTrait,
131-
RouterAssertionsTrait,
132-
SecurityAssertionsTrait,
133-
ServicesAssertionsTrait,
134-
SessionAssertionsTrait,
135-
TwigAssertionsTrait
136-
;
123+
use BrowserAssertionsTrait;
124+
use ConsoleAssertionsTrait;
125+
use DoctrineAssertionsTrait;
126+
use EventsAssertionsTrait;
127+
use FormAssertionsTrait;
128+
use MailerAssertionsTrait;
129+
use ParameterAssertionsTrait;
130+
use RouterAssertionsTrait;
131+
use SecurityAssertionsTrait;
132+
use ServicesAssertionsTrait;
133+
use SessionAssertionsTrait;
134+
use TwigAssertionsTrait;
137135

138136
/**
139137
* @var Kernel
@@ -219,7 +217,7 @@ public function _after(TestInterface $test): void
219217
parent::_after($test);
220218
}
221219

222-
protected function onReconfigure($settings = []): void
220+
protected function onReconfigure(array $settings = []): void
223221
{
224222
parent::_beforeSuite($settings);
225223
$this->_initialize();
@@ -235,9 +233,10 @@ public function _getEntityManager()
235233
if ($this->kernel === null) {
236234
$this->fail('Symfony module is not loaded');
237235
}
238-
if (!isset($this->permanentServices[$this->config['em_service']])) {
239-
// try to persist configured EM
240-
$this->persistPermanentService($this->config['em_service']);
236+
$emService = $this->config['em_service'];
237+
if (!isset($this->permanentServices[$emService])) {
238+
// Try to persist configured entity manager
239+
$this->persistPermanentService($emService);
241240
$container = $this->_getContainer();
242241
if ($container->has('doctrine')) {
243242
$this->persistPermanentService('doctrine');
@@ -249,7 +248,7 @@ public function _getEntityManager()
249248
$this->persistPermanentService('doctrine.dbal.backend_connection');
250249
}
251250
}
252-
return $this->permanentServices[$this->config['em_service']];
251+
return $this->permanentServices[$emService];
253252
}
254253

255254
/**
@@ -277,19 +276,18 @@ protected function getTestContainer(): ?object
277276

278277
/**
279278
* Attempts to guess the kernel location.
280-
*
281279
* When the Kernel is located, the file is required.
282280
*
283281
* @return string The Kernel class name
284-
* @throws ModuleRequireException|ReflectionException|ModuleException
282+
* @throws ModuleRequireException|ReflectionException
285283
*/
286284
protected function getKernelClass(): string
287285
{
288286
$path = codecept_root_dir() . $this->config['app_path'];
289-
if (!file_exists(codecept_root_dir() . $this->config['app_path'])) {
287+
if (!file_exists($path)) {
290288
throw new ModuleRequireException(
291289
self::class,
292-
"Can't load Kernel from $path.\n"
290+
"Can't load Kernel from {$path}.\n"
293291
. 'Directory does not exists. Use `app_path` parameter to provide valid application path'
294292
);
295293
}
@@ -300,15 +298,12 @@ protected function getKernelClass(): string
300298
if ($results === []) {
301299
throw new ModuleRequireException(
302300
self::class,
303-
"File with Kernel class was not found at $path. "
301+
"File with Kernel class was not found at {$path}.\n"
304302
. 'Specify directory where file with Kernel class for your application is located with `app_path` parameter.'
305303
);
306304
}
307305

308-
if (file_exists(codecept_root_dir() . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php')) {
309-
// ensure autoloader from this dir is loaded
310-
require_once codecept_root_dir() . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
311-
}
306+
$this->requireAdditionalAutoloader();
312307

313308
$filesRealPath = array_map(function ($file) {
314309
require_once $file;
@@ -333,9 +328,6 @@ protected function getKernelClass(): string
333328
);
334329
}
335330

336-
/**
337-
* @return Profile|null
338-
*/
339331
protected function getProfile(): ?Profile
340332
{
341333
/** @var Profiler $profiler */
@@ -451,4 +443,16 @@ protected function getInternalDomains(): array
451443

452444
return array_unique($internalDomains);
453445
}
446+
447+
/**
448+
* Ensures autoloader loading of additional directories.
449+
* It is only required for CI jobs to run correctly.
450+
*/
451+
private function requireAdditionalAutoloader(): void
452+
{
453+
$autoLoader = codecept_root_dir() . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
454+
if (file_exists($autoLoader)) {
455+
require_once $autoLoader;
456+
}
457+
}
454458
}

src/Codeception/Module/Symfony/ConsoleAssertionsTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ trait ConsoleAssertionsTrait
1414
* Run Symfony console command, grab response and return as string.
1515
* Recommended to use for integration or functional testing.
1616
*
17-
* ``` php
17+
* ```php
1818
* <?php
1919
* $result = $I->runSymfonyConsoleCommand('hello:world', ['arg' => 'argValue', 'opt1' => 'optValue'], ['input']);
2020
* ```

src/Codeception/Module/Symfony/EventsAssertionsTrait.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ trait EventsAssertionsTrait
1616
/**
1717
* Make sure events did not fire during the test.
1818
*
19-
* ``` php
19+
* ```php
2020
* <?php
2121
* $I->dontSeeEventTriggered('App\MyEvent');
2222
* $I->dontSeeEventTriggered(new App\Events\MyEvent());
@@ -40,18 +40,18 @@ public function dontSeeEventTriggered($expected): void
4040
$expectedEvent = is_object($expectedEvent) ? get_class($expectedEvent) : $expectedEvent;
4141

4242
foreach ($actual as $actualEvent) {
43-
if (strpos($actualEvent['pretty'], $expectedEvent) === 0) {
43+
if (strpos($actualEvent['pretty'], (string) $expectedEvent) === 0) {
4444
$notTriggered = true;
4545
}
4646
}
47-
$this->assertTrue($notTriggered, "The '$expectedEvent' event triggered");
47+
$this->assertTrue($notTriggered, "The '{$expectedEvent}' event triggered");
4848
}
4949
}
5050

5151
/**
5252
* Make sure events fired during the test.
5353
*
54-
* ``` php
54+
* ```php
5555
* <?php
5656
* $I->seeEventTriggered('App\MyEvent');
5757
* $I->seeEventTriggered(new App\Events\MyEvent());
@@ -79,11 +79,11 @@ public function seeEventTriggered($expected): void
7979
$expectedEvent = is_object($expectedEvent) ? get_class($expectedEvent) : $expectedEvent;
8080

8181
foreach ($actual as $actualEvent) {
82-
if (strpos($actualEvent['pretty'], $expectedEvent) === 0) {
82+
if (strpos($actualEvent['pretty'], (string) $expectedEvent) === 0) {
8383
$triggered = true;
8484
}
8585
}
86-
$this->assertTrue($triggered, "The '$expectedEvent' event did not trigger");
86+
$this->assertTrue($triggered, "The '{$expectedEvent}' event did not trigger");
8787
}
8888
}
8989

src/Codeception/Module/Symfony/FormAssertionsTrait.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ trait FormAssertionsTrait
1515
/**
1616
* Verifies that there are no errors bound to the submitted form.
1717
*
18-
* ``` php
18+
* ```php
1919
* <?php
2020
* $I->dontSeeFormErrors();
2121
* ```
@@ -35,7 +35,7 @@ public function dontSeeFormErrors(): void
3535
* Verifies that a form field has an error.
3636
* You can specify the expected error message as second parameter.
3737
*
38-
* ``` php
38+
* ```php
3939
* <?php
4040
* $I->seeFormErrorMessage('username');
4141
* $I->seeFormErrorMessage('username', 'Username is empty');
@@ -70,11 +70,11 @@ public function seeFormErrorMessage(string $field, ?string $message = null): voi
7070
}
7171

7272
if (!in_array($field, $fields)) {
73-
$this->fail("the field '$field' does not exist in the form.");
73+
$this->fail("the field '{$field}' does not exist in the form.");
7474
}
7575

7676
if (!array_key_exists($field, $errors)) {
77-
$this->fail("No form error message for field '$field'.");
77+
$this->fail("No form error message for field '{$field}'.");
7878
}
7979

8080
if (!$message) {
@@ -97,7 +97,7 @@ public function seeFormErrorMessage(string $field, ?string $message = null): voi
9797
* If you only specify the name of the fields, this method will
9898
* verify that the field contains at least one error of any type:
9999
*
100-
* ``` php
100+
* ```php
101101
* <?php
102102
* $I->seeFormErrorMessages(['telephone', 'address']);
103103
* ```
@@ -110,7 +110,7 @@ public function seeFormErrorMessage(string $field, ?string $message = null): voi
110110
* is contained in the actual error message, that is,
111111
* you can specify either the entire error message or just a part of it:
112112
*
113-
* ``` php
113+
* ```php
114114
* <?php
115115
* $I->seeFormErrorMessages([
116116
* 'address' => 'The address is too long'
@@ -123,7 +123,7 @@ public function seeFormErrorMessage(string $field, ?string $message = null): voi
123123
* or you can directly omit the value of that field. If that is the case,
124124
* it will be validated that that field has at least one error of any type:
125125
*
126-
* ``` php
126+
* ```php
127127
* <?php
128128
* $I->seeFormErrorMessages([
129129
* 'telephone' => 'too short',
@@ -148,7 +148,7 @@ public function seeFormErrorMessages(array $expectedErrors): void
148148
/**
149149
* Verifies that there are one or more errors bound to the submitted form.
150150
*
151-
* ``` php
151+
* ```php
152152
* <?php
153153
* $I->seeFormHasErrors();
154154
* ```

src/Codeception/Module/Symfony/RouterAssertionsTrait.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ trait RouterAssertionsTrait
2020
/**
2121
* Opens web page by action name
2222
*
23-
* ``` php
23+
* ```php
2424
* <?php
2525
* $I->amOnAction('PostController::index');
2626
* $I->amOnAction('HomeController');
@@ -54,10 +54,10 @@ public function amOnAction(string $action, array $params = []): void
5454
/**
5555
* Opens web page using route name and parameters.
5656
*
57-
* ``` php
57+
* ```php
5858
* <?php
5959
* $I->amOnRoute('posts.create');
60-
* $I->amOnRoute('posts.show', array('id' => 34));
60+
* $I->amOnRoute('posts.show', ['id' => 34]);
6161
* ```
6262
*
6363
* @param string $routeName
@@ -84,7 +84,7 @@ public function invalidateCachedRouter(): void
8484
/**
8585
* Checks that current page matches action
8686
*
87-
* ``` php
87+
* ```php
8888
* <?php
8989
* $I->seeCurrentActionIs('PostController::index');
9090
* $I->seeCurrentActionIs('HomeController');
@@ -105,20 +105,20 @@ public function seeCurrentActionIs(string $action): void
105105
$request = $this->client->getRequest();
106106
$currentActionFqcn = $request->attributes->get('_controller');
107107

108-
$this->assertStringEndsWith($action, $currentActionFqcn, "Current action is '$currentActionFqcn'.");
108+
$this->assertStringEndsWith($action, $currentActionFqcn, "Current action is '{$currentActionFqcn}'.");
109109
return;
110110
}
111111
}
112-
$this->fail("Action '$action' does not exist");
112+
$this->fail("Action '{$action}' does not exist");
113113
}
114114

115115
/**
116116
* Checks that current url matches route.
117117
*
118-
* ``` php
118+
* ```php
119119
* <?php
120120
* $I->seeCurrentRouteIs('posts.index');
121-
* $I->seeCurrentRouteIs('posts.show', array('id' => 8));
121+
* $I->seeCurrentRouteIs('posts.show', ['id' => 8]);
122122
* ```
123123
*
124124
* @param string $routeName
@@ -147,7 +147,7 @@ public function seeCurrentRouteIs(string $routeName, array $params = []): void
147147
* Checks that current url matches route.
148148
* Unlike seeCurrentRouteIs, this can matches without exact route parameters
149149
*
150-
* ``` php
150+
* ```php
151151
* <?php
152152
* $I->seeInCurrentRoute('my_blog_pages');
153153
* ```

src/Codeception/Module/Symfony/SecurityAssertionsTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public function seeUserHasRole(string $role): void
137137
/**
138138
* Verifies that the current user has multiple roles
139139
*
140-
* ``` php
140+
* ```php
141141
* <?php
142142
* $I->seeUserHasRoles(['ROLE_USER', 'ROLE_ADMIN']);
143143
* ```

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

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

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


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy