如何在Zend项目中测试模型

3

我希望了解如何在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测试?
1个回答

1

他们应该扩展PHPUnit_Framework_TestCase而不是ControllerTestCase

您正在测试模型作为功能单元,而不是控制器,因此模型是独立的代码片段,应该能够与应用程序及其控制器分开运行。

您不需要专门测试数据库,因此不需要扩展Zend_Test_PHPUnit_Db

您的PHPUnit设置应足以启动应用程序,以便配置Zend和任何自动加载程序以便加载模型。然后,您的测试类应仅测试模型代码的各个元素,而不涉及应用程序中的其他内容。

编辑

因此,考虑您要在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)
                               ); 
}

在扩展PHPUnit_Framework_TestCase的测试类中,这里有一个如何文档以及Asset Functions,你可能会有类似这样的代码:

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_Framework_TestCase 足够吗? - user707549
是的,ratzip。PHPUnit_Framework_TestCase 是基本测试用例,并为您提供访问 PHPUnit 测试函数的权限。在这里,您不是专门测试数据库,而是测试模型,因此基本测试函数就足够了。为了清楚地解决这个问题,最好知道您想要测试什么? - Jake N
该项目包含多个模型,每个模型都扩展了一个基础模型。这些模型包含方法,可以在数据库中插入、删除、更新和查询条目。我想测试这些方法。 - user707549
例如,我想测试它的查询功能是否正常工作,因此我想测试它是否能在数据库中找到某些特定用户,并与预期值进行比较。 Jake,你能提供一些例子吗?现在我对此感到困惑。 - user707549
我想测试以下函数: 静态函数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) ); } 我该如何测试它? - user707549

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接