-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
258 additions
and
60 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
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
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,23 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Neusta\Pimcore\FixtureBundle\Tests\Fixtures; | ||
|
||
use Neusta\Pimcore\FixtureBundle\Fixture; | ||
|
||
final class DependantFixture implements Fixture | ||
{ | ||
public float $createdAt; | ||
|
||
public function create(): void | ||
{ | ||
if (isset($this->createdAt)) { | ||
// Ensures that this method doesn't get called twice on the same object. | ||
throw new \LogicException('Should never happen.'); | ||
} | ||
|
||
$this->createdAt = microtime(true); | ||
|
||
// ensure the next fixture is not created in the same microsecond | ||
usleep(1); | ||
} | ||
} |
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,15 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Neusta\Pimcore\FixtureBundle\Tests\Fixtures; | ||
|
||
use Neusta\Pimcore\FixtureBundle\Fixture; | ||
|
||
final class FixtureWithDependency implements Fixture | ||
{ | ||
public float $createdAt; | ||
|
||
public function create(DependantFixture $fixture): void | ||
{ | ||
$this->createdAt = microtime(true); | ||
} | ||
} |
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,15 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Neusta\Pimcore\FixtureBundle\Tests\Fixtures; | ||
|
||
use Neusta\Pimcore\FixtureBundle\Fixture; | ||
|
||
final class SomeFixture implements Fixture | ||
{ | ||
public bool $created; | ||
|
||
public function create(): void | ||
{ | ||
$this->created = true; | ||
} | ||
} |
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,128 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Neusta\Pimcore\FixtureBundle\Tests\Functional\Factory; | ||
|
||
use Neusta\Pimcore\FixtureBundle\Factory\FixtureFactory; | ||
use Neusta\Pimcore\FixtureBundle\Factory\FixtureInstantiator\FixtureInstantiatorForAll; | ||
use Neusta\Pimcore\FixtureBundle\Fixture; | ||
use Neusta\Pimcore\FixtureBundle\Tests\Fixtures\DependantFixture; | ||
use Neusta\Pimcore\FixtureBundle\Tests\Fixtures\FixtureWithDependency; | ||
use Neusta\Pimcore\FixtureBundle\Tests\Fixtures\SomeFixture; | ||
use Pimcore\Test\KernelTestCase; | ||
|
||
final class FixtureFactoryTest extends KernelTestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
self::bootKernel(); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_creates_a_fixture(): void | ||
{ | ||
$factory = new FixtureFactory([], [new FixtureInstantiatorForAll()]); | ||
|
||
$factory->createFixtures([SomeFixture::class]); | ||
|
||
$fixture = $factory->getFixture(SomeFixture::class); | ||
self::assertInstanceOf(SomeFixture::class, $fixture); | ||
self::assertTrue($fixture->created); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_creates_a_fixture_from_a_mapped_name(): void | ||
{ | ||
$factory = new FixtureFactory(['something' => SomeFixture::class], [new FixtureInstantiatorForAll()]); | ||
|
||
$factory->createFixtures(['something']); | ||
|
||
$fixture = $factory->getFixture(SomeFixture::class); | ||
self::assertInstanceOf(SomeFixture::class, $fixture); | ||
self::assertTrue($fixture->created); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_throws_when_prompted_to_create_a_fixture_that_does_not_implement_the_Fixture_interface(): void | ||
{ | ||
$factory = new FixtureFactory([], [new FixtureInstantiatorForAll()]); | ||
|
||
$this->expectException(\InvalidArgumentException::class); | ||
$this->expectExceptionMessage('Expected "stdClass" to implement "Neusta\Pimcore\FixtureBundle\Fixture", but it does not.'); | ||
|
||
$factory->createFixtures([\stdClass::class]); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_creates_depending_fixtures_first(): void | ||
{ | ||
$factory = new FixtureFactory([], [new FixtureInstantiatorForAll()]); | ||
|
||
$factory->createFixtures([FixtureWithDependency::class]); | ||
|
||
$fixtureWithDependency = $factory->getFixture(FixtureWithDependency::class); | ||
$dependantFixture = $factory->getFixture(DependantFixture::class); | ||
self::assertInstanceOf(FixtureWithDependency::class, $fixtureWithDependency); | ||
self::assertInstanceOf(DependantFixture::class, $dependantFixture); | ||
self::assertGreaterThan($dependantFixture->createdAt, $fixtureWithDependency->createdAt); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_throws_when_dependency_has_invalid_type(): void | ||
{ | ||
$factory = new FixtureFactory([], [new FixtureInstantiatorForAll()]); | ||
$fixture = new class() implements Fixture { | ||
public function create($something): void | ||
{ | ||
} | ||
}; | ||
|
||
$this->expectException(\LogicException::class); | ||
$this->expectExceptionMessageMatches('/Parameter "\$something" of .+::create\(\) has an invalid type/'); | ||
|
||
$factory->createFixtures([$fixture::class]); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_throws_when_depending_on_a_non_fixture_object(): void | ||
{ | ||
$factory = new FixtureFactory([], [new FixtureInstantiatorForAll()]); | ||
$fixture = new class() implements Fixture { | ||
public function create(\stdClass $noFixture): void | ||
{ | ||
} | ||
}; | ||
|
||
$this->expectException(\LogicException::class); | ||
$this->expectExceptionMessage('Expected "stdClass" to implement "Neusta\Pimcore\FixtureBundle\Fixture", but it does not.'); | ||
|
||
$factory->createFixtures([$fixture::class]); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_creates_a_fixture_only_once(): void | ||
{ | ||
$factory = new FixtureFactory([], [new FixtureInstantiatorForAll()]); | ||
|
||
$factory->createFixtures([FixtureWithDependency::class]); | ||
|
||
$dependantFixture = $factory->getFixture(DependantFixture::class); | ||
|
||
$factory->createFixtures([DependantFixture::class]); | ||
|
||
self::assertSame($dependantFixture, $factory->getFixture(DependantFixture::class)); | ||
} | ||
} |
Oops, something went wrong.