【发布时间】:2011-08-22 14:38:53
【问题描述】:
我想知道如何在zend框架中测试模型,我已经在zend项目中测试了一个控制器。
我的 phpunit.xml 是:
<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>
bootstrap.php 与 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';
ControllerTestCase.php 是:
<?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();
}
}
我以ControllerTestCase为基础,为控制器编写了一个测试用例,它可以工作,但是我不知道如何为模型编写测试用例,模型的测试用例也应该扩展ControllerTestCase?或者它应该扩展 Zend_Test_PHPUnit_Db?既然模型将连接到数据库,那么我该如何测试呢?谁可以帮我这个事?例如我有一个模型:
<?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
);
}
}
如何测试?
【问题讨论】:
标签: php zend-framework testing phpunit