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

Split purger tests #197

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
57 changes: 57 additions & 0 deletions tests/Bridge/Doctrine/Purger/PurgerIntegrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

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

declare(strict_types=1);

namespace Fidry\AliceDataFixtures\Bridge\Doctrine\Purger;

use Doctrine\ORM\EntityManagerInterface;
use Fidry\AliceDataFixtures\Bridge\Doctrine\Entity\Dummy;
use Fidry\AliceDataFixtures\Persistence\PurgeMode;
use PHPUnit\Framework\TestCase;

/**
* @coversNothing
*/
class PurgerIntegrationTest extends TestCase
{
private EntityManagerInterface $manager;

public function setUp(): void
{
$this->manager = $GLOBALS['entity_manager_factory']();
}

protected function tearDown(): void
{
$this->manager->clear();
}

public function testEmptyDatabase(): void
{
$dummy = new Dummy();
$this->manager->persist($dummy);
$this->manager->flush();

self::assertCount(1, $this->manager->getRepository(Dummy::class)->findAll());

$purger = new Purger($this->manager, PurgeMode::createDeleteMode());
$purger->purge();

self::assertCount(0, $this->manager->getRepository(Dummy::class)->findAll());

// Ensures the schema has been restored
$dummy = new Dummy();
$this->manager->persist($dummy);
$this->manager->flush();
self::assertCount(1, $this->manager->getRepository(Dummy::class)->findAll());
}
}
28 changes: 0 additions & 28 deletions tests/Bridge/Doctrine/Purger/PurgerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
use Doctrine\DBAL\Connection;
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;
use Fidry\AliceDataFixtures\Persistence\PurgerFactoryInterface;
Expand Down Expand Up @@ -89,30 +87,4 @@ public function testDisableFKChecksOnDeleteIsPerformed(): void

$purger->purge();
}

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

$dummy = new Dummy();
$manager->persist($dummy);
$manager->flush();

self::assertCount(1, $manager->getRepository(Dummy::class)->findAll());

$purger = new Purger($manager, PurgeMode::createDeleteMode());
$purger->purge();

self::assertCount(0, $manager->getRepository(Dummy::class)->findAll());

// Ensures the schema has been restored
$dummy = new Dummy();
$manager->persist($dummy);
$manager->flush();
self::assertCount(1, $manager->getRepository(Dummy::class)->findAll());

// TODO: move to a tearDown()
$manager->clear();
}
}
59 changes: 59 additions & 0 deletions tests/Bridge/DoctrineMongoDB/Purger/PurgerIntegrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

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

declare(strict_types=1);

namespace Fidry\AliceDataFixtures\Bridge\DoctrineMongoDB\Purger;

use Doctrine\ODM\MongoDB\DocumentManager;
use Fidry\AliceDataFixtures\Bridge\Doctrine\MongoDocument\Dummy;
use Fidry\AliceDataFixtures\Bridge\Doctrine\Purger\Purger;
use PHPUnit\Framework\TestCase;

/**
* @coversNothing
*
* @requires extension mongodb
*/
class PurgerIntegrationTest extends TestCase
{
private DocumentManager $manager;

protected function setUp(): void
{
$this->manager = $GLOBALS['document_manager_factory']();
}

protected function tearDown(): void
{
$this->manager->clear();
}

public function testEmptyDatabase(): void
{
$dummy = new Dummy();
$this->manager->persist($dummy);
$this->manager->flush();

self::assertCount(1, $this->manager->getRepository(Dummy::class)->findAll());

$purger = new Purger($this->manager);
$purger->purge();

self::assertCount(0, $this->manager->getRepository(Dummy::class)->findAll());

// Ensures the schema has been restored
$dummy = new Dummy();
$this->manager->persist($dummy);
$this->manager->flush();
self::assertCount(1, $this->manager->getRepository(Dummy::class)->findAll());
}
}
28 changes: 0 additions & 28 deletions tests/Bridge/DoctrineMongoDB/Purger/PurgerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

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;
use Prophecy\PhpUnit\ProphecyTrait;
Expand All @@ -43,30 +41,4 @@ public function testCreatesADoctrineOdmPurgerWithTheAppropriateManager(): void
self::assertInstanceOf(MongoDBPurger::class, $decoratedPurger);
self::assertEquals($manager, $decoratedPurger->getObjectManager());
}

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

$dummy = new Dummy();
$manager->persist($dummy);
$manager->flush();

self::assertCount(1, $manager->getRepository(Dummy::class)->findAll());

$purger = new Purger($manager);
$purger->purge();

self::assertCount(0, $manager->getRepository(Dummy::class)->findAll());

// Ensures the schema has been restored
$dummy = new Dummy();
$manager->persist($dummy);
$manager->flush();
self::assertCount(1, $manager->getRepository(Dummy::class)->findAll());

// TODO: move to a tearDown()
$manager->clear();
}
}
62 changes: 62 additions & 0 deletions tests/Bridge/DoctrinePhpCr/Purger/PurgerIntegrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

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

declare(strict_types=1);

namespace Fidry\AliceDataFixtures\Bridge\DoctrinePhpCr\Purger;

