Php 如何在zend项目中测试模型
我想知道如何在zend框架中测试模型,我已经在zend项目中测试了一个控制器 我的phpunit.xml是:Php 如何在zend项目中测试模型,php,zend-framework,testing,phpunit,Php,Zend Framework,Testing,Phpunit,我想知道如何在zend框架中测试模型,我已经在zend项目中测试了一个控制器 我的phpunit.xml是: <phpunit bootstrap="./bootstrap.php" colors="true"> <testsuite> <directory>./</directory> </testsuite> <filter> &l
<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>
./
../application/
../application/Bootstrap.php
../application/controllers/ErrorController.php
../application/
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';
他们应该扩展PHPUnit\u框架\u测试用例,而不是ControllerTestCase
您将模型作为功能单元而不是控制器进行测试,因此模型是一段独立的代码,应该能够与应用程序及其控制器分开运行
您没有专门测试数据库,因此不需要扩展Zend\u Test\u PHPUnit\u Db
PHPUnit设置应足以启动应用程序,以便配置Zend和任何自动加载程序,以便加载模型。然后,测试类应该只测试模型代码的元素,而不测试应用程序中的其他元素
编辑
>请考虑下面的函数来测试,在类<代码> AppultOpjyToMule2E/2>代码:
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)
);
}
在扩展了PHPUnit\u Framework\u TestCase
的测试类中,有一个
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));
}
}
您还可以使用诸如assertGreaterThan()
之类的函数来确定顺序是否正确
注意。这只是一个简单的例子。嗨,杰克,谢谢你的回答,但是模型连接到数据库,它会对数据库进行一些操作,PHPUnit_框架测试用例就足够了?是的PHPUnit\u Framework\u TestCase
是基本的测试用例,它允许您访问PHPUnit的测试功能,您不需要在这里专门测试数据库,您需要测试模型,这样基本的测试功能就可以了。为了正确地解决这个问题,最好知道您想要测试的是什么?项目包含几个模型,每个模型扩展一个基本模型,这些模型包含将在数据库中插入、删除、更新和查询项的方法,我想测试这些方法例如,我想测试它的查询功能是否工作,所以我想测试它是否能在数据库中找到某个用户,并检查是否符合预期值。杰克,你能举个例子吗?现在我对它感到困惑,例如:我想测试以下函数:静态函数find($name,$order=null,$limit=null,$offset=null){返回self::_selectAndBind(get_class(),self::getDefaultAdapter()->select()->from($_table)->where('name=?',array($name))->order($order)->limit($limit,$offset));}如何测试它?
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)
);
}
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));
}
}