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

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

问题描述

我使用PHP CodeIgniter框架编写一个应用程序。我试图使用CI_Unit,通过扩展PHPUnit测试应用程序。要测试模型,我试图加载一个YAML数据提供程序,如PHPUnit文档中所定义,我收到一个错误。如果我fugge数据提供程序对象,我得到另一个错误。如果我提供一个香草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 p>

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


资料集「客户」无效。

Data set "Clients" is invalid.

如果我循环由 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。在第100行的/Users/eric/pear/share/pear/PHPUnit/Util/Skeleton/Test.php中

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模型

phpunit models

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

编辑:
附加的是我的模型/ 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 文件是不相关的

您也不需要 try / catch

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 方法,您的代码也会更加干燥。

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天全站免登陆