【问题标题】:how to test model in zend project如何在zend项目中测试模型
【发布时间】: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


    【解决方案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 的测试类中,这里有一个how-todocsAsset 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() 等函数来确定订单是否正确。

    注意。这只是一个简单的例子。

    【讨论】:

    • 你好,Jake,谢谢你的回答,但是模型是连接数据库的,它会对数据库做一些操作,PHPUnit_Framework_TestCase就够了吗?
    • 是的ratzip。 PHPUnit_Framework_TestCase 是基本的测试用例,让您可以访问 PHPUnit 的测试功能,您在这里没有专门测试数据库,而是测试模型,所以基本的测试功能就可以了。要正确清除这一点,最好知道您要测试什么?
    • 项目包含几个模型,每个模型扩展一个基础模型,这些模型包含插入、删除、更新和查询数据库条目的方法,我想测试这些方法
    • 例如,我想测试它的查询功能是否有效,所以我想测试它是否可以在数据库中找到某个用户,并检查是否符合预期值。杰克,你能举个例子吗?现在我对此感到困惑
    • 例如:我要测试以下函数: 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, $偏移量)); } 我该如何测试它?
    猜你喜欢
    • 2019-02-17
    • 1970-01-01
    • 1970-01-01
    • 2021-11-21
    • 1970-01-01
    • 2011-03-26
    • 1970-01-01
    • 1970-01-01
    • 2011-06-20
    相关资源
    最近更新 更多