Skip to content

Commit

Permalink
Update existing tests and add new tests cover added classes
Browse files Browse the repository at this point in the history
  • Loading branch information
GwendolenLynch committed Jul 1, 2023
1 parent 1d78c25 commit 1764330
Show file tree
Hide file tree
Showing 13 changed files with 1,052 additions and 461 deletions.
50 changes: 37 additions & 13 deletions tests/AbstractFunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Jsor\Doctrine\PostGIS;

use Doctrine\Common\EventManager;
use Doctrine\DBAL\Configuration as DBALConfiguration;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;
Expand All @@ -13,11 +14,13 @@
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
use Doctrine\ORM\Tools\SchemaTool;
use Doctrine\ORM\Tools\ToolEvents;
use Doctrine\Persistence\Mapping\Driver\MappingDriver;
use Jsor\Doctrine\PostGIS\Event\DBALSchemaEventSubscriber;
use Jsor\Doctrine\PostGIS\Event\ORMSchemaEventSubscriber;
use Jsor\Doctrine\PostGIS\Driver\Middleware;
use Jsor\Doctrine\PostGIS\Event\ORMSchemaEventListener;
use Jsor\Doctrine\PostGIS\Functions\Configurator;
use Symfony\Bridge\Doctrine\SchemaListener\MessengerTransportDoctrineSchemaSubscriber;
use Jsor\Doctrine\PostGIS\Schema\SchemaManagerFactory;
use Symfony\Bridge\Doctrine\SchemaListener\MessengerTransportDoctrineSchemaListener;
use Symfony\Component\Messenger\Bridge\Doctrine\Transport\Connection as MessengerConnection;
use Symfony\Component\Messenger\Bridge\Doctrine\Transport\DoctrineTransport;
use Symfony\Component\Messenger\Bridge\Doctrine\Transport\PostgreSqlConnection;
Expand All @@ -27,6 +30,8 @@ abstract class AbstractFunctionalTestCase extends AbstractTestCase
{
private static ?Connection $_conn = null;

private static ?EventManager $_eventManager = null;

private static ?MessengerConnection $_messengerConn = null;

/**
Expand All @@ -38,6 +43,13 @@ abstract class AbstractFunctionalTestCase extends AbstractTestCase

private ?SchemaTool $_schemaTool = null;

protected function setUp(): void
{
parent::setUp();

static::_registerTypes();
}

protected function tearDown(): void
{
parent::tearDown();
Expand Down Expand Up @@ -88,15 +100,28 @@ protected function _getDbParams(): array
];
}

protected function _getEventManager(): EventManager
{
if (!self::$_eventManager) {
self::$_eventManager = new EventManager();
}

return self::$_eventManager;
}

protected function _getConnection(): Connection
{
if (!isset(self::$_conn)) {
if (class_exists(ORMConfiguration::class)) {
self::$_conn = DriverManager::getConnection($this->_getDbParams(), new ORMConfiguration());
Configurator::configure(self::$_conn->getConfiguration());
$config = new ORMConfiguration();
Configurator::configure($config);
} else {
self::$_conn = DriverManager::getConnection($this->_getDbParams(), new DBALConfiguration());
$config = new DBALConfiguration();
}
$config->setMiddlewares([new Middleware()]);
$config->setSchemaManagerFactory(new SchemaManagerFactory());

self::$_conn = DriverManager::getConnection($this->_getDbParams(), $config, $this->_getEventManager());

self::$_messengerConn = new PostgreSqlConnection(
[
Expand All @@ -106,8 +131,9 @@ protected function _getConnection(): Connection
self::$_conn,
);

self::$_conn->getEventManager()->addEventSubscriber(
new MessengerTransportDoctrineSchemaSubscriber(
$this->_getEventManager()->addEventListener(
ToolEvents::postGenerateSchema,
new MessengerTransportDoctrineSchemaListener(
[
new DoctrineTransport(
self::$_messengerConn,
Expand All @@ -118,9 +144,7 @@ protected function _getConnection(): Connection
);

if (class_exists(ORMConfiguration::class)) {
self::$_conn->getEventManager()->addEventSubscriber(new ORMSchemaEventSubscriber());
} else {
self::$_conn->getEventManager()->addEventSubscriber(new DBALSchemaEventSubscriber());
$this->_getEventManager()->addEventListener('postGenerateSchemaTable', new ORMSchemaEventListener());
}

if (!Type::hasType('tsvector')) {
Expand All @@ -145,7 +169,7 @@ protected function _getMessengerConnection(): MessengerConnection
return self::$_messengerConn;
}

protected function _getEntityManager(ORMConfiguration $config = null): EntityManager
protected function _getEntityManager(ORMConfiguration $config = null): EntityManagerInterface
{
if (null !== $this->_em) {
return $this->_em;
Expand All @@ -159,7 +183,7 @@ protected function _getEntityManager(ORMConfiguration $config = null): EntityMan

$this->_setupConfiguration($config);

$em = EntityManager::create($connection, $config);
$em = new EntityManager($connection, $config, $this->_getEventManager());

return $this->_em = $em;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/AbstractTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

abstract class AbstractTestCase extends TestCase
{
protected function _registerTypes(): void
protected static function _registerTypes(): void
{
if (!Type::hasType('geometry')) {
Type::addType('geometry', GeometryType::class);
Expand Down
76 changes: 76 additions & 0 deletions tests/Driver/DriverTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

declare(strict_types=1);

namespace Jsor\Doctrine\PostGIS\Driver;

use Doctrine\DBAL\Driver\API\ExceptionConverter;
use Doctrine\DBAL\Driver\PDO\PgSQL\Driver as PgSQLDriver;
use Doctrine\DBAL\Types\Type;
use Jsor\Doctrine\PostGIS\Types\PostGISType;
use PHPUnit\Framework\TestCase;

/**
* @covers \Jsor\Doctrine\PostGIS\Driver\Driver
*
* @runTestsInSeparateProcesses
*
* @internal
*/
final class DriverTest extends TestCase
{
public function testConnect(): void
{
static::assertFalse(Type::hasType(PostGISType::GEOGRAPHY));
static::assertFalse(Type::hasType(PostGISType::GEOMETRY));

$driver = $this->getDriver();
$driver->connect([
'driver' => getenv('DB_TYPE'),
'user' => getenv('DB_USER'),
'password' => getenv('DB_PASSWORD'),
'host' => getenv('DB_HOST'),
'dbname' => getenv('DB_NAME'),
'port' => getenv('DB_PORT'),
]);

static::assertTrue(Type::hasType(PostGISType::GEOGRAPHY));
static::assertTrue(Type::hasType(PostGISType::GEOMETRY));
}

public function testGetDatabasePlatform(): void
{
$driver = $this->getDriver();

static::assertInstanceOf(PostGISPlatform::class, $driver->getDatabasePlatform());
}

public function providerVersions(): iterable
{
yield [11];
yield [12];
yield [13];
yield [14];
yield [15];
}

/** @dataProvider providerVersions */
public function testCreateDatabasePlatformForVersion(int $version): void
{
$driver = $this->getDriver();

static::assertInstanceOf(PostGISPlatform::class, $driver->createDatabasePlatformForVersion($version));
}

public function testGetExceptionConverter(): void
{
$driver = $this->getDriver();

static::assertInstanceOf(ExceptionConverter::class, $driver->getExceptionConverter());
}

private function getDriver(): Driver
{
return new Driver(new PgSQLDriver());
}
}
23 changes: 23 additions & 0 deletions tests/Driver/MiddlewareTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Jsor\Doctrine\PostGIS\Driver;

use Doctrine\DBAL\Driver\PgSQL\Driver as PgSQLDriver;
use PHPUnit\Framework\TestCase;

/**
* @covers \Jsor\Doctrine\PostGIS\Driver\Middleware
*
* @internal
*/
final class MiddlewareTest extends TestCase
{
public function testWrap(): void
{
$middleware = new Middleware();

static::assertInstanceOf(Driver::class, $middleware->wrap(new PgSQLDriver()));
}
}
Loading

0 comments on commit 1764330

Please sign in to comment.