diff --git a/Tests/Mapping/ContainerEntityListenerResolverTest.php b/Tests/Mapping/ContainerEntityListenerResolverTest.php index e71a62eae..0f2e58bd6 100644 --- a/Tests/Mapping/ContainerEntityListenerResolverTest.php +++ b/Tests/Mapping/ContainerEntityListenerResolverTest.php @@ -5,10 +5,9 @@ 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; @@ -16,8 +15,7 @@ class ContainerEntityListenerResolverTest extends TestCase { private ContainerEntityListenerResolver $resolver; - /** @var ContainerInterface&MockObject */ - private ContainerInterface $container; + private Container $container; public static function setUpBeforeClass(): void { @@ -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); } @@ -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)); @@ -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'); diff --git a/Tests/RegistryTest.php b/Tests/RegistryTest.php index db3da9663..ebcecbdfc 100644 --- a/Tests/RegistryTest.php +++ b/Tests/RegistryTest.php @@ -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; @@ -26,16 +26,14 @@ 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()); } @@ -43,11 +41,8 @@ public function testGetDefaultEntityManagerName(): void 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'); @@ -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'); @@ -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.'); @@ -80,8 +71,7 @@ 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()); } @@ -89,11 +79,8 @@ public function testGetConnectionNames(): void 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'); @@ -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'); @@ -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( @@ -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( @@ -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', @@ -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', diff --git a/Tests/Repository/ContainerRepositoryFactoryTest.php b/Tests/Repository/ContainerRepositoryFactoryTest.php index eac247801..ff53decd5 100644 --- a/Tests/Repository/ContainerRepositoryFactoryTest.php +++ b/Tests/Repository/ContainerRepositoryFactoryTest.php @@ -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; @@ -127,19 +127,13 @@ public function testCustomRepositoryIsNotAValidClass(): void } /** @param array $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; }