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

Eri*_*ope 4 php phpunit yaml codeigniter

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

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

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

数据集"客户端"无效.

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

PHPUnit_Framework_Exception:既不能打开"models.php"也不能打开"models.php".在/Users/eric/pear/share/pear/PHPUnit/Util/Skeleton/Test.php第100行

如果我提供一个vanilla 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
Run Code Online (Sandbox Code Playgroud)

我使用的是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,1,'test','text 2'),
                array(1,2,1,'test 3', 'test 3'));
    return $data;
}
Run Code Online (Sandbox Code Playgroud)

mko*_*ala 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;
    }
Run Code Online (Sandbox Code Playgroud)

似乎PHPUnit应该提供一个函数来将数据集表直接转换为数组数组,但是在快速浏览后我没有看到任何内容.

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

您也不需要try/catchPHPUnit测试方法中的块--PHPUnit会为您处理这个问题.

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

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