Skip to content

Commit 417143b

Browse files
derrabusostrolucky
authored andcommitted
Don't mock the service container
1 parent ca3678d commit 417143b

File tree

3 files changed

+31
-83
lines changed

3 files changed

+31
-83
lines changed

Tests/Mapping/ContainerEntityListenerResolverTest.php

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,17 @@
55
use Doctrine\Bundle\DoctrineBundle\Mapping\ContainerEntityListenerResolver;
66
use Doctrine\ORM\EntityManagerInterface;
77
use InvalidArgumentException;
8-
use PHPUnit\Framework\MockObject\MockObject;
98
use PHPUnit\Framework\TestCase;
10-
use Psr\Container\ContainerInterface;
119
use RuntimeException;
10+
use Symfony\Component\DependencyInjection\Container;
1211

1312
use function interface_exists;
1413

1514
class ContainerEntityListenerResolverTest extends TestCase
1615
{
1716
private ContainerEntityListenerResolver $resolver;
1817

19-
/** @var ContainerInterface&MockObject */
20-
private ContainerInterface $container;
18+
private Container $container;
2119

2220
public static function setUpBeforeClass(): void
2321
{
@@ -32,7 +30,7 @@ protected function setUp(): void
3230
{
3331
parent::setUp();
3432

35-
$this->container = $this->createMock(ContainerInterface::class);
33+
$this->container = new Container();
3634
$this->resolver = new ContainerEntityListenerResolver($this->container);
3735
}
3836

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

6462
$this->resolver->registerService($className, $serviceId);
65-
$this->container
66-
->expects($this->any())
67-
->method('has')
68-
->with($serviceId)
69-
->will($this->returnValue(true));
70-
$this->container
71-
->expects($this->any())
72-
->method('get')
73-
->with($serviceId)
74-
->will($this->returnValue($object));
63+
$this->container->set($serviceId, $object);
7564

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

8574
$this->resolver->registerService($className, $serviceId);
86-
$this->container
87-
->expects($this->any())
88-
->method('has')
89-
->with($serviceId)
90-
->will($this->returnValue(false));
9175

9276
$this->expectException(RuntimeException::class);
9377
$this->expectExceptionMessage('There is no service named');

Tests/RegistryTest.php

Lines changed: 20 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use InvalidArgumentException;
1414
use ProxyManager\Proxy\ProxyInterface;
1515
use stdClass;
16-
use Symfony\Component\DependencyInjection\ContainerInterface;
16+
use Symfony\Component\DependencyInjection\Container;
1717
use Symfony\Component\VarExporter\LazyObjectInterface;
1818

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

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

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

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

4341
public function testGetDefaultConnection(): void
4442
{
4543
$conn = $this->getMockBuilder(Connection::class)->disableOriginalConstructor()->getMock();
46-
$container = $this->getMockBuilder(ContainerInterface::class)->getMock();
47-
$container->expects($this->once())
48-
->method('get')
49-
->with($this->equalTo('doctrine.dbal.default_connection'))
50-
->will($this->returnValue($conn));
44+
$container = new Container();
45+
$container->set('doctrine.dbal.default_connection', $conn);
5146

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

@@ -57,11 +52,8 @@ public function testGetDefaultConnection(): void
5752
public function testGetConnection(): void
5853
{
5954
$conn = $this->getMockBuilder(Connection::class)->disableOriginalConstructor()->getMock();
60-
$container = $this->getMockBuilder(ContainerInterface::class)->getMock();
61-
$container->expects($this->once())
62-
->method('get')
63-
->with($this->equalTo('doctrine.dbal.default_connection'))
64-
->will($this->returnValue($conn));
55+
$container = new Container();
56+
$container->set('doctrine.dbal.default_connection', $conn);
6557

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

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

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

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

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

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

8979
public function testGetDefaultEntityManager(): void
9080
{
9181
$em = new stdClass();
92-
$container = $this->getMockBuilder(ContainerInterface::class)->getMock();
93-
$container->expects($this->once())
94-
->method('get')
95-
->with($this->equalTo('doctrine.orm.default_entity_manager'))
96-
->will($this->returnValue($em));
82+
$container = new Container();
83+
$container->set('doctrine.orm.default_entity_manager', $em);
9784

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

@@ -103,11 +90,8 @@ public function testGetDefaultEntityManager(): void
10390
public function testGetEntityManager(): void
10491
{
10592
$em = new stdClass();
106-
$container = $this->getMockBuilder(ContainerInterface::class)->getMock();
107-
$container->expects($this->once())
108-
->method('get')
109-
->with($this->equalTo('doctrine.orm.default_entity_manager'))
110-
->will($this->returnValue($em));
93+
$container = new Container();
94+
$container->set('doctrine.orm.default_entity_manager', $em);
11195

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

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

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

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

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

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

156-
$container = $this->getMockBuilder(ContainerInterface::class)->getMock();
157-
$container->expects($this->any())
158-
->method('initialized')
159-
->withConsecutive(['doctrine.orm.uninitialized_entity_manager'], ['doctrine.orm.noproxy_entity_manager'], ['doctrine.orm.proxy_entity_manager'])
160-
->willReturnOnConsecutiveCalls(false, true, true, true);
161-
162-
$container->expects($this->any())
163-
->method('get')
164-
->withConsecutive(['doctrine.orm.noproxy_entity_manager'], ['doctrine.orm.proxy_entity_manager'], ['doctrine.orm.proxy_entity_manager'], ['doctrine.orm.proxy_entity_manager'])
165-
->willReturnOnConsecutiveCalls($noProxyManager, $proxyManager, $proxyManager, $proxyManager);
138+
$container = new Container();
139+
$container->set('doctrine.orm.noproxy_entity_manager', $noProxyManager);
140+
$container->set('doctrine.orm.proxy_entity_manager', $proxyManager);
166141

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

188-
$container = $this->createMock(ContainerInterface::class);
189-
$container->method('initialized')
190-
->withConsecutive(['doctrine.orm.uninitialized_entity_manager'], ['doctrine.orm.ghost_entity_manager'])
191-
->willReturnOnConsecutiveCalls(false, true, true);
192-
$container->method('get')
193-
->withConsecutive(['doctrine.orm.ghost_entity_manager'], ['doctrine.orm.ghost_entity_manager'], ['doctrine.orm.ghost_entity_manager'])
194-
->willReturnOnConsecutiveCalls($ghostManager, $ghostManager, $ghostManager);
163+
$container = new Container();
164+
$container->set('doctrine.orm.ghost_entity_manager', $ghostManager);
195165

196166
$entityManagers = [
197167
'uninitialized' => 'doctrine.orm.uninitialized_entity_manager',

Tests/Repository/ContainerRepositoryFactoryTest.php

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
use Doctrine\ORM\Mapping\ClassMetadata;
1111
use Doctrine\Persistence\ObjectRepository;
1212
use PHPUnit\Framework\TestCase;
13-
use Psr\Container\ContainerInterface;
1413
use RuntimeException;
1514
use stdClass;
15+
use Symfony\Component\DependencyInjection\Container;
1616

1717
use function interface_exists;
1818

@@ -127,19 +127,13 @@ public function testCustomRepositoryIsNotAValidClass(): void
127127
}
128128

129129
/** @param array<string, object> $services */
130-
private function createContainer(array $services): ContainerInterface
130+
private function createContainer(array $services): Container
131131
{
132-
$container = $this->getMockBuilder(ContainerInterface::class)->getMock();
133-
$container->expects($this->any())
134-
->method('has')
135-
->willReturnCallback(static function ($id) use ($services) {
136-
return isset($services[$id]);
137-
});
138-
$container->expects($this->any())
139-
->method('get')
140-
->willReturnCallback(static function ($id) use ($services) {
141-
return $services[$id];
142-
});
132+
$container = new Container();
133+
134+
foreach ($services as $id => $service) {
135+
$container->set($id, $service);
136+
}
143137

144138
return $container;
145139
}

0 commit comments

Comments
 (0)