PHP code example of arnolem / magic-fixtures

1. Go to this page and download the library: Download arnolem/magic-fixtures 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/ */

    

arnolem / magic-fixtures example snippets


namespace Tests\Account;

use Arnolem\MagicFixtures\MagicFixtures;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

class AccountCase extends KernelTestCase
{

    /**
     * @throws DirectoryNotFoundException
     */
    public static function setUpBeforeClass(): void
    {
        // Boot Symfony Kernel
        self::bootKernel();

        // Provide Symfony Container to Magic Fixtures
        $magicFixtures = new MagicFixtures(self::getContainer());
        
        // Load all Fixtures form "/Tests/Fixtures/*"
        $magicFixtures->loadFromDirectory(__DIR__ . '/Fixture');
        
        // Executes Fixtures
        $magicFixtures->execute();
    }
    
    public function testTrue(): void
    {
        $this->assertTrue(true);
    }

}

namespace Tests\Fixture;

use App\Domain\Account;
use App\Domain\Company;
use App\Infrastructure\AccountPersister;
use Arnolem\MagicFixtures\Fixture;

readonly class AccountFixture extends Fixture
{

    public function __construct()
    {
        $this->accountPersister = $this->getService(AccountPersister::class);
    }

    public function execute(): void
    {
        $account = new Account(
            firstname: 'Arnaud',
            name: 'Lemercier',
            company: $this->getRandomReference(Company::class);
        );
        
        $this->accountPersister->save($account);
    }

    public function needs(): array
    {
        return [
            Company::class
        ];
    }
}

namespace Tests\Fixture;

use App\Domain\Company;
use App\Infrastructure\CompanyPersister;
use Arnolem\MagicFixtures\Fixture;

readonly class CompanyFixture extends Fixture
{

    public function __construct()
    {
        $this->companyPersister = $this->getService(CompanyPersister::class);
    }

    public function execute(): void
    {
    
        // Create a default and activated company
        $company = new Company(
            id: 0
            name: 'Wixiweb',
            isActivate: true,
        );
        
        $this->companyPersister->save($company);
        $this->addReference($company, $company->getId(), ['activate', 'default']);
            
        // Create others 10 inactivates companies
        for ($idCompany = 1; $idCompany <= 10; $idCompany++) {
        
            $company = new Company(
                id: $idCompany
                name: $this->faker->company(),
                isActivate: false,
            );
            
            $this->companyPersister->save($company);
            $this->addReference($company, $company->getId();
        }
        
    }
}