PHP code example of golossus / php-test-fixture-loading

1. Go to this page and download the library: Download golossus/php-test-fixture-loading 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/ */

    

golossus / php-test-fixture-loading example snippets


 declare(strict_types = 1);

namespace App\Tests\Rest;

use Golossus\TestFixtureLoading\Fixture;
use Golossus\TestFixtureLoading\FixtureLoaderTrait;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class FunctionalTestCase extends WebTestCase
{
    use FixtureLoaderTrait;

    protected function buildFixture(string $namespace): Fixture
    {
        return self::$container->get($namespace);
    }

}

 declare(strict_types = 1);

namespace Tests\Fixtures;

use Golossus\TestFixtureLoading\AbstractFixture;
use Golossus\TestFixtureLoading\FixtureRepository;
...

class AdminUserFixture extends AbstractFixture
{
    ...
    const ADMIN_USER = 'admin-user';

    public function load(FixtureRepository $fixtureRepository): void
    {
        $company = $fixtureRepository->get(CompanyFixture::COMPANY);

        $user = $this->createAdminUserForCompany($company);

        $fixtureRepository->set(self::ADMIN_USER, $user);
    }

    public function depends(): array
    {
        return [
            CompanyFixture::class,
        ];
    }
}

public function testWhateverYouLike()
{
    // First load the ::class,
        AnotherDataFixture::class,
    ]);
    
    // You can get a data fixture object by key
    $dummy = $fixtures->get('some-key'); 
    
    // The rest should be a normal test
}
yaml
# in cofig/services_test.yaml 
services:
  _defaults:
    autowire: true
    autoconfigure: true

  App\Tests\DataFixtures\:
    resource: '../tests/DataFixtures/*'
    public: true