PHPUnit: dataProvider の使い方

こちらと同様のことを行いました。
[PHPUnit入門①](https://note.com/88gram/n/ncbbd02618f5c)

<ol>

<li>プロジェクトの作成</li>

```bash
$ mkdir test01
$ mkdir test01/src
$ mkdir test01/tests
$ cd test01
$ touch phpunit.xml
$ touch src/Calculator.php
$ touch tests/CalculatorTest.php
$ composer require --dev phpunit/phpunit
```

フォルダー構造

```text
$ tree -L 2
.
├── composer.json
├── composer.lock
├── phpunit.xml
├── src
│   └── Calculator.php
├── tests
│   └── CalculatorTest.php
└── vendor
    ├── autoload.php
    ├── bin
    ├── composer
    ├── myclabs
    ├── nikic
    ├── phar-io
    ├── phpunit
    ├── sebastian
    └── theseer
```

```xml:phpunit.xml
<phpunit colors="true"></phpunit>
```

<li>ソースコードの作成</li>

```src/Calculator.php
<?php

namespace Src;

class Calculator
{
    /**
     * 足し算
     *
     * @param integer $number1
     * @param integer $number2
     * @return integer
     */
    public function add(int $number1, int $number2): int
    {
        return $number1 + $number2;
    }

    /**
     * 引き算
     *
     * @param integer $number1
     * @param integer $number2
     * @return integer
     */
    public function sub(int $number1, int $number2): int
    {
        return $number1 - $number2;
    }

    /**
     * 掛け算
     *
     * @param integer $number1
     * @param integer $number2
     * @return integer
     */
    public function multi(int $number1, int $number2): int
    {
        return $number1 * $number2;
    }

    /**
     * 割り算
     *
     * @param integer $number1
     * @param integer $number2
     * @return integer
     */
    public function div(int $number1, int $number2): int
    {
        return $number1 / $number2;
    }
}
```

<li>テストコードの作成</li>

```php:tests/CalculatorTest.php
<?php 

use PHPUnit\Framework\TestCase;
use Src\Calculator;

require_once __DIR__ . '/../src/Calculator.php';

// --------------------------------------------------------------------
class CalculatorTest extends TestCase
{

// --------------------------------------------------------------------
public static function add_DataProvider(): array
{
        return [
            [3, 4, 7],
            [1, 1, 2],
            [3, -1, 2],
            [4, 0, 4],
            [5, 9, 14],
        ];
}

/**
	@dataProvider add_DataProvider
*/

public function test_add_proc
	(int $number1, int $number2, int $expected): void
{
	error_log("*** test_add_proc() *** $number1 , $number2");
	$calculator = new Calculator();
        $ret = $calculator->add($number1, $number2);
        $this->assertSame($expected, $ret);
}

// --------------------------------------------------------------------
public static function sub_DataProvider(): array
    {
        return [
            [4, 3, 1],
            [1, 1, 0],
            [3, -1, 4],
            [4, 0, 4],
            [5, 9, -4],
        ];
    }

/**
	@dataProvider sub_DataProvider
*/

public function test_sub_proc
	    (int $number1, int $number2, int $expected): void
    {
	error_log("*** test_sub_proc() *** $number1 , $number2");
	$calculator = new Calculator();
        $ret = $calculator->sub($number1, $number2);
        $this->assertSame($expected, $ret);
    }


// --------------------------------------------------------------------
    public static function multi_DataProvider(): array
    {
        return [
            [4, 3, 12],
            [1, 1, 1],
            [3, -1, -3],
            [4, 0, 0],
            [-5, -9, 45],
        ];
    }

/**
	@dataProvider multi_DataProvider
*/

public function test_multi_proc
	    (int $number1, int $number2, int $expected): void
    {
	error_log("*** test_multi_proc() *** $number1 , $number2");
	$calculator = new Calculator();
        $ret = $calculator->multi($number1, $number2);
        $this->assertSame($expected, $ret);
    }


// --------------------------------------------------------------------
    public static function div_DataProvider(): array
    {
        return [
            [6, 3, 2],
            [1, 1, 1],
            [3, -1, -3],
            [-8, -2, 4],
        ];
    }

/**
	@dataProvider div_DataProvider
*/

public function test_div_proc
	    (int $number1, int $number2, int $expected): void
    {
	error_log("*** test_div_proc() *** $number1 , $number2");
	$calculator = new Calculator();
        $ret = $calculator->div($number1, $number2);
        $this->assertSame($expected, $ret);
    }

// --------------------------------------------------------------------
public function test_div_zero_proc(): void
{
	error_log("*** test_div_zero_proc() ***");
        $this->expectException(DivisionByZeroError::class);
        $this->calculator->div(6, 0);
}

// --------------------------------------------------------------------
}

// --------------------------------------------------------------------
```

<li>テストの実行</li>

```bash
./vendor/bin/phpunit tests/CalculatorTest.php
```

テスト結果

```text
$ ./vendor/bin/phpunit tests/CalculatorTest.php
PHPUnit 10.5.5 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.2.10-2ubuntu1
Configuration: /home/uchida/tmp/dec29/test01/phpunit.xml

*** test_add_proc() *** 3 , 4
.*** test_add_proc() *** 1 , 1
.*** test_add_proc() *** 3 , -1
.*** test_add_proc() *** 4 , 0
.*** test_add_proc() *** 5 , 9
.*** test_sub_proc() *** 4 , 3
.*** test_sub_proc() *** 1 , 1
.*** test_sub_proc() *** 3 , -1
.*** test_sub_proc() *** 4 , 0
.*** test_sub_proc() *** 5 , 9
.*** test_multi_proc() *** 4 , 3
.*** test_multi_proc() *** 1 , 1
.*** test_multi_proc() *** 3 , -1
.*** test_multi_proc() *** 4 , 0
.*** test_multi_proc() *** -5 , -9
.*** test_div_proc() *** 6 , 3
.*** test_div_proc() *** 1 , 1
.*** test_div_proc() *** 3 , -1
.*** test_div_proc() *** -8 , -2
.*** test_div_zero_proc() ***
F                                              20 / 20 (100%)

Time: 00:00.011, Memory: 8.00 MB

There was 1 failure:

1) CalculatorTest::test_div_zero_proc
Failed asserting that exception of type "Error" matches expected exception "DivisionByZeroError". Message was: "Call to a member function div() on null" at
/home/uchida/tmp/dec29/test01/tests/CalculatorTest.php:118
.

FAILURES!
Tests: 20, Assertions: 20, Failures: 1, Warnings: 1.
```

最後の　zero の割り算がエラーになっています。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/42e2f914-2d3c-adb2-d756-e77c41425d3b.png)


</ol>