Skip to content

Commit

Permalink
Don't mock the service container
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabus authored and ostrolucky committed Sep 5, 2023
1 parent ca3678d commit 417143b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 83 deletions.
24 changes: 4 additions & 20 deletions Tests/Mapping/ContainerEntityListenerResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,17 @@
use Doctrine\Bundle\DoctrineBundle\Mapping\ContainerEntityListenerResolver;
use Doctrine\ORM\EntityManagerInterface;
use InvalidArgumentException;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use RuntimeException;
use Symfony\Component\DependencyInjection\Container;

use function interface_exists;

class ContainerEntityListenerResolverTest extends TestCase
{
private ContainerEntityListenerResolver $resolver;

/** @var ContainerInterface&MockObject */
private ContainerInterface $container;
private Container $container;

public static function setUpBeforeClass(): void
{
Expand All @@ -32,7 +30,7 @@ protected function setUp(): void
{
parent::setUp();

$this->container = $this->createMock(ContainerInterface::class);
$this->container = new Container();
$this->resolver = new ContainerEntityListenerResolver($this->container);
}

Expand Down Expand Up @@ -62,16 +60,7 @@ public function testRegisterServiceAndResolve(): void
$object = new $className();

$this->resolver->registerService($className, $serviceId);
$this->container
->expects($this->any())
->method('has')
->with($serviceId)
->will($this->returnValue(true));
$this->container
->expects($this->any())
->method('get')
->with($serviceId)
->will($this->returnValue($object));
$this->container->set($serviceId, $object);

$this->assertInstanceOf($className, $this->resolver->resolve($className));
$this->assertSame($object, $this->resolver->resolve($className));
Expand All @@ -83,11 +72,6 @@ public function testRegisterMissingServiceAndResolve(): void
$serviceId = 'app.entity_listener';

$this->resolver->registerService($className, $serviceId);
$this->container
->expects($this->any())
->method('has')
->with($serviceId)
->will($this->returnValue(false));

$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('There is no service named');
Expand Down
70 changes: 20 additions & 50 deletions Tests/RegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use InvalidArgumentException;
use ProxyManager\Proxy\ProxyInterface;
use stdClass;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\VarExporter\LazyObjectInterface;

use function assert;
Expand All @@ -26,28 +26,23 @@ class RegistryTest extends TestCase
{
public function testGetDefaultConnectionName(): void
{
$container = $this->getMockBuilder(ContainerInterface::class)->getMock();
$registry = new Registry($container, [], [], 'default', 'default');
$registry = new Registry(new Container(), [], [], 'default', 'default');

$this->assertEquals('default', $registry->getDefaultConnectionName());
}

public function testGetDefaultEntityManagerName(): void
{
$container = $this->getMockBuilder(ContainerInterface::class)->getMock();
$registry = new Registry($container, [], [], 'default', 'default');
$registry = new Registry(new Container(), [], [], 'default', 'default');

$this->assertEquals('default', $registry->getDefaultManagerName());
}

public function testGetDefaultConnection(): void
{
$conn = $this->getMockBuilder(Connection::class)->disableOriginalConstructor()->getMock();
$container = $this->getMockBuilder(ContainerInterface::class)->getMock();
$container->expects($this->once())
->method('get')
->with($this->equalTo('doctrine.dbal.default_connection'))
->will($this->returnValue($conn));
$container = new Container();
$container->set('doctrine.dbal.default_connection', $conn);

$registry = new Registry($container, ['default' => 'doctrine.dbal.default_connection'], [], 'default', 'default');

Expand All @@ -57,11 +52,8 @@ public function testGetDefaultConnection(): void
public function testGetConnection(): void
{
$conn = $this->getMockBuilder(Connection::class)->disableOriginalConstructor()->getMock();
$container = $this->getMockBuilder(ContainerInterface::class)->getMock();
$container->expects($this->once())
->method('get')
->with($this->equalTo('doctrine.dbal.default_connection'))
->will($this->returnValue($conn));
$container = new Container();
$container->set('doctrine.dbal.default_connection', $conn);

$registry = new Registry($container, ['default' => 'doctrine.dbal.default_connection'], [], 'default', 'default');

Expand All @@ -70,8 +62,7 @@ public function testGetConnection(): void

public function testGetUnknownConnection(): void
{
$container = $this->getMockBuilder(ContainerInterface::class)->getMock();
$registry = new Registry($container, [], [], 'default', 'default');
$registry = new Registry(new Container(), [], [], 'default', 'default');

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Doctrine ORM Connection named "default" does not exist.');
Expand All @@ -80,20 +71,16 @@ public function testGetUnknownConnection(): void

public function testGetConnectionNames(): void
{
$container = $this->getMockBuilder(ContainerInterface::class)->getMock();
$registry = new Registry($container, ['default' => 'doctrine.dbal.default_connection'], [], 'default', 'default');
$registry = new Registry(new Container(), ['default' => 'doctrine.dbal.default_connection'], [], 'default', 'default');

$this->assertEquals(['default' => 'doctrine.dbal.default_connection'], $registry->getConnectionNames());
}

public function testGetDefaultEntityManager(): void
{
$em = new stdClass();
$container = $this->getMockBuilder(ContainerInterface::class)->getMock();
$container->expects($this->once())
->method('get')
->with($this->equalTo('doctrine.orm.default_entity_manager'))
->will($this->returnValue($em));
$container = new Container();
$container->set('doctrine.orm.default_entity_manager', $em);

$registry = new Registry($container, [], ['default' => 'doctrine.orm.default_entity_manager'], 'default', 'default');

Expand All @@ -103,11 +90,8 @@ public function testGetDefaultEntityManager(): void
public function testGetEntityManager(): void
{
$em = new stdClass();
$container = $this->getMockBuilder(ContainerInterface::class)->getMock();
$container->expects($this->once())
->method('get')
->with($this->equalTo('doctrine.orm.default_entity_manager'))
->will($this->returnValue($em));
$container = new Container();
$container->set('doctrine.orm.default_entity_manager', $em);

$registry = new Registry($container, [], ['default' => 'doctrine.orm.default_entity_manager'], 'default', 'default');

Expand All @@ -116,8 +100,7 @@ public function testGetEntityManager(): void

public function testGetUnknownEntityManager(): void
{
$container = $this->getMockBuilder(ContainerInterface::class)->getMock();
$registry = new Registry($container, [], [], 'default', 'default');
$registry = new Registry(new Container(), [], [], 'default', 'default');

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage(
Expand All @@ -128,8 +111,7 @@ public function testGetUnknownEntityManager(): void

public function testResetUnknownEntityManager(): void
{
$container = $this->getMockBuilder(ContainerInterface::class)->getMock();
$registry = new Registry($container, [], [], 'default', 'default');
$registry = new Registry(new Container(), [], [], 'default', 'default');

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage(
Expand All @@ -153,16 +135,9 @@ public function testReset(): void
->method('setProxyInitializer')
->with($this->isInstanceOf(Closure::class));

$container = $this->getMockBuilder(ContainerInterface::class)->getMock();
$container->expects($this->any())
->method('initialized')
->withConsecutive(['doctrine.orm.uninitialized_entity_manager'], ['doctrine.orm.noproxy_entity_manager'], ['doctrine.orm.proxy_entity_manager'])
->willReturnOnConsecutiveCalls(false, true, true, true);

$container->expects($this->any())
->method('get')
->withConsecutive(['doctrine.orm.noproxy_entity_manager'], ['doctrine.orm.proxy_entity_manager'], ['doctrine.orm.proxy_entity_manager'], ['doctrine.orm.proxy_entity_manager'])
->willReturnOnConsecutiveCalls($noProxyManager, $proxyManager, $proxyManager, $proxyManager);
$container = new Container();
$container->set('doctrine.orm.noproxy_entity_manager', $noProxyManager);
$container->set('doctrine.orm.proxy_entity_manager', $proxyManager);

$entityManagers = [
'uninitialized' => 'doctrine.orm.uninitialized_entity_manager',
Expand All @@ -185,13 +160,8 @@ public function testResetLazyObject(): void
/** @psalm-suppress MissingDependency https://github.com/vimeo/psalm/issues/8258 */
$ghostManager->expects($this->once())->method('resetLazyObject')->willReturn(true);

$container = $this->createMock(ContainerInterface::class);
$container->method('initialized')
->withConsecutive(['doctrine.orm.uninitialized_entity_manager'], ['doctrine.orm.ghost_entity_manager'])
->willReturnOnConsecutiveCalls(false, true, true);
$container->method('get')
->withConsecutive(['doctrine.orm.ghost_entity_manager'], ['doctrine.orm.ghost_entity_manager'], ['doctrine.orm.ghost_entity_manager'])
->willReturnOnConsecutiveCalls($ghostManager, $ghostManager, $ghostManager);
$container = new Container();
$container->set('doctrine.orm.ghost_entity_manager', $ghostManager);

$entityManagers = [
'uninitialized' => 'doctrine.orm.uninitialized_entity_manager',
Expand Down
20 changes: 7 additions & 13 deletions Tests/Repository/ContainerRepositoryFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\Persistence\ObjectRepository;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use RuntimeException;
use stdClass;
use Symfony\Component\DependencyInjection\Container;

use function interface_exists;

Expand Down Expand Up @@ -127,19 +127,13 @@ public function testCustomRepositoryIsNotAValidClass(): void
}

/** @param array<string, object> $services */
private function createContainer(array $services): ContainerInterface
private function createContainer(array $services): Container
{
$container = $this->getMockBuilder(ContainerInterface::class)->getMock();
$container->expects($this->any())
->method('has')
->willReturnCallback(static function ($id) use ($services) {
return isset($services[$id]);
});
$container->expects($this->any())
->method('get')
->willReturnCallback(static function ($id) use ($services) {
return $services[$id];
});
$container = new Container();

foreach ($services as $id => $service) {
$container->set($id, $service);
}

return $container;
}
Expand Down

0 comments on commit 417143b

Please sign in to comment.