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.
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.