adrien/fixtures-for-tests

A set of traits to be used in test cases to load fixtures.

v0.2.0 2020-04-02 12:30 UTC

This package is auto-updated.

Last update: 2024-12-13 19:48:30 UTC


README

Latest Version Software License Build Status Code Coverage Quality Score

A set of traits to be used in test cases

Install

Via Composer

$ composer require adrien/fixtures-for-tests

Usage

  • Use the FixtureLoaderTrait to add a fixture loading shortcut method.
  • Use the FixtureAttachedTrait within a KernelTestCase extending class to have fixture loaded automatically before each tests.

Exemple using PHPUnit + Symfony's KernelTestCase :

<?php

namespace SomeNamespace\Test;

use Adrien\FixturesForTests\FixtureAttachedTrait;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

class SomeFeatureTest extends KernelTestCase
{
    use FixtureAttachedTrait;

    public function testItDoesWhatIsExpected(): void
    {
        $dummy = $this->fixtureRepository->getReference('my_dummy');
        // ...
    }
}
<?php

namespace SomeNamespace\Test;

use App\Entity\SomeEntity;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Persistence\ObjectManager;

class SomeFeatureFixture extends AbstractFixture
{
    public function load(ObjectManager $manager): void
    {
        $dummy = new SomeEntity();
        $dummy->setSomething('something');

        $manager->persist($dummy);
        $manager->flush();

        $this->referenceRepository->addReference('my_dummy', $dummy);
    }
}

Exemple using Behat (with PHPCR) :

<?php

namespace SomeNamespace\Behat;

use Adrien\FixturesForTests\FixtureLoaderTrait;
use Behat\Behat\Context\Context;
use Doctrine\ODM\PHPCR\DocumentManager;

class FeatureContext implements Context
{
    use FixtureLoaderTrait;

    /** @BeforeScenario */
    public function prepareScenarioFixtures()
    {
        $persistenceManager = new DocumentManager(/*...*/);
        $this->loadFixture($persistenceManager, new SomeScenarioFixture());
    }
}
<?php

namespace SomeNamespace\Behat;

use App\Entity\SomeEntity;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Persistence\ObjectManager;

class SomeFeatureFixture implements FixtureInterface
{
    public function load(ObjectManager $manager): void
    {
        $dummy = new SomeEntity();
        $dummy->setSomething('something');
        
        $manager->persist($dummy);
        $manager->flush();
    }
}

Contributing and testing

$ composer update --prefer-lowest --prefer-source
$ ./vendor/bin/phpunit

Please maintain the test suite : if you add a feature, prove the new behavior; if you fix a bug, ensure the non-regression.