Skip to content

Commit 47ff875

Browse files
Merge pull request #275 from possi/2.x
refactor: MongoDB Integration + MongoDB Tests
2 parents 94d9484 + 743a5ab commit 47ff875

22 files changed

+470
-35
lines changed

.github/workflows/qa.yml

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ jobs:
99

1010
steps:
1111
- uses: actions/checkout@master
12+
- name: Delete composer files to avoid installing dependencies
13+
run: rm -v composer.json
1214
- name: PHP-CS-Fixer
1315
uses: docker://jakzal/phpqa:1.80.0-php7.4-alpine
1416
with:

.github/workflows/tests.yml

+11-1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ jobs:
6868
--health-retries 5
6969
ports:
7070
- 5432:5432
71+
mongodb:
72+
image: mongo:5
73+
ports:
74+
- 27017:27017
7175

7276
steps:
7377
- name: Install mysqldump
@@ -76,6 +80,12 @@ jobs:
7680
sudo apt install -y -q ${{ matrix.mysql-client }}
7781
mysqldump --version
7882
83+
- name: Install mongodb database tools
84+
run: |
85+
wget https://fastdl.mongodb.org/tools/db/mongodb-database-tools-debian92-x86_64-100.3.1.deb
86+
sudo apt install ./mongodb-database-tools-*.deb
87+
rm -f mongodb-database-tools-*.deb
88+
7989
- name: Checkout
8090
uses: actions/checkout@v4
8191

@@ -119,7 +129,7 @@ jobs:
119129
run: composer show
120130

121131
- name: Set up hosts file
122-
run: echo '127.0.0.1 mariadb postgres' | sudo tee -a /etc/hosts
132+
run: echo '127.0.0.1 mariadb postgres mongodb' | sudo tee -a /etc/hosts
123133

124134
- name: Run tests
125135
# In phpunit.xml.dist, tests annotated with "@group mysql" are excluded, revert this

Dockerfile

+5-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ RUN apt-get update \
1212
pdo_mysql \
1313
pdo_pgsql \
1414
pdo_sqlite \
15+
&& pecl install mongodb \
16+
&& docker-php-ext-enable mongodb \
17+
&& wget https://fastdl.mongodb.org/tools/db/mongodb-database-tools-debian92-x86_64-100.3.1.deb \
18+
&& apt install ./mongodb-database-tools-*.deb \
1519
&& apt-get clean \
16-
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /usr/share/doc/*
20+
&& rm -rf mongodb-database-tools-*.deb /var/lib/apt/lists/* /tmp/* /var/tmp/* /usr/share/doc/*
1721

1822
COPY --from=composer:2 /usr/bin/composer /usr/local/bin/composer

composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"doctrine/data-fixtures": "^1.7",
3030
"doctrine/doctrine-bundle": "^2.11",
3131
"doctrine/doctrine-fixtures-bundle": "^3.5.1 || ^4.0",
32+
"doctrine/mongodb-odm-bundle": "^4.2 || ^5.0",
3233
"doctrine/orm": "^2.7",
3334
"doctrine/phpcr-bundle": "^2.4.3 || ^3.0",
3435
"doctrine/phpcr-odm": "^1.7.2 || ^2.0",

docker-compose.yml

+5
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,8 @@ services:
2323
working_dir: /application
2424
volumes:
2525
- '.:/application'
26+
27+
mongodb:
28+
image: mongo:5
29+
ports:
30+
- '27017:27017'
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Liip\TestFixturesBundle;
6+
7+
use Doctrine\Common\DataFixtures\Loader;
8+
9+
interface FixturesLoaderFactoryInterface
10+
{
11+
/**
12+
* Retrieve Doctrine DataFixtures loader.
13+
*/
14+
public function getFixtureLoader(array $classNames): Loader;
15+
16+
}

src/Resources/config/database_tools.xml

+6-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
<argument type="service" id="service_container" />
1010
<argument type="service" id="doctrine.fixtures.loader" on-invalid="null"/>
1111
</service>
12+
<service id="Liip\TestFixturesBundle\Services\MongoDBFixturesLoaderFactory" public="true">
13+
<argument type="service" id="service_container" />
14+
<argument type="service" id="doctrine_mongodb.odm.symfony.fixtures.loader" on-invalid="null"/>
15+
</service>
1216

