Skip to content

PHPUnit & Tests

In addition to running fixtures via console command, you can also execute fixtures within your PHPUnit tests. For this purpose, we provide a trait called FixtureTrait, which contains all the necessary methods to run fixtures.

Run specific fixtures

To run specific fixtures, simply add the trait to your test and call the runSpecificFixtures method:

php
use Basecom\FixturePlugin\FixtureTrait; 

class MyTest extends TestCase {
    use FixtureTrait; 

    public function testThatSomethingWorks(): void {
        $this->runSpecificFixtures(['CustomerFixture', 'ProductFixture']); 
    }
}

Optionally, you can also specify that all dependencies of the given fixtures should be loaded as well by setting the second argument to true:

php
use Basecom\FixturePlugin\FixtureTrait;

class MyTest extends TestCase {
    use FixtureTrait; 

    public function testThatSomethingWorks(): void {
        $this->runSpecificFixtures(['CustomerFixture', 'ProductFixture']); 
        $this->runSpecificFixtures(['CustomerFixture', 'ProductFixture'], true); 
    }
}

Run a single fixture

To run a single fixture, simply add the trait to your test and call the runSingleFixture method:

php
use Basecom\FixturePlugin\FixtureTrait; 

class MyTest extends TestCase {
    use FixtureTrait; 

    public function testThatSomethingWorks(): void {
        $this->runSingleFixture('CustomerFixture'); 
    }
}

Optionally, you can also specify that all dependencies of the given fixture should be loaded as well by setting the second argument to true:

php
use Basecom\FixturePlugin\FixtureTrait;

class MyTest extends TestCase {
    use FixtureTrait; 

    public function testThatSomethingWorks(): void {
        $this->runSingleFixture('CustomerFixture'); 
        $this->runSingleFixture('CustomerFixture', true); 
    }
}

Run a fixture group

To run a whole fixture group, simply add the trait to your test and call the runFixtureGroup method:

php
use Basecom\FixturePlugin\FixtureTrait; 

class MyTest extends TestCase {
    use FixtureTrait; 

    public function testThatSomethingWorks(): void {
        $this->runFixtureGroup('PDP'); 
    }
}

Optionally, you can also specify that all dependencies of the given fixture group should be loaded as well by setting the second argument to true:

php
use Basecom\FixturePlugin\FixtureTrait;

class MyTest extends TestCase {
    use FixtureTrait; 

    public function testThatSomethingWorks(): void {
        $this->runFixtureGroup('PDP'); 
        $this->runFixtureGroup('PDP', true); 
    }
}

More complex scenarios

If you want or need more fine control over which fixtures run, you can use the runFixtures method. This method takes a FixtureOption parameter, which can be freely configured:

php
use Basecom\FixturePlugin\FixtureTrait; 
use Basecom\FixturePlugin\FixtureOption; 

class MyTest extends TestCase {
    use FixtureTrait; 

    public function testThatSomethingWorks(): void {
        $this->runFixtures(new FixtureOption( 
            groupName: 'PDP', 
            fixtureNames: ['CategoryFixture', 'AnotherFixture'], 
            withDependencies: true, 
            withVendor: true
        )); 
    }
}

All these parameters are combinable and allow for a very specific execution of fixtures. All other methods are simply alias methods for this one method with preconfigured options.

Released under the MIT License.