0

I was reading documentation of PHPUnit. I'm stuck on Example 2.7 & Example 2.8. It's showing two errors.

  1. PHP Warning: fopen(data.csv): failed to open stream: No such file or directory
  2. 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.

0Zef0
  • 77
  • 2
  • 11
  • Please see the first error message: _fopen(data.csv): failed to open stream: No such file or directory_ perhaps using an absolute path helps, e.g. `__DIR__ . '/data.csv'` - depending on where data.csv and DataTest.php are stored this may vary. – hakre Jul 12 '21 at 22:06
  • It's on same folder. – 0Zef0 Jul 12 '21 at 22:11
  • Where I should be adding this__DIR__ ? On CsvFileIterator('data.csv')? – 0Zef0 Jul 12 '21 at 22:21
  • I did `return new CsvFileIterator(__DIR__. DIRECTORY_SEPARATOR .'data.csv');`. So error 1 & 2 goes away. But new error showing up, `TypeError: Argument 1 passed to DataTest::testAdd() must be of the type int, string given`. At best my knowledge, `data.csv` is correct. – 0Zef0 Jul 13 '21 at 01:28
  • Well, it's not. I'm just trying to follow along PHPUnit documentation. – 0Zef0 Jul 18 '21 at 22:35
  • Ah, I see. You still have the TypeError, right? The TypeError is not so clear to me, Phpunit docs show the test is running through, but it's fishy: `1) DataTest::testAdd with data set #3 ('1', '1', '3')` these look like strings, code example shows `int` typehints and declare_strict=1 skalar types are on. Which PHP version are you using? – hakre Jul 18 '21 at 23:32
  • Yeah, documentation saying it working. But in reality it's not working. I have tried different combination possible at my best knowledge. I'm using PHP 7.3 which required version for PHPUnit 9.5. I also try with PHP 7.4. I remove `declare_strict=1` for test, now it's showing `TypeError: Argument 1 passed to DataThreeTest::testAdd() must be of the type int, null given.` Data.csv [link](https://ibb.co/YQjKv0S) – 0Zef0 Jul 19 '21 at 00:20
  • Its probably really meant as an example. The errors show that the iterator work as data-provider. NULL is the last line in the CSV file (it comes as `[null]`) and the error makes sense as when you remove the strict types you still can't pass null onto int IIRC. string is fine and gets coerced to integer. Take it as an example and write your own iterator and/or make use of a generator in a data-provider method, it is also an iterator. Another idea: Change the CsvFileIterator so that it has integers in the array (and skips the terminating line `[null]`. – hakre Jul 19 '21 at 01:39

0 Answers0