Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a csv file in PHPUnit test

Tags:

phpunit

I wrote a DataTest case following the example 4.5 of PHPUnit manual, the url is:
http://www.phpunit.de/manual/3.6/en/writing-tests-for-phpunit.html#writing-tests-for-phpunit.data-providers.
But I came across with an error:

The data provider specified for DataTest::testAdd is invalid.
Data set #0 is invalid.

I thought it maybe that I edit the data.csv file in a wrong way, then I used php function fputcsv() to create data.csv file, but it also didn't work, I want to know why, and how to resolve this problem. Thanks!

P. S.: the data in data.csv is:

0,0,0
0,1,1


The codes are show as follows:
DataTest.php
require 'CsvFileIterator.php';
class DataTest extends PHPUnit_Framework_TestCase
{
public function provider()
    {
        return new CsvFileIterator('data.csv');
    }

    /**
    * @dataProvider provider
    */
    public function testAdd($a, $b, $c)
    {
         $this->assertEquals($c, $a + $b);
    }
}

CsvFileIterator.php

class CsvFileIterator implements Iterator
{
    protected $file;
    protected $key = 0;
protected $current;

public function __construct($file)
{
    $this->file = fopen($file, 'r');
}

public function __destruct()
{
    fclose($this->file);
}

public function rewind()
{
    rewind($this->file);
    $this->current = fgetcsv($this->file);
    $this->key = 0;
}

public function valid()
{
    return !feof($this->file);
}

public function key()
{
    return $this->key;
}

public function current()
{
    return $this->current;
}

public function next()
{
    $this->current = fgetcsv($this->file);
    $this->key++;
}
}

The data.csv file is create by function fputcsv():

$data = array(
array(0, 0, 0),
array(0, 1, 1)
);

$fp = fopen('data.csv', 'w');

foreach($data as $v)
{
fputcsv($fp, $v);
}
fclose($fp);
like image 714
Zongshu Lin Avatar asked May 18 '12 02:05

Zongshu Lin


People also ask

How do I create a test csv file?

Create a test data CSV file: Open a spreadsheet or a text editor. In a CSV file, the first row of data must be the column definitions. Follow this format: ColumnName: TYPE , where type can be STRING , NUMBER , BOOLEAN , or ENUMERATION .

What is test csv file?

Files containing comma-separated values (CSV) are a commonly used for transferring simple text data between programs. The CSV format uses a comma as a field separator and a new line as a record separator. Double quotation marks are used to embed commas, new lines, or double quotation marks within strings.

What is PHPUnit used for?

PHPUnit is a programmer-oriented testing framework for PHP. It is an instance of the xUnit architecture for unit testing frameworks. PHPUnit 9 is the current stable version. PHPUnit 10 is currently in development.


2 Answers

Example :-)

/**
 * @dataProvider provider
 * @group csv
 */
public function testAdd($a, $b, $c)
{
    $this->assertEquals($c, $a + $b);
}

/**
 * @return array
 */
public function provider()
{
    $file = file_get_contents("/Volumes/htdocs/contacts.csv","r");
    foreach ( explode("\n", $file, -1) as $line )
    {
        $data[] = explode(',', $line);
    }
    return $data;
}

/*
 * CREATE TO CSV FILE DATAPROVIDER
 * don't create this file in your test case
 */
public function saveToCsv()
{
    $list = array(
        array(0,0,0),
        array(0,1,1)
    );

    $file = fopen("/Volumes/htdocs/contacts.csv","w");

    foreach ($list as $line)
    {
        fputcsv($file,$line);
    }

    fclose($file);
}
like image 172
RDPanek Avatar answered Sep 29 '22 15:09

RDPanek


@ZongshuLin Similar issue here. Possible Solutions:

  • Check this data.csv. Basically I had to add a row after the last line.
  • You can also check my approach on GitHub when specifying the data.csv location

Use DIRECTORY_SEPARATOR constant so the script may run on any OS--Windows uses backslashes while Linux slashes.

 /**
 * @return CsvFileIterator
 */
public function additionProvider()
{
    return new CsvFileIterator(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'storage/data.csv');
}
like image 26
rdok Avatar answered Sep 29 '22 17:09

rdok