Skip to content

Commit

Permalink
Test the fixture factory
Browse files Browse the repository at this point in the history
  • Loading branch information
jdreesen committed Mar 18, 2024
1 parent 646e81f commit 1ec30c2
Show file tree
Hide file tree
Showing 13 changed files with 258 additions and 60 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/qa.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@ jobs:
php-version: 8.1

- name: Validate composer.json
run: composer validate --strict
run: composer validate --ansi --strict

- name: Install dependencies
uses: ramsey/composer-install@v2

- name: Run ergebnis/composer-normalize
run: composer normalize --ansi --dry-run

- name: Check CS-Fixer
run: composer cs:check

Expand Down
23 changes: 22 additions & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,27 @@ jobs:
- php-version: "8.1"
dependencies: "highest"

env:
MYSQL_HOST: 127.0.0.1
MYSQL_PORT: 33006
MYSQL_USER: pimcore
MYSQL_PASSWORD: pimcore
MYSQL_DATABASE: pimcore
MYSQL_SERVER_VERSION: "10.11.5-MariaDB"

services:
mariadb:
image: mariadb:10.11.5
ports:
- 33006:3306
env:
MYSQL_ALLOW_EMPTY_PASSWORD: yes
options: >-
--health-cmd="mysqladmin ping"
--health-interval=10s
--health-timeout=5s
--health-retries=5
steps:
- name: Git Checkout
uses: actions/checkout@v4
Expand All @@ -36,7 +57,7 @@ jobs:
php-version: ${{ matrix.php-version }}

- name: Install dependencies
uses: ramsey/composer-install@v2
uses: ramsey/composer-install@v3
with:
dependency-versions: ${{ matrix.dependencies }}

Expand Down
22 changes: 15 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "teamneusta/pimcore-fixture-bundle",
"type": "pimcore-bundle",
"description": "Provide basic functionality to enable installing fixtures in Pimcore",
"license": "GPL-3.0-or-later",
"type": "pimcore-bundle",
"authors": [
{
"name": "team neusta GmbH",
Expand All @@ -11,18 +11,14 @@
"role": "Developer"
}
],
"config": {
"sort-packages": true,
"allow-plugins": {
"phpstan/extension-installer": true
}
},
"require": {
"php": "~8.1.0",
"pimcore/pimcore": "^10.5"
},
"require-dev": {
"ergebnis/composer-normalize": "^2.42",
"friendsofphp/php-cs-fixer": "^3.11",
"laminas/laminas-zendframework-bridge": "^1.8",
"phpspec/prophecy-phpunit": "^2.0",
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan": "^1.8",
Expand All @@ -31,6 +27,11 @@
"phpunit/phpunit": "^9.5",
"teamneusta/pimcore-testing-framework": "^0.11"
},
"conflict": {
"presta/sitemap-bundle": "<3.2",
"symfony/form": "<5.4",
"symfony/routing": "<5.4"
},
"autoload": {
"psr-4": {
"Neusta\\Pimcore\\FixtureBundle\\": "src/"
Expand All @@ -44,6 +45,13 @@
"tests/app/TestKernel.php"
]
},
"config": {
"allow-plugins": {
"ergebnis/composer-normalize": true,
"phpstan/extension-installer": true
},
"sort-packages": true
},
"scripts": {
"cs:check": "@cs:fix --dry-run",
"cs:check:gitlab-ci": "php-cs-fixer fix --dry-run --ansi --verbose --diff --format=gitlab > .reports/php-cs-fixer.json",
Expand Down
3 changes: 1 addition & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
bootstrap="tests/bootstrap.php"
cacheResultFile=".phpunit.cache/test-results"
colors="true"
failOnRisky="true"
Expand All @@ -14,8 +15,6 @@
<ini name="display_startup_errors" value="On"/>
<ini name="display_errors" value="On"/>
<ini name="error_reporting" value="-1"/>
<env name="KERNEL_CLASS" value="TestKernel"/>
<env name="SYMFONY_DEPRECATIONS_HELPER" value="disabled"/>
</php>

<testsuites>
Expand Down
21 changes: 21 additions & 0 deletions src/Factory/FixtureFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,27 @@ public function createFixtures(array $fixtures): void
Cache::enable();
}

/**
* @template T of Fixture
*
* @param class-string<T> $fixture
*
* @return T
*
* @throws \RuntimeException when the fixture hasn't been created
*/
public function getFixture(string $fixture): Fixture
{
if (!isset($this->instances[$fixture])) {
throw new \RuntimeException(sprintf('Fixture "%s" has not been created.', $fixture));
}

$instance = $this->instances[$fixture];
\assert($instance instanceof $fixture);

return $instance;
}

/**
* @param class-string<Fixture> $fixtureClass
*/
Expand Down
21 changes: 0 additions & 21 deletions tests/ExampleKernelTest.php

This file was deleted.

14 changes: 0 additions & 14 deletions tests/ExampleUnitTest.php

This file was deleted.

23 changes: 23 additions & 0 deletions tests/Fixtures/DependantFixture.php
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);
}
}
15 changes: 15 additions & 0 deletions tests/Fixtures/FixtureWithDependency.php
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);
}
}
15 changes: 15 additions & 0 deletions tests/Fixtures/SomeFixture.php
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;
}
}
128 changes: 128 additions & 0 deletions tests/Functional/Factory/FixtureFactoryTest.php
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));
}
}
Loading

0 comments on commit 1ec30c2

Please sign in to comment.