|
| 1 | +Faker Extension for Yii 2 |
| 2 | +========================= |
| 3 | + |
| 4 | +This extension provides a [`Faker`](https://github.com/fzaninotto/Faker) fixture command for Yii 2. |
| 5 | + |
| 6 | +This repository is a git submodule of <https://github.com/yiisoft/yii2>. |
| 7 | +Please submit issue reports and pull requests to the main repository. |
| 8 | +For license information check the [LICENSE](LICENSE.md)-file. |
| 9 | + |
| 10 | +Installation |
| 11 | +------------ |
| 12 | + |
| 13 | +The preferred way to install this extension is through [composer](http://getcomposer.org/download/). |
| 14 | + |
| 15 | +Either run |
| 16 | + |
| 17 | +``` |
| 18 | +php composer.phar require --prefer-dist yiisoft/yii2-faker |
| 19 | +``` |
| 20 | + |
| 21 | +or add |
| 22 | + |
| 23 | +```json |
| 24 | +"yiisoft/yii2-faker": "~2.0.0" |
| 25 | +``` |
| 26 | + |
| 27 | +to the require section of your composer.json. |
| 28 | + |
| 29 | + |
| 30 | +Usage |
| 31 | +----- |
| 32 | + |
| 33 | +To use this extension, simply add the following code in your application configuration (console.php): |
| 34 | + |
| 35 | +```php |
| 36 | +'controllerMap' => [ |
| 37 | + 'fixture' => [ |
| 38 | + 'class' => 'yii\faker\FixtureController', |
| 39 | + ], |
| 40 | +], |
| 41 | +``` |
| 42 | + |
| 43 | +Define a `tests` alias in your console config. For example, for the `basic` application template, this should be added |
| 44 | +to the `console.php` configuration: `Yii::setAlias('tests', __DIR__ . '/../tests');` |
| 45 | +To start using this command you need to be familiar (read guide) with the [Faker](https://github.com/fzaninotto/Faker) library and |
| 46 | +generate fixture template files, according to the given format: |
| 47 | + |
| 48 | +```php |
| 49 | +// users.php file under template path (by default @tests/unit/templates/fixtures) |
| 50 | +/** |
| 51 | + * @var $faker \Faker\Generator |
| 52 | + * @var $index integer |
| 53 | + */ |
| 54 | +return [ |
| 55 | + 'name' => $faker->firstName, |
| 56 | + 'phone' => $faker->phoneNumber, |
| 57 | + 'city' => $faker->city, |
| 58 | + 'password' => Yii::$app->getSecurity()->generatePasswordHash('password_' . $index), |
| 59 | + 'auth_key' => Yii::$app->getSecurity()->generateRandomString(), |
| 60 | + 'intro' => $faker->sentence(7, true), // generate a sentence with 7 words |
| 61 | +]; |
| 62 | +``` |
| 63 | + |
| 64 | +As you can see, the template file is just a regular PHP script. The script should return an array of key-value |
| 65 | +pairs, where the keys represent the table column names and the values the corresponding value. When you run |
| 66 | +the `fixture/generate` command, the script will be executed once for every data row being generated. |
| 67 | +In this script, you can use the following two predefined variables: |
| 68 | + |
| 69 | +* `$faker`: the Faker generator instance |
| 70 | +* `$index`: the current fixture index. For example if user need to generate 3 fixtures for user table, it will be 0..2. |
| 71 | + |
| 72 | +With such a template file, you can generate your fixtures using the commands like the following: |
| 73 | + |
| 74 | +``` |
| 75 | +# generate fixtures from user fixture template |
| 76 | +php yii fixture/generate user |
| 77 | +
|
| 78 | +# to generate several fixture data files |
| 79 | +php yii fixture/generate user profile team |
| 80 | +``` |
| 81 | + |
| 82 | +In the code above `users` is template name. After running this command, a new file with the same template name |
| 83 | +will be created under the fixture path in the `@tests/unit/fixtures`) folder. |
| 84 | + |
| 85 | +``` |
| 86 | +php yii fixture/generate-all |
| 87 | +``` |
| 88 | + |
| 89 | +This command will generate fixtures for all template files that are stored under template path and |
| 90 | +store fixtures under fixtures path with file names same as templates names. |
| 91 | +You can specify how many fixtures per file you need by the `--count` option. In the code below we generate |
| 92 | +all fixtures and in each file there will be 3 rows (fixtures). |
| 93 | + |
| 94 | +``` |
| 95 | +php yii fixture/generate-all --count=3 |
| 96 | +``` |
| 97 | +You can specify different options of this command: |
| 98 | + |
| 99 | +``` |
| 100 | +# generate fixtures in russian language |
| 101 | +php yii fixture/generate User --count=5 --language='ru_RU' |
| 102 | +
|
| 103 | +# read templates from the other path |
| 104 | +php yii fixture/generate-all --templatePath='@app/path/to/my/custom/templates' |
| 105 | +
|
| 106 | +# generate fixtures into other directory. |
| 107 | +php yii fixture/generate-all --fixtureDataPath='@tests/acceptance/fixtures/data' |
| 108 | +``` |
| 109 | + |
| 110 | +You can see all available templates by running command: |
| 111 | + |
| 112 | +``` |
| 113 | +# list all templates under default template path (i.e. '@tests/unit/templates/fixtures') |
| 114 | +php yii fixture/templates |
| 115 | +
|
| 116 | +# list all templates under specified template path |
| 117 | +php yii fixture/templates --templatePath='@app/path/to/my/custom/templates' |
| 118 | +``` |
| 119 | + |
| 120 | +You also can create your own data providers for custom tables fields, see [Faker](https://github.com/fzaninotto/Faker) library guide for more info; |
| 121 | +After you created custom provider, for example: |
| 122 | + |
| 123 | +```php |
| 124 | +class Book extends \Faker\Provider\Base |
| 125 | +{ |
| 126 | + |
| 127 | + public function title($nbWords = 5) |
| 128 | + { |
| 129 | + $sentence = $this->generator->sentence($nbWords); |
| 130 | + return mb_substr($sentence, 0, mb_strlen($sentence) - 1); |
| 131 | + } |
| 132 | + |
| 133 | + } |
| 134 | +``` |
| 135 | + |
| 136 | +You can use it by adding it to the `$providers` property of the current command. In your console.php config: |
| 137 | + |
| 138 | +```php |
| 139 | +'controllerMap' => [ |
| 140 | + 'fixture' => [ |
| 141 | + 'class' => 'yii\faker\FixtureController', |
| 142 | + 'providers' => [ |
| 143 | + 'app\tests\unit\faker\providers\Book', |
| 144 | + ], |
| 145 | + ], |
| 146 | +] |
| 147 | +``` |
0 commit comments