I was reading documentation of PHPUnit. I'm stuck on Example 2.7 & Example 2.8. It's showing two errors.
- PHP Warning: fopen(data.csv): failed to open stream: No such file or directory
- The data provider specified for DataTest::testAdd is invalid. TypeError: fclose() expects parameter 1 to be resource, bool given
Below code of CsvFileIterator class
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
final class CsvFileIterator implements Iterator
{
private $file;
private $key = 0;
private $current;
public function __construct(string $file)
{
$this->file = fopen($file, 'r');
}
public function __destruct()
{
fclose($this->file);
}
public function rewind(): void
{
rewind($this->file);
$this->current = fgetcsv($this->file);
$this->key = 0;
}
public function valid(): bool
{
return !feof($this->file);
}
public function key(): int
{
return $this->key;
}
public function current(): array
{
return $this->current;
}
public function next(): void
{
$this->current = fgetcsv($this->file);
$this->key++;
}
}
Below code of DataTest class
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
require "CsvFileIterator.php";
final class DataTest extends TestCase
{
/**
* @dataProvider additionProvider
*/
public function testAdd(int $a, int $b, int $expected): void
{
$this->assertSame($expected, $a + $b);
}
public function additionProvider(): CsvFileIterator
{
return new CsvFileIterator('data.csv');
}
}
Below sample data.csv
0, 0, 0
0, 0, 0
0, 0, 0
Please help me to figure out, what I'm doing wrong. Thanks in advance.
Note: My local is windows 10.