Skip to content

Encapsulate services and collectors #103

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 18, 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
19 changes: 7 additions & 12 deletions src/Codeception/Module/Symfony.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
use Symfony\Component\HttpKernel\Profiler\Profiler;
use Symfony\Component\Mailer\DataCollector\MessageDataCollector;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\VarDumper\Cloner\Data;
use function array_keys;
use function array_map;
Expand Down Expand Up @@ -280,14 +279,14 @@ public function _getEntityManager()
if (!isset($this->permanentServices[$this->config['em_service']])) {
// try to persist configured EM
$this->persistPermanentService($this->config['em_service']);

if ($this->_getContainer()->has('doctrine')) {
$container = $this->_getContainer();
if ($container->has('doctrine')) {
$this->persistPermanentService('doctrine');
}
if ($this->_getContainer()->has('doctrine.orm.default_entity_manager')) {
if ($container->has('doctrine.orm.default_entity_manager')) {
$this->persistPermanentService('doctrine.orm.default_entity_manager');
}
if ($this->_getContainer()->has('doctrine.dbal.backend_connection')) {
if ($container->has('doctrine.dbal.backend_connection')) {
$this->persistPermanentService('doctrine.dbal.backend_connection');
}
}
Expand Down Expand Up @@ -377,13 +376,10 @@ protected function getKernelClass(): string
*/
protected function getProfile(): ?Profile
{
$container = $this->_getContainer();
if (!$container->has('profiler')) {
/** @var Profiler $profiler */
if (!$profiler = $this->getService('profiler')) {
return null;
}

/** @var Profiler $profiler */
$profiler = $this->grabService('profiler');
try {
/** @var Response $response */
$response = $this->client->getResponse();
Expand Down Expand Up @@ -479,8 +475,7 @@ protected function getInternalDomains(): array
{
$internalDomains = [];

/** @var RouterInterface $router */
$router = $this->grabService('router');
$router = $this->grabRouterService();
$routes = $router->getRouteCollection();
/* @var Route $route */
foreach ($routes as $route) {
Expand Down
8 changes: 7 additions & 1 deletion src/Codeception/Module/Symfony/ConsoleAssertionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\HttpKernel\KernelInterface;

trait ConsoleAssertionsTrait
{
Expand All @@ -26,7 +27,7 @@ trait ConsoleAssertionsTrait
*/
public function runSymfonyConsoleCommand(string $command, array $parameters = [], array $consoleInputs = [], int $expectedExitCode = 0): string
{
$kernel = $this->grabService('kernel');
$kernel = $this->grabKernelService();
$application = new Application($kernel);
$consoleCommand = $application->find($command);
$commandTester = new CommandTester($consoleCommand);
Expand All @@ -45,4 +46,9 @@ public function runSymfonyConsoleCommand(string $command, array $parameters = []

return $output;
}

protected function grabKernelService(): KernelInterface
{
return $this->grabService('kernel');
}
}
11 changes: 7 additions & 4 deletions src/Codeception/Module/Symfony/EventsAssertionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ trait EventsAssertionsTrait
*/
public function dontSeeEventTriggered($expected): void
{
/** @var EventDataCollector $eventCollector */
$eventCollector = $this->grabCollector('events', __FUNCTION__);
$eventCollector = $this->grabEventCollector(__FUNCTION__);

/** @var Data $data */
$data = $eventCollector->getNotCalledListeners();
Expand Down Expand Up @@ -63,8 +62,7 @@ public function dontSeeEventTriggered($expected): void
*/
public function seeEventTriggered($expected): void
{
/** @var EventDataCollector $eventCollector */
$eventCollector = $this->grabCollector('events', __FUNCTION__);
$eventCollector = $this->grabEventCollector(__FUNCTION__);

/** @var Data $data */
$data = $eventCollector->getCalledListeners();
Expand All @@ -88,4 +86,9 @@ public function seeEventTriggered($expected): void
$this->assertTrue($triggered, "The '$expectedEvent' event did not trigger");
}
}

protected function grabEventCollector(string $function): EventDataCollector
{
return $this->grabCollector('events', $function);
}
}
14 changes: 8 additions & 6 deletions src/Codeception/Module/Symfony/FormAssertionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ trait FormAssertionsTrait
*/
public function dontSeeFormErrors(): void
{
/** @var FormDataCollector $formCollector */
$formCollector = $this->grabCollector('form', __FUNCTION__);
$formCollector = $this->grabFormCollector(__FUNCTION__);

$this->assertEquals(
0,
Expand All @@ -47,8 +46,7 @@ public function dontSeeFormErrors(): void
*/
public function seeFormErrorMessage(string $field, ?string $message = null): void
{
/** @var FormDataCollector $formCollector */
$formCollector = $this->grabCollector('form', __FUNCTION__);
$formCollector = $this->grabFormCollector(__FUNCTION__);

if (!$forms = $formCollector->getData()->getValue('forms')['forms']) {
$this->fail('There are no forms on the current page.');
Expand Down Expand Up @@ -157,13 +155,17 @@ public function seeFormErrorMessages(array $expectedErrors): void
*/
public function seeFormHasErrors(): void
{
/** @var FormDataCollector $formCollector */
$formCollector = $this->grabCollector('form', __FUNCTION__);
$formCollector = $this->grabFormCollector(__FUNCTION__);

$this->assertGreaterThan(
0,
$formCollector->getData()->offsetGet('nb_errors'),
'Expecting that the form has errors, but there were none!'
);
}

protected function grabFormCollector(string $function): FormDataCollector
{
return $this->grabCollector('form', $function);
}
}
8 changes: 2 additions & 6 deletions src/Codeception/Module/Symfony/MailerAssertionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,13 @@ public function seeEmailIsSent(int $expectedCount = 1): void

protected function getMessageMailerEvents(): MessageEvents
{
$container = $this->_getContainer();

if ($container->has('mailer.message_logger_listener')) {
if ($messageLogger = $this->getService('mailer.message_logger_listener')) {
/** @var MessageLoggerListener $messageLogger */
$messageLogger = $container->get('mailer.message_logger_listener');
return $messageLogger->getEvents();
}

if ($container->has('mailer.logger_message_listener')) {
if ($messageLogger = $this->getService('mailer.logger_message_listener')) {
/** @var MessageLoggerListener $messageLogger */
$messageLogger = $container->get('mailer.logger_message_listener');
return $messageLogger->getEvents();
}

Expand Down
8 changes: 6 additions & 2 deletions src/Codeception/Module/Symfony/ParameterAssertionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ trait ParameterAssertionsTrait
*/
public function grabParameter(string $name)
{
/** @var ParameterBagInterface $parameterBag */
$parameterBag = $this->grabService('parameter_bag');
$parameterBag = $this->grabParameterBagService();
return $parameterBag->get($name);
}

protected function grabParameterBagService(): ParameterBagInterface
{
return $this->grabService('parameter_bag');
}
}
20 changes: 10 additions & 10 deletions src/Codeception/Module/Symfony/RouterAssertionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ trait RouterAssertionsTrait
*/
public function amOnAction(string $action, array $params = []): void
{
/** @var RouterInterface $router */
$router = $this->grabService('router');
$router = $this->grabRouterService();

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

Expand Down Expand Up @@ -66,8 +65,7 @@ public function amOnAction(string $action, array $params = []): void
*/
public function amOnRoute(string $routeName, array $params = []): void
{
/** @var RouterInterface $router */
$router = $this->grabService('router');
$router = $this->grabRouterService();
if ($router->getRouteCollection()->get($routeName) === null) {
$this->fail(sprintf('Route with name "%s" does not exists.', $routeName));
}
Expand Down Expand Up @@ -96,8 +94,7 @@ public function invalidateCachedRouter(): void
*/
public function seeCurrentActionIs(string $action): void
{
/** @var RouterInterface $router */
$router = $this->grabService('router');
$router = $this->grabRouterService();

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

Expand Down Expand Up @@ -129,8 +126,7 @@ public function seeCurrentActionIs(string $action): void
*/
public function seeCurrentRouteIs(string $routeName, array $params = []): void
{
/** @var RouterInterface $router */
$router = $this->grabService('router');
$router = $this->grabRouterService();
if ($router->getRouteCollection()->get($routeName) === null) {
$this->fail(sprintf('Route with name "%s" does not exists.', $routeName));
}
Expand Down Expand Up @@ -160,8 +156,7 @@ public function seeCurrentRouteIs(string $routeName, array $params = []): void
*/
public function seeInCurrentRoute(string $routeName): void
{
/** @var RouterInterface $router */
$router = $this->grabService('router');
$router = $this->grabRouterService();
if ($router->getRouteCollection()->get($routeName) === null) {
$this->fail(sprintf('Route with name "%s" does not exists.', $routeName));
}
Expand All @@ -175,4 +170,9 @@ public function seeInCurrentRoute(string $routeName): void

$this->assertEquals($matchedRouteName, $routeName);
}

protected function grabRouterService(): RouterInterface
{
return $this->grabService('router');
}
}
33 changes: 19 additions & 14 deletions src/Codeception/Module/Symfony/SecurityAssertionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Codeception\Module\Symfony;

use Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\User\UserInterface;
use function sprintf;
Expand All @@ -21,8 +22,7 @@ trait SecurityAssertionsTrait
*/
public function dontSeeAuthentication(): void
{
/** @var Security $security */
$security = $this->grabService('security.helper');
$security = $this->grabSecurityService();

$this->assertFalse(
$security->isGranted(AuthenticatedVoter::IS_AUTHENTICATED_FULLY),
Expand All @@ -40,8 +40,7 @@ public function dontSeeAuthentication(): void
*/
public function dontSeeRememberedAuthentication(): void
{
/** @var Security $security */
$security = $this->grabService('security.helper');
$security = $this->grabSecurityService();

$hasRememberMeCookie = $this->client->getCookieJar()->get('REMEMBERME');
$hasRememberMeRole = $security->isGranted(AuthenticatedVoter::IS_AUTHENTICATED_REMEMBERED);
Expand All @@ -63,8 +62,7 @@ public function dontSeeRememberedAuthentication(): void
*/
public function seeAuthentication(): void
{
/** @var Security $security */
$security = $this->grabService('security.helper');
$security = $this->grabSecurityService();

$user = $security->getUser();

Expand All @@ -88,8 +86,7 @@ public function seeAuthentication(): void
*/
public function seeRememberedAuthentication(): void
{
/** @var Security $security */
$security = $this->grabService('security.helper');
$security = $this->grabSecurityService();

$user = $security->getUser();

Expand Down Expand Up @@ -119,8 +116,7 @@ public function seeRememberedAuthentication(): void
*/
public function seeUserHasRole(string $role): void
{
/** @var Security $security */
$security = $this->grabService('security.helper');
$security = $this->grabSecurityService();

$user = $security->getUser();

Expand Down Expand Up @@ -172,15 +168,24 @@ public function seeUserHasRoles(array $roles): void
public function seeUserPasswordDoesNotNeedRehash(UserInterface $user = null): void
{
if ($user === null) {
/** @var Security $security */
$security = $this->grabService('security.helper');
$security = $this->grabSecurityService();
$user = $security->getUser();
if ($user === null) {
$this->fail('No user found to validate');
}
}
$encoder = $this->grabService('security.user_password_encoder.generic');
$hasher = $this->grabPasswordHasherService();

$this->assertFalse($encoder->needsRehash($user), 'User password needs rehash');
$this->assertFalse($hasher->needsRehash($user), 'User password needs rehash');
}

protected function grabSecurityService(): Security
{
return $this->grabService('security.helper');
}

protected function grabPasswordHasherService(): UserPasswordEncoderInterface
{
return $this->grabService('security.user_password_encoder.generic');
}
}
22 changes: 15 additions & 7 deletions src/Codeception/Module/Symfony/ServicesAssertionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,17 @@ trait ServicesAssertionsTrait
* ```
*
* @part services
* @param string $service
* @return mixed
* @param string $serviceId
* @return object
*/
public function grabService(string $service)
public function grabService(string $serviceId): object
{
$container = $this->_getContainer();
if (!$container->has($service)) {
$this->fail("Service $service is not available in container.
if (!$service = $this->getService($serviceId)) {
$this->fail("Service $serviceId is not available in container.
If the service isn't injected anywhere in your app, you need to set it to `public` in your `config/services_test.php`/`.yaml`,
see https://symfony.com/doc/current/testing.html#accessing-the-container");
}
return $container->get($service);
return $service;
}

/**
Expand Down Expand Up @@ -83,4 +82,13 @@ public function unpersistService(string $serviceName): void
unset($this->client->persistentServices[$serviceName]);
}
}

protected function getService(string $serviceId): ?object
{
$container = $this->_getContainer();
if ($container->has($serviceId)) {
return $container->get($serviceId);
}
return null;
}
}
Loading