Skip to content

Commit

Permalink
feat: don't call deprecated method getName() on doctrine platform
Browse files Browse the repository at this point in the history
  • Loading branch information
alexislefebvre committed Jan 20, 2024
1 parent 0b936c0 commit 9ca61a2
Showing 1 changed file with 31 additions and 11 deletions.
42 changes: 31 additions & 11 deletions src/Services/DatabaseTools/AbstractDatabaseTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,23 @@

namespace Liip\TestFixturesBundle\Services\DatabaseTools;

use BadMethodCallException;
use Doctrine\Bundle\FixturesBundle\Loader\SymfonyFixturesLoader;
use Doctrine\Common\DataFixtures\Executor\AbstractExecutor;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Platforms\AbstractMySQLPlatform;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Persistence\ObjectManager;
use InvalidArgumentException;
use Liip\TestFixturesBundle\Services\DatabaseBackup\DatabaseBackupInterface;
use Liip\TestFixturesBundle\Services\FixturesLoaderFactory;
use ReflectionClass;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use function get_class;
use function is_string;

/**
* @author Aleksey Tupichenkov <[email protected]>
Expand Down Expand Up @@ -58,10 +66,7 @@ abstract class AbstractDatabaseTool
*/
protected $om;

/**
* @var Connection
*/
protected $connection;
protected Connection $connection;

/**
* @var int|null
Expand Down Expand Up @@ -158,21 +163,21 @@ public function loadAllFixtures(array $groups = []): ?AbstractExecutor
$loader = $this->container->get('test.service_container')->get('doctrine.fixtures.loader');
$fixtures = $loader->getFixtures($groups);
foreach ($fixtures as $fixture) {
$fixtureClasses[] = \get_class($fixture);
$fixtureClasses[] = get_class($fixture);
}
}

return $this->loadFixtures($fixtureClasses);
}

/**
* @throws \BadMethodCallException
* @throws BadMethodCallException
*/
public function loadAliceFixture(array $paths = [], bool $append = false): array
{
$persisterLoaderServiceName = 'fidry_alice_data_fixtures.loader.doctrine';
if (!$this->container->has($persisterLoaderServiceName)) {
throw new \BadMethodCallException('theofidry/alice-data-fixtures must be installed to use this method.');
throw new BadMethodCallException('theofidry/alice-data-fixtures must be installed to use this method.');
}

if (false === $append) {
Expand All @@ -193,13 +198,13 @@ protected function getBackupService(): ?DatabaseBackupInterface
{
$backupServiceParamName = strtolower('liip_test_fixtures.cache_db.'.(
('ORM' === $this->getType())
? $this->connection->getDatabasePlatform()->getName()
? $this->getPlatformName()
: $this->getType()
));

if ($this->container->hasParameter($backupServiceParamName)) {
$backupServiceName = $this->container->getParameter($backupServiceParamName);
if (\is_string($backupServiceName) && $this->container->has($backupServiceName)) {
if (is_string($backupServiceName) && $this->container->has($backupServiceName)) {
$backupService = $this->container->get($backupServiceName);
} else {
@trigger_error("Could not find {$backupServiceName} in container. Possible misconfiguration.");
Expand All @@ -217,7 +222,7 @@ protected function cleanDatabase(): void
/**
* Locate fixture files.
*
* @throws \InvalidArgumentException if a wrong path is given outside a bundle
* @throws InvalidArgumentException if a wrong path is given outside a bundle
*/
protected function locateResources(array $paths): array
{
Expand All @@ -228,7 +233,7 @@ protected function locateResources(array $paths): array
foreach ($paths as $path) {
if ('@' !== $path[0]) {
if (!file_exists($path)) {
throw new \InvalidArgumentException(sprintf('Unable to find file "%s".', $path));
throw new InvalidArgumentException(sprintf('Unable to find file "%s".', $path));
}
$files[] = $path;

Expand Down Expand Up @@ -270,4 +275,19 @@ protected function getCacheMetadataParameter()
return $this->container->hasParameter(self::CACHE_METADATA_PARAMETER_NAME)
&& false !== $this->container->getParameter(self::CACHE_METADATA_PARAMETER_NAME);
}

private function getPlatformName(): string
{
$platform = $this->connection->getDatabasePlatform();

if ($platform instanceof AbstractMySQLPlatform) {
return 'mysql';
} elseif ($platform instanceof SqlitePlatform) {
return 'sqlite';
} elseif ($platform instanceof PostgreSQLPlatform) {
return 'pgsql';
}

return (new ReflectionClass($platform))->getShortName();
}
}

0 comments on commit 9ca61a2

Please sign in to comment.