adrien / fixtures-for-tests
A set of traits to be used in test cases to load fixtures.
Installs: 1 639
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: ^7.2
- doctrine/data-fixtures: ^1.3
Requires (Dev)
- doctrine/mongodb-odm: ^1.2
- doctrine/orm: ^2.6
- doctrine/phpcr-odm: ^1.4
- jackalope/jackalope: ^1.3
- jackalope/jackalope-doctrine-dbal: ^1.3
- phpunit/phpunit: ^8.1
- symfony/framework-bundle: ^4.2
This package is auto-updated.
Last update: 2024-10-13 19:33:16 UTC
README
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 aKernelTestCase
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.