Skip to content

Commit c55b239

Browse files
committed
Fix inconsistencies across assertion traits
1 parent c37d23e commit c55b239

9 files changed

Lines changed: 33 additions & 9 deletions

src/Codeception/Module/Symfony/BrowserAssertionsTrait.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
use Symfony\Component\HttpFoundation\Test\Constraint\ResponseIsUnprocessable;
2222
use Symfony\Component\HttpFoundation\Test\Constraint\ResponseStatusCodeSame;
2323

24+
use function class_exists;
25+
use function count;
2426
use function sprintf;
2527

2628
trait BrowserAssertionsTrait

src/Codeception/Module/Symfony/ConsoleAssertionsTrait.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Symfony\Component\Console\Tester\CommandTester;
1010
use Symfony\Component\HttpKernel\KernelInterface;
1111

12+
use function is_int;
1213
use function sprintf;
1314

1415
trait ConsoleAssertionsTrait

src/Codeception/Module/Symfony/FormAssertionsTrait.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use Symfony\Component\Form\Extension\DataCollector\FormDataCollector;
88
use Symfony\Component\VarDumper\Cloner\Data;
99

10+
use function count;
11+
use function implode;
1012
use function is_array;
1113
use function is_int;
1214
use function is_numeric;

src/Codeception/Module/Symfony/LoggerAssertionsTrait.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77
use Symfony\Component\HttpKernel\DataCollector\LoggerDataCollector;
88
use Symfony\Component\VarDumper\Cloner\Data;
99

10+
use function array_map;
11+
use function count;
12+
use function implode;
13+
use function is_scalar;
14+
use function is_string;
15+
use function json_encode;
1016
use function sprintf;
1117

1218
trait LoggerAssertionsTrait

src/Codeception/Module/Symfony/MimeAssertionsTrait.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use Symfony\Component\Mime\Email;
1010
use Symfony\Component\Mime\Test\Constraint as MimeConstraint;
1111

12+
use function sprintf;
13+
1214
trait MimeAssertionsTrait
1315
{
1416
/**

src/Codeception/Module/Symfony/NotifierAssertionsTrait.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use Symfony\Component\Notifier\Message\MessageInterface;
1414
use Symfony\Component\Notifier\Test\Constraint as NotifierConstraint;
1515

16-
use function end;
16+
use function array_key_last;
1717
use function version_compare;
1818

1919
trait NotifierAssertionsTrait
@@ -163,10 +163,9 @@ public function dontSeeNotificationIsSent(): void
163163
*/
164164
public function grabLastSentNotification(): ?MessageInterface
165165
{
166-
$notification = $this->getNotifierMessages();
167-
$lastNotification = end($notification);
166+
$notifications = $this->getNotifierMessages();
168167

169-
return $lastNotification ?: null;
168+
return $notifications ? $notifications[array_key_last($notifications)] : null;
170169
}
171170

172171
/**
@@ -205,7 +204,7 @@ public function seeNotificationIsSent(int $expectedCount = 1): void
205204
}
206205

207206
/** @return MessageEvent[] */
208-
public function getNotifierEvents(?string $transportName = null): array
207+
protected function getNotifierEvents(?string $transportName = null): array
209208
{
210209
return $this->getNotificationEvents()->getEvents($transportName);
211210
}
@@ -224,7 +223,7 @@ public function getNotifierEvent(int $index = 0, ?string $transportName = null):
224223
}
225224

226225
/** @return MessageInterface[] */
227-
public function getNotifierMessages(?string $transportName = null): array
226+
protected function getNotifierMessages(?string $transportName = null): array
228227
{
229228
return $this->getNotificationEvents()->getMessages($transportName);
230229
}

src/Codeception/Module/Symfony/SessionAssertionsTrait.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ public function amLoggedInAs(UserInterface $user, string $firewallName = 'main',
4646
$this->amLoggedInWithToken($this->createAuthenticationToken($user, $firewallName), $firewallName, $firewallContext);
4747
}
4848

49+
/**
50+
* Login with the given authentication token.
51+
* If you have more than one firewall or firewall context, you can specify the desired one as a parameter.
52+
*
53+
* ```php
54+
* <?php
55+
* $I->amLoggedInWithToken($token);
56+
* ```
57+
*/
4958
public function amLoggedInWithToken(TokenInterface $token, string $firewallName = 'main', ?string $firewallContext = null): void
5059
{
5160
$this->getTokenStorage()->setToken($token);

src/Codeception/Module/Symfony/TimeAssertionsTrait.php

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

src/Codeception/Module/Symfony/ValidatorAssertionsTrait.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
use Symfony\Component\Validator\ConstraintViolationInterface;
88
use Symfony\Component\Validator\Validator\ValidatorInterface;
99

10+
use function array_filter;
11+
use function iterator_to_array;
12+
use function str_contains;
13+
1014
trait ValidatorAssertionsTrait
1115
{
1216
/**
@@ -71,7 +75,7 @@ public function seeViolatedConstraintMessage(string $expected, object $subject,
7175
$violations = $this->getViolationsForSubject($subject, $propertyPath);
7276
$containsExpected = false;
7377
foreach ($violations as $violation) {
74-
if ($violation->getPropertyPath() === $propertyPath && str_contains((string) $violation->getMessage(), $expected)) {
78+
if (str_contains((string) $violation->getMessage(), $expected)) {
7579
$containsExpected = true;
7680
break;
7781
}
@@ -93,7 +97,6 @@ protected function getViolationsForSubject(object $subject, ?string $propertyPat
9397
$violations,
9498
static fn(ConstraintViolationInterface $violation): bool => $violation->getConstraint() !== null
9599
&& $violation->getConstraint()::class === $constraint
96-
&& ($propertyPath === null || $violation->getPropertyPath() === $propertyPath)
97100
);
98101
}
99102

0 commit comments

Comments
 (0)