dknx01/data-fixtures-phpunit
This is a Symfony Bundle to use Symfony DoctrineFixturesBundle in PHPUnit test directly.
composer require --dev dknx01/data-fixtures-phpunitIf you want to use data fixtures in you tests you can do it in multiple ways. You can write a method to fill data in the database. or you can use a fixture and reuse it in multiple tests.
Ensure you have the following file:
# config/packages/data_fixtures_php_unit.yaml
data_fixtures_php_unit:
faker:
locale: 'de_DE' # optional - defaults to en_EN
providers:
- 'App\Tests\Faker\Bundeslaender' # optional - defaults to empty array
locale and providers are optional and only needed if you want to change the default behavior.
The minimal setup would look like:
# config/packages/data_fixtures_php_unit.yaml
data_fixtures_php_unit:
faker:
You should have all your Fixture classes registered as a services - at leat inside the test container.
All Fixtures that are in the namespace App\Tests\Fixtures\ and inside the folder '%kernel.project_dir%/tests/Fixtures' are automatically registered.
Example registration:
when@test:
services:
_defaults:
autowire: true
autoconfigure: true
public: true
App\Tests\Fixtures\:
resource: '%kernel.project_dir%/tests/Fixtures'
Each fixture must implement the Doctrine\Common\DataFixtures\FixtureInterface;.
Example:
<?php
namespace App\Tests\Fixtures;
use App\Entity\User;
use Dknx01\DataFixturesPhpUnit\Contract\FakerAware;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Persistence\ObjectManager;
use Faker\Generator;
readonly class UserFixture implements FixtureInterface, FakerAware
{
private Generator $faker;
public function __construct(private string $email = '')
{
}
public function setFaker(Generator $faker): void
{
$this->faker = $faker;
}
public function load(ObjectManager $manager): void
{
$user = new User();
$user->setEmail(!empty($this->email) ? $this->email : $this->faker->unique()->safeEmail());
$user->setPassword('foo');
$user->setRoles(['ROLE_USER']);
$manager->persist($user);
$manager->flush();
}
}
#[DataFixture(new UserFixture('test@fooo.nlkdjlfs'))]
class ApiTest extends ApplicationTestCase {
// your code
}
#[DataFixture(UserFixture::class)]
class ApiTest extends ApplicationTestCase {
// your code
}
As you can see the fixture can have constructor arguments for individual data in different tests.
Data fixtures can be used on class level (see above) and on method level.
#[DataFixture(UserFixture::class)]
#[DataFixture(new BlaFixture(
name: 'Test123',
fileName: 'Test123'
))]
public function testFoo(): void
{
// cor code
}
Fixtures can depend on other fixtures. You can use the way Doctrine data fixtures is suggesting, or you can use an attribute.
#[DependFixture(BarFixture::class)]
class FooFixture implements FixtureInterface, FakerAware
{
// your code
}
As you can see it is possible to use PHPFaker inside a fixture class.
If you implement the FakerAware interface a Faker instance is automatically injected into the data fixture.
#[DataFixture(new BarFixture('first'))]
#[DataFixture(BarFixture::class)]
public function testFoo(): void
{
// code
}
How can I help you explore Laravel packages today?