Yii Framework Forum: Access the fixture data in test methods - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Access the fixture data in test methods Rate Topic: -----

#1 User is offline   Stéphane 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 15-October 09

Posted 15 October 2009 - 08:21 AM

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

#2 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,907
  • Joined: 04-October 08
  • Location:DC, USA

Posted 15 October 2009 - 09:41 AM

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

#3 User is offline   Stéphane 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 15-October 09

Posted 16 October 2009 - 03:07 AM

View Postqiang, on 15 October 2009 - 09:41 AM, said:

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

#4 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,907
  • Joined: 04-October 08
  • Location:DC, USA

Posted 16 October 2009 - 12:11 PM

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

#5 User is offline   Stéphane 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 15-October 09

Posted 19 October 2009 - 03:00 AM

View Postqiang, on 16 October 2009 - 12:11 PM, said:

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

#6 User is offline   Stéphane 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 15-October 09

Posted 29 October 2009 - 08:54 AM

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

#7 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,907
  • Joined: 04-October 08
  • Location:DC, USA

Posted 29 October 2009 - 03:51 PM

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')
0

#8 User is offline   Stéphane 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 15-October 09

Posted 30 October 2009 - 05:43 AM

View Postqiang, on 29 October 2009 - 03:51 PM, said:

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

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users