Skip to content

Commit 0db7d92

Browse files
authored
Add functional tests for grabEntityManager(), resetDoctrineManager() and grabContainer() (#76)
* Add functional test for grabEntityManager() Cover grabEntityManager(), added in codeception/module-symfony: assert it returns an open EntityManagerInterface, and that it is the very instance the application uses rather than a copy, by comparing it against the doctrine.orm.default_entity_manager service. No app fixtures are needed: the existing User entity and its fixture cover it. The test is identical on every branch, since the method behaves the same on all supported Symfony versions. * Add functional test for resetDoctrineManager() Cover resetDoctrineManager(), added in codeception/module-symfony. Assert both halves of its contract: an open manager is only cleared, so a previously managed entity is no longer in the identity map, and a closed manager is reopened. The seeNumRecords() call at the end proves the transaction the Doctrine module opened for the test survived the reset. No app fixtures are needed: the test closes the manager itself rather than provoking a failed flush, which keeps it deterministic under settings.shuffle. The test is identical on every branch. The module recovers through Doctrine's registry where the entity manager service is lazy and by rebooting the kernel where it is not, but both paths are observable only as a reopened manager. * Add functional test for grabContainer() Cover grabContainer(), added in codeception/module-symfony: assert it returns the container for the test environment, and that it is the test container rather than the kernel's own. The last two assertions are the point of the method: the test container resolves a private service that the kernel container cannot see, which is what made the widespread grabService('kernel')->getContainer() workaround subtly wrong. No app fixtures are needed: EntityManagerInterface is registered as a private autowiring alias by doctrine-bundle on every branch. The test is identical on every branch.
1 parent 41e29e2 commit 0db7d92

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

tests/Functional/DoctrineCest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,19 @@
88
use App\Repository\Model\UserRepositoryInterface;
99
use App\Repository\UserRepository;
1010
use App\Tests\Support\FunctionalTester;
11+
use Doctrine\ORM\EntityManagerInterface;
1112

1213
final class DoctrineCest
1314
{
15+
public function grabEntityManager(FunctionalTester $I): void
16+
{
17+
$em = $I->grabEntityManager();
18+
19+
$I->assertInstanceOf(EntityManagerInterface::class, $em);
20+
$I->assertTrue($em->isOpen());
21+
$I->assertSame($em, $I->grabService('doctrine.orm.default_entity_manager'));
22+
}
23+
1424
public function grabNumRecords(FunctionalTester $I)
1525
{
1626
$numRecords = $I->grabNumRecords(User::class);
@@ -39,6 +49,22 @@ public function grabRepository(FunctionalTester $I)
3949
$I->assertInstanceOf(UserRepository::class, $repository);
4050
}
4151

52+
public function resetDoctrineManager(FunctionalTester $I): void
53+
{
54+
$em = $I->grabEntityManager();
55+
$user = $em->getRepository(User::class)->findOneBy(['email' => 'john_doe@gmail.com']);
56+
$I->assertTrue($em->contains($user));
57+
58+
$I->resetDoctrineManager();
59+
$I->assertFalse($I->grabEntityManager()->contains($user));
60+
61+
$I->grabEntityManager()->close();
62+
$I->resetDoctrineManager();
63+
64+
$I->assertTrue($I->grabEntityManager()->isOpen());
65+
$I->seeNumRecords(1, User::class);
66+
}
67+
4268
public function seeNumRecords(FunctionalTester $I)
4369
{
4470
$I->seeNumRecords(1, User::class);

tests/Functional/ServicesCest.php

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

77
use App\Service\Greeting;
88
use App\Tests\Support\FunctionalTester;
9+
use Doctrine\ORM\EntityManagerInterface;
10+
use Symfony\Component\DependencyInjection\ContainerInterface;
911
use Symfony\Component\Security\Core\Security;
1012

1113
final class ServicesCest
1214
{
15+
public function grabContainer(FunctionalTester $I): void
16+
{
17+
$container = $I->grabContainer();
18+
19+
$I->assertInstanceOf(ContainerInterface::class, $container);
20+
$I->assertSame('test', $container->getParameter('kernel.environment'));
21+
22+
$I->assertTrue($container->has(EntityManagerInterface::class));
23+
$I->assertFalse($I->grabService('kernel')->getContainer()->has(EntityManagerInterface::class));
24+
}
25+
1326
public function grabService(FunctionalTester $I)
1427
{
1528
$security = $I->grabService('security.helper');

0 commit comments

Comments
 (0)