Diffenerent Fixtures for Different Test Cases

Hi!

Can I use different fixture data in different testcases? As far as I can see




    public $fixtures=array(

        'posts'=>'Post',

        'comments'=>'Comment',

    );

does imply to use fixtures/Post.php to fill the table "Post".

What I need is: using (e.g.) Post1.php for test case 1, and using Php2.php for test case 2.

Thanks!

Hi,

I was just looking for the same today. As far as I can see, answer is no as tableName has to match an existing table.

I modified CDbFixtureManager to provide this functionnality by supporting variations. Try the attached patch.

501

fixtureManagerWithVariations.patch.txt

You need to add fixtures with the tableName convention, and add an "#yourvariation" to the table name e.g.:




public $fixtures=array(

    'posts'=>':post#variation1',

    'comments'=>'Comment',

);

This will expect a fixture file named : post#variation1.php

Please test it (works for me). I could write it as an extension of CDbFixtureManager, or send a patch to improve existing component.

I also needed this functionality, but went a different route. I wanted to do this without changing CDbFixtureManager but it did need some small changes. Everywhere with $this->basePath.DIRECTORY_SEPARATOR.$foobar i replaced with $this->findFixtureFile($foobar) and findFixtureFile (in CDbFixtureManager) just returns the string as expected.

Then i made an extending class to CDbFixtureManager which has 3 functions:




setSubFixture(string $currentTest) expects to receive the results of PHPUnit_Framework_TestCase::toString and stores the data

resetSubFixture() disables the checking for sub dirs

findFixtureFile(string $fileName) searches for fileName in the following order:

- basepath/className/funcName/

- basepath/className/

- basepath/



finally CDbTestCase and CWebTestCase need to have their setUp and tearDown functions extended in a class you can then extend your tests from




setUp should call $this->fixtureManager->setSubFixture($this->toString())

tearDown should call $this->fixtureManager->resetSubFixture()



With this setup you can insert specific fixtures as needed in various tests in a fairly straightforward manner, and the location of the fixture file lets you know exactly which tests its used in

One issue i can see is using the same fixture in multiple tests, but not for the whole class. in this instance i use symlinks(linux command:ln -s) so the same file is seen in multiple directories but there might be a better solution. One possible solution would be to allow yii paths(application.test.fixtures.foo.bar, but minus the basepath so just foo.bar) when setting the subFixture, and a smarter setUp() accessing perhaps a member array that contains testName->path settings for fixture directories.

Im not sure what the best solution is going to be, but i think Yii needs something to allow multiple fixtures for a given table.

journey4712

Hi,

I’m new to Fixture and I also needed different fixture for different tests, so what I did is define the CDbFixtureManager basePath in the setUp method of my test.


class UserTest extends CDbTestCase

{

    public $fixtures=array(

        'users'=>'user',

    );

    protected function setUp()

    {

	$this->getFixtureManager()->basePath = Yii::getPathOfAlias('application.tests.fixtures.user');

	parent::setUp();

    } 

    // tests below

}

Then I’ve created folder protected/tests/fixtures/user and copy specific fixtures files in it. I’m not sure it’s the best solution but it seems to work fine.

ciao

B)