|
|
@ -18,120 +18,122 @@ use yii\helpers\Console; |
|
|
|
* Fixtures are one of the important paths in unit testing. To speed up developers |
|
|
|
* Fixtures are one of the important paths in unit testing. To speed up developers |
|
|
|
* work these fixtures can be generated automatically, based on prepared template. |
|
|
|
* work these fixtures can be generated automatically, based on prepared template. |
|
|
|
* This command is a simple wrapper for the fixtures library [Faker](https://github.com/fzaninotto/Faker). |
|
|
|
* This command is a simple wrapper for the fixtures library [Faker](https://github.com/fzaninotto/Faker). |
|
|
|
* |
|
|
|
* |
|
|
|
* You should configure this command as follows (you can use any alias, not only "faker:fixture"): |
|
|
|
* You should configure this command as follows (you can use any alias, not only "fixture"): |
|
|
|
* |
|
|
|
* |
|
|
|
* ~~~ |
|
|
|
* ~~~ |
|
|
|
* 'controllerMap' => [ |
|
|
|
* 'controllerMap' => [ |
|
|
|
* 'fixture' => [ |
|
|
|
* 'fixture' => [ |
|
|
|
* 'class' => 'yii\faker\FixtureController', |
|
|
|
* 'class' => 'yii\faker\FixtureController', |
|
|
|
* ], |
|
|
|
* ], |
|
|
|
* ], |
|
|
|
* ], |
|
|
|
* ~~~ |
|
|
|
* ~~~ |
|
|
|
* |
|
|
|
* |
|
|
|
* To start using this command you need to be familiar (read guide) for the Faker library and |
|
|
|
* To start using this command you need to be familiar (read guide) for the Faker library and |
|
|
|
* generate fixtures template files, according to the given format: |
|
|
|
* generate fixtures template files, according to the given format: |
|
|
|
* |
|
|
|
* |
|
|
|
* ~~~ |
|
|
|
* ~~~ |
|
|
|
* #users.php file under $templatePath |
|
|
|
* #users.php file under $templatePath |
|
|
|
* |
|
|
|
* |
|
|
|
* return [ |
|
|
|
* return [ |
|
|
|
* [ |
|
|
|
* [ |
|
|
|
* 'table_column0' => 'faker_formatter', |
|
|
|
* 'table_column0' => 'faker_formatter', |
|
|
|
* ... |
|
|
|
* ... |
|
|
|
* 'table_columnN' => 'other_faker_formatter |
|
|
|
* 'table_columnN' => 'other_faker_formatter |
|
|
|
* 'table_columnN+1' => function ($fixture, $faker, $index) { |
|
|
|
* 'table_columnN+1' => function ($fixture, $faker, $index) { |
|
|
|
* //set needed fixture fields based on different conditions |
|
|
|
* //set needed fixture fields based on different conditions |
|
|
|
* return $fixture; |
|
|
|
* return $fixture; |
|
|
|
* } |
|
|
|
* } |
|
|
|
* ], |
|
|
|
* ], |
|
|
|
* ]; |
|
|
|
* ]; |
|
|
|
* ~~~ |
|
|
|
* ~~~ |
|
|
|
* |
|
|
|
* |
|
|
|
* If you use callback as a attribute value, then it will be called as shown with three parameters: |
|
|
|
* If you use callback as a attribute value, then it will be called as shown with three parameters: |
|
|
|
* - $fixture - current fixture array. |
|
|
|
* |
|
|
|
* - $faker - faker generator instance |
|
|
|
* - `$fixture` - current fixture array. |
|
|
|
* - $index - current fixture index. For example if user need to generate 3 fixtures for tbl_user, it will be 0..2 |
|
|
|
* - `$faker` - faker generator instance |
|
|
|
|
|
|
|
* - `$index` - current fixture index. For example if user need to generate 3 fixtures for tbl_user, it will be 0..2 |
|
|
|
|
|
|
|
* |
|
|
|
* After you set all needed fields in callback, you need to return $fixture array back from the callback. |
|
|
|
* After you set all needed fields in callback, you need to return $fixture array back from the callback. |
|
|
|
* |
|
|
|
* |
|
|
|
* After you prepared needed templates for tables you can simply generate your fixtures via command |
|
|
|
* After you prepared needed templates for tables you can simply generate your fixtures via command |
|
|
|
* |
|
|
|
* |
|
|
|
* ~~~ |
|
|
|
* ~~~ |
|
|
|
* php yii fixture/generate users |
|
|
|
* yii fixture/generate users |
|
|
|
* |
|
|
|
* |
|
|
|
* //also a short version of this command (generate action is default) |
|
|
|
* //also a short version of this command (generate action is default) |
|
|
|
* php yii fixture users |
|
|
|
* yii fixture users |
|
|
|
* |
|
|
|
* |
|
|
|
* //to generate fixtures for several tables, use "," as a separator, for example: |
|
|
|
* //to generate fixtures for several tables, use "," as a separator, for example: |
|
|
|
* php yii fixture users,profile |
|
|
|
* yii fixture users,profile |
|
|
|
* ~~~ |
|
|
|
* ~~~ |
|
|
|
* |
|
|
|
* |
|
|
|
* In the code above "users" is template name, after this command run, new file named same as template |
|
|
|
* In the code above "users" is template name, after this command run, new file named same as template |
|
|
|
* will be created under the $fixturesPath folder. |
|
|
|
* will be created under the $fixturesPath folder. |
|
|
|
* You can generate fixtures for all templates by specifying keyword "all_fixtures" |
|
|
|
* You can generate fixtures for all templates by specifying keyword "all" |
|
|
|
* |
|
|
|
* |
|
|
|
* ~~~ |
|
|
|
* ~~~ |
|
|
|
* php yii fixture/generate all |
|
|
|
* yii fixture/generate all |
|
|
|
* ~~~ |
|
|
|
* ~~~ |
|
|
|
* |
|
|
|
* |
|
|
|
* This command will generate fixtures for all template files that are stored under $templatePath and |
|
|
|
* This command will generate fixtures for all template files that are stored under $templatePath and |
|
|
|
* store fixtures under $fixturesPath with file names same as templates names. |
|
|
|
* store fixtures under $fixturePath with file names same as templates names. |
|
|
|
* |
|
|
|
* |
|
|
|
* You can specify how many fixtures per file you need by the second parameter. In the code below we generate |
|
|
|
* You can specify how many fixtures per file you need by the second parameter. In the code below we generate |
|
|
|
* all fixtures and in each file there will be 3 rows (fixtures). |
|
|
|
* all fixtures and in each file there will be 3 rows (fixtures). |
|
|
|
* |
|
|
|
* |
|
|
|
* ~~~ |
|
|
|
* ~~~ |
|
|
|
* php yii fixture/generate all 3 |
|
|
|
* yii fixture/generate all 3 |
|
|
|
* ~~~ |
|
|
|
* ~~~ |
|
|
|
* |
|
|
|
* |
|
|
|
* You can specify different options of this command: |
|
|
|
* You can specify different options of this command: |
|
|
|
* |
|
|
|
* |
|
|
|
* ~~~ |
|
|
|
* ~~~ |
|
|
|
* //generate fixtures in russian languge |
|
|
|
* //generate fixtures in russian language |
|
|
|
* php yii fixture/generate users 5 --language='ru_RU' |
|
|
|
* yii fixture/generate users 5 --language='ru_RU' |
|
|
|
* |
|
|
|
* |
|
|
|
* //read templates from the other path |
|
|
|
* //read templates from the other path |
|
|
|
* php yii fixture/generate all --templatePath='@app/path/to/my/custom/templates' |
|
|
|
* yii fixture/generate all --templatePath='@app/path/to/my/custom/templates' |
|
|
|
* |
|
|
|
* |
|
|
|
* //generate fixtures into other folders, but be sure that this folders exists or you will get notice about that. |
|
|
|
* //generate fixtures into other folders, but be sure that this folders exists or you will get notice about that. |
|
|
|
* php yii fixture/generate all --fixturesPath='@tests/unit/fixtures/subfolder1/subfolder2/subfolder3' |
|
|
|
* yii fixture/generate all --fixturePath='@tests/unit/fixtures/subfolder1/subfolder2/subfolder3' |
|
|
|
* ~~~ |
|
|
|
* ~~~ |
|
|
|
* |
|
|
|
* |
|
|
|
* You also can create your own data providers for custom tables fields, see Faker library guide for more info (https://github.com/fzaninotto/Faker); |
|
|
|
* You also can create your own data providers for custom tables fields, see Faker library guide for more info (https://github.com/fzaninotto/Faker); |
|
|
|
* After you created custom provider, for example: |
|
|
|
* After you created custom provider, for example: |
|
|
|
* |
|
|
|
* |
|
|
|
* ~~~ |
|
|
|
* ~~~ |
|
|
|
* |
|
|
|
* class Book extends \Faker\Provider\Base |
|
|
|
* class Book extends \Faker\Provider\Base |
|
|
|
* { |
|
|
|
* { |
|
|
|
* public function title($nbWords = 5) |
|
|
|
* public function title($nbWords = 5) |
|
|
|
* { |
|
|
|
* { |
|
|
|
* $sentence = $this->generator->sentence($nbWords); |
|
|
|
* $sentence = $this->generator->sentence($nbWords); |
|
|
|
* return mb_substr($sentence, 0, mb_strlen($sentence) - 1); |
|
|
|
* return mb_substr($sentence, 0, mb_strlen($sentence) - 1); |
|
|
|
* } |
|
|
|
* } |
|
|
|
|
|
|
|
* |
|
|
|
* |
|
|
|
* public function ISBN() |
|
|
|
* public function ISBN() |
|
|
|
* { |
|
|
|
* { |
|
|
|
* return $this->generator->randomNumber(13); |
|
|
|
* return $this->generator->randomNumber(13); |
|
|
|
* } |
|
|
|
* } |
|
|
|
* } |
|
|
|
* } |
|
|
|
* ~~~ |
|
|
|
* ~~~ |
|
|
|
* |
|
|
|
* |
|
|
|
* you can use it by adding it to the $providers property of the current command. In your console.php config: |
|
|
|
* you can use it by adding it to the $providers property of the current command. In your console.php config: |
|
|
|
* |
|
|
|
* |
|
|
|
* ~~~ |
|
|
|
* ~~~ |
|
|
|
* 'controllerMap' => [ |
|
|
|
* 'controllerMap' => [ |
|
|
|
* 'fixture' => [ |
|
|
|
* 'fixture' => [ |
|
|
|
* 'class' => 'yii\faker\FixtureController', |
|
|
|
* 'class' => 'yii\faker\FixtureController', |
|
|
|
* 'providers' => [ |
|
|
|
* 'providers' => [ |
|
|
|
* 'app\tests\unit\faker\providers\Book', |
|
|
|
* 'app\tests\unit\faker\providers\Book', |
|
|
|
* ], |
|
|
|
* ], |
|
|
|
* ], |
|
|
|
* ], |
|
|
|
* ], |
|
|
|
* ], |
|
|
|
* ~~~ |
|
|
|
* ~~~ |
|
|
|
* |
|
|
|
* |
|
|
|
* @property \Faker\Generator $generator |
|
|
|
* @property \Faker\Generator $generator |
|
|
|
* |
|
|
|
* |
|
|
|
|
|
|
|
* @author Mark Jebri <mark.github@yandex.ru> |
|
|
|
* @since 2.0.0 |
|
|
|
* @since 2.0.0 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
class FixtureController extends \yii\console\controllers\FixtureController |
|
|
|
class FixtureController extends \yii\console\controllers\FixtureController |
|
|
@ -179,7 +181,7 @@ class FixtureController extends \yii\console\controllers\FixtureController |
|
|
|
public function globalOptions() |
|
|
|
public function globalOptions() |
|
|
|
{ |
|
|
|
{ |
|
|
|
return array_merge(parent::globalOptions(), [ |
|
|
|
return array_merge(parent::globalOptions(), [ |
|
|
|
'templatePath','language' |
|
|
|
'templatePath', 'language' |
|
|
|
]); |
|
|
|
]); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -197,19 +199,19 @@ class FixtureController extends \yii\console\controllers\FixtureController |
|
|
|
/** |
|
|
|
/** |
|
|
|
* Generates fixtures and fill them with Faker data. |
|
|
|
* Generates fixtures and fill them with Faker data. |
|
|
|
* @param string $file filename for the table template. You can generate all fixtures for all tables |
|
|
|
* @param string $file filename for the table template. You can generate all fixtures for all tables |
|
|
|
* by specifiyng keyword "all" as filename. |
|
|
|
* by specifying keyword "all" as filename. |
|
|
|
* @param integer $times how much fixtures do you want per table |
|
|
|
* @param integer $times how much fixtures do you want per table |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function actionGenerate(array $file, $times = 2) |
|
|
|
public function actionGenerate(array $file, $times = 2) |
|
|
|
{ |
|
|
|
{ |
|
|
|
$templatePath = Yii::getAlias($this->templatePath); |
|
|
|
$templatePath = Yii::getAlias($this->templatePath); |
|
|
|
$fixturesPath = Yii::getAlias($this->fixturesPath); |
|
|
|
$fixturePath = Yii::getAlias($this->fixturePath); |
|
|
|
|
|
|
|
|
|
|
|
if ($this->needToGenerateAll($file[0])) { |
|
|
|
if ($this->needToGenerateAll($file[0])) { |
|
|
|
$files = FileHelper::findFiles($templatePath, ['only' => ['.php']]); |
|
|
|
$files = FileHelper::findFiles($templatePath, ['only' => ['.php']]); |
|
|
|
} |
|
|
|
} else { |
|
|
|
else { |
|
|
|
$filesToSearch = []; |
|
|
|
foreach($file as $fileName) { |
|
|
|
foreach ($file as $fileName) { |
|
|
|
$filesToSearch[] = $fileName . '.php'; |
|
|
|
$filesToSearch[] = $fileName . '.php'; |
|
|
|
} |
|
|
|
} |
|
|
|
$files = FileHelper::findFiles($templatePath, ['only' => $filesToSearch]); |
|
|
|
$files = FileHelper::findFiles($templatePath, ['only' => $filesToSearch]); |
|
|
@ -217,8 +219,8 @@ class FixtureController extends \yii\console\controllers\FixtureController |
|
|
|
|
|
|
|
|
|
|
|
if (empty($files)) { |
|
|
|
if (empty($files)) { |
|
|
|
throw new Exception( |
|
|
|
throw new Exception( |
|
|
|
"No files were found by name: \"{$file}\". \n" |
|
|
|
"No files were found by name: \"{$file}\". \n" |
|
|
|
. "Check that template with these name exists, under template path: \n\"{$templatePath}\"." |
|
|
|
. "Check that template with these name exists, under template path: \n\"{$templatePath}\"." |
|
|
|
); |
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -235,9 +237,9 @@ class FixtureController extends \yii\console\controllers\FixtureController |
|
|
|
$fixtures[$i] = $this->generateFixture($template, $i); |
|
|
|
$fixtures[$i] = $this->generateFixture($template, $i); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$content = $this->exportFixtures($fixtures); |
|
|
|
$content = $this->exportFixtures($fixtures); |
|
|
|
file_put_contents($fixturesPath.'/'.$fixtureFileName, $content); |
|
|
|
file_put_contents($fixturePath . '/' . $fixtureFileName, $content); |
|
|
|
$this->stdout("Fixture file was generated under: " . realpath($fixturesPath . "/" . $fixtureFileName) . "\n", Console::FG_GREEN); |
|
|
|
$this->stdout("Fixture file was generated under: " . realpath($fixturePath . "/" . $fixtureFileName) . "\n", Console::FG_GREEN); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -250,7 +252,7 @@ class FixtureController extends \yii\console\controllers\FixtureController |
|
|
|
if (is_null($this->_generator)) { |
|
|
|
if (is_null($this->_generator)) { |
|
|
|
//replacing - on _ because Faker support only en_US format and not intl |
|
|
|
//replacing - on _ because Faker support only en_US format and not intl |
|
|
|
|
|
|
|
|
|
|
|
$language = is_null($this->language) ? str_replace('-','_', Yii::$app->language) : $this->language; |
|
|
|
$language = is_null($this->language) ? str_replace('-', '_', Yii::$app->language) : $this->language; |
|
|
|
$this->_generator = \Faker\Factory::create($language); |
|
|
|
$this->_generator = \Faker\Factory::create($language); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -274,7 +276,7 @@ class FixtureController extends \yii\console\controllers\FixtureController |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function addProviders() |
|
|
|
public function addProviders() |
|
|
|
{ |
|
|
|
{ |
|
|
|
foreach($this->providers as $provider) { |
|
|
|
foreach ($this->providers as $provider) { |
|
|
|
$this->generator->addProvider(new $provider($this->generator)); |
|
|
|
$this->generator->addProvider(new $provider($this->generator)); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -308,18 +310,18 @@ class FixtureController extends \yii\console\controllers\FixtureController |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Returns exported to the string representation of given fixtures array. |
|
|
|
* Returns exported to the string representation of given fixtures array. |
|
|
|
* @param type $fixtures |
|
|
|
* @param array $fixtures |
|
|
|
* @return string exported fixtures format |
|
|
|
* @return string exported fixtures format |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function exportFixtures($fixtures) |
|
|
|
public function exportFixtures($fixtures) |
|
|
|
{ |
|
|
|
{ |
|
|
|
$content = "<?php\n\nreturn [";
|
|
|
|
$content = "<?php\n\nreturn [";
|
|
|
|
|
|
|
|
|
|
|
|
foreach($fixtures as $fixture) { |
|
|
|
foreach ($fixtures as $fixture) { |
|
|
|
|
|
|
|
|
|
|
|
$content .= "\n\t["; |
|
|
|
$content .= "\n\t["; |
|
|
|
|
|
|
|
|
|
|
|
foreach($fixture as $name=>$value) { |
|
|
|
foreach ($fixture as $name => $value) { |
|
|
|
$content .= "\n\t\t'{$name}' => '{$value}',"; |
|
|
|
$content .= "\n\t\t'{$name}' => '{$value}',"; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -340,9 +342,9 @@ class FixtureController extends \yii\console\controllers\FixtureController |
|
|
|
{ |
|
|
|
{ |
|
|
|
$fixture = []; |
|
|
|
$fixture = []; |
|
|
|
|
|
|
|
|
|
|
|
foreach($template as $attribute => $fakerProperty) { |
|
|
|
foreach ($template as $attribute => $fakerProperty) { |
|
|
|
if (!is_string($fakerProperty)) { |
|
|
|
if (!is_string($fakerProperty)) { |
|
|
|
$fixture = call_user_func_array($fakerProperty,[$fixture,$this->generator, $index]); |
|
|
|
$fixture = call_user_func_array($fakerProperty, [$fixture, $this->generator, $index]); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
$fixture[$attribute] = $this->generator->$fakerProperty; |
|
|
|
$fixture[$attribute] = $this->generator->$fakerProperty; |
|
|
|
} |
|
|
|
} |
|
|
@ -359,12 +361,12 @@ class FixtureController extends \yii\console\controllers\FixtureController |
|
|
|
public function confirmGeneration($files) |
|
|
|
public function confirmGeneration($files) |
|
|
|
{ |
|
|
|
{ |
|
|
|
$this->stdout("Fixtures will be generated under the path: \n", Console::FG_YELLOW); |
|
|
|
$this->stdout("Fixtures will be generated under the path: \n", Console::FG_YELLOW); |
|
|
|
$this->stdout(realpath(Yii::getAlias($this->fixturesPath, false)) ."\n\n", Console::FG_GREEN); |
|
|
|
$this->stdout(realpath(Yii::getAlias($this->fixturePath, false)) . "\n\n", Console::FG_GREEN); |
|
|
|
$this->stdout("Templates will be taken from path: \n", Console::FG_YELLOW); |
|
|
|
$this->stdout("Templates will be taken from path: \n", Console::FG_YELLOW); |
|
|
|
$this->stdout(realpath(Yii::getAlias($this->templatePath, false)) . "\n\n", Console::FG_GREEN); |
|
|
|
$this->stdout(realpath(Yii::getAlias($this->templatePath, false)) . "\n\n", Console::FG_GREEN); |
|
|
|
|
|
|
|
|
|
|
|
foreach ($files as $index => $fileName) { |
|
|
|
foreach ($files as $index => $fileName) { |
|
|
|
$this->stdout(" " . $index +1 . ". " . basename($fileName) . "\n",Console::FG_GREEN); |
|
|
|
$this->stdout(" " . ($index + 1) . ". " . basename($fileName) . "\n", Console::FG_GREEN); |
|
|
|
} |
|
|
|
} |
|
|
|
return $this->confirm('Generate above fixtures?'); |
|
|
|
return $this->confirm('Generate above fixtures?'); |
|
|
|
} |
|
|
|
} |
|
|
|