phpunit-documentation-english
phpunit-documentation-english copied to clipboard
bug in PHPUnit Manual 9.5: Examples 2.7 and 2.8
Problem
| Q | A |
|---|---|
| PHPUnit version | 9.5.4 |
| PHP version | 8.0.1 |
| Installation Method | Composer |
TypeError: DataTest::testAdd(): Argument #1 ($a) must be of type int, string given, called in /workspaces/phpunit-tutorial/www/vendor/phpunit/phpunit/src/Framework/TestCase.php on line 1526
Fix
https://github.com/sebastianbergmann/phpunit-documentation-english/compare/master...ricfio:patch-1
How to reproduce
Example 2.7 Using a data provider that returns an Iterator object
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
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');
}
}
Example 2.8 The 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++;
}
}
data.csv
0,0,0
0,1,1
1,0,1
1,1,3
# phpunit tests/DataTest.php
PHPUnit 9.5.4 by Sebastian Bergmann and contributors.
EEEE 4 / 4 (100%)
Time: 00:00.215, Memory: 4.00 MB
There were 4 errors:
1) DataTest::testAdd with data set #0 ('0', '0', '0')
TypeError: DataTest::testAdd(): Argument #1 ($a) must be of type int, string given, called in /workspaces/phpunit-tutorial/www/vendor/phpunit/phpunit/src/Framework/TestCase.php on line 1526
/workspaces/phpunit-tutorial/www/tests/DataTest.php:10
2) DataTest::testAdd with data set #1 ('0', '1', '1')
TypeError: DataTest::testAdd(): Argument #1 ($a) must be of type int, string given, called in /workspaces/phpunit-tutorial/www/vendor/phpunit/phpunit/src/Framework/TestCase.php on line 1526
/workspaces/phpunit-tutorial/www/tests/DataTest.php:10
3) DataTest::testAdd with data set #2 ('1', '0', '1')
TypeError: DataTest::testAdd(): Argument #1 ($a) must be of type int, string given, called in /workspaces/phpunit-tutorial/www/vendor/phpunit/phpunit/src/Framework/TestCase.php on line 1526
/workspaces/phpunit-tutorial/www/tests/DataTest.php:10
4) DataTest::testAdd with data set #3 ('1', '1', '3')
TypeError: DataTest::testAdd(): Argument #1 ($a) must be of type int, string given, called in /workspaces/phpunit-tutorial/www/vendor/phpunit/phpunit/src/Framework/TestCase.php on line 1526
/workspaces/phpunit-tutorial/www/tests/DataTest.php:10
ERRORS!
Tests: 4, Assertions: 0, Errors: 4.
How to solve
In src/CsvFileIterator change this
$this->current = fgetcsv($this->file);
with this
$this->current = fgetcsv($this->file);
if (is_array($row)) {
$row = array_map('intval', $row);
}