Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Bundle\DoctrineBundle\Tests; | ||
|
||
use Doctrine\DBAL\Connection; | ||
use Doctrine\DBAL\Exception as DBALException; | ||
use Doctrine\DBAL\Platforms\AbstractPlatform; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
use Doctrine\Persistence\ManagerRegistry; | ||
use PHPUnit\Framework\TestCase; | ||
use ProxyManager\Proxy\LazyLoadingInterface; | ||
use Symfony\Bridge\Doctrine\Event\ForceKernelRebootEvent; | ||
use Symfony\Bridge\Doctrine\Listener\DoctrineConnectionListener; | ||
use Symfony\Component\DependencyInjection\Container; | ||
use Symfony\Component\EventDispatcher\EventDispatcher; | ||
use Symfony\Component\HttpKernel\Event\RequestEvent; | ||
|
||
/** | ||
* Based on https://github.com/Baldinof/roadrunner-bundle/blob/3.x/src/Integration/Doctrine/DoctrineORMMiddleware.php | ||
*/ | ||
class DoctrineConnectionListenerTest extends TestCase | ||
{ | ||
public const CONNECTION_NAME = 'doctrine.connection'; | ||
public const MANAGER_NAME = 'doctrine.manager'; | ||
|
||
private $managerRegistryMock; | ||
Check failure on line 28 in Tests/DoctrineConnectionListenerTest.php GitHub Actions / Coding Standards / Coding Standards (8.2)
|
||
private $connectionMock; | ||
Check failure on line 29 in Tests/DoctrineConnectionListenerTest.php GitHub Actions / Coding Standards / Coding Standards (8.2)
|
||
private $container; | ||
Check failure on line 30 in Tests/DoctrineConnectionListenerTest.php GitHub Actions / Coding Standards / Coding Standards (8.2)
|
||
private $dispatcher; | ||
Check failure on line 31 in Tests/DoctrineConnectionListenerTest.php GitHub Actions / Coding Standards / Coding Standards (8.2)
|
||
private $listener; | ||
Check failure on line 32 in Tests/DoctrineConnectionListenerTest.php GitHub Actions / Coding Standards / Coding Standards (8.2)
|
||
private $event; | ||
Check failure on line 33 in Tests/DoctrineConnectionListenerTest.php GitHub Actions / Coding Standards / Coding Standards (8.2)
|
||
|
||
public function setUp(): void | ||
{ | ||
$platform = $this->createMock(AbstractPlatform::class); | ||
$platform->method('getDummySelectSQL')->willReturn('SELECT 1'); | ||
|
||
$this->managerRegistryMock = $this->createMock(ManagerRegistry::class); | ||
$this->connectionMock = $this->createMock(Connection::class); | ||
$this->connectionMock->method('getDatabasePlatform')->willReturn($platform); | ||
|
||
$this->container = new Container(); | ||
$this->container->set(self::CONNECTION_NAME, $this->connectionMock); | ||
|
||
$this->managerRegistryMock->method('getConnectionNames')->willReturn([self::CONNECTION_NAME]); | ||
$this->managerRegistryMock->method('getManagerNames')->willReturn([self::MANAGER_NAME]); | ||
|
||
$this->dispatcher = new EventDispatcher(); | ||
$this->event = $this->createMock(RequestEvent::class); | ||
$this->listener = new DoctrineConnectionListener($this->managerRegistryMock, $this->container, $this->dispatcher); | ||
Check failure on line 52 in Tests/DoctrineConnectionListenerTest.php GitHub Actions / Static Analysis with PsalmUndefinedClass
|
||
} | ||
|
||
public function test_skip_not_initialized_connections() | ||
{ | ||
$this->container->set(self::CONNECTION_NAME, null); | ||
|
||
$this->connectionMock->expects($this->never())->method('isConnected'); | ||
$this->connectionMock->expects($this->never())->method('executeQuery'); | ||
$this->connectionMock->expects($this->never())->method('close'); | ||
$this->connectionMock->expects($this->never())->method('connect'); | ||
|
||
$this->listener->onKernelRequest($this->event); | ||
} | ||
|
||
public function test_skip_when_not_connected(): void | ||
{ | ||
$this->connectionMock->method('isConnected')->willReturn(false); | ||
$this->connectionMock->expects($this->never())->method('executeQuery'); | ||
$this->connectionMock->expects($this->never())->method('close'); | ||
$this->connectionMock->expects($this->never())->method('connect'); | ||
|
||
$this->listener->onKernelRequest($this->event); | ||
} | ||
|
||
public function test_it_closes_not_pingable_connection(): void | ||
{ | ||
$this->connectionMock->expects($this->once())->method('executeQuery')->will($this->throwException(new DBALException())); | ||
|
||
$this->connectionMock->method('isConnected')->willReturn(true); | ||
$this->connectionMock->expects($this->once())->method('close'); | ||
$this->connectionMock->expects($this->never())->method('connect'); | ||
|
||
$this->listener->onKernelRequest($this->event); | ||
} | ||
|
||
public function test_it_does_not_close_pingable_connection(): void | ||
{ | ||
$this->connectionMock->expects($this->once())->method('executeQuery'); | ||
$this->connectionMock->method('isConnected')->willReturn(true); | ||
$this->connectionMock->expects($this->never())->method('close'); | ||
$this->connectionMock->expects($this->never())->method('connect'); | ||
|
||
$this->listener->onKernelRequest($this->event); | ||
} | ||
|
||
public function test_it_force_reboot_on_closed_manager_when_missing_proxy_support() | ||
{ | ||
$rebootForced = false; | ||
|
||
$this->dispatcher->addListener(ForceKernelRebootEvent::class, function () use (&$rebootForced) { | ||
Check failure on line 102 in Tests/DoctrineConnectionListenerTest.php GitHub Actions / Static Analysis with PsalmUndefinedClass
|
||
$rebootForced = true; | ||
}); | ||
|
||
$manager = $this->createMock(EntityManagerInterface::class); | ||
$manager->expects($this->once())->method('isOpen')->willReturn(false); | ||
|
||
$this->container->set(self::MANAGER_NAME, $manager); | ||
|
||
$this->listener->onKernelRequest($this->event); | ||
|
||
$this->assertTrue($rebootForced, 'A ForceKernelRebootEvent should have been dispatched'); | ||
} | ||
|
||
public function test_it_skip_lazy_entity_managers() | ||
{ | ||
$rebootForced = false; | ||
|
||
$this->dispatcher->addListener(ForceKernelRebootEvent::class, function () use (&$rebootForced) { | ||
Check failure on line 120 in Tests/DoctrineConnectionListenerTest.php GitHub Actions / Static Analysis with PsalmUndefinedClass
|
||
$rebootForced = true; | ||
}); | ||
|
||
$manager = $this->createMock(LazyEntityManager::class); | ||
$manager->expects($this->never())->method('isOpen'); | ||
|
||
$this->container->set(self::MANAGER_NAME, $manager); | ||
|
||
$this->listener->onKernelRequest($this->event); | ||
$this->assertFalse($rebootForced, 'A ForceKernelRebootEvent should not have been dispatched'); | ||
} | ||
} | ||
|
||
/** | ||
* @internal Allow mock of multiple interface | ||
*/ | ||
interface LazyEntityManager extends EntityManagerInterface, LazyLoadingInterface | ||
Check failure on line 137 in Tests/DoctrineConnectionListenerTest.php GitHub Actions / Static Analysis with PsalmMissingTemplateParam
|
||
{ | ||
} | ||
|