Skip to content

Commit cb13340

Browse files
authored
Various improvements (#171)
* Traits improvements * Optimize imports
1 parent beccd5e commit cb13340

12 files changed

+51
-87
lines changed

src/Codeception/Module/Symfony/BrowserAssertionsTrait.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ trait BrowserAssertionsTrait
2323
* // Perform other requests
2424
*
2525
* ```
26-
*
2726
*/
2827
public function rebootClientKernel(): void
2928
{

src/Codeception/Module/Symfony/ConsoleAssertionsTrait.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,12 @@ public function runSymfonyConsoleCommand(string $command, array $parameters = []
4040
$this->assertSame(
4141
$expectedExitCode,
4242
$exitCode,
43-
'Command did not exit with code ' . $expectedExitCode
44-
. ' but with ' . $exitCode . ': ' . $output
43+
sprintf(
44+
'Command did not exit with code %d but with %d: %s',
45+
$expectedExitCode,
46+
$exitCode,
47+
$output
48+
)
4549
);
4650

4751
return $output;

src/Codeception/Module/Symfony/DoctrineAssertionsTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use Doctrine\ORM\EntityRepository;
88
use function class_exists;
9-
use function get_class;
109
use function interface_exists;
1110
use function is_object;
1211
use function is_string;
@@ -66,6 +65,7 @@ public function grabRepository(object|string $mixed): ?EntityRepository
6665
$getRepo = function () use ($mixed, $entityRepoClass, $isNotARepo): ?EntityRepository {
6766
if (!$repo = $this->grabService($mixed)) return null;
6867

68+
/** @var EntityRepository $repo */
6969
if (!$repo instanceof $entityRepoClass) {
7070
$isNotARepo();
7171
return null;

src/Codeception/Module/Symfony/EventsAssertionsTrait.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@
66

77
use Symfony\Component\HttpKernel\DataCollector\EventDataCollector;
88
use Symfony\Component\VarDumper\Cloner\Data;
9-
use function get_class;
109
use function is_array;
1110
use function is_object;
12-
use function strpos;
1311

1412
trait EventsAssertionsTrait
1513
{
@@ -34,7 +32,6 @@ public function dontSeeOrphanEvent(array|object|string $expected = null): void
3432
{
3533
$eventCollector = $this->grabEventCollector(__FUNCTION__);
3634

37-
/** @var Data $data */
3835
$data = $eventCollector->getOrphanedEvents();
3936
$expected = is_array($expected) ? $expected : [$expected];
4037

@@ -80,7 +77,6 @@ public function dontSeeEventListenerIsCalled(array|object|string $expected): voi
8077
{
8178
$eventCollector = $this->grabEventCollector(__FUNCTION__);
8279

83-
/** @var Data $data */
8480
$data = $eventCollector->getCalledListeners();
8581
$expected = is_array($expected) ? $expected : [$expected];
8682

@@ -107,7 +103,6 @@ public function seeOrphanEvent(array|object|string $expected): void
107103
{
108104
$eventCollector = $this->grabEventCollector(__FUNCTION__);
109105

110-
/** @var Data $data */
111106
$data = $eventCollector->getOrphanedEvents();
112107
$expected = is_array($expected) ? $expected : [$expected];
113108

@@ -149,7 +144,6 @@ public function seeEventListenerIsCalled(array|object|string $expected): void
149144
{
150145
$eventCollector = $this->grabEventCollector(__FUNCTION__);
151146

152-
/** @var Data $data */
153147
$data = $eventCollector->getCalledListeners();
154148
$expected = is_array($expected) ? $expected : [$expected];
155149

src/Codeception/Module/Symfony/FormAssertionsTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function seeFormErrorMessage(string $field, string $message = null): void
5151
$formCollector = $this->grabFormCollector(__FUNCTION__);
5252

5353
if (!$forms = $formCollector->getData()->getValue(true)['forms']) {
54-
$this->fail('There are no forms on the current page.');
54+
$this->fail('No forms found on the current page.');
5555
}
5656

5757
$fields = [];
@@ -73,7 +73,7 @@ public function seeFormErrorMessage(string $field, string $message = null): void
7373
}
7474

7575
if (!in_array($field, $fields)) {
76-
$this->fail("the field '{$field}' does not exist in the form.");
76+
$this->fail("The field '{$field}' does not exist in the form.");
7777
}
7878

7979
if (!array_key_exists($field, $errors)) {

src/Codeception/Module/Symfony/MailerAssertionsTrait.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,11 @@ public function seeEmailIsSent(int $expectedCount = 1): void
5656
*/
5757
public function grabLastSentEmail(): ?Email
5858
{
59+
/** @var Email[] $emails */
5960
$emails = $this->getMessageMailerEvents()->getMessages();
60-
/** @var Email|false $lastEmail */
61-
if ($lastEmail = end($emails)) {
62-
return $lastEmail;
63-
}
61+
$lastEmail = end($emails);
6462

65-
return null;
63+
return $lastEmail ?: null;
6664
}
6765

6866
/**

src/Codeception/Module/Symfony/ParameterAssertionsTrait.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ trait ParameterAssertionsTrait
1616
* $I->grabParameter('app.business_name');
1717
* ```
1818
*
19-
* @param string $name
19+
* @param string $parameterName
2020
* @return array|bool|float|int|string|null
2121
*/
22-
public function grabParameter(string $name)
22+
public function grabParameter(string $parameterName)
2323
{
2424
$parameterBag = $this->grabParameterBagService();
25-
return $parameterBag->get($name);
25+
return $parameterBag->get($parameterName);
2626
}
2727

2828
protected function grabParameterBagService(): ParameterBagInterface

src/Codeception/Module/Symfony/RouterAssertionsTrait.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
use function array_merge;
1212
use function explode;
1313
use function sprintf;
14-
use function strlen;
15-
use function substr_compare;
1614

1715
trait RouterAssertionsTrait
1816
{
@@ -32,7 +30,6 @@ trait RouterAssertionsTrait
3230
public function amOnAction(string $action, array $params = []): void
3331
{
3432
$router = $this->grabRouterService();
35-
3633
$routes = $router->getRouteCollection()->getIterator();
3734

3835
foreach ($routes as $route) {
@@ -66,7 +63,7 @@ public function amOnRoute(string $routeName, array $params = []): void
6663
{
6764
$router = $this->grabRouterService();
6865
if ($router->getRouteCollection()->get($routeName) === null) {
69-
$this->fail(sprintf('Route with name "%s" does not exists.', $routeName));
66+
$this->fail(sprintf('Route with name "%s" does not exist.', $routeName));
7067
}
7168

7269
$url = $router->generate($routeName, $params);
@@ -95,7 +92,6 @@ public function invalidateCachedRouter(): void
9592
public function seeCurrentActionIs(string $action): void
9693
{
9794
$router = $this->grabRouterService();
98-
9995
$routes = $router->getRouteCollection()->getIterator();
10096

10197
foreach ($routes as $route) {
@@ -128,7 +124,7 @@ public function seeCurrentRouteIs(string $routeName, array $params = []): void
128124
{
129125
$router = $this->grabRouterService();
130126
if ($router->getRouteCollection()->get($routeName) === null) {
131-
$this->fail(sprintf('Route with name "%s" does not exists.', $routeName));
127+
$this->fail(sprintf('Route with name "%s" does not exist.', $routeName));
132128
}
133129

134130
$uri = explode('?', $this->grabFromCurrentUrl())[0];
@@ -160,7 +156,7 @@ public function seeInCurrentRoute(string $routeName): void
160156
{
161157
$router = $this->grabRouterService();
162158
if ($router->getRouteCollection()->get($routeName) === null) {
163-
$this->fail(sprintf('Route with name "%s" does not exists.', $routeName));
159+
$this->fail(sprintf('Route with name "%s" does not exist.', $routeName));
164160
}
165161

166162
$uri = explode('?', $this->grabFromCurrentUrl())[0];

src/Codeception/Module/Symfony/ServicesAssertionsTrait.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ trait ServicesAssertionsTrait
2020
* ```
2121
*
2222
* @part services
23-
* @param string $serviceId
2423
*/
2524
public function grabService(string $serviceId): object
2625
{
@@ -69,15 +68,10 @@ public function persistPermanentService(string $serviceName): void
6968
*/
7069
public function unpersistService(string $serviceName): void
7170
{
72-
if (isset($this->persistentServices[$serviceName])) {
73-
unset($this->persistentServices[$serviceName]);
74-
}
75-
76-
if (isset($this->permanentServices[$serviceName])) {
77-
unset($this->permanentServices[$serviceName]);
78-
}
71+
unset($this->persistentServices[$serviceName]);
72+
unset($this->permanentServices[$serviceName]);
7973

80-
if ($this->client instanceof SymfonyConnector && isset($this->client->persistentServices[$serviceName])) {
74+
if ($this->client instanceof SymfonyConnector) {
8175
unset($this->client->persistentServices[$serviceName]);
8276
}
8377
}

src/Codeception/Module/Symfony/SessionAssertionsTrait.php

Lines changed: 28 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -29,37 +29,17 @@ trait SessionAssertionsTrait
2929
* ]);
3030
* $I->amLoggedInAs($user);
3131
* ```
32-
*
33-
* @param UserInterface $user
34-
* @param string $firewallName
35-
* @param null $firewallContext
3632
*/
37-
public function amLoggedInAs(UserInterface $user, string $firewallName = 'main', $firewallContext = null): void
33+
public function amLoggedInAs(UserInterface $user, string $firewallName = 'main', string $firewallContext = null): void
3834
{
3935
$session = $this->getCurrentSession();
36+
$roles = $user->getRoles();
4037

41-
if ($this->getSymfonyMajorVersion() < 6) {
42-
if ($this->config['guard']) {
43-
$token = new PostAuthenticationGuardToken($user, $firewallName, $user->getRoles());
44-
} else {
45-
$token = new UsernamePasswordToken($user, null, $firewallName, $user->getRoles());
46-
}
47-
} else {
48-
if ($this->config['authenticator']) {
49-
$token = new PostAuthenticationToken($user, $firewallName, $user->getRoles());
50-
} else {
51-
$token = new UsernamePasswordToken($user, $firewallName, $user->getRoles());
52-
}
53-
}
54-
38+
$token = $this->createAuthenticationToken($user, $firewallName, $roles);
5539
$this->getTokenStorage()->setToken($token);
5640

57-
if ($firewallContext) {
58-
$session->set('_security_' . $firewallContext, serialize($token));
59-
} else {
60-
$session->set('_security_' . $firewallName, serialize($token));
61-
}
62-
41+
$sessionKey = $firewallContext ? "_security_{$firewallContext}" : "_security_{$firewallName}";
42+
$session->set($sessionKey, serialize($token));
6343
$session->save();
6444

6545
$cookie = new Cookie($session->getName(), $session->getId());
@@ -74,16 +54,13 @@ public function amLoggedInAs(UserInterface $user, string $firewallName = 'main',
7454
* $I->dontSeeInSession('attribute');
7555
* $I->dontSeeInSession('attribute', 'value');
7656
* ```
77-
*
7857
*/
7958
public function dontSeeInSession(string $attribute, mixed $value = null): void
8059
{
8160
$session = $this->getCurrentSession();
8261

83-
if ($attributeExists = $session->has($attribute)) {
84-
$this->fail("Session attribute with name '{$attribute}' does exist");
85-
}
86-
$this->assertFalse($attributeExists);
62+
$attributeExists = $session->has($attribute);
63+
$this->assertFalse($attributeExists, "Session attribute '{$attribute}' exists.");
8764

8865
if (null !== $value) {
8966
$this->assertNotSame($value, $session->get($attribute));
@@ -98,8 +75,7 @@ public function dontSeeInSession(string $attribute, mixed $value = null): void
9875
*/
9976
public function goToLogoutPath(): void
10077
{
101-
$logoutUrlGenerator = $this->getLogoutUrlGenerator();
102-
$logoutPath = $logoutUrlGenerator->getLogoutPath();
78+
$logoutPath = $this->getLogoutUrlGenerator()->getLogoutPath();
10379
$this->amOnPage($logoutPath);
10480
}
10581

@@ -132,17 +108,14 @@ public function logoutProgrammatically(): void
132108
}
133109

134110
$session = $this->getCurrentSession();
135-
136111
$sessionName = $session->getName();
137112
$session->invalidate();
138113

139114
$cookieJar = $this->client->getCookieJar();
115+
$cookiesToExpire = ['MOCKSESSID', 'REMEMBERME', $sessionName];
140116
foreach ($cookieJar->all() as $cookie) {
141117
$cookieName = $cookie->getName();
142-
if ($cookieName === 'MOCKSESSID' ||
143-
$cookieName === 'REMEMBERME' ||
144-
$cookieName === $sessionName
145-
) {
118+
if (in_array($cookieName, $cookiesToExpire, true)) {
146119
$cookieJar->expire($cookieName);
147120
}
148121
}
@@ -163,10 +136,8 @@ public function seeInSession(string $attribute, mixed $value = null): void
163136
{
164137
$session = $this->getCurrentSession();
165138

166-
if (!$attributeExists = $session->has($attribute)) {
167-
$this->fail("No session attribute with name '{$attribute}'");
168-
}
169-
$this->assertTrue($attributeExists);
139+
$attributeExists = $session->has($attribute);
140+
$this->assertTrue($attributeExists, "No session attribute with name '{$attribute}'");
170141

171142
if (null !== $value) {
172143
$this->assertSame($value, $session->get($attribute));
@@ -181,8 +152,6 @@ public function seeInSession(string $attribute, mixed $value = null): void
181152
* $I->seeSessionHasValues(['key1', 'key2']);
182153
* $I->seeSessionHasValues(['key1' => 'value1', 'key2' => 'value2']);
183154
* ```
184-
*
185-
* @param array $bindings
186155
*/
187156
public function seeSessionHasValues(array $bindings): void
188157
{
@@ -227,4 +196,20 @@ protected function getSymfonyMajorVersion(): int
227196
{
228197
return $this->kernel::MAJOR_VERSION;
229198
}
199+
200+
/**
201+
* @return UsernamePasswordToken|PostAuthenticationGuardToken|PostAuthenticationToken
202+
*/
203+
protected function createAuthenticationToken(UserInterface $user, string $firewallName, array $roles)
204+
{
205+
if ($this->getSymfonyMajorVersion() < 6) {
206+
return $this->config['guard']
207+
? new PostAuthenticationGuardToken($user, $firewallName, $roles)
208+
: new UsernamePasswordToken($user, null, $firewallName, $roles);
209+
}
210+
211+
return $this->config['authenticator']
212+
? new PostAuthenticationToken($user, $firewallName, $roles)
213+
: new UsernamePasswordToken($user, $firewallName, $roles);
214+
}
230215
}

src/Codeception/Module/Symfony/TimeAssertionsTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function seeRequestTimeIsLessThan(int|float $expectedMilliseconds): void
3636
$expectedMilliseconds,
3737
$actualMilliseconds,
3838
sprintf(
39-
'The request was expected to last less than %d ms, but it actually lasted %d ms.',
39+
'The request duration was expected to be less than %d ms, but it was actually %d ms.',
4040
$expectedMilliseconds,
4141
$actualMilliseconds
4242
)

src/Codeception/Module/Symfony/TwigAssertionsTrait.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ trait TwigAssertionsTrait
1616
* <?php
1717
* $I->dontSeeRenderedTemplate('home.html.twig');
1818
* ```
19-
*
20-
* @param string $template
2119
*/
2220
public function dontSeeRenderedTemplate(string $template): void
2321
{
@@ -39,15 +37,13 @@ public function dontSeeRenderedTemplate(string $template): void
3937
* <?php
4038
* $I->seeCurrentTemplateIs('home.html.twig');
4139
* ```
42-
*
43-
* @param string $expectedTemplate
4440
*/
4541
public function seeCurrentTemplateIs(string $expectedTemplate): void
4642
{
4743
$twigCollector = $this->grabTwigCollector(__FUNCTION__);
4844

4945
$templates = (array)$twigCollector->getTemplates();
50-
$actualTemplate = (string)array_key_first($templates);
46+
$actualTemplate = !empty($templates) ? (string) array_key_first($templates) : 'N/A';
5147

5248
$this->assertSame(
5349
$expectedTemplate,
@@ -65,8 +61,6 @@ public function seeCurrentTemplateIs(string $expectedTemplate): void
6561
* $I->seeRenderedTemplate('home.html.twig');
6662
* $I->seeRenderedTemplate('layout.html.twig');
6763
* ```
68-
*
69-
* @param string $template
7064
*/
7165
public function seeRenderedTemplate(string $template): void
7266
{

0 commit comments

Comments
 (0)