Commit c8079ed2 by Qiang Xue

Merge pull request #4891 from Ragazzo/faker_console_controller_adjustments

Faker console controller adjustments
parents ff809683 da261afd
......@@ -84,7 +84,8 @@
"smarty/smarty": "*",
"imagine/imagine": "v0.5.0",
"swiftmailer/swiftmailer": "*",
"cebe/indent": "*"
"cebe/indent": "*",
"fzaninotto/faker": "*"
},
"suggest": {
"phpdocumentor/reflection": "required by yii2-apidoc extension",
......
......@@ -69,14 +69,11 @@ In this script, you can use the following two predefined variables:
With such a template file, you can generate your fixtures using the commands like the following:
```
# generate fixtures for the users table based on users fixture template
php yii fixture/generate User
# also a short version of this command ("generate" action is default)
php yii fixture User
# generate fixtures from user fixture template
php yii fixture/generate user
# to generate several fixture data files
php yii fixture User Profile Team
php yii fixture/generate user profile team
```
In the code above `users` is template name. After running this command, a new file with the same template name
......@@ -107,23 +104,29 @@ php yii fixture/generate-all --templatePath='@app/path/to/my/custom/templates'
php yii fixture/generate-all --fixtureDataPath='@tests/acceptance/fixtures/data'
```
You can see all available templates by running command:
```
# list all templates under default template path (i.e. '@tests/unit/templates/fixtures')
php yii fixture/templates
# list all templates under specified template path
php yii fixture/templates --templatePath='@app/path/to/my/custom/templates'
```
You also can create your own data providers for custom tables fields, see [Faker](https://github.com/fzaninotto/Faker) library guide for more info;
After you created custom provider, for example:
```php
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);
}
public function ISBN()
{
return $this->generator->randomNumber(13);
}
}
```
......
<?php
namespace yiiunit\data\extensions\faker\providers;
use \Faker\Provider\Base;
class Book extends Base
{
public function title($nbWords = 5)
{
$sentence = $this->generator->sentence($nbWords);
return mb_substr($sentence, 0, mb_strlen($sentence) - 1);
}
}
<?php
/**
* @var $faker \Faker\Generator
* @var $index integer
*/
return [
'title' => $faker->title,
];
<?php
/**
* @var $faker \Faker\Generator
* @var $index integer
*/
$security = Yii::$app->getSecurity();
return [
'address' => $faker->address,
'phone' => $faker->phoneNumber,
'first_name' => $faker->firstName,
];
<?php
/**
* @var $faker \Faker\Generator
* @var $index integer
*/
$security = Yii::$app->getSecurity();
return [
'username' => $faker->userName,
'email' => $faker->email,
'auth_key' => $security->generateRandomString(),
'created_at' => time(),
'updated_at' => time(),
];
<?php
namespace yiiunit\extensions\faker;
use Yii;
use yiiunit\TestCase;
use yii\faker\FixtureController;
/**
* Unit test for [[\yii\faker\FixtureController]].
* @see FixtureController
*
* @group console
*/
class FixtureControllerTest extends TestCase
{
/**
* @var \yiiunit\extensions\faker\FixtureConsoledController
*/
private $_fixtureController;
protected function setUp()
{
parent::setUp();
$this->mockApplication();
$this->_fixtureController = Yii::createObject([
'class' => 'yiiunit\extensions\faker\FixtureConsoledController',
'interactive' => false,
'fixtureDataPath' => '@runtime/faker',
'templatePath' => '@yiiunit/data/extensions/faker/templates'
],['fixture-faker', Yii::$app]);
}
public function tearDown()
{
@unlink(Yii::getAlias('@runtime/faker/user.php'));
@unlink(Yii::getAlias('@runtime/faker/profile.php'));
@unlink(Yii::getAlias('@runtime/faker/book.php'));
parent::tearDown();
}
public function testGenerateOne()
{
$filename = Yii::getAlias('@runtime/faker/user.php');
$this->assertFileNotExists($filename, 'file to be generated should not exist before');
$this->_fixtureController->actionGenerate('user');
$this->assertFileExists($filename, 'fixture template file should be generated');
$generatedData = require Yii::getAlias('@runtime/faker/user.php');
$this->assertCount(2, $generatedData, 'by default only 2 fixtures should be generated');
foreach ($generatedData as $fixtureData) {
$this->assertNotNull($fixtureData['username'], 'generated "username" should not be empty');
$this->assertNotNull($fixtureData['email'], 'generated "email" should not be empty');
$this->assertNotNull($fixtureData['auth_key'], 'generated "auth_key" should not be empty');
$this->assertNotNull($fixtureData['created_at'],'generated "created_at" should not be empty');
$this->assertNotNull($fixtureData['updated_at'],'generated "updated_at" should not be empty');
}
}
public function testGenerateBoth()
{
$userFilename = Yii::getAlias('@runtime/faker/user.php');
$this->assertFileNotExists($userFilename, 'file to be generated should not exist before');
$profileFilename = Yii::getAlias('@runtime/faker/profile.php');
$this->assertFileNotExists($profileFilename, 'file to be generated should not exist before');
$this->_fixtureController->actionGenerate('user', 'profile');
$this->assertFileExists($userFilename, 'fixture template file should be generated');
$this->assertFileExists($profileFilename, 'fixture template file should be generated');
}
public function testGenerateNotFound()
{
$fileName = Yii::getAlias('@runtime/faker/not_existing_template.php');
$this->_fixtureController->actionGenerate('not_existing_template');
$this->assertFileNotExists($fileName, 'not existing template should not be generated');
}
public function testGenerateProvider()
{
$bookFilename = Yii::getAlias('@runtime/faker/book.php');
$this->assertFileNotExists($bookFilename, 'file to be generated should not exist before');
$this->_fixtureController->providers[] = 'yiiunit\data\extensions\faker\providers\Book';
$this->_fixtureController->run('generate',['book']);
$this->assertFileExists($bookFilename, 'fixture template file should be generated');
}
/**
* @expectedException yii\console\Exception
*/
public function testNothingToGenerateException()
{
$this->_fixtureController->actionGenerate();
}
/**
* @expectedException yii\console\Exception
*/
public function testWrongTemplatePathException()
{
$this->_fixtureController->templatePath = '@not/existing/fixtures/templates/path';
$this->_fixtureController->run('generate',['user']);
}
public function testGenerateParticularTimes()
{
$filename = Yii::getAlias('@runtime/faker/user.php');
$this->assertFileNotExists($filename, 'file to be generated should not exist before');
$this->_fixtureController->count = 5;
$this->_fixtureController->actionGenerate('user');
$this->assertFileExists($filename, 'fixture template file should be generated');
$generatedData = require Yii::getAlias('@runtime/faker/user.php');
$this->assertCount(5, $generatedData, 'exactly 5 fixtures should be generated for the given template');
}
public function testGenerateParticlularLanguage()
{
$filename = Yii::getAlias('@runtime/faker/profile.php');
$this->assertFileNotExists($filename, 'file to be generated should not exist before');
$this->_fixtureController->language = 'ru_RU';
$this->_fixtureController->actionGenerate('profile');
$this->assertFileExists($filename, 'fixture template file should be generated');
$generatedData = require Yii::getAlias('@runtime/faker/profile.php');
$this->assertEquals(1, preg_match('/^[а-я]*$/iu', $generatedData[0]['first_name']), 'generated value should be in ru-RU language');
}
public function testGenerateAll()
{
$userFilename = Yii::getAlias('@runtime/faker/user.php');
$this->assertFileNotExists($userFilename, 'file to be generated should not exist before');
$profileFilename = Yii::getAlias('@runtime/faker/profile.php');
$this->assertFileNotExists($profileFilename, 'file to be generated should not exist before');
$bookFilename = Yii::getAlias('@runtime/faker/book.php');
$this->assertFileNotExists($bookFilename, 'file to be generated should not exist before');
$this->_fixtureController->providers[] = 'yiiunit\data\extensions\faker\providers\Book';
$this->_fixtureController->run('generate-all');
$this->assertFileExists($userFilename, 'fixture template file should be generated');
$this->assertFileExists($profileFilename, 'fixture template file should be generated');
$this->assertFileExists($bookFilename, 'fixture template file should be generated');
}
}
class FixtureConsoledController extends FixtureController
{
public function stdout($string)
{
}
}
......@@ -6,6 +6,12 @@ use Yii;
use yiiunit\TestCase;
use yii\console\controllers\CacheController;
/**
* Unit test for [[\yii\console\controllers\CacheController]].
* @see CacheController
*
* @group console
*/
class CacheControllerTest extends TestCase
{
......@@ -83,7 +89,6 @@ class CacheControllerTest extends TestCase
$this->assertFalse(Yii::$app->firstCache->get('firstKey'),'first cache data should be flushed');
$this->assertFalse(Yii::$app->secondCache->get('thirdKey'), 'second cache data should be flushed');
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment