1
- Faker Extension for Yii 2
2
- =========================
1
+ Yii::$app stubs generator extension for Yii 2
2
+ =============================================
3
3
4
- This extension provides a [ ` Faker ` ] ( https://github.com/fzaninotto/Faker ) fixture command for Yii 2.
4
+ This extension provides no-more-butthurt generator command for Yii 2.
5
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.
6
+ ![ in action] ( http://monosnap.com/image/vCnH1SWaXwKXLuchkutNGZXkeeoYWK.png )
9
7
10
8
Installation
11
9
------------
@@ -15,13 +13,13 @@ The preferred way to install this extension is through [composer](http://getcomp
15
13
Either run
16
14
17
15
```
18
- php composer.phar require --prefer-dist yiisoft /yii2-faker
16
+ php composer.phar require --prefer-dist bazilio /yii2-stubs-generator
19
17
```
20
18
21
19
or add
22
20
23
21
``` json
24
- "yiisoft /yii2-faker " : " ~2. 0.0"
22
+ "bazilio /yii2-stubs-generator " : " ~0.0.1 "
25
23
```
26
24
27
25
to the require section of your composer.json.
@@ -35,113 +33,15 @@ To use this extension, simply add the following code in your application config
35
33
``` php
36
34
'controllerMap' => [
37
35
'fixture' => [
38
- 'class' => 'yii\faker\FixtureController ',
36
+ 'class' => 'bazilio\stubsgenerator\StubsController ',
39
37
],
40
38
],
41
39
```
42
40
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
41
```
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'
42
+ # generate fixtures for console application
43
+ php yii stubs console
105
44
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
- ```
45
+ # to generate stubs for several apps
46
+ php yii stubs console common frontend
47
+ ```
0 commit comments