在 PHPUnit (CIUnit) 中使用 YAML 文件作为数据提供者 [英] Using YAML Files as data provider in PHPUnit (CIUnit)

查看:44
本文介绍了在 PHPUnit (CIUnit) 中使用 YAML 文件作为数据提供者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am writing an application using the PHP CodeIgniter Framework. I am trying to test the application using CI_Unit, by extension PHPUnit. To test a model, I am trying to load a YAML data provider as defined in the PHPUnit documentation, I receive an error. If I fudge the data provider object, I get another error. If I provide it a vanilla PHP array, it runs as expected.

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

What am I doing wrong? What is the correct way to do this? Below are my results:

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

If I return the object PHPUnit_Extensions_Database_DataSet_YamlDataSet of the Yaml file below, I get:

数据集客户"无效.

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

If I loop around the object returned by PHPUnit_Extensions_Database_DataSet_YamlDataSet and return that: I get this error:

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

PHPUnit_Framework_Exception: Neither "models.php" nor "models.php" could be opened. in /Users/eric/pear/share/pear/PHPUnit/Util/Skeleton/Test.php on line 100

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

If I provide it a vanilla PHP array, the tests run just fine. The command I use to run the tests is:

phpunit 模型

以下是我的 YAML 文件的示例.

Below is an example of my YAML file.

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.

I am using PHP 5.3, PHPUnit 3.6.10, DBUnit 1.1.2, CodeIgniter 2.1.0, and CI_unit associated with CI 2.1.0.

附上我的models/Test.php文件:

Attached is my models/Test.php file:

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

推荐答案

数据提供者:

数据提供者方法必须是 public 并且返回一个数组实现 Iterator 接口并产生的数组或对象每个迭代步骤的数组.对于作为一部分的每个数组收集测试方法将被调用的内容数组作为它的参数.

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 源代码,您似乎想要这样的东西:

Based on your Test.php source code, it seems you want something like this:

    /**
     * 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 应该提供一个函数来将数据集表直接转换为数组数组,但快速浏览后我没有看到任何东西.

Seems PHPUnit should provide a function to turn a dataset table directly in to an array of arrays, but I didn't see anything after a quick glance.

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

The phpunit.xml file is irrelevant, and can be removed from your question, as far as I can tell.

您也不需要 PHPUnit 测试方法中的 try/catch 块 - PHPUnit 会为您解决这个问题.

You also don't need the try/catch block in the PHPUnit test method - PHPUnit will take care of that for you.

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

Note that your $company_id wasn't defined, so I just set it to 0. Your method arguments & YAML data above don't seem to match up fully above either, but that should be easy to fix.

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

By passing an array in to the test function, which gets passed immediately to the add_client method, your code is a bit more DRY as well.

这篇关于在 PHPUnit (CIUnit) 中使用 YAML 文件作为数据提供者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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