phpunit,在yii1中使用其他fixture

时间:2016-02-26 21:51:48

标签: php yii phpunit fixtures

我找到了如何在手册中使用灯具的示例

class CommentTest extends CDbTestCase
{
    public $fixtures=array(
        'posts'=>'Post',
        'comments'=>'Comment',
    );

    …
}

或者我可以像这样使用' post' => ':交&#39 ;. 但我想在其他测试中使用其他灯具,例如:当我测试模型后,我使用" post.php"夹具,当我测试模型作者我想使用post_for_author.php(数据必须在表格中插入)

我尝试这样写:

class CommentTest extends CDbTestCase
{
    public $fixtures=array(
        'posts_for_author'=>'Post',
        'comments'=>'Comment',
    );

    …
}

和其他方式' posts_for_author' =>':post', 但它不起作用。请帮帮我,怎么做?

1 个答案:

答案 0 :(得分:1)

使用像这样的隔离装置。

应用/测试/单位:

    class CommentTest extends IsolatedFixtureDbTestCase {
        public $fixtures=array(
            'posts_for_author'=>'Post',
            'comments'=>'Comment',
        );
        ...
    }

应用/测试:

abstract class IsolatedFixtureDbTestCase extends CDbTestCase
{
    private $basePathOld = null;

    public function setUp()
    {
        $fixturePath = Yii::getPathOfAlias('application.tests.fixtures.' . get_class($this));
        if (is_dir($fixturePath)) {
            $this->basePathOld = $this->getFixtureManager()->basePath;
            $this->getFixtureManager()->basePath = $fixturePath;
        }
        parent::setUp();
    }

    public function tearDown()
    {
        parent::tearDown();
        if (null !== $this->basePathOld) {
            $this->getFixtureManager()->basePath = $this->basePathOld;
        }
    }
}

然后在application / tests / fixtures中你可以创建文件夹CommentTest以及那个类的灯具