FixtureControllerTest.php 6.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
<?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();

27 28 29 30 31
        if (defined('HHVM_VERSION')) {
            // https://github.com/facebook/hhvm/issues/1447
            $this->markTestSkipped('Can not test on HHVM because require is cached.');
        }

32 33 34 35 36 37 38
        $this->mockApplication();

        $this->_fixtureController = Yii::createObject([
            'class' => 'yiiunit\extensions\faker\FixtureConsoledController',
            'interactive' => false,
            'fixtureDataPath' => '@runtime/faker',
            'templatePath' => '@yiiunit/data/extensions/faker/templates'
Mark committed
39
        ],['fixture-faker', Yii::$app]);
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
    }

    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)
    {
    }

}