/vendor/yiisoft/yii2-faker/README.md

https://gitlab.com/Griffolion/Game-Embargo-Tracker · Markdown · 144 lines · 108 code · 36 blank · 0 comment · 0 complexity · 4de1b82c38ed770d750676fd79da10a5 MD5 · raw file

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