(PHP):使用 Zend_Test_PHPUnit_DatabaseTestCase 测试模型

发布于 2024-10-11 03:20:54 字数 2347 浏览 5 评论 0原文

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

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() 调用引起的,但我不知道为什么。我完全被困住了。任何帮助将不胜感激。

// /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);
    }
}

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

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);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最单纯的乌龟 2024-10-18 03:20:54

不确定它是否对您有帮助,但问题可能在于:
$this->bootstrap = array($this, 'appBootstrap');

您会看到 Zend_Test_PHPUnit_DatabaseTestCase 中不存在 $bootstrap 属性,这与 Zend_Test_PHPUnit_ControllerTestCase 不同。所以我认为你的引导方法没有被调用。

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

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

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.

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

梦行七里 2024-10-18 03:20:54

我今天也遇到了同样的问题。这里的原因是,XML 夹具是由 MySQLDump 生成的,并且缺少 节点。这将 PHPUnit 中的 $this->tables 变为 NULL 而不是数组。

I had the same problem today. The reason here was, that the XML fixture was generated by MySQLDump and missing the <database name="xyz"> node. This turned $this->tables in PHPUnit into NULL instead Array.

玩物 2024-10-18 03:20:54

完整的解决方案和代码位于 http:// www.unexpectedit.com/zend-php/testing-database-model-with-phpunit-on-zend-studio

编辑 application/configs/application.ini

[testing : production]
resources.db.adapter = "pdo_mysql"
resources.db.params.host = 127.0.0.1
resources.db.params.port = 8889
resources.db.params.username = root
resources.db.params.password = root
resources.db.params.dbname = "test_myproject_com"
xmlseeds.folder = APPLICATION_PATH "/../tests/xmlseeds/"

创建文件夹并文件MyProject/library/Application/Test/PHPUnit/DatabaseTestCase/Abstract.php

创建文件夹和文件MyProject/tests/application/models/ProjectTest.php

创建文件/tests/ xmlseeds/*.xml

I. Pascual www.unexpectedit.com

Full solution and code at http://www.unexpectedit.com/zend-php/testing-database-model-with-phpunit-on-zend-studio

Edit application/configs/application.ini

[testing : production]
resources.db.adapter = "pdo_mysql"
resources.db.params.host = 127.0.0.1
resources.db.params.port = 8889
resources.db.params.username = root
resources.db.params.password = root
resources.db.params.dbname = "test_myproject_com"
xmlseeds.folder = APPLICATION_PATH "/../tests/xmlseeds/"

Create folders and file MyProject/library/Application/Test/PHPUnit/DatabaseTestCase/Abstract.php

Create folders and file MyProject/tests/application/models/ProjectTest.php

Create files /tests/xmlseeds/*.xml

I. Pascual www.unexpectedit.com

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文