微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用YAML文件作为PHPUnit(CIUnit)中的数据提供程序

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

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

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

Data set “Clients” is invalid.

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

PHPUnit_Framework_Exception: Neither “models.PHPnor “models.PHPCould be opened. in /Users/eric/pear/share/pear/PHPUnit/Util/Skeleton/Test.PHP on line 100

如果我提供一个vanilla PHP数组,那么测试运行得很好.我用来运行测试的命令是:

PHPunit models

下面是我的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.

编辑:
附件是我的models / 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,'test','text 2'),array(1,2,'test 3','test 3'));
    return $data;
}

解决方法

Data Providers上的PHPUnit文档中所述:

A data provider method must be public and either return an array of
arrays or an object that implements the Iterator interface and yields
an array for each iteration step. For each array that is part of the
collection the test method will be called with the contents of the
array as its arguments.

根据您的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,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文件是无关紧要的,据我所知,可以从你的问题中删除.

您还不需要PHPUnit测试方法中的try / catch块–PHPUnit将为您处理这个问题.

请注意,您的$company_id未定义,因此我将其设置为0.您的方法参数&上面的YAML数据似乎也没有完全匹配,但这应该很容易修复.

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