Using YAML Files as a Data Provider in PHPUnit (CIUnit)

I am writing an application using the PHP CodeIgniter Framework. I am trying to test an application using CI_Unit, with the extension PHPUnit. To test the model, I try to load the YAML data provider, as defined in the PHPUnit documentation, I get an error. If I mess up the data provider object, I get another error. If I provided him with a vanilla PHP array, it would work as expected.

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

If I return PHPUnit_Extensions_Database_DataSet_YamlDataSetthe Yaml file object below, I get:

Clients dataset is invalid.

If I loop the object returned PHPUnit_Extensions_Database_DataSet_YamlDataSetand return it: I get this error:

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

If I provided him with a vanilla PHP array, the tests would work fine. The command I use to run the tests is:

phpunit models

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

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

Edit: Attached 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;
}
+4
source share
2 answers

As described in the PHPUnit documentation for Data Providers :

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/catch PHPUnit - PHPUnit .

, $company_id , 0. YAML, , , , .

, add_client, DRY.

+5

PHPUnit

CSV, JSON, PHP, XML YAML PHPUnit.

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

0

All Articles