-
-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable loader to retrieve subtree of fixtures
- Loading branch information
Toni Uebernickel
committed
Apr 16, 2020
1 parent
886cf2e
commit 88ee524
Showing
2 changed files
with
168 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Bundle\FixturesBundle\Tests\Loader; | ||
|
||
use Doctrine\Bundle\FixturesBundle\Loader\SymfonyFixturesLoader; | ||
use Doctrine\Bundle\FixturesBundle\Tests\Fixtures\FooBundle\DataFixtures\OtherFixtures; | ||
use Doctrine\Bundle\FixturesBundle\Tests\Fixtures\FooBundle\DataFixtures\WithDependenciesFixtures; | ||
use Doctrine\Common\DataFixtures\DependentFixtureInterface; | ||
use Doctrine\Common\DataFixtures\FixtureInterface; | ||
use Doctrine\Persistence\ObjectManager; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\DependencyInjection\Container; | ||
use function class_alias; | ||
use function interface_exists; | ||
|
||
/** | ||
* @covers \Doctrine\Bundle\FixturesBundle\Loader\SymfonyFixturesLoader::getFixturesDependencyTree | ||
* @covers \Doctrine\Bundle\FixturesBundle\Loader\SymfonyFixturesLoader::resolveFixturesDependencyTree | ||
*/ | ||
class ResolveFixturesSubtreeTest extends TestCase | ||
{ | ||
/** @var SymfonyFixturesLoader */ | ||
private $loader; | ||
|
||
public static function setUpBeforeClass() : void | ||
{ | ||
if (interface_exists(ObjectManager::class)) { | ||
return; | ||
} | ||
|
||
class_alias('Doctrine\Common\Persistence\ObjectManager', 'Doctrine\Persistence\ObjectManager', false); | ||
} | ||
|
||
protected function setUp() : void | ||
{ | ||
$this->loader = new SymfonyFixturesLoader(new Container()); | ||
} | ||
|
||
public function testGetBasicFixturesTree() : void | ||
{ | ||
$fixtures = new OtherFixtures(); | ||
$this->loader->addFixture($fixtures); | ||
|
||
$tree = $this->loader->getFixturesDependencyTree([$fixtures]); | ||
|
||
static::assertCount(1, $tree); | ||
static::assertContains($fixtures, $tree); | ||
} | ||
|
||
public function testResolveDependentFixtures() : void | ||
{ | ||
$otherFixtures = new OtherFixtures(); | ||
$this->loader->addFixture($otherFixtures); | ||
|
||
$withDependenciesFixtures = new WithDependenciesFixtures(); | ||
$this->loader->addFixture($withDependenciesFixtures); | ||
|
||
$tree = $this->loader->getFixturesDependencyTree([$withDependenciesFixtures]); | ||
|
||
static::assertCount(2, $tree); | ||
static::assertSame([$otherFixtures, $withDependenciesFixtures], $tree); | ||
} | ||
|
||
public function testOmitFixturesOutsideTree() : void | ||
{ | ||
$otherFixtures = new OtherFixtures(); | ||
$this->loader->addFixture($otherFixtures); | ||
|
||
$withDependenciesFixtures = new WithDependenciesFixtures(); | ||
$this->loader->addFixture($withDependenciesFixtures); | ||
|
||
$omittedFixtures = $this->createFixture([]); | ||
$this->loader->addFixture($omittedFixtures); | ||
|
||
$tree = $this->loader->getFixturesDependencyTree([$withDependenciesFixtures]); | ||
|
||
static::assertCount(2, $tree); | ||
static::assertSame([$otherFixtures, $withDependenciesFixtures], $tree); | ||
} | ||
|
||
public function testResolveRecursively() : void | ||
{ | ||
$otherFixtures = new OtherFixtures(); | ||
$this->loader->addFixture($otherFixtures); | ||
|
||
$withDependenciesFixtures = new WithDependenciesFixtures(); | ||
$this->loader->addFixture($withDependenciesFixtures); | ||
|
||
$treeTopFixtures = $this->createFixture([WithDependenciesFixtures::class]); | ||
$this->loader->addFixture($treeTopFixtures); | ||
|
||
$tree = $this->loader->getFixturesDependencyTree([$treeTopFixtures]); | ||
|
||
static::assertCount(3, $tree); | ||
static::assertSame([$otherFixtures, $withDependenciesFixtures, $treeTopFixtures], $tree); | ||
} | ||
|
||
private function createFixture(array $dependencies) : FixtureInterface | ||
{ | ||
return new class ($dependencies) implements FixtureInterface, DependentFixtureInterface { | ||
/** @var string[] */ | ||
private $dependencies; | ||
|
||
public function __construct(array $dependencies) | ||
{ | ||
$this->dependencies = $dependencies; | ||
} | ||
|
||
public function load(ObjectManager $manager) : void | ||
{ | ||
} | ||
|
||
public function getDependencies() : array | ||
{ | ||
return $this->dependencies; | ||
} | ||
}; | ||
} | ||
} |