(PHP):使用Zend_Test_PHPUnit_DatabaseTestCase测试模型 [英] (PHP): Testing models with Zend_Test_PHPUnit_DatabaseTestCase

查看:146
本文介绍了(PHP):使用Zend_Test_PHPUnit_DatabaseTestCase测试模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行PHP单元测试时,我得到:

When I run my PHP unit test I get:

1) Test_Model_Mapper_TestTest::testTest
Argument 1 passed to PHPUnit_Extensions_Database_DataSet_DefaultTableIterator::__construct() must be an array, null given, called in /usr/share/php/PHPUnit/Extensions/Database/DataSet/AbstractXmlDataSet.php on line 134 and defined

/var/www/kosheroven/library/Zend/Test/PHPUnit/Db/Operation/Truncate.php:73
/var/www/kosheroven/tests/ModelTestCase.php:79
/var/www/kosheroven/tests/application/models/mappers/TestTest.php:33

显然,预期结果是要通过测试.通过散布一些回声,我发现这是由parent :: setUp()调用引起的,但我不知道为什么.我完全被困住了.任何帮助将不胜感激.

Expected result is for the test to pass, obviously. By sprinkling around a few echoes I discovered that this is caused from within the parent::setUp() call, but I have no idea why. I am completely stuck. Any help would be greatly appreciated.

// /tests/ModelTestCase.php

abstract class Test_ModelTestCase extends Zend_Test_PHPUnit_DatabaseTestCase
{
    public $application;
    protected $_db;
    protected $_model;
    protected $_modelClass;
    protected $_filesDir;

    public function setUp()
    {
        $this->application = new Zend_Application(
            APPLICATION_ENV,
            APPLICATION_PATH . '/configs/application.ini'
        );

        $this->bootstrap = array($this, 'appBootstrap');

        $this->_filesDir  = dirname(__FILE__) . '/files/';
        $this->_filesDir .= str_replace('_', '/', get_class($this));
        $this->_model     = new $this->_modelClass();
        // echo '123'; is printed
        parent::setUp();
        // echo '456'; is not
    }

    public function appBootstrap()
    {
        $this->application->bootstrap();
    }

    protected function getConnection()
    {
        if(empty($this->_db))
        {
            $options = $this->application->getOptions();
            $schema = $options['resources']['db']['params']['dbname'];
            $db = $this->application->getBootstrap()->getPluginResource('db')
                    ->getDbAdapter();

            $this->_db = $this->createZendDbConnection($db, $schema);
        }

        return $this->_db;
    }

    protected function getDataSet()
    {
        return $this->createXmlDataSet(dirname(__FILE__) . '/files/seed.xml');
    }
}

// /tests/Model/Mapper/TestTest.php

class Test_Model_Mapper_TestTest extends Test_ModelTestCase
{
    protected $_modelClass = 'Application_Model_Mapper_Ingredients';

    public function testTest()
    {
        $this->assertTrue(true);
    }
}

推荐答案

不确定是否会帮助您,但问题可能出在以下方面: $this->bootstrap = array($this, 'appBootstrap');

Not sure if it will help you, but the problem may be with: $this->bootstrap = array($this, 'appBootstrap');

您看到Zend_Test_PHPUnit_DatabaseTestCase中不存在$ bootstrap属性,这与Zend_Test_PHPUnit_ControllerTestCase不同.因此,我认为您的引导方法没有被调用.

You see $bootstrap property does not exist in Zend_Test_PHPUnit_DatabaseTestCase, unlike for Zend_Test_PHPUnit_ControllerTestCase. So I think that your bootstrap method is not called.

因此,您可以尝试将$this->bootstrap = array($this, 'appBootstrap');行替换为$this->appBootstrap();.

Thus you could try to replace line $this->bootstrap = array($this, 'appBootstrap'); with $this->appBootstrap();.

这篇关于(PHP):使用Zend_Test_PHPUnit_DatabaseTestCase测试模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