1317
<service id="Liip\TestFixturesBundle\Services\DatabaseBackup\SqliteDatabaseBackup" public="true">
1418
<argument type="service" id="service_container" />
@@ -22,7 +26,7 @@
2226

2327
<service id="Liip\TestFixturesBundle\Services\DatabaseBackup\MongodbDatabaseBackup" public="true">
2428
<argument type="service" id="service_container" />
25-
<argument type="service" id="Liip\TestFixturesBundle\Services\FixturesLoaderFactory" />
29+
<argument type="service" id="Liip\TestFixturesBundle\Services\MongoDBFixturesLoaderFactory" />
2630
</service>
2731

2832
<service id="Liip\TestFixturesBundle\Services\DatabaseTools\ORMDatabaseTool" public="false">
@@ -35,7 +39,7 @@
3539
</service>
3640
<service id="Liip\TestFixturesBundle\Services\DatabaseTools\MongoDBDatabaseTool" public="false">
3741
<argument type="service" id="service_container" />
38-
<argument type="service" id="Liip\TestFixturesBundle\Services\FixturesLoaderFactory" />
42+
<argument type="service" id="Liip\TestFixturesBundle\Services\MongoDBFixturesLoaderFactory" />
3943
</service>
4044
<service id="Liip\TestFixturesBundle\Services\DatabaseTools\PHPCRDatabaseTool" public="false">
4145
<argument type="service" id="service_container" />

src/Services/DatabaseBackup/AbstractDatabaseBackup.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
namespace Liip\TestFixturesBundle\Services\DatabaseBackup;
1515

16-
use Liip\TestFixturesBundle\Services\FixturesLoaderFactory;
16+
use Liip\TestFixturesBundle\FixturesLoaderFactoryInterface;
1717
use Symfony\Component\DependencyInjection\ContainerInterface;
1818

1919
/**
@@ -37,7 +37,7 @@ abstract class AbstractDatabaseBackup implements DatabaseBackupInterface
3737
*/
3838
protected $classNames = [];
3939