use function bin2hex;
use Doctrine\ODM\PHPCR\DocumentManagerInterface;
use Fidry\AliceDataFixtures\Bridge\Doctrine\PhpCrDocument\Dummy;
use Fidry\AliceDataFixtures\Bridge\Doctrine\Purger\Purger;
use Fidry\AliceDataFixtures\Persistence\PurgeMode;
use PHPUnit\Framework\TestCase;
use function random_bytes;

/**
* @coversNothing
*/
class PurgerIntegrationTest extends TestCase
{
private DocumentManagerInterface $manager;

protected function setUp(): void
{
$this->manager = $GLOBALS['document_manager_factory']();
}

protected function tearDown(): void
{
$this->manager->clear();
}

public function testEmptyDatabase(): void
{
$dummy = new Dummy();
$dummy->id = '/dummy_'.bin2hex(random_bytes(6));
$this->manager->persist($dummy);
$this->manager->flush();

self::assertCount(1, $this->manager->getRepository(Dummy::class)->findAll());

$purger = new Purger($this->manager, PurgeMode::createDeleteMode());
$purger->purge();

self::assertCount(0, $this->manager->getRepository(Dummy::class)->findAll());

// Ensures the schema has been restored
$dummy = new Dummy();
$dummy->id = '/dummy_'.bin2hex(random_bytes(6));
$this->manager->persist($dummy);
$this->manager->flush();
self::assertCount(1, $this->manager->getRepository(Dummy::class)->findAll());
}
}
32 changes: 0 additions & 32 deletions tests/Bridge/DoctrinePhpCr/Purger/PurgerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,14 @@

namespace Fidry\AliceDataFixtures\Bridge\DoctrinePhpCr\Purger;

use function bin2hex;
use Doctrine\Common\DataFixtures\Purger\ORMPurger as DoctrineOrmPurger;
use Doctrine\Common\DataFixtures\Purger\PHPCRPurger;
use Doctrine\ODM\PHPCR\DocumentManager;
use Fidry\AliceDataFixtures\Bridge\Doctrine\PhpCrDocument\Dummy;
use Fidry\AliceDataFixtures\Bridge\Doctrine\Purger\Purger;
use Fidry\AliceDataFixtures\Persistence\PurgeMode;
use Fidry\AliceDataFixtures\Persistence\PurgerFactoryInterface;
use Fidry\AliceDataFixtures\Persistence\PurgerInterface;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use function random_bytes;
use ReflectionClass;
use ReflectionObject;

Expand Down Expand Up @@ -63,32 +59,4 @@ public function testCreatesADoctrineOrmPurgerWithTheAppropriateManagerAndPurgeMo
self::assertInstanceOf(PHPCRPurger::class, $decoratedPurger);
self::assertEquals($manager, $decoratedPurger->getObjectManager());
}

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

$dummy = new Dummy();
$dummy->id = '/dummy_'.bin2hex(random_bytes(6));
$manager->persist($dummy);
$manager->flush();

self::assertCount(1, $manager->getRepository(Dummy::class)->findAll());

$purger = new Purger($manager, PurgeMode::createDeleteMode());
$purger->purge();

self::assertCount(0, $manager->getRepository(Dummy::class)->findAll());

// Ensures the schema has been restored
$dummy = new Dummy();
$dummy->id = '/dummy_'.bin2hex(random_bytes(6));
$manager->persist($dummy);
$manager->flush();
self::assertCount(1, $manager->getRepository(Dummy::class)->findAll());

// TODO: move to a tearDown()
$manager->clear();
}
}
60 changes: 60 additions & 0 deletions tests/Bridge/Eloquent/Purger/ModelPurgerIntegrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

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

declare(strict_types=1);

namespace Fidry\AliceDataFixtures\Bridge\Eloquent\Purger;

use Fidry\AliceDataFixtures\Bridge\Eloquent\Model\AnotherDummy;
use Illuminate\Database\Migrations\MigrationRepositoryInterface;
use Illuminate\Database\Migrations\Migrator;
use PHPUnit\Framework\TestCase;

/**
* @coversNothing
*/
class ModelPurgerIntegrationTest extends TestCase
{
private MigrationRepositoryInterface $repository;
private Migrator $migrator;

protected function setUp(): void
{
[$_manager, $repository, $migrator] = $GLOBALS['manager_repository_migrator_factory']();

$this->repository = $repository;
$this->migrator = $migrator;
}

public function testEmptyDatabase(): void
{
$purger = new ModelPurger($this->repository, 'migrations', $this->migrator);
// Doing a purge here is just to make the test slightly more robust when being run multiple times
// The real purge test is done at the next one
$purger->purge();

AnotherDummy::create([
'address' => 'Wonderlands',
]);
self::assertEquals(1, AnotherDummy::all()->count());

$purger = new ModelPurger($this->repository, 'migrations', $this->migrator);
$purger->purge();

self::assertEquals(0, AnotherDummy::all()->count());

// Ensures the schema has been restored
AnotherDummy::create([
'address' => 'Wonderlands'
]);
self::assertEquals(1, AnotherDummy::all()->count());
}
}
Loading