Faker fixture data generator

The fzaninotto/faker is a PHP library that generates fake data of many kinds: names, phones, addresses, random strings and numbers, and so on. It can help you to generate many randomized records for performance and logic testing. You can extend your supported types collection by writing your own formatters and generators.

In the Yii2 application skeletons, the yiisoft/yii2-faker wrapper is included in the require-dev section of the composer.json file and is used for testing code (Chapter 11, Testing). This wrapper provides the FixtureController console for use in your console application and test environment.

Getting ready

Create a new application by using composer as described in the official guide at http://www.yiiframework.com/doc-2.0/guide-start-installation.html.

How to do it…

  1. Open the directory tests/codeception/templates and add the fixture template file, users.txt:
    <?php
    /**
     * @var $faker FakerGenerator
     * @var $index integer
     */
        return [
            'name' => $faker->firstName,
            'phone' => $faker->phoneNumber,
            'city' => $faker->city,
            'about' => $faker->sentence(7, true),
            'password' => Yii::$app->getSecurity()
            ->generatePasswordHash('password_' . $index),
            'auth_key' => Yii::$app->getSecurity()
            ->generateRandomString(),
        ];
  2. Run the test console yii command:
    php tests/codeception/bin/yii fixture/generate users --count=2
    
  3. Confirm migration generation.
  4. Check that the tests/codeception/fixtures directory contains the new users.php file, with autogenerated data like this:
    return [
        [
            'name' => 'Isadore',
            'phone' => '952.877.8545x190',
            'city' => 'New Marvinburgh',
            'about' => 'Ut quidem voluptatem itaque veniam voluptas dolores.',
            'password' => '$2y$13$Fi3LOl/sKlomUH.DLgqBkOB/uCLmgCoPPL1KXiW0hffnkrdkjCzAC',
            'auth_key' => '1m05hlgaAG8zfm0cyDyoRGMkbQ9W6hj1',
        ],
        [
            'name' => 'Raleigh',
            'phone' => '1-655-488-3585x699',
            'city' => 'Reedstad',
            'about' => 'Dolorem quae impedit tempore libero doloribus nobis dicta tempora facere.',
            'password' => '$2y$13$U7Qte5Y1jVLrx/pnhwdwt.1uXDegGXuNVzEQyUsb65WkBtjyjUuYm',
            'auth_key' => 'uWWJDgy5jNRk6KjqpxS5JuPv0OHearqE',
        ],
    ],

Working with your own data types

  1. Create your own provider with your custom value generating logic:
    <?php
        namespace testscodeceptionfakerproviders;
    
        use FakerProviderBase;
    
        class UserStatus extends Base
        {
            public function userStatus()
            {
                return $this->randomElement([0, 10, 20, 30]);
            }
        }
  2. Add the provider into the providers list in the /tests/codeception/config/config.php file:
    return [
        'controllerMap' => [
            'fixture' => [
                'class' => 'yiifakerFixtureController',
                'fixtureDataPath' => '@tests/codeception/fixtures',
                'templatePath' => '@tests/codeception/templates',
                'namespace' => 'testscodeceptionfixtures',
                'providers' => [
                    'testscodeceptionfakerprovidersUserStatus',
                ],
            ],
        ],
        // ...
    ];
  3. Add the status field into your fixture template file:
    <?php
    /**
     * @var $faker FakerGenerator
     * @var $index integer
     */
        return [
            'name' => $faker->firstName,
            'status' => $faker->userStatus,
        ];
  4. Regenerate fixtures with the console command:
    php tests/codeception/bin/yii fixture/generate users --count=2
    
  5. Check that the generated code in the fixtures/users.php file contains your custom values:
    return [
        [
            'name' => 'Christelle',
            'status' => 30,
        ],
        [
            'name' => 'Theo',
            'status' => 10,
        ],
    ];

How it works…

The yii2-faker extension contains a console generator (which uses your templates for generating fixture data files) and gives you a prepared instance of the original Faker object. You can generate all or specific fixtures and can pass custom counts or language in console arguments.

Note

Note: Be careful with the existing test files if your tests use these fixtures, because autogenerating totally rewrites old data.

See also

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.126.241