Skip to content

bug in PHPUnit Manual 9.5: Examples 2.7 and 2.8 #224

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ricfio opened this issue Mar 28, 2021 · 0 comments
Closed

bug in PHPUnit Manual 9.5: Examples 2.7 and 2.8 #224

ricfio opened this issue Mar 28, 2021 · 0 comments

Comments

@ricfio
Copy link
Contributor

ricfio commented Mar 28, 2021

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

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);
        }
ricfio added a commit to ricfio/phpunit-documentation-english that referenced this issue Mar 28, 2021
@ricfio ricfio closed this as completed Oct 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant