PHP code example of adrien / fixtures-for-tests

1. Go to this page and download the library: Download adrien/fixtures-for-tests library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

adrien / fixtures-for-tests example snippets




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');
        // ...
    }
}



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);
    }
}



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());
    }
}



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();
    }
}