4

我正在使用 PHP CodeIgniter 框架编写应用程序。我正在尝试通过扩展 PHPUnit 使用 CI_Unit 测试应用程序。为了测试模型,我尝试加载 PHPUnit 文档中定义的 YAML 数据提供程序,但收到错误消息。如果我捏造数据提供者对象,我会得到另一个错误。如果我为它提供一个普通的 PHP 数组,它会按预期运行。

我究竟做错了什么?这样做的正确方法是什么?以下是我的结果:

如果我返回PHPUnit_Extensions_Database_DataSet_YamlDataSet下面的 Yaml 文件的对象,我会得到:

数据集“客户”无效。

如果我循环返回的对象PHPUnit_Extensions_Database_DataSet_YamlDataSet并返回:我收到此错误:

PHPUnit_Framework_Exception:“models.php”和“models.php”都无法打开。在第 100 行的 /Users/eric/pear/share/pear/PHPUnit/Util/Skeleton/Test.php

如果我为它提供一个普通的 PHP 数组,测试运行得很好。我用来运行测试的命令是:

phpunit 模型

下面是我的 YAML 文件的示例。

Clients:
    1:
        client_id: 1
        client_information: "info number 1"
        client_key: 48fb10b15f3d44a09dc82d
    2:
        client_id: 2
        client_information: "info number 2"
        client_key: 48fb10b15f3d44addd

我正在使用 PHP 5.3、PHPUnit 3.6.10、DBUnit 1.1.2、CodeIgniter 2.1.0 和与 CI 2.1.0 关联的 CI_unit。

编辑:附件是我的模型/Test.php 文件:

/**
 * test_add_client
 * @dataProvider add_client_provider
 */
public function test_add_client($client_id,$company_id,$software_id,$client_information,$client_key)
{
    $data = array('software_id' => $software_id,
                  'client_information' => $client_information,
                  'client_key'         => $client_key);
    try {
        $id = $this->_m->add_client($company_id,$data);
        $this->assertEquals(true, is_int($id));
    } catch (Exception $e){
        $this->assertEquals(true,false);
    }
}

public function add_client_provider()
{
    $result = new PHPUnit_Extensions_Database_DataSet_YamlDataSet(
        dirname(__FILE__)."/../fixtures/Clients.yml");

    // Case #1 returns this $result
    //return $result;

    foreach($result as $key => $value){
        if($key == 'Clients'){
            $substructure = $value;
        }
    }

    // Case #2 return the inner structure that is the table
    return $substructure;

    // Case #3 return an array of arrays
    $data = array(
                array(1,1,1,'test','text 2'),
                array(1,2,1,'test 3', 'test 3'));
    return $data;
}
4

2 回答 2

5

数据提供者的 PHPUnit 文档中所述:

数据提供者方法必须public返回一个数组数组或一个实现Iterator接口并为每个迭代步骤生成一个数组的对象。对于集合中的每个数组,将调用测试方法,并将数组的内容作为其参数。

根据您的Test.php源代码,您似乎想要这样的东西:

    /**
     * test_add_client
     * @dataProvider add_client_provider
     */
    public function test_add_client($data)
    {
        $company_id = 0;
        $id = $this->_m->add_client($company_id, $data);
        $this->assertEquals(true, is_int($id));
    }

    public function add_client_provider()
    {
        $result = new PHPUnit_Extensions_Database_DataSet_YamlDataSet(
            dirname(__FILE__)."/../fixtures/Clients.yml");          

        // Return the Clients data
        $clients = array();
        $tbl = $result->getTable('Clients');
        for ($i = 0; $i < $tbl->getRowCount(); $i++) {
            $clients[] = $tbl->getRow($i);
        }
        return $clients;
    }

似乎 PHPUnit 应该提供一个函数来将数据集表直接转换为数组数组,但快速浏览后我什么也没看到。

据我所知,该phpunit.xml文件无关紧要,可以从您的问题中删除。

您也不需要try/catchPHPUnit 测试方法中的块 - PHPUnit 会为您处理这些。

请注意,您$company_id的未定义,因此我将其设置为 0。您上面的方法参数和 YAML 数据似乎也不完全匹配,但这应该很容易解决。

通过将数组传递给测试函数,该函数会立即传递给add_client方法,您的代码也更加干燥。

于 2012-05-18T21:03:54.860 回答
0

PHPUnit 提供程序自动加载器

在 PHPUnit 中自动加载 CSV、JSON、PHP、XML 和 YAML 数据提供程序的魔术助手。

安装

composer require redaxmedia/phpunit-provider-autoloader

用法

为您的测试套件创建 TestCaseAbstract:

<?php
namespace ExampleProject\Tests;

use PHPUnitProviderAutoloader;

/**
 * TestCaseAbstract
 *
 * @since 2.0.0
 *
 * @package ExampleProject
 * @category Tests
 */

abstract class TestCaseAbstract extends PHPUnitProviderAutoloader\TestCaseAbstract
{
    /**
     * directory of the provider
     *
     * @var string
     */

    protected $_providerDirectory = 'tests' . DIRECTORY_SEPARATOR . 'provider';

    /**
     * namespace of the testing suite
     *
     * @var string
     */

    protected $_testNamespace = __NAMESPACE__;
}

从 TestCaseAbstract 扩展以自动加载 ExampleTest{_testMethod}.{csv|json|php|xml|yml} 文件:

<?php
namespace ExampleProject\Tests;

/**
 * ExampleTest
 *
 * @since 2.0.0
 *
 * @package ExampleProject
 * @category Tests
 */

class ExampleTest extends TestCaseAbstract
{
    /**
     * testMethod
     *
     * @since 2.0.0
     *
     * @param string $expect
     *
     * @dataProvider providerAutoloader
     */

    public function testMethod(string $expect = null)
    {
        $this->assertEquals($expect, 'test');
    }
}

阅读更多

相关仓库:https ://github.com/redaxmedia/phpunit-provider-autoloader

示例集成:YAML 测试自动加载YAML 类提供者YAML 方法提供者

于 2019-01-12T15:11:06.340 回答