20.11.2014 Views

O Guia Definitivo do Yii 1.1

O Guia Definitivo do Yii 1.1

O Guia Definitivo do Yii 1.1

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

In this class, we specify the fixtures member variable to be an array that specifies which<br />

fixtures will be used by this test. The array represents a mapping from fixture names to<br />

model class names or fixture table names (e.g. from fixture name posts to model class<br />

Post). Note that when mapping to fixture table names, we should prefix the table name<br />

with a colon (e.g. :Post) to differentiate it from model class name. And when using model<br />

class names, the corresponding tables will be considered as fixture tables. As we<br />

described earlier, fixture tables will be reset to some known state each time when a test<br />

method is executed.<br />

Fixture names allow us to access the fixture data in test methods in a convenient way. The<br />

following code shows its typical usage:<br />

// return all rows in the 'Comment' fixture table<br />

$comments = $this->comments;<br />

// return the row whose alias is 'sample1' in the `Post` fixture table<br />

$post = $this->posts['sample1'];<br />

// return the AR instance representing the 'sample1' fixture data row<br />

$post = $this->posts('sample1');<br />

Note: If a fixture is declared using its table name (e.g. 'posts'=>':Post'), then the third<br />

usage in the above is not valid because we have no information about which model<br />

class the table is associated with.<br />

Next, we write the testApprove method to test the approve method in the Comment model<br />

class. The code is very straightforward: we first insert a comment that is pending status;<br />

we then verify this comment is in pending status by retrieving it from database; and finally<br />

we call the approve method and verify the status is changed as expected.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!