phpunit,在 yii1 中使用其他 fixture

phpunit, use other fixture in yii1

我在手册中找到了如何使用 fixtures 的例子

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

    …
}

或者我可以这样使用 'post' => ':post'。 但是我想在其他测试中使用其他夹具,例如:当我测试模型 Post 时,我使用 "post.php" 夹具,而当我测试模型作者时,我想使用 post_for_author.php (数据必须插入 table post)

我试着这样写:

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

    …
}

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

像这样使用独立的固定装置。

application/tests/unit:

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

application/tests:

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 并在其中放置此 class

的固定装置