ConorHolt
ConorHolt

Reputation: 291

phpunit, use other fixture in yii1

I found example how use fixtures in manual

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

    …
}

or i can use like this 'post' => ':post'. But i want use other fixtures in other tests, example: when i tested model Post i use "post.php" fixture, and when i tested model Author i want use post_for_author.php (data must insert in table post)

I try write like this:

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

    …
}

and other way 'posts_for_author'=>':post', but it doesn't work. Please help me, how do it?

Upvotes: 1

Views: 321

Answers (1)

Samuel Loog
Samuel Loog

Reputation: 272

Use isolated fixtures like this.

application/tests/unit:

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

and

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;
        }
    }
}

and then in application/tests/fixtures you can create folder CommentTest and there your fixtures for this class

Upvotes: 1

Related Questions