-
-
Notifications
You must be signed in to change notification settings - Fork 455
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Bundle\DoctrineBundle\Tests; | ||
|
||
use Doctrine\DBAL\Connection; | ||
use Doctrine\DBAL\Driver; | ||
use Doctrine\DBAL\Exception as DBALException; | ||
use Doctrine\DBAL\Platforms\AbstractPlatform; | ||
use Doctrine\DBAL\Result; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
use Doctrine\Persistence\ManagerRegistry; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Bridge\Doctrine\Middleware\DoctrineConnectionDriver; | ||
use Symfony\Bridge\Doctrine\Middleware\DoctrineConnectionMiddleware; | ||
use Symfony\Component\DependencyInjection\Container; | ||
|
||
/** | ||
* Based on https://github.com/Baldinof/roadrunner-bundle/blob/3.x/src/Integration/Doctrine/DoctrineORMMiddleware.php | ||
*/ | ||
class DoctrineConnectionMiddlewareTest extends TestCase | ||
{ | ||
public const CONNECTION_NAME = 'doctrine.connection'; | ||
public const MANAGER_NAME = 'doctrine.manager'; | ||
|
||
private $managerRegistryMock; | ||
Check failure on line 27 in Tests/DoctrineConnectionMiddlewareTest.php GitHub Actions / Coding Standards / Coding Standards (8.2)
|
||
private $connectionMock; | ||
Check failure on line 28 in Tests/DoctrineConnectionMiddlewareTest.php GitHub Actions / Coding Standards / Coding Standards (8.2)
|
||
private $container; | ||
Check failure on line 29 in Tests/DoctrineConnectionMiddlewareTest.php GitHub Actions / Coding Standards / Coding Standards (8.2)
|
||
private $driver; | ||
Check failure on line 30 in Tests/DoctrineConnectionMiddlewareTest.php GitHub Actions / Coding Standards / Coding Standards (8.2)
|
||
private $doctrineConnectionDriver; | ||
Check failure on line 31 in Tests/DoctrineConnectionMiddlewareTest.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->connectionDriverMock = $this->createMock(Driver\Connection::class); | ||
Check failure on line 40 in Tests/DoctrineConnectionMiddlewareTest.php GitHub Actions / Static Analysis with PsalmUndefinedThisPropertyAssignment
|
||
$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->driver = $this->createMock(Driver::class); | ||
$this->driver->method('connect')->willReturn($this->connectionDriverMock); | ||
|
||
$this->doctrineConnectionDriver = new DoctrineConnectionDriver($this->driver, $this->managerRegistryMock, $this->container); | ||
Check failure on line 52 in Tests/DoctrineConnectionMiddlewareTest.php GitHub Actions / Static Analysis with PsalmUndefinedClass
|
||
} | ||
|
||
public function testSkipNotInitializedConnections() | ||
{ | ||
$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->doctrineConnectionDriver->connect([]); | ||
} | ||
|
||
public function testSkipWhenNotConnected(): 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->doctrineConnectionDriver->connect([]); | ||
} | ||
|
||
public function testItClosesNotPingableConnection(): void | ||
{ | ||
$this->connectionMock->expects($this->exactly(2))->method('executeQuery') | ||
->willReturnCallback(function () { | ||
static $counter = 0; | ||
|
||
if (1 === ++$counter) { | ||
throw $this->createMock(DBALException::class); | ||
} | ||
|
||
return $this->createMock(Result::class); | ||
}); | ||
|
||
$this->connectionMock->method('isConnected')->willReturn(true); | ||
$this->connectionMock->expects($this->once())->method('close'); | ||
$this->connectionMock->expects($this->never())->method('connect'); | ||
|
||
$this->doctrineConnectionDriver->connect([]); | ||
} | ||
|
||
public function testItDoesNotClosePingableConnection(): 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->doctrineConnectionDriver->connect([]); | ||
} | ||
|
||
public function testItForcesRebootOnClosedManagerWhenMissingProxySupport() | ||
{ | ||
$manager = $this->createMock(EntityManagerInterface::class); | ||
$this->container->set(self::MANAGER_NAME, $manager); | ||
|
||
$manager->expects($this->once())->method('isOpen')->willReturn(false); | ||
$this->managerRegistryMock->expects($this->once()) | ||
->method('resetManager') | ||
->with(self::MANAGER_NAME) | ||
; | ||
|
||
$this->doctrineConnectionDriver->connect([]); | ||
} | ||
} |