PHP code example of stwarog / fuel-fixtures

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

    

stwarog / fuel-fixtures example snippets




namespace Tests\Fixtures;

use Model_Orm_Currency;
use Stwarog\FuelFixtures\Fuel\Factory;

final class CurrencyFixture extends Factory
{
    # Constants representing the "state" name
    public const USD = 'usd';
    public const EUR = 'eur';
    public const PLN = 'pln';

    
    /** @return array<string, string> */
    public function getDefaults(): array
    {
        # This values must be as random as possible
        return [
            'code' => $this->faker->countryISOAlpha3,
            'rate' => $this->faker->randomFloat(1, 1, 10),
        ];
    }

    # Class name for wchich we are going to create a new instance
    public static function getClass(): string
    {
        return Model_Orm_Currency::class;
    }

    /** array<string, callable> */
    public function getStates(): array
    {
        # Main method, returning list of available states (used by calling "with" method)
        return [
            self::USD => function (Model_Orm_Currency $model, array $attributes = []) {
                $model->code = 'USD';
                $model->rate = 1.0;
            },
            self::EUR => function (Model_Orm_Currency $model, array $attributes = []) {
                $model->code = 'EUR';
                $model->rate = 0.8418;
            },
            self::PLN => function (Model_Orm_Currency $model, array $attributes = []) {
                $model->code = 'PLN';
                $model->rate = 3.5896;
            },
        ];
    }
}

    /**
     * @param array<string, mixed> $attributes
     * @return Model<array>
     */
    public function makeOne(array $attributes = []): Model;

    /**
     * @param array<string, mixed> $attributes
     * @return array<Model>
     */
    public function makeMany(array $attributes = [], int $count = 5): array;

    /**
     * @param array<string, mixed> $attributes
     * @return Model<array>
     */
    public function createOne(array $attributes = []): Model;

    /**
     * @param array<string, mixed> $attributes
     * @return array<Model>
     */
    public function createMany(array $attributes = [], int $count = 5): array;
 
    $fixture = CurrencyFixture::initialize();
    # All states will be executed in the consecutive order
    $fixture->with('usd', 'rate1000', fn(Model $m, array $attrs = []) => $m->rate = 1.5)->makeOne();