40-
public function __construct(ContainerInterface $container, FixturesLoaderFactory $fixturesLoaderFactory)
40+
public function __construct(ContainerInterface $container, FixturesLoaderFactoryInterface $fixturesLoaderFactory)
4141
{
4242
$this->container = $container;
4343
$this->fixturesLoaderFactory = $fixturesLoaderFactory;

src/Services/DatabaseTools/AbstractDatabaseTool.php

+4-24
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,10 @@
1515

1616
use Doctrine\Bundle\FixturesBundle\Loader\SymfonyFixturesLoader;
1717
use Doctrine\Common\DataFixtures\Executor\AbstractExecutor;
18-
use Doctrine\DBAL\Connection;
19-
use Doctrine\DBAL\Platforms\AbstractMySQLPlatform;
20-
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
21-
use Doctrine\DBAL\Platforms\SqlitePlatform;
2218
use Doctrine\Persistence\ManagerRegistry;
2319
use Doctrine\Persistence\ObjectManager;
20+
use Liip\TestFixturesBundle\FixturesLoaderFactoryInterface;
2421
use Liip\TestFixturesBundle\Services\DatabaseBackup\DatabaseBackupInterface;
25-
use Liip\TestFixturesBundle\Services\FixturesLoaderFactory;
2622
use Symfony\Component\DependencyInjection\ContainerInterface;
2723
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
2824

@@ -39,7 +35,7 @@ abstract class AbstractDatabaseTool
3935
/** @var EventDispatcherInterface */
4036
protected $eventDispatcher;
4137

42-
protected $fixturesLoaderFactory;
38+
protected FixturesLoaderFactoryInterface $fixturesLoaderFactory;
4339

4440
/**
4541
* @var ManagerRegistry
@@ -61,8 +57,6 @@ abstract class AbstractDatabaseTool
6157
*/
6258
protected $om;
6359

64-
protected Connection $connection;
65-
6660
/**
6761
* @var int|null
6862
*/
@@ -80,7 +74,7 @@ abstract class AbstractDatabaseTool
8074
*/
8175
private static $cachedMetadatas = [];
8276

83-
public function __construct(ContainerInterface $container, FixturesLoaderFactory $fixturesLoaderFactory)
77+
public function __construct(ContainerInterface $container, FixturesLoaderFactoryInterface $fixturesLoaderFactory)
8478
{
8579
$this->container = $container;
8680
$this->eventDispatcher = $container->get('event_dispatcher');
@@ -106,7 +100,6 @@ public function setObjectManagerName(string $omName = null): void
106100
{
107101
$this->omName = $omName;
108102
$this->om = $this->registry->getManager($omName);
109-
$this->connection = $this->registry->getConnection($omName);
110103
}
111104

112105
public function setRegistryName(string $registryName): void
@@ -271,18 +264,5 @@ protected function getCacheMetadataParameter()
271264
&& false !== $this->container->getParameter(self::CACHE_METADATA_PARAMETER_NAME);
272265
}
273266

274-
private function getPlatformName(): string
275-
{
276-
$platform = $this->connection->getDatabasePlatform();
277-
278-
if ($platform instanceof AbstractMySQLPlatform) {
279-
return 'mysql';
280-
} elseif ($platform instanceof SqlitePlatform) {
281-
return 'sqlite';
282-
} elseif ($platform instanceof PostgreSQLPlatform) {
283-
return 'pgsql';
284-
}
285-
286-
return (new \ReflectionClass($platform))->getShortName();
287-
}
267+
abstract protected function getPlatformName(): string;
288268
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Liip/TestFixturesBundle
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Liip\TestFixturesBundle\Services\DatabaseTools;
13+
14+
use Doctrine\DBAL\Connection;
15+
use Doctrine\DBAL\Platforms\AbstractMySQLPlatform;
16+
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
17+
use Doctrine\DBAL\Platforms\SqlitePlatform;
18+
19+
abstract class AbstractDbalDatabaseTool extends AbstractDatabaseTool
20+
{
21+
protected Connection $connection;
22+
23+
public function setObjectManagerName(string $omName = null): void
24+
{
25+
parent::setObjectManagerName($omName);
26+
$this->connection = $this->registry->getConnection($omName);
27+
}
28+
29+
protected function getPlatformName(): string
30+
{
31+
$platform = $this->connection->getDatabasePlatform();
32+
33+
if ($platform instanceof AbstractMySQLPlatform) {
34+
return 'mysql';
35+
} elseif ($platform instanceof SqlitePlatform) {
36+
return 'sqlite';
37+
} elseif ($platform instanceof PostgreSQLPlatform) {
38+
return 'pgsql';
39+
}
40+
41+
return parent::getPlatformName();
42+
}
43+
}

src/Services/DatabaseTools/MongoDBDatabaseTool.php

+5
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,9 @@ protected function createDatabaseOnce(): void
119119
self::$databaseCreated = true;
120120
}
121121
}
122+
123+
protected function getPlatformName(): string
124+
{
125+
return 'mongodb';
126+
}
122127
}

src/Services/DatabaseTools/ORMDatabaseTool.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
/**
3232
* @author Aleksey Tupichenkov <[email protected]>
3333
*/
34-
class ORMDatabaseTool extends AbstractDatabaseTool
34+
class ORMDatabaseTool extends AbstractDbalDatabaseTool
3535
{
3636
/**
3737
* @var EntityManager

src/Services/DatabaseTools/PHPCRDatabaseTool.php

+5
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,9 @@ protected function getInitializerManager(): ?InitializerManager
119119

120120
return $this->container->has($serviceName) ? $this->container->get($serviceName) : null;
121121
}
122+
123+
protected function getPlatformName(): string
124+
{
125+
return 'phpcr';
126+
}
122127
}

src/Services/FixturesLoaderFactory.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515

1616
use Doctrine\Bundle\FixturesBundle\Loader\SymfonyFixturesLoader;
1717
use Doctrine\Common\DataFixtures\Loader;
18+
use Liip\TestFixturesBundle\FixturesLoaderFactoryInterface;
1819
use Symfony\Component\DependencyInjection\ContainerInterface;
1920

2021
/**
2122
* @author Aleksey Tupichenkov <[email protected]>
2223
*/
23-
final class FixturesLoaderFactory
24+
final class FixturesLoaderFactory implements FixturesLoaderFactoryInterface
2425
{
2526
private ContainerInterface $container;
2627

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Liip\TestFixturesBundle\Services;
6+
7+
use Doctrine\Bundle\MongoDBBundle\Loader\SymfonyFixturesLoader;
8+
use Doctrine\Common\DataFixtures\Loader;
9+
use Liip\TestFixturesBundle\FixturesLoaderFactoryInterface;
10+
use Symfony\Component\DependencyInjection\ContainerInterface;
11+
12+
class MongoDBFixturesLoaderFactory implements FixturesLoaderFactoryInterface
13+
{
14+
private ContainerInterface $container;
15+
16+
private ?SymfonyFixturesLoader $loader;
17+
18+
public function __construct(ContainerInterface $container, SymfonyFixturesLoader $loader = null)
19+
{
20+
$this->container = $container;
21+
$this->loader = $loader;
22+
}
23+
24+
/**
25+
* Retrieve Doctrine DataFixtures loader.
26+
*/
27+
public function getFixtureLoader(array $classNames): Loader
28+
{
29+
if (null === $this->loader) {
30+
throw new \BadMethodCallException('doctrine/doctrine-fixtures-bundle must be installed to use this method.');
31+
}
32+
33+
$loader = new SymfonyFixturesLoaderWrapper($this->loader);
34+
foreach ($classNames as $className) {
35+
$loader->loadFixturesClass($className);
36+
}
37+
38+
return $loader;
39+
}
40+
}

src/Services/SymfonyFixturesLoaderWrapper.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,14 @@
1313

1414
namespace Liip\TestFixturesBundle\Services;
1515

16-
use Doctrine\Bundle\FixturesBundle\Loader\SymfonyFixturesLoader;
1716
use Doctrine\Common\DataFixtures\FixtureInterface;
1817
use Doctrine\Common\DataFixtures\Loader;
1918

2019
final class SymfonyFixturesLoaderWrapper extends Loader
2120
{
22-
private SymfonyFixturesLoader $symfonyFixturesLoader;
21+
private Loader $symfonyFixturesLoader;
2322

24-
public function __construct(SymfonyFixturesLoader $symfonyFixturesLoader)
23+
public function __construct(Loader $symfonyFixturesLoader)
2524
{
2625
$this->symfonyFixturesLoader = $symfonyFixturesLoader;
2726
}

tests/App/config/bundles.php

+1
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@
2121
Liip\TestFixturesBundle\LiipTestFixturesBundle::class => ['all' => true],
2222
Liip\Acme\Tests\App\AcmeBundle::class => ['all' => true],
2323
Doctrine\Bundle\PHPCRBundle\DoctrinePHPCRBundle::class => ['phpcr' => true],
24+
Doctrine\Bundle\MongoDBBundle\DoctrineMongoDBBundle::class => ['mongodb' => true],
2425
];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Liip/TestFixturesBundle
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Liip\Acme\Tests\AppConfigMongodb;
13+
14+
use Doctrine\Bundle\MongoDBBundle\DoctrineMongoDBBundle;
15+
use Liip\Acme\Tests\AppConfig\AppConfigKernel;
16+
use Symfony\Component\Config\Loader\LoaderInterface;
17+
use Symfony\Component\DependencyInjection\ContainerBuilder;
18+
19+
class AppConfigMongodbKernel extends AppConfigKernel
20+
{
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
public function getCacheDir(): string
25+
{
26+
return __DIR__.'/var/cache/';
27+
}
28+
29+
protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader): void
30+
{
31+
// Load the default file.
32+
parent::configureContainer($container, $loader);
33+
34+
// Load the file with MongoDB configuration
35+
if (class_exists(DoctrineMongoDBBundle::class)) {
36+
$loader->load(__DIR__.'/config.yml');
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)