我希望了解如何在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
);
}
}
如何进行IT测试?
PHPUnit_Framework_TestCase
是基本测试用例,并为您提供访问 PHPUnit 测试函数的权限。在这里,您不是专门测试数据库,而是测试模型,因此基本测试函数就足够了。为了清楚地解决这个问题,最好知道您想要测试什么? - Jake N