Skip to content
Merged
1 change: 0 additions & 1 deletion src/Codeception/Module/Symfony/BrowserAssertionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ trait BrowserAssertionsTrait
* // Perform other requests
*
* ```
*
*/
public function rebootClientKernel(): void
{
Expand Down
8 changes: 6 additions & 2 deletions src/Codeception/Module/Symfony/ConsoleAssertionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,12 @@ public function runSymfonyConsoleCommand(string $command, array $parameters = []
$this->assertSame(
$expectedExitCode,
$exitCode,
'Command did not exit with code ' . $expectedExitCode
. ' but with ' . $exitCode . ': ' . $output
sprintf(
'Command did not exit with code %d but with %d: %s',
$expectedExitCode,
$exitCode,
$output
)
);

return $output;
Expand Down
2 changes: 1 addition & 1 deletion src/Codeception/Module/Symfony/DoctrineAssertionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Doctrine\ORM\EntityRepository;
use function class_exists;
use function get_class;
use function interface_exists;
use function is_object;
use function is_string;
Expand Down Expand Up @@ -66,6 +65,7 @@ public function grabRepository(object|string $mixed): ?EntityRepository
$getRepo = function () use ($mixed, $entityRepoClass, $isNotARepo): ?EntityRepository {
if (!$repo = $this->grabService($mixed)) return null;

/** @var EntityRepository $repo */
if (!$repo instanceof $entityRepoClass) {
$isNotARepo();
return null;
Expand Down
6 changes: 0 additions & 6 deletions src/Codeception/Module/Symfony/EventsAssertionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@

use Symfony\Component\HttpKernel\DataCollector\EventDataCollector;
use Symfony\Component\VarDumper\Cloner\Data;
use function get_class;
use function is_array;
use function is_object;
use function strpos;

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

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

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

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

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

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

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

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

Expand Down
4 changes: 2 additions & 2 deletions src/Codeception/Module/Symfony/FormAssertionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function seeFormErrorMessage(string $field, string $message = null): void
$formCollector = $this->grabFormCollector(__FUNCTION__);

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

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

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)) {
Expand Down
8 changes: 3 additions & 5 deletions src/Codeception/Module/Symfony/MailerAssertionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,11 @@ public function seeEmailIsSent(int $expectedCount = 1): void
*/
public function grabLastSentEmail(): ?Email
{
/** @var Email[] $emails */
$emails = $this->getMessageMailerEvents()->getMessages();
/** @var Email|false $lastEmail */
if ($lastEmail = end($emails)) {
return $lastEmail;
}
$lastEmail = end($emails);

return null;
return $lastEmail ?: null;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Codeception/Module/Symfony/ParameterAssertionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ trait ParameterAssertionsTrait
* $I->grabParameter('app.business_name');
* ```
*
* @param string $name
* @param string $parameterName
* @return array|bool|float|int|string|null
*/
public function grabParameter(string $name)
public function grabParameter(string $parameterName)
{
$parameterBag = $this->grabParameterBagService();
return $parameterBag->get($name);
return $parameterBag->get($parameterName);
}

protected function grabParameterBagService(): ParameterBagInterface
Expand Down
10 changes: 3 additions & 7 deletions src/Codeception/Module/Symfony/RouterAssertionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
use function array_merge;
use function explode;
use function sprintf;
use function strlen;
use function substr_compare;

trait RouterAssertionsTrait
{
Expand All @@ -32,7 +30,6 @@ trait RouterAssertionsTrait
public function amOnAction(string $action, array $params = []): void
{
$router = $this->grabRouterService();

$routes = $router->getRouteCollection()->getIterator();

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

$url = $router->generate($routeName, $params);
Expand Down Expand Up @@ -95,7 +92,6 @@ public function invalidateCachedRouter(): void
public function seeCurrentActionIs(string $action): void
{
$router = $this->grabRouterService();

$routes = $router->getRouteCollection()->getIterator();

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

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

$uri = explode('?', $this->grabFromCurrentUrl())[0];
Expand Down
12 changes: 3 additions & 9 deletions src/Codeception/Module/Symfony/ServicesAssertionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ trait ServicesAssertionsTrait
* ```
*
* @part services
* @param string $serviceId
*/
public function grabService(string $serviceId): object
{
Expand Down Expand Up @@ -69,15 +68,10 @@ public function persistPermanentService(string $serviceName): void
*/
public function unpersistService(string $serviceName): void
{
if (isset($this->persistentServices[$serviceName])) {
unset($this->persistentServices[$serviceName]);
}

if (isset($this->permanentServices[$serviceName])) {
unset($this->permanentServices[$serviceName]);
}
unset($this->persistentServices[$serviceName]);
unset($this->permanentServices[$serviceName]);

if ($this->client instanceof SymfonyConnector && isset($this->client->persistentServices[$serviceName])) {
if ($this->client instanceof SymfonyConnector) {
unset($this->client->persistentServices[$serviceName]);
}
}
Expand Down
71 changes: 28 additions & 43 deletions src/Codeception/Module/Symfony/SessionAssertionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,37 +29,17 @@ trait SessionAssertionsTrait
* ]);
* $I->amLoggedInAs($user);
* ```
*
* @param UserInterface $user
* @param string $firewallName
* @param null $firewallContext
*/
public function amLoggedInAs(UserInterface $user, string $firewallName = 'main', $firewallContext = null): void
public function amLoggedInAs(UserInterface $user, string $firewallName = 'main', string $firewallContext = null): void
{
$session = $this->getCurrentSession();
$roles = $user->getRoles();

if ($this->getSymfonyMajorVersion() < 6) {
if ($this->config['guard']) {
$token = new PostAuthenticationGuardToken($user, $firewallName, $user->getRoles());
} else {
$token = new UsernamePasswordToken($user, null, $firewallName, $user->getRoles());
}
} else {
if ($this->config['authenticator']) {
$token = new PostAuthenticationToken($user, $firewallName, $user->getRoles());
} else {
$token = new UsernamePasswordToken($user, $firewallName, $user->getRoles());
}
}

$token = $this->createAuthenticationToken($user, $firewallName, $roles);
$this->getTokenStorage()->setToken($token);

if ($firewallContext) {
$session->set('_security_' . $firewallContext, serialize($token));
} else {
$session->set('_security_' . $firewallName, serialize($token));
}

$sessionKey = $firewallContext ? "_security_{$firewallContext}" : "_security_{$firewallName}";
$session->set($sessionKey, serialize($token));
$session->save();

$cookie = new Cookie($session->getName(), $session->getId());
Expand All @@ -74,16 +54,13 @@ public function amLoggedInAs(UserInterface $user, string $firewallName = 'main',
* $I->dontSeeInSession('attribute');
* $I->dontSeeInSession('attribute', 'value');
* ```
*
*/
public function dontSeeInSession(string $attribute, mixed $value = null): void
{
$session = $this->getCurrentSession();

if ($attributeExists = $session->has($attribute)) {
$this->fail("Session attribute with name '{$attribute}' does exist");
}
$this->assertFalse($attributeExists);
$attributeExists = $session->has($attribute);
$this->assertFalse($attributeExists, "Session attribute '{$attribute}' exists.");

if (null !== $value) {
$this->assertNotSame($value, $session->get($attribute));
Expand All @@ -98,8 +75,7 @@ public function dontSeeInSession(string $attribute, mixed $value = null): void
*/
public function goToLogoutPath(): void
{
$logoutUrlGenerator = $this->getLogoutUrlGenerator();
$logoutPath = $logoutUrlGenerator->getLogoutPath();
$logoutPath = $this->getLogoutUrlGenerator()->getLogoutPath();
$this->amOnPage($logoutPath);
}

Expand Down Expand Up @@ -132,17 +108,14 @@ public function logoutProgrammatically(): void
}

$session = $this->getCurrentSession();

$sessionName = $session->getName();
$session->invalidate();

$cookieJar = $this->client->getCookieJar();
$cookiesToExpire = ['MOCKSESSID', 'REMEMBERME', $sessionName];
foreach ($cookieJar->all() as $cookie) {
$cookieName = $cookie->getName();
if ($cookieName === 'MOCKSESSID' ||
$cookieName === 'REMEMBERME' ||
$cookieName === $sessionName
) {
if (in_array($cookieName, $cookiesToExpire, true)) {
$cookieJar->expire($cookieName);
}
}
Expand All @@ -163,10 +136,8 @@ public function seeInSession(string $attribute, mixed $value = null): void
{
$session = $this->getCurrentSession();

if (!$attributeExists = $session->has($attribute)) {
$this->fail("No session attribute with name '{$attribute}'");
}
$this->assertTrue($attributeExists);
$attributeExists = $session->has($attribute);
$this->assertTrue($attributeExists, "No session attribute with name '{$attribute}'");

if (null !== $value) {
$this->assertSame($value, $session->get($attribute));
Expand All @@ -181,8 +152,6 @@ public function seeInSession(string $attribute, mixed $value = null): void
* $I->seeSessionHasValues(['key1', 'key2']);
* $I->seeSessionHasValues(['key1' => 'value1', 'key2' => 'value2']);
* ```
*
* @param array $bindings
*/
public function seeSessionHasValues(array $bindings): void
{
Expand Down Expand Up @@ -227,4 +196,20 @@ protected function getSymfonyMajorVersion(): int
{
return $this->kernel::MAJOR_VERSION;
}

/**
* @return UsernamePasswordToken|PostAuthenticationGuardToken|PostAuthenticationToken
*/
protected function createAuthenticationToken(UserInterface $user, string $firewallName, array $roles)
{
if ($this->getSymfonyMajorVersion() < 6) {
return $this->config['guard']
? new PostAuthenticationGuardToken($user, $firewallName, $roles)
: new UsernamePasswordToken($user, null, $firewallName, $roles);
}

return $this->config['authenticator']
? new PostAuthenticationToken($user, $firewallName, $roles)
: new UsernamePasswordToken($user, $firewallName, $roles);
}
}
2 changes: 1 addition & 1 deletion src/Codeception/Module/Symfony/TimeAssertionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function seeRequestTimeIsLessThan(int|float $expectedMilliseconds): void
$expectedMilliseconds,
$actualMilliseconds,
sprintf(
'The request was expected to last less than %d ms, but it actually lasted %d ms.',
'The request duration was expected to be less than %d ms, but it was actually %d ms.',
$expectedMilliseconds,
$actualMilliseconds
)
Expand Down
8 changes: 1 addition & 7 deletions src/Codeception/Module/Symfony/TwigAssertionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ trait TwigAssertionsTrait
* <?php
* $I->dontSeeRenderedTemplate('home.html.twig');
* ```
*
* @param string $template
*/
public function dontSeeRenderedTemplate(string $template): void
{
Expand All @@ -39,15 +37,13 @@ public function dontSeeRenderedTemplate(string $template): void
* <?php
* $I->seeCurrentTemplateIs('home.html.twig');
* ```
*
* @param string $expectedTemplate
*/
public function seeCurrentTemplateIs(string $expectedTemplate): void
{
$twigCollector = $this->grabTwigCollector(__FUNCTION__);

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

$this->assertSame(
$expectedTemplate,
Expand All @@ -65,8 +61,6 @@ public function seeCurrentTemplateIs(string $expectedTemplate): void
* $I->seeRenderedTemplate('home.html.twig');
* $I->seeRenderedTemplate('layout.html.twig');
* ```
*
* @param string $template
*/
public function seeRenderedTemplate(string $template): void
{
Expand Down