Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensures managers are not shared within tests #196

Merged
merged 2 commits into from
Dec 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion bin/doctrine_purge
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@
*/

use Doctrine\Common\DataFixtures\Purger\ORMPurger;
use Doctrine\ORM\EntityManagerInterface;

require_once __DIR__.'/../tests/Bridge/Doctrine/autoload.php';

$purger = new ORMPurger($GLOBALS['entityManager']);
/** @var EntityManagerInterface $entityManager */
$entityManager = $GLOBALS['entityManager'];

$purger = new ORMPurger($entityManager);
$purger->purge();

return 1;
13 changes: 12 additions & 1 deletion bin/eloquent_migrate
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@
* file that was distributed with this source code.
*/

use Illuminate\Database\Capsule\Manager;
use Illuminate\Database\Migrations\MigrationRepositoryInterface;
use Illuminate\Database\Migrations\Migrator;

require_once __DIR__.'/../tests/Bridge/Eloquent/autoload.php';

$GLOBALS['migrator']->run('migrations');
/**
* @var Manager $manager
* @var MigrationRepositoryInterface $repository
* @var Migrator $migrator
*/
[$manager, $repository, $migrator] = $GLOBALS['manager_repository_migrator_factory']();

$migrator->run('migrations');
13 changes: 12 additions & 1 deletion bin/eloquent_rollback
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@
* file that was distributed with this source code.
*/

use Illuminate\Database\Capsule\Manager;
use Illuminate\Database\Migrations\MigrationRepositoryInterface;
use Illuminate\Database\Migrations\Migrator;

require_once __DIR__.'/../tests/Bridge/Eloquent/autoload.php';

$GLOBALS['migrator']->rollback();
/**
* @var Manager $manager
* @var MigrationRepositoryInterface $repository
* @var Migrator $migrator
*/
[$manager, $repository, $migrator] = $GLOBALS['manager_repository_migrator_factory']();

$migrator->rollback();
12 changes: 9 additions & 3 deletions cli-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
*/

use Doctrine\DBAL\DriverManager;
use Doctrine\ODM\PHPCR\DocumentManagerInterface as PHPCRDocumentManager;
use Doctrine\ODM\PHPCR\Tools\Console\Helper\DocumentManagerHelper as PhpcrDocumentManagerHelperAlias;
use Doctrine\ORM\EntityManagerInterface as ORMEntityManager;
use Doctrine\ORM\Tools\Console\ConsoleRunner as DoctrineORMConsoleRunner;
use Jackalope\Tools\Console\Command\InitDoctrineDbalCommand as JackalopeInitDbalCommand;
use Jackalope\Tools\Console\Helper\DoctrineDbalHelper as DoctrinePHPCRHelper;
Expand All @@ -37,7 +39,10 @@
if ($isDoctrineORM) {
require_once __DIR__.'/tests/Bridge/Doctrine/autoload.php';

return DoctrineORMConsoleRunner::createHelperSet($GLOBALS['entity_manager']);
/** @var ORMEntityManager $entityManager */
$entityManager = $GLOBALS['entity_manager_factory']();

return DoctrineORMConsoleRunner::createHelperSet($entityManager);
}

if ($isDoctrinePHPCR) {
Expand Down Expand Up @@ -66,8 +71,9 @@

require_once __DIR__.'/tests/Bridge/DoctrinePhpCr/autoload.php';

$session = $GLOBALS['session'];
$documentManager = $GLOBALS['document_manager'];
/** @var PHPCRDocumentManager $documentManager */
$documentManager = $GLOBALS['document_manager_factory']();
$session = $documentManager->getPhpcrSession();

$helperSet = new HelperSet([
'phpcr' => new PhpcrHelper($session),
Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@
"fixtures",
"tests"
]
}
},
"files": [
"fixtures/get-param.php"
]
},

"config": {
Expand Down
6 changes: 0 additions & 6 deletions doctrine-odm-db-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@

declare(strict_types=1);

function get_param(string $envName, $default) {
$env = getenv($envName);

return false !== $env ? $env : $default;
}

// To keep in sync with docker-compose.yml
return [
'username' => get_param('DOCTRINE_ODM_DB_USER', 'root'),
Expand Down
6 changes: 0 additions & 6 deletions doctrine-orm-db-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@

declare(strict_types=1);

function get_param(string $envName, $default) {
$env = getenv($envName);

return false !== $env ? $env : $default;
}

// To keep in sync with docker-compose.yml
return [
'driver' => get_param('DOCTRINE_ORM_DB_DRIVER', 'pdo_mysql'),
Expand Down
6 changes: 0 additions & 6 deletions doctrine-phpcr-db-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@

declare(strict_types=1);

function get_param(string $envName, $default) {
$env = getenv($envName);

return false !== $env ? $env : $default;
}

// To keep in sync with docker-compose.yml
return [
'driver' => get_param('DOCTRINE_PHPCR_DB_DRIVER', 'pdo_mysql'),
Expand Down
6 changes: 0 additions & 6 deletions eloquent-db-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@

declare(strict_types=1);

function get_param(string $envName, $default) {
$env = getenv($envName);

return false !== $env ? $env : $default;
}

// To keep in sync with docker-compose.yml
return [
'driver' => get_param('ELOQUENT_DB_DRIVER', 'mysql'),
Expand Down
19 changes: 19 additions & 0 deletions fixtures/get-param.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/*
* This file is part of the Fidry\AliceDataFixtures package.
*
* (c) Théo FIDRY <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

function get_param(string $envName, $default)
{
$env = getenv($envName);

return false !== $env ? $env : $default;
}
11 changes: 5 additions & 6 deletions tests/Bridge/Doctrine/Persister/ObjectManagerPersisterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

namespace Fidry\AliceDataFixtures\Bridge\Doctrine\Persister;

use Doctrine\Common\DataFixtures\Purger\ORMPurger;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\ORMInvalidArgumentException;
use Fidry\AliceDataFixtures\Bridge\Doctrine\Entity\Dummy;
Expand All @@ -37,18 +36,18 @@ class ObjectManagerPersisterTest extends TestCase

private EntityManagerInterface $entityManager;

private ORMPurger $purger;

public function setUp(): void
{
$this->entityManager = $GLOBALS['entity_manager'];
$this->entityManager = $GLOBALS['entity_manager_factory']();
$this->persister = new ObjectManagerPersister($this->entityManager);
$this->purger = new ORMPurger($this->entityManager);

$this->entityManager->getConnection()->beginTransaction();
}

public function tearDown(): void
{
$this->purger->purge();
$this->entityManager->getConnection()->rollBack();
$this->entityManager->clear();
}

public function testIsAPersister(): void
Expand Down
9 changes: 6 additions & 3 deletions tests/Bridge/Doctrine/Purger/PurgerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@

use Doctrine\Common\DataFixtures\Purger\ORMPurger as DoctrineOrmPurger;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\AbstractMySQLDriver;
use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Fidry\AliceDataFixtures\Bridge\Doctrine\Entity\Dummy;
use Fidry\AliceDataFixtures\Bridge\Doctrine\ORM\FakeEntityManager;
use Fidry\AliceDataFixtures\Persistence\PurgeMode;
Expand Down Expand Up @@ -92,8 +92,8 @@ public function testDisableFKChecksOnDeleteIsPerformed(): void

public function testEmptyDatabase(): void
{
/** @var EntityManager $manager */
$manager = $GLOBALS['entity_manager'];
/** @var EntityManagerInterface $manager */
$manager = $GLOBALS['entity_manager_factory']();

$dummy = new Dummy();
$manager->persist($dummy);
Expand All @@ -111,5 +111,8 @@ public function testEmptyDatabase(): void
$manager->persist($dummy);
$manager->flush();
self::assertCount(1, $manager->getRepository(Dummy::class)->findAll());

// TODO: move to a tearDown()
$manager->clear();
}
}
4 changes: 2 additions & 2 deletions tests/Bridge/Doctrine/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
true,
);

$entityManager = EntityManager::create(
$entityManagerFactory = static fn () => EntityManager::create(
require ROOT.'/doctrine-orm-db-settings.php',
$config,
);

$GLOBALS['entity_manager'] = $entityManager;
$GLOBALS['entity_manager_factory'] = $entityManagerFactory;
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Fidry\AliceDataFixtures\Bridge\DoctrineMongoDB\Persister;

use Doctrine\Common\DataFixtures\Purger\MongoDBPurger;
use Doctrine\Common\DataFixtures\Purger\PurgerInterface;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\MongoDBException;
use Fidry\AliceDataFixtures\Bridge\Doctrine\MongoDocument\Dummy;
Expand All @@ -38,18 +39,19 @@ class ObjectManagerPersisterTest extends TestCase

private DocumentManager $documentManager;

private MongoDBPurger $purger;
private PurgerInterface $purger;

public function setUp(): void
{
$this->documentManager = $GLOBALS['document_manager'];
$this->documentManager = $GLOBALS['document_manager_factory']();
$this->persister = new ObjectManagerPersister($this->documentManager);
$this->purger = new MongoDBPurger($this->documentManager);
}

public function tearDown(): void
{
$this->purger->purge();
$this->documentManager->clear();
}

public function testIsAPersister(): void
Expand Down
8 changes: 6 additions & 2 deletions tests/Bridge/DoctrineMongoDB/Purger/PurgerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Doctrine\Common\DataFixtures\Purger\MongoDBPurger;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\PHPCR\DocumentManagerInterface;
use Fidry\AliceDataFixtures\Bridge\Doctrine\MongoDocument\Dummy;
use Fidry\AliceDataFixtures\Bridge\Doctrine\Purger\Purger;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -45,8 +46,8 @@ public function testCreatesADoctrineOdmPurgerWithTheAppropriateManager(): void

public function testEmptyDatabase(): void
{
/** @var DocumentManager $manager */
$manager = $GLOBALS['document_manager'];
/** @var DocumentManagerInterface $manager */
$manager = $GLOBALS['document_manager_factory']();

$dummy = new Dummy();
$manager->persist($dummy);
Expand All @@ -64,5 +65,8 @@ public function testEmptyDatabase(): void
$manager->persist($dummy);
$manager->flush();
self::assertCount(1, $manager->getRepository(Dummy::class)->findAll());

// TODO: move to a tearDown()
$manager->clear();
}
}
4 changes: 2 additions & 2 deletions tests/Bridge/DoctrineMongoDB/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
$port,
);

$documentManager = DocumentManager::create(
$documentManagerFactory = static fn () => DocumentManager::create(
new Client(
$uri,
[],
Expand All @@ -52,4 +52,4 @@
$config,
);

$GLOBALS['document_manager'] = $documentManager;
$GLOBALS['document_manager_factory'] = $documentManagerFactory;
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@

use function bin2hex;
use Doctrine\Common\DataFixtures\Purger\PHPCRPurger;
use Doctrine\ODM\PHPCR\DocumentManager;
use Doctrine\Common\DataFixtures\Purger\PurgerInterface;
use Doctrine\ODM\PHPCR\DocumentManagerInterface;
use Doctrine\ODM\PHPCR\Exception\InvalidArgumentException;
use Fidry\AliceDataFixtures\Bridge\Doctrine\Persister\ObjectManagerPersister;
use Fidry\AliceDataFixtures\Bridge\Doctrine\PhpCrDocument\Dummy;
Expand All @@ -33,20 +34,21 @@ class ObjectManagerPersisterTest extends TestCase
{
private ObjectManagerPersister $persister;

private DocumentManager $documentManager;
private DocumentManagerInterface $documentManager;

private PHPCRPurger $purger;
private PurgerInterface $purger;

public function setUp(): void
{
$this->documentManager = $GLOBALS['document_manager'];
$this->documentManager = $GLOBALS['document_manager_factory']();
$this->persister = new ObjectManagerPersister($this->documentManager);
$this->purger = new PHPCRPurger($this->documentManager);
}

public function tearDown(): void
{
$this->purger->purge();
$this->documentManager->clear();
}

public function testIsAPersister(): void
Expand Down
5 changes: 4 additions & 1 deletion tests/Bridge/DoctrinePhpCr/Purger/PurgerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function testCreatesADoctrineOrmPurgerWithTheAppropriateManagerAndPurgeMo
public function testEmptyDatabase(): void
{
/** @var DocumentManager $manager */
$manager = $GLOBALS['document_manager'];
$manager = $GLOBALS['document_manager_factory']();

$dummy = new Dummy();
$dummy->id = '/dummy_'.bin2hex(random_bytes(6));
Expand All @@ -87,5 +87,8 @@ public function testEmptyDatabase(): void
$manager->persist($dummy);
$manager->flush();
self::assertCount(1, $manager->getRepository(Dummy::class)->findAll());

// TODO: move to a tearDown()
$manager->clear();
}
}
Loading