PHPUnitマニュアルの例4.5に従ってDataTestケースを作成しました。URLは次のとおりです
。http ://www.phpunit.de/manual/3.6/en/writing-tests-for-phpunit.html#writing-tests-for-phpunit .data-providers。
しかし、私はエラーに遭遇しました:
DataTest::testAddに指定されたデータプロバイダーが無効です。
データセット#0が無効です。
data.csvファイルを間違った方法で編集したのではないかと思い、php関数fputcsv()を使用してdata.csvファイルを作成しましたが、機能しませんでした。理由と解決方法を知りたいです。この問題。ありがとう!
PS:data.csvのデータは次のとおりです。
0,0,0
0,1,1
コードは次のように表示されます:
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++;
}
}
data.csvファイルは、関数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);