Access the fixture data in test methods

Hello,

I’m writing some unit tests with Yii 1.1 alpha and i don’t succeed to use the access feature to fixture data. Here is my code :

File test/fixtures/AccountStatus.php :




return array(

	'unknown'=>array(

                'id' => 1,

		'code' => 'unknown',

		'name' => 'Unknown',

	),

	'active'=>array(

                'id' => 2,

		'code' => 'active',

		'name' => 'Active',

	),

        'inactive'=>array(

                'id' => 3,

		'code' => 'inactive',

		'name' => 'Inactive',

	),

);



File test/unit/AccountTest.php :




class AccountTest extends CDbTestCase

{

	public $fixtures=array(

                'status' => 'AccountStatus',

		'accounts' => 'Account',

	);


	public function testCreate()

	{

            $account=new Account;

            $account->setAttributes(array(

                'login' => 'new login',

                'name' => 'new name',

                'password' => 'new password',

                'email' => 'new@email.test',

                'accountstatus_id' => 2,

            ),false);

            $this->assertTrue($account->save(false));

	}

}



With this code, my test pass as it should. But if i change one line in AccountTest.php :




    'accountstatus_id' => 1,

    

to



    'accountstatus_id' => $this->status['active']['id'],

    

the test fails. It seems that $this->status[‘active’][‘id’] doesn’t have any value. As far as i can understand, $this->status[‘active’][‘id’] should return 2. Am i doing something wrong ??

Thanks for your help.

Regards,

Stéphane.

Yeah, your understanding is correct and the code looks good to me. What did you get for $this->status[‘active’] ? Any error?

I put a var_dump() in file protected/test/unit/AccountTest.php :




private function _dataAccount() {

    return (array(

        'login' => 'new login',

        'name' => 'new name',

        'password' => 'new password',

        'email' => 'new@email.test',

        'accountstatus_id' => 2,

    ));

}


public function testCreate()

{

    $account=new Account;

    $account->setAttributes($this->_dataAccount());


    // DEBUG

    echo "\n** \n" ; var_dump($this->status); echo "** \n";


    $this->assertTrue($account->save());

}



And it shows that $this->status is null. So, this explains why i get this error. But i can’t see why this attribute is null (?). Is there a way to add some debug statements to help to find the problem ?

Thank you.

Regards,

Stéphane.

What is your table name? Note that the fixture file name should be the table name, not model class name.

It seems ok for me. My tables are named "Account" and "AccountStatus". My fixture files are named "Account.php" and "AccountStatus.php". I have also a "AccountStatus.init.php" file for the fixture init. My models are named "Account.php" and "AccountStatus.php".

Which framework file can i inspect to try to narrow where is the problem ?

Thank you.

Stéphane.

Follow up to myself :

The problem happens only for the "status" part of $fixtures but not for the "accounts" part. I mean, i have :




public $fixtures=array(

	'accounts' => 'Account',

        'status' => 'AccountStatus',

);



and i can use :




$this->accounts['admin']['name']



I got the right value. But, as i said, i can’t use :




$this->status['active']['id']



I got no value at all.

Is this a known restriction ?

Thank you.

Regards,

Stéphane.

status is a protected member inherited from TestCase of PHPUnit.

As a workaround, you may access this data via $this->getFixtureManager()->getRows(‘status’).

I also checked in a shortcut which allows you to do: $this->getFixtureData(‘status’)

Ah, ok ! Indeed, it works now (i tried the workaround). Thank you very very much !!

Regards,

Stéphane.