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.

$yiit='path/to/yii/framework/yiit.php';<br />

$config=dirname(__FILE__).'/../config/test.php';<br />

require_once($yiit);<br />

require_once(dirname(__FILE__).'/WebTestCase.php');<br />

<strong>Yii</strong>::createWebApplication($config);<br />

In the above, we first include the yiit.php file from the <strong>Yii</strong> framework, which initializes some<br />

global constants and includes necessary test base classes. We then create a Web<br />

application instance using the test.phpconfiguration file. If we check test.php, we shall find<br />

that it inherits from the main.php configuration file and adds a fixture application<br />

component whose class is CDbFixtureManager. We will describe fixtures in detail in the<br />

next section.<br />

return CMap::mergeArray(<br />

require(dirname(__FILE__).'/main.php'),<br />

array(<br />

'components'=>array(<br />

'fixture'=>array(<br />

'class'=>'system.test.CDbFixtureManager',<br />

),<br />

/* uncomment the following to provide test database connection<br />

'db'=>array(<br />

'connectionString'=>'DSN for test database',<br />

),<br />

*/<br />

),<br />

)<br />

);<br />

When we run tests that involve database, we should provide a test database so that the<br />

test execution <strong>do</strong>es not interfere with normal development or production activities. To <strong>do</strong><br />

so, we just need to uncomment the dbconfiguration in the above and fill in the<br />

connectionString property with the DSN (data source name) to the test database.<br />

With such a bootstrap script, when we run unit tests, we will have an application instance<br />

that is nearly the same as the one that serves for Web requests. The main difference is<br />

that it has the fixture manager and is using the test database.<br />

Definin<strong>do</strong> Fixtures<br />

Automated tests need to be executed many times. To ensure the testing process is<br />

repeatable, we would like to run the tests in some known state called fixture. For example,<br />

to test the post creation feature in a blog application, each time when we run the tests, the<br />

tables storing relevant data about posts (e.g. the Post table, the Comment table) should be

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

Saved successfully!

Ooh no, something went wrong!