FixtureControllerTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yiiunit\faker;
  8. use Yii;
  9. use yii\faker\FixtureController;
  10. /**
  11. * Unit test for [[\yii\faker\FixtureController]].
  12. * @see FixtureController
  13. *
  14. * @group console
  15. */
  16. class FixtureControllerTest extends TestCase
  17. {
  18. /**
  19. * @var FixtureConsoledController
  20. */
  21. private $_fixtureController;
  22. /**
  23. * {@inheritdoc}
  24. */
  25. protected function setUp()
  26. {
  27. parent::setUp();
  28. if (defined('HHVM_VERSION')) {
  29. // https://github.com/facebook/hhvm/issues/1447
  30. $this->markTestSkipped('Can not test on HHVM because require is cached.');
  31. }
  32. $this->mockApplication();
  33. $this->_fixtureController = Yii::createObject([
  34. 'class' => 'yiiunit\faker\FixtureConsoledController',
  35. 'interactive' => false,
  36. 'fixtureDataPath' => '@runtime/faker',
  37. 'templatePath' => '@yiiunit/faker/data/templates'
  38. ],['fixture-faker', Yii::$app]);
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function tearDown()
  44. {
  45. @unlink(Yii::getAlias('@runtime/faker/user.php'));
  46. @unlink(Yii::getAlias('@runtime/faker/profile.php'));
  47. @unlink(Yii::getAlias('@runtime/faker/book.php'));
  48. parent::tearDown();
  49. }
  50. public function testGenerateOne()
  51. {
  52. $filename = Yii::getAlias('@runtime/faker/user.php');
  53. $this->assertFileNotExists($filename, 'file to be generated should not exist before');
  54. $this->_fixtureController->actionGenerate('user');
  55. $this->assertFileExists($filename, 'fixture template file should be generated');
  56. $generatedData = require Yii::getAlias('@runtime/faker/user.php');
  57. $this->assertCount(2, $generatedData, 'by default only 2 fixtures should be generated');
  58. foreach ($generatedData as $fixtureData) {
  59. $this->assertNotNull($fixtureData['username'], 'generated "username" should not be empty');
  60. $this->assertNotNull($fixtureData['email'], 'generated "email" should not be empty');
  61. $this->assertNotNull($fixtureData['auth_key'], 'generated "auth_key" should not be empty');
  62. $this->assertNotNull($fixtureData['created_at'],'generated "created_at" should not be empty');
  63. $this->assertNotNull($fixtureData['updated_at'],'generated "updated_at" should not be empty');
  64. }
  65. }
  66. public function testGenerateBoth()
  67. {
  68. $userFilename = Yii::getAlias('@runtime/faker/user.php');
  69. $this->assertFileNotExists($userFilename, 'file to be generated should not exist before');
  70. $profileFilename = Yii::getAlias('@runtime/faker/profile.php');
  71. $this->assertFileNotExists($profileFilename, 'file to be generated should not exist before');
  72. $this->_fixtureController->actionGenerate('user', 'profile');
  73. $this->assertFileExists($userFilename, 'fixture template file should be generated');
  74. $this->assertFileExists($profileFilename, 'fixture template file should be generated');
  75. }
  76. public function testGenerateNotFound()
  77. {
  78. $fileName = Yii::getAlias('@runtime/faker/not_existing_template.php');
  79. $this->_fixtureController->actionGenerate('not_existing_template');
  80. $this->assertFileNotExists($fileName, 'not existing template should not be generated');
  81. }
  82. public function testGenerateProvider()
  83. {
  84. $bookFilename = Yii::getAlias('@runtime/faker/book.php');
  85. $this->assertFileNotExists($bookFilename, 'file to be generated should not exist before');
  86. $this->_fixtureController->providers[] = 'yiiunit\faker\data\providers\Book';
  87. $this->_fixtureController->run('generate',['book']);
  88. $this->assertFileExists($bookFilename, 'fixture template file should be generated');
  89. }
  90. /**
  91. * @expectedException \yii\console\Exception
  92. */
  93. public function testNothingToGenerateException()
  94. {
  95. $this->_fixtureController->actionGenerate();
  96. }
  97. /**
  98. * @expectedException \yii\console\Exception
  99. */
  100. public function testWrongTemplatePathException()
  101. {
  102. $this->_fixtureController->templatePath = '@not/existing/fixtures/templates/path';
  103. $this->_fixtureController->run('generate',['user']);
  104. }
  105. public function testGenerateParticularTimes()
  106. {
  107. $filename = Yii::getAlias('@runtime/faker/user.php');
  108. $this->assertFileNotExists($filename, 'file to be generated should not exist before');
  109. $this->_fixtureController->count = 5;
  110. $this->_fixtureController->actionGenerate('user');
  111. $this->assertFileExists($filename, 'fixture template file should be generated');
  112. $generatedData = require Yii::getAlias('@runtime/faker/user.php');
  113. $this->assertCount(5, $generatedData, 'exactly 5 fixtures should be generated for the given template');
  114. }
  115. public function testGenerateParticlularLanguage()
  116. {
  117. $filename = Yii::getAlias('@runtime/faker/profile.php');
  118. $this->assertFileNotExists($filename, 'file to be generated should not exist before');
  119. $this->_fixtureController->language = 'ru_RU';
  120. $this->_fixtureController->actionGenerate('profile');
  121. $this->assertFileExists($filename, 'fixture template file should be generated');
  122. $generatedData = require Yii::getAlias('@runtime/faker/profile.php');
  123. $this->assertEquals(1, preg_match('/^[а-яё]*$/iu', $generatedData['profile0']['first_name']), 'generated value should be in ru-RU language but is: ' . $generatedData['profile0']['first_name']);
  124. }
  125. public function testGenerateAll()
  126. {
  127. $userFilename = Yii::getAlias('@runtime/faker/user.php');
  128. $this->assertFileNotExists($userFilename, 'file to be generated should not exist before');
  129. $profileFilename = Yii::getAlias('@runtime/faker/profile.php');
  130. $this->assertFileNotExists($profileFilename, 'file to be generated should not exist before');
  131. $bookFilename = Yii::getAlias('@runtime/faker/book.php');
  132. $this->assertFileNotExists($bookFilename, 'file to be generated should not exist before');
  133. $this->_fixtureController->providers[] = 'yiiunit\faker\data\providers\Book';
  134. $this->_fixtureController->run('generate-all');
  135. $this->assertFileExists($userFilename, 'fixture template file should be generated');
  136. $this->assertFileExists($profileFilename, 'fixture template file should be generated');
  137. $this->assertFileExists($bookFilename, 'fixture template file should be generated');
  138. }
  139. }
  140. class FixtureConsoledController extends FixtureController
  141. {
  142. public function stdout($string)
  143. {
  144. }
  145. }