FixtureTest.php 4.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yiiunit\framework\test;

use yii\test\Fixture;
use yii\test\FixtureTrait;
use yiiunit\TestCase;

class Fixture1 extends Fixture
{
16
    public $depends = ['yiiunit\framework\test\Fixture2'];
17

18 19 20 21
    public function load()
    {
        MyTestCase::$load .= '1';
    }
22

23 24 25 26
    public function unload()
    {
        MyTestCase::$unload .= '1';
    }
27 28 29 30
}

class Fixture2 extends Fixture
{
31 32 33 34 35 36 37 38 39 40
    public $depends = ['yiiunit\framework\test\Fixture3'];
    public function load()
    {
        MyTestCase::$load .= '2';
    }

    public function unload()
    {
        MyTestCase::$unload .= '2';
    }
41 42 43 44
}

class Fixture3 extends Fixture
{
45 46 47 48 49 50 51 52 53
    public function load()
    {
        MyTestCase::$load .= '3';
    }

    public function unload()
    {
        MyTestCase::$unload .= '3';
    }
54 55 56 57
}

class MyTestCase
{
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
    use FixtureTrait;

    public $scenario = 1;
    public static $load;
    public static $unload;

    public function setUp()
    {
        $this->loadFixtures();
    }

    public function tearDown()
    {
        $this->unloadFixtures();
    }

    public function fetchFixture($name)
    {
        return $this->getFixture($name);
    }

    public function fixtures()
    {
        switch ($this->scenario) {
            case 0: return [];
            case 1: return [
                'fixture1' => Fixture1::className(),
            ];
            case 2: return [
                'fixture2' => Fixture2::className(),
            ];
            case 3: return [
                'fixture3' => Fixture3::className(),
            ];
            case 4: return [
                'fixture1' => Fixture1::className(),
                'fixture2' => Fixture2::className(),
            ];
            case 5: return [
                'fixture2' => Fixture2::className(),
                'fixture3' => Fixture3::className(),
            ];
            case 6: return [
                'fixture1' => Fixture1::className(),
                'fixture3' => Fixture3::className(),
            ];
            case 7:
            default: return [
                'fixture1' => Fixture1::className(),
                'fixture2' => Fixture2::className(),
                'fixture3' => Fixture3::className(),
            ];
        }
    }
112 113 114 115
}

class FixtureTest extends TestCase
{
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
    public function testDependencies()
    {
        foreach ($this->getDependencyTests() as $scenario => $result) {
            $test = new MyTestCase();
            $test->scenario = $scenario;
            $test->setUp();
            foreach ($result as $name => $loaded) {
                $this->assertEquals($loaded, $test->fetchFixture($name) !== null, "Verifying scenario $scenario fixture $name");
            }
        }
    }

    public function testLoadSequence()
    {
        foreach ($this->getLoadSequenceTests() as $scenario => $result) {
            $test = new MyTestCase();
            $test->scenario = $scenario;
            MyTestCase::$load = '';
            MyTestCase::$unload = '';
            $test->setUp();
            $this->assertEquals($result[0], MyTestCase::$load, "Verifying scenario $scenario load sequence");
            $test->tearDown();
            $this->assertEquals($result[1], MyTestCase::$unload, "Verifying scenario $scenario unload sequence");
        }
    }

    protected function getDependencyTests()
    {
        return [
            0 => ['fixture1' => false, 'fixture2' => false, 'fixture3' => false],
            1 => ['fixture1' => true, 'fixture2' => false, 'fixture3' => false],
            2 => ['fixture1' => false, 'fixture2' => true, 'fixture3' => false],
            3 => ['fixture1' => false, 'fixture2' => false, 'fixture3' => true],
            4 => ['fixture1' => true, 'fixture2' => true, 'fixture3' => false],
            5 => ['fixture1' => false, 'fixture2' => true, 'fixture3' => true],
            6 => ['fixture1' => true, 'fixture2' => false, 'fixture3' => true],
            7 => ['fixture1' => true, 'fixture2' => true, 'fixture3' => true],
        ];
    }

    protected function getLoadSequenceTests()
    {
        return [
            0 => ['', ''],
            1 => ['321', '123'],
            2 => ['32', '23'],
            3 => ['3', '3'],
            4 => ['321', '123'],
            5 => ['32', '23'],
            6 => ['321', '123'],
            7 => ['321', '123'],
        ];
    }
169
}