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.
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.
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(), ];
yii
command:php tests/codeception/bin/yii fixture/generate users --count=2
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', ], ],
<?php namespace testscodeceptionfakerproviders; use FakerProviderBase; class UserStatus extends Base { public function userStatus() { return $this->randomElement([0, 10, 20, 30]); } }
/tests/codeception/config/config.php
file:return [ 'controllerMap' => [ 'fixture' => [ 'class' => 'yiifakerFixtureController', 'fixtureDataPath' => '@tests/codeception/fixtures', 'templatePath' => '@tests/codeception/templates', 'namespace' => 'testscodeceptionfixtures', 'providers' => [ 'testscodeceptionfakerprovidersUserStatus', ], ], ], // ... ];
status
field into your fixture template file:<?php /** * @var $faker FakerGenerator * @var $index integer */ return [ 'name' => $faker->firstName, 'status' => $faker->userStatus, ];
php tests/codeception/bin/yii fixture/generate users --count=2
fixtures/users.php
file contains your custom values:return [ [ 'name' => 'Christelle', 'status' => 30, ], [ 'name' => 'Theo', 'status' => 10, ], ];
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.
18.118.126.241