You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
369 lines
10 KiB
369 lines
10 KiB
11 years ago
|
<?php
|
||
|
/**
|
||
|
* @link http://www.yiiframework.com/
|
||
|
* @copyright Copyright (c) 2008 Yii Software LLC
|
||
|
* @license http://www.yiiframework.com/license/
|
||
|
*/
|
||
|
|
||
|
namespace yii\faker;
|
||
|
|
||
|
use Yii;
|
||
|
use yii\console\Exception;
|
||
|
use yii\helpers\FileHelper;
|
||
|
use yii\helpers\Console;
|
||
|
|
||
|
/**
|
||
|
* This command manage fixtures creations based on given template.
|
||
|
*
|
||
|
* Fixtures are one of the important paths in unit testing. To speed up developers
|
||
11 years ago
|
* work these fixtures can be generated automatically, based on prepared template.
|
||
11 years ago
|
* This command is a simple wrapper for the fixtures library [Faker](https://github.com/fzaninotto/Faker).
|
||
11 years ago
|
*
|
||
|
* You should configure this command as follows (you can use any alias, not only "fixture"):
|
||
|
*
|
||
11 years ago
|
* ~~~
|
||
11 years ago
|
* 'controllerMap' => [
|
||
|
* 'fixture' => [
|
||
|
* 'class' => 'yii\faker\FixtureController',
|
||
|
* ],
|
||
|
* ],
|
||
11 years ago
|
* ~~~
|
||
11 years ago
|
*
|
||
11 years ago
|
* 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:
|
||
11 years ago
|
*
|
||
11 years ago
|
* ~~~
|
||
|
* #users.php file under $templatePath
|
||
11 years ago
|
*
|
||
11 years ago
|
* return [
|
||
11 years ago
|
* [
|
||
|
* 'table_column0' => 'faker_formatter',
|
||
|
* ...
|
||
|
* 'table_columnN' => 'other_faker_formatter
|
||
|
* 'table_columnN+1' => function ($fixture, $faker, $index) {
|
||
|
* //set needed fixture fields based on different conditions
|
||
|
* return $fixture;
|
||
|
* }
|
||
|
* ],
|
||
11 years ago
|
* ];
|
||
|
* ~~~
|
||
11 years ago
|
*
|
||
11 years ago
|
* If you use callback as a attribute value, then it will be called as shown with three parameters:
|
||
11 years ago
|
*
|
||
|
* - `$fixture` - current fixture array.
|
||
|
* - `$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
|
||
|
*
|
||
11 years ago
|
* After you set all needed fields in callback, you need to return $fixture array back from the callback.
|
||
11 years ago
|
*
|
||
11 years ago
|
* After you prepared needed templates for tables you can simply generate your fixtures via command
|
||
11 years ago
|
*
|
||
11 years ago
|
* ~~~
|
||
11 years ago
|
* yii fixture/generate users
|
||
|
*
|
||
11 years ago
|
* //also a short version of this command (generate action is default)
|
||
11 years ago
|
* yii fixture users
|
||
11 years ago
|
*
|
||
|
* //to generate fixtures for several tables, use "," as a separator, for example:
|
||
11 years ago
|
* yii fixture users,profile
|
||
11 years ago
|
* ~~~
|
||
11 years ago
|
*
|
||
11 years ago
|
* In the code above "users" is template name, after this command run, new file named same as template
|
||
11 years ago
|
* will be created under the `$fixturePath` folder.
|
||
11 years ago
|
* You can generate fixtures for all templates by specifying keyword "all"
|
||
|
*
|
||
11 years ago
|
* ~~~
|
||
11 years ago
|
* yii fixture/generate all
|
||
11 years ago
|
* ~~~
|
||
11 years ago
|
*
|
||
|
* This command will generate fixtures for all template files that are stored under $templatePath and
|
||
|
* store fixtures under $fixturePath with file names same as templates names.
|
||
|
*
|
||
11 years ago
|
* 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).
|
||
11 years ago
|
*
|
||
11 years ago
|
* ~~~
|
||
11 years ago
|
* yii fixture/generate all 3
|
||
11 years ago
|
* ~~~
|
||
11 years ago
|
*
|
||
11 years ago
|
* You can specify different options of this command:
|
||
11 years ago
|
*
|
||
11 years ago
|
* ~~~
|
||
11 years ago
|
* //generate fixtures in russian language
|
||
11 years ago
|
* yii fixture/generate users 5 --language=ru_RU
|
||
11 years ago
|
*
|
||
11 years ago
|
* //read templates from the other path
|
||
11 years ago
|
* yii fixture/generate all --templatePath=@app/path/to/my/custom/templates
|
||
11 years ago
|
*
|
||
11 years ago
|
* //generate fixtures into other folders, but be sure that this folders exists or you will get notice about that.
|
||
11 years ago
|
* yii fixture/generate all --fixturePath=@tests/unit/fixtures/subfolder1/subfolder2/subfolder3
|
||
11 years ago
|
* ~~~
|
||
11 years ago
|
*
|
||
11 years ago
|
* 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:
|
||
11 years ago
|
*
|
||
11 years ago
|
* ~~~
|
||
11 years ago
|
* class Book extends \Faker\Provider\Base
|
||
|
* {
|
||
|
* public function title($nbWords = 5)
|
||
|
* {
|
||
|
* $sentence = $this->generator->sentence($nbWords);
|
||
|
* return mb_substr($sentence, 0, mb_strlen($sentence) - 1);
|
||
|
* }
|
||
11 years ago
|
*
|
||
11 years ago
|
* public function ISBN()
|
||
|
* {
|
||
|
* return $this->generator->randomNumber(13);
|
||
|
* }
|
||
|
* }
|
||
11 years ago
|
* ~~~
|
||
11 years ago
|
*
|
||
11 years ago
|
* you can use it by adding it to the $providers property of the current command. In your console.php config:
|
||
11 years ago
|
*
|
||
11 years ago
|
* ~~~
|
||
11 years ago
|
* 'controllerMap' => [
|
||
|
* 'fixture' => [
|
||
|
* 'class' => 'yii\faker\FixtureController',
|
||
|
* 'providers' => [
|
||
|
* 'app\tests\unit\faker\providers\Book',
|
||
|
* ],
|
||
|
* ],
|
||
|
* ],
|
||
11 years ago
|
* ~~~
|
||
11 years ago
|
*
|
||
11 years ago
|
* @property \Faker\Generator $generator
|
||
11 years ago
|
*
|
||
|
* @author Mark Jebri <mark.github@yandex.ru>
|
||
11 years ago
|
* @since 2.0.0
|
||
|
*/
|
||
|
class FixtureController extends \yii\console\controllers\FixtureController
|
||
|
{
|
||
|
/**
|
||
|
* type of fixture generating
|
||
|
*/
|
||
11 years ago
|
const GENERATE_ALL = 'all';
|
||
11 years ago
|
|
||
|
/**
|
||
|
* @var string controller default action ID.
|
||
|
*/
|
||
|
public $defaultAction = 'generate';
|
||
|
/**
|
||
|
* Alias to the template path, where all tables templates are stored.
|
||
|
* @var string
|
||
|
*/
|
||
|
public $templatePath = '@tests/unit/templates/fixtures';
|
||
|
/**
|
||
|
* Language to use when generating fixtures data.
|
||
|
* @var string
|
||
|
*/
|
||
|
public $language;
|
||
|
/**
|
||
|
* Additional data providers that can be created by user and will be added to the Faker generator.
|
||
|
* More info in [Faker](https://github.com/fzaninotto/Faker.) library docs.
|
||
|
* @var array
|
||
|
*/
|
||
11 years ago
|
public $providers = [];
|
||
11 years ago
|
/**
|
||
|
* Faker generator instance
|
||
|
* @var \Faker\Generator
|
||
|
*/
|
||
|
private $_generator;
|
||
|
|
||
11 years ago
|
|
||
11 years ago
|
/**
|
||
|
* Returns the names of the global options for this command.
|
||
|
* @return array the names of the global options for this command.
|
||
|
*/
|
||
|
public function globalOptions()
|
||
|
{
|
||
|
return array_merge(parent::globalOptions(), [
|
||
11 years ago
|
'templatePath', 'language'
|
||
11 years ago
|
]);
|
||
|
}
|
||
|
|
||
|
public function beforeAction($action)
|
||
|
{
|
||
|
if (parent::beforeAction($action)) {
|
||
|
$this->checkPaths();
|
||
|
$this->addProviders();
|
||
|
return true;
|
||
|
} else {
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Generates fixtures and fill them with Faker data.
|
||
|
* @param string $file filename for the table template. You can generate all fixtures for all tables
|
||
11 years ago
|
* by specifying keyword "all" as filename.
|
||
11 years ago
|
* @param integer $times how much fixtures do you want per table
|
||
|
*/
|
||
11 years ago
|
public function actionGenerate(array $file, $times = 2)
|
||
11 years ago
|
{
|
||
|
$templatePath = Yii::getAlias($this->templatePath);
|
||
11 years ago
|
$fixturePath = Yii::getAlias($this->fixturePath);
|
||
11 years ago
|
|
||
11 years ago
|
if ($this->needToGenerateAll($file[0])) {
|
||
11 years ago
|
$files = FileHelper::findFiles($templatePath, ['only' => ['.php']]);
|
||
11 years ago
|
} else {
|
||
|
$filesToSearch = [];
|
||
|
foreach ($file as $fileName) {
|
||
11 years ago
|
$filesToSearch[] = $fileName . '.php';
|
||
|
}
|
||
|
$files = FileHelper::findFiles($templatePath, ['only' => $filesToSearch]);
|
||
11 years ago
|
}
|
||
|
|
||
|
if (empty($files)) {
|
||
11 years ago
|
throw new Exception("No files were found by name: \"" . implode(', ', $file) . "\".\n"
|
||
11 years ago
|
. "Check that template with these name exists, under template path: \n\"{$templatePath}\"."
|
||
11 years ago
|
);
|
||
|
}
|
||
11 years ago
|
|
||
11 years ago
|
if (!$this->confirmGeneration($files)) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
11 years ago
|
foreach ($files as $templateFile) {
|
||
11 years ago
|
$fixtureFileName = basename($templateFile);
|
||
|
$template = $this->getTemplate($templateFile);
|
||
|
$fixtures = [];
|
||
|
|
||
|
for ($i = 0; $i < $times; $i++) {
|
||
|
$fixtures[$i] = $this->generateFixture($template, $i);
|
||
|
}
|
||
|
|
||
11 years ago
|
$content = $this->exportFixtures($fixtures);
|
||
|
file_put_contents($fixturePath . '/' . $fixtureFileName, $content);
|
||
|
$this->stdout("Fixture file was generated under: " . realpath($fixturePath . "/" . $fixtureFileName) . "\n", Console::FG_GREEN);
|
||
11 years ago
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns Faker generator instance. Getter for private property.
|
||
|
* @return \Faker\Generator
|
||
|
*/
|
||
|
public function getGenerator()
|
||
|
{
|
||
11 years ago
|
if (is_null($this->_generator)) {
|
||
|
//replacing - on _ because Faker support only en_US format and not intl
|
||
11 years ago
|
|
||
11 years ago
|
$language = is_null($this->language) ? str_replace('-', '_', Yii::$app->language) : $this->language;
|
||
11 years ago
|
$this->_generator = \Faker\Factory::create($language);
|
||
|
}
|
||
|
|
||
|
return $this->_generator;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Check if the template path and migrations path exists and writable.
|
||
|
*/
|
||
|
public function checkPaths()
|
||
|
{
|
||
|
$path = Yii::getAlias($this->templatePath);
|
||
|
|
||
11 years ago
|
if (!is_dir($path)) {
|
||
11 years ago
|
throw new Exception("The template path \"{$this->templatePath}\" not exist");
|
||
11 years ago
|
}
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Adds users providers to the faker generator.
|
||
|
*/
|
||
|
public function addProviders()
|
||
|
{
|
||
11 years ago
|
foreach ($this->providers as $provider) {
|
||
11 years ago
|
$this->generator->addProvider(new $provider($this->generator));
|
||
11 years ago
|
}
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Checks if needed to generate all fixtures.
|
||
|
* @param string $file
|
||
|
* @return bool
|
||
|
*/
|
||
|
public function needToGenerateAll($file)
|
||
|
{
|
||
|
return $file == self::GENERATE_ALL;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns generator template for the given fixture name
|
||
|
* @param string $file template file
|
||
|
* @return array generator template
|
||
|
* @throws \yii\console\Exception if wrong file format
|
||
|
*/
|
||
|
public function getTemplate($file)
|
||
|
{
|
||
|
$template = require($file);
|
||
|
|
||
|
if (!is_array($template)) {
|
||
|
throw new Exception("The template file \"$file\" has wrong format. It should return valid template array");
|
||
|
}
|
||
|
|
||
|
return $template;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns exported to the string representation of given fixtures array.
|
||
11 years ago
|
* @param array $fixtures
|
||
11 years ago
|
* @return string exported fixtures format
|
||
|
*/
|
||
11 years ago
|
public function exportFixtures($fixtures)
|
||
11 years ago
|
{
|
||
|
$content = "<?php\n\nreturn [";
|
||
|
|
||
11 years ago
|
foreach ($fixtures as $fixture) {
|
||
11 years ago
|
|
||
|
$content .= "\n\t[";
|
||
|
|
||
11 years ago
|
foreach ($fixture as $name => $value) {
|
||
11 years ago
|
$content .= "\n\t\t'{$name}' => '{$value}',";
|
||
|
}
|
||
|
|
||
|
$content .= "\n\t],";
|
||
|
|
||
|
}
|
||
|
$content .= "\n];\n";
|
||
|
return $content;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Generates fixture from given template
|
||
|
* @param array $template fixture template
|
||
|
* @param integer $index current fixture index
|
||
|
* @return array fixture
|
||
|
*/
|
||
|
public function generateFixture($template, $index)
|
||
|
{
|
||
|
$fixture = [];
|
||
|
|
||
11 years ago
|
foreach ($template as $attribute => $fakerProperty) {
|
||
11 years ago
|
if (!is_string($fakerProperty)) {
|
||
11 years ago
|
$fixture = call_user_func_array($fakerProperty, [$fixture, $this->generator, $index]);
|
||
11 years ago
|
} else {
|
||
|
$fixture[$attribute] = $this->generator->$fakerProperty;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return $fixture;
|
||
|
}
|
||
|
|
||
11 years ago
|
/**
|
||
|
* Prompts user with message if he confirm generation with given fixture templates files.
|
||
|
* @param array $files
|
||
|
* @return boolean
|
||
|
*/
|
||
|
public function confirmGeneration($files)
|
||
|
{
|
||
|
$this->stdout("Fixtures will be generated under the path: \n", Console::FG_YELLOW);
|
||
11 years ago
|
$this->stdout(realpath(Yii::getAlias($this->fixturePath, false)) . "\n\n", Console::FG_GREEN);
|
||
11 years ago
|
$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);
|
||
|
|
||
|
foreach ($files as $index => $fileName) {
|
||
11 years ago
|
$this->stdout(" " . ($index + 1) . ". " . basename($fileName) . "\n", Console::FG_GREEN);
|
||
11 years ago
|
}
|
||
|
return $this->confirm('Generate above fixtures?');
|
||
|
}
|
||
11 years ago
|
}
|