The way I have been testing my models is through using PHPUnit. I just wrote a tutorial for installing and configuring the Zend Framework and I'll be putting together another lesson about testing in the future.
After installing PHPUnit, I create a 'tests' folder on the same level as my application and library folders - outside the root directory of the web site. Then I create a bootstrap file just for my tests that looks something like this.
Code:
<?php
//File name: TestConfiguration.php
TestConfiguration::setUp();
class TestConfiguration {
public static function setUp() {
require_once 'PHPUnit/Framework.php';
require_once 'PHPUnit/Framework/TestSuite.php';
require_once 'PHPUnit/TextUI/TestRunner.php';
error_reporting( E_ALL | E_STRICT );
$appRoot = realpath(dirname(basename(__FILE__)).'/..');
set_include_path($appRoot . '/application/models/'
. PATH_SEPARATOR . $appRoot . '/library/'
. PATH_SEPARATOR . $appRoot . '/application/config/'
. PATH_SEPARATOR . get_include_path());
require_once 'Zend/Loader.php';
Zend_Loader::registerAutoload();
// Set up the config in the registry path relative to public/index.php
$cfg = new Zend_Config_Ini('config.ini', 'test');
Zend_Registry::set('config', $cfg);
// Set up the database in the Registry
$db = Zend_Db::factory($cfg->db);
Zend_Db_Table_Abstract::setDefaultAdapter($db);
// Set timezone
date_default_timezone_set("America/New_York");
}
}
?>
Than I write tests and put them in my tests/models folder. A test file looks something like this.
Code:
<?php
// File name: Models_AddressTest.php
require_once dirname(__FILE__) . '/../TestConfiguration.php';
class Models_AddressTest extends PHPUnit_Framework_TestCase {
public function testValidObject() {
/* some code to test stuff */
$this->assertEquals(0, count($errors));
}
}
?>
Finally, to run the test I will open up a terminal, cd to my tests directory then type: phpunit Models_AddressTest