Yii2 uses faker to generate false data.
1. Add a configuration item in config \ console. php.
‘controllerMap‘ => [ ‘fixture‘ => [ ‘class‘ => ‘yii\faker\FixtureController‘, ],],
Note that the code at the top defines the location of the test directory.
Yii::setAlias(‘@tests‘, dirname(__DIR__) . ‘/tests‘);
2. Create a template file that generates false information
Create the unit/templates/fixtures directory in sequence under the test directory.
Create the users. php file in the fixtures directory. The content is as follows:
// users.php file under template path (by default @tests/unit/templates/fixtures)/** * @var $faker \Faker\Generator * @var $index integer */return [ ‘name‘ => $faker->firstName, ‘phone‘ => $faker->phoneNumber, ‘city‘ => $faker->city, ‘password‘ => Yii::$app->getSecurity()->generatePasswordHash(‘password_‘ . $index), ‘auth_key‘ => Yii::$app->getSecurity()->generateRandomString(), ‘intro‘ => $faker->sentence(7, true), // generate a sentence with 7 words];
3. Open the command line and execute the command to generate false records
php yii fixture/generate users
The generated false data is in tests \ unit \ fixtures \ data \ users. php
Content
<?phpreturn [ [ ‘name‘ => ‘Dawn‘, ‘phone‘ => ‘1-931-080-3527x751‘, ‘city‘ => ‘Schinnerberg‘, ‘password‘ => ‘$2y$13$ax7ubTMdQoUBkAdQfgW38uPD3Lp9C/X7XfdfSMFl8L1N90PpdXQoK‘, ‘auth_key‘ => ‘zmk_N4ZX_kOgu72iiWvPFZDUHEhQGYsg‘, ‘intro‘ => ‘Adipisci nostrum et autem molestias odio.‘, ], [ ‘name‘ => ‘Hubert‘, ‘phone‘ => ‘1-618-327-3265x917‘, ‘city‘ => ‘Auerstad‘, ‘password‘ => ‘$2y$13$gSjD/GJZiuPQdjB5FXyf0.fZCD9Jm6KquYRFyoaU6fgT3HHHiRa/q‘, ‘auth_key‘ => ‘zAVYEriQJJF16DtwwzesVM84gA1UhxFK‘, ‘intro‘ => ‘Maiores voluptas possimus nihil nesciunt qui commodi maiores qui.‘, ],];
Yii2 uses faker to generate false data