|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace BehatApiContext\Tests\Unit\Service\ResetManager; |
| 6 | + |
| 7 | +use BehatApiContext\Service\ResetManager\DoctrineResetManager; |
| 8 | +use Doctrine\DBAL\Connection; |
| 9 | +use Doctrine\ORM\EntityManagerInterface; |
| 10 | +use PHPUnit\Framework\TestCase; |
| 11 | +use Symfony\Component\DependencyInjection\ContainerInterface; |
| 12 | +use Symfony\Component\HttpFoundation\Request; |
| 13 | +use Symfony\Component\HttpKernel\KernelInterface; |
| 14 | + |
| 15 | +class DoctrineResetManagerTest extends TestCase |
| 16 | +{ |
| 17 | + private DoctrineResetManager $resetManager; |
| 18 | + |
| 19 | + protected function setUp(): void |
| 20 | + { |
| 21 | + $this->resetManager = new DoctrineResetManager(); |
| 22 | + } |
| 23 | + |
| 24 | + public function testNeedsResetReturnsFalseForGetMethod(): void |
| 25 | + { |
| 26 | + $this->assertFalse($this->resetManager->needsReset(Request::METHOD_GET)); |
| 27 | + $this->assertFalse($this->resetManager->needsReset('get')); |
| 28 | + } |
| 29 | + |
| 30 | + public function testNeedsResetReturnsTrueForOtherMethods(): void |
| 31 | + { |
| 32 | + $this->assertTrue($this->resetManager->needsReset(Request::METHOD_POST)); |
| 33 | + $this->assertTrue($this->resetManager->needsReset(Request::METHOD_PUT)); |
| 34 | + $this->assertTrue($this->resetManager->needsReset('delete')); |
| 35 | + } |
| 36 | + |
| 37 | + public function testResetDoesNothingIfNoEntityManagersParameter(): void |
| 38 | + { |
| 39 | + $kernel = $this->createMock(KernelInterface::class); |
| 40 | + $container = $this->createMock(ContainerInterface::class); |
| 41 | + |
| 42 | + $kernel->expects($this->once()) |
| 43 | + ->method('getContainer') |
| 44 | + ->willReturn($container); |
| 45 | + |
| 46 | + $container->expects($this->once()) |
| 47 | + ->method('hasParameter') |
| 48 | + ->with('doctrine.entity_managers') |
| 49 | + ->willReturn(false); |
| 50 | + |
| 51 | + $container->expects($this->never())->method('getParameter'); |
| 52 | + $container->expects($this->never())->method('initialized'); |
| 53 | + $container->expects($this->never())->method('get'); |
| 54 | + |
| 55 | + $this->resetManager->reset($kernel); |
| 56 | + } |
| 57 | + |
| 58 | + public function testResetClearsEntityManagersAndClosesConnections(): void |
| 59 | + { |
| 60 | + $kernel = $this->createMock(KernelInterface::class); |
| 61 | + $container = $this->createMock(ContainerInterface::class); |
| 62 | + $entityManager = $this->createMock(EntityManagerInterface::class); |
| 63 | + $connection = $this->createMock(Connection::class); |
| 64 | + |
| 65 | + $entityManagers = ['em1', 'em2']; |
| 66 | + |
| 67 | + $kernel->method('getContainer')->willReturn($container); |
| 68 | + $container->method('hasParameter')->with('doctrine.entity_managers')->willReturn(true); |
| 69 | + $container->method('getParameter')->with('doctrine.entity_managers')->willReturn($entityManagers); |
| 70 | + |
| 71 | + $container->method('initialized') |
| 72 | + ->willReturnMap([ |
| 73 | + ['em1', true], |
| 74 | + ['em2', false], |
| 75 | + ]); |
| 76 | + |
| 77 | + $container->method('get')->willReturnCallback(function ($id) use ($entityManager) { |
| 78 | + if ($id === 'em1') { |
| 79 | + return $entityManager; |
| 80 | + } |
| 81 | + return null; |
| 82 | + }); |
| 83 | + |
| 84 | + $entityManager->expects($this->once())->method('clear'); |
| 85 | + |
| 86 | + $entityManager->method('getConnection')->willReturn($connection); |
| 87 | + |
| 88 | + $connection->method('isConnected')->willReturn(true); |
| 89 | + $connection->expects($this->once())->method('close'); |
| 90 | + |
| 91 | + $this->resetManager->reset($kernel); |
| 92 | + } |
| 93 | + |
| 94 | + public function testResetDoesNotCloseConnectionIfNotConnected(): void |
| 95 | + { |
| 96 | + $kernel = $this->createMock(KernelInterface::class); |
| 97 | + $container = $this->createMock(ContainerInterface::class); |
| 98 | + $entityManager = $this->createMock(EntityManagerInterface::class); |
| 99 | + $connection = $this->createMock(Connection::class); |
| 100 | + |
| 101 | + $entityManagers = ['em1']; |
| 102 | + |
| 103 | + $kernel->method('getContainer')->willReturn($container); |
| 104 | + $container->method('hasParameter')->with('doctrine.entity_managers')->willReturn(true); |
| 105 | + $container->method('getParameter')->with('doctrine.entity_managers')->willReturn($entityManagers); |
| 106 | + |
| 107 | + $container->method('initialized')->with('em1')->willReturn(true); |
| 108 | + $container->method('get')->with('em1')->willReturn($entityManager); |
| 109 | + |
| 110 | + $entityManager->expects($this->once())->method('clear'); |
| 111 | + |
| 112 | + $entityManager->method('getConnection')->willReturn($connection); |
| 113 | + |
| 114 | + $connection->method('isConnected')->willReturn(false); |
| 115 | + $connection->expects($this->never())->method('close'); |
| 116 | + |
| 117 | + $this->resetManager->reset($kernel); |
| 118 | + } |
| 119 | +} |
0 commit comments