Reputation: 63
I want to know how to test the models in zend framework, I already test a controller in zend project.
my phpunit.xml is:
<phpunit bootstrap="./bootstrap.php" colors="true">
<testsuite>
<directory>./</directory>
</testsuite>
<filter>
<whitelist>
<directory suffix="MyApp.php">../application/</directory>
<exclude>
<file>../application/Bootstrap.php</file>
<file>../application/controllers/ErrorController.php</file>
<directory suffix=".phtml">../application/</directory>
</exclude>
</whitelist>
</filter>
<logging>
<log type="coverage-html" target="./log/report" charset="UTF-8"
yui="true" highlight="true" lowUpperBound="50" highLowerBound="80"/>
<log type="testdox-html" target="./log/testdox.html" />
</logging>
</phpunit>
the bootstrap.php is under the same folder as phpunit.xml
<?php
error_reporting(E_ALL);
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'testing'));
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
realpath(APPLICATION_PATH . '/../tests'),
get_include_path(),
)));
require_once 'Zend/Application.php';
require_once './application/controllers/ControllerTestCase.php';
the ControllerTestCase.php is:
<?php
require_once 'Zend/Application.php';
require_once 'Zend/Test/PHPUnit/ControllerTestCase.php';
class ControllerTestCase
extends Zend_Test_PHPUnit_ControllerTestCase
{
protected $application;
public function setUp() {
$this->bootstrap = array($this,'appBootstrap');
parent::setUp();
}
public function appBootstrap() {
$this->application =
new Zend_Application(APPLICATION_ENV,
APPLICATION_PATH.'/configs/application.ini');
$this->application->bootstrap();
}
}
I used a ControllerTestCase as a base, and write a test case for the controller, and it works,but I do not know how to write test case for models, the test case for model should also extends ControllerTestCase? or it should extends Zend_Test_PHPUnit_Db? And since the model will connect to database, so how can I test it? can anyone help me on this? for example I have a model:
<?php class Application_Model_User2 extends Custom_Model_Base {
public function __construct() {
parent::__construct();
}
static function create(array $data) {
return parent::_create(
$_table,
get_class(),
$data,
self::$_columns,
true
);
}
}
how to test it?
Upvotes: 3
Views: 980
Reputation: 10583
They should extend PHPUnit_Framework_TestCase
and not ControllerTestCase
.
Your testing the Model as a unit of functionality and not a controller, so the Model is a standalone piece of code which should be able to operate separately from your application and it's controllers.
You are not testing the database specifically so you do not need to extend Zend_Test_PHPUnit_Db
.
Your PHPUnit set up should be sufficient to start your application, so that Zend and any autoloader is configured in order for your Model to be loaded. Your test classes should then just test elements of the Model's code and nothing else from your application.
EDIT
So consider you have the following function to test, within the class Application_Model_User2
:
static function find($name, $order=null, $limit=null, $offset=null)
{
return self::_selectAndBind(get_class(),
self::getDefaultAdapter()
->select()
->from($_table)
->where('name = ?', array($name))
->order($order)
->limit($limit, $offset)
);
}
Within your test class which extends PHPUnit_Framework_TestCase
, here is a how-to and the docs and Asset Functions, you might have something like this:
require_once 'PHPUnit/Framework.php';
class Application_Model_User2Test extends PHPUnit_Framework_TestCase
{
public function testFind()
{
// Get a result from the function we are testing
$result = Application_Model_User2::find("foo", "date", 10, 0);
// Test that there are 10 results
$this->assertEquals(10, count($result));
}
}
You could also use functions like assertGreaterThan()
to determine if the order was correct.
Note. This is only a simple example.
Upvotes: 1