こちらと同様のことを行いました。
PHPUnit入門①
- プロジェクトの作成
- ソースコードの作成
- テストコードの作成
- テストの実行
$ 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
フォルダー構造
$ 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
phpunit.xml
<phpunit colors="true"></phpunit>
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;
}
}
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);
}
// --------------------------------------------------------------------
}
// --------------------------------------------------------------------
./vendor/bin/phpunit tests/CalculatorTest.php
テスト結果
$ ./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 の割り算がエラーになっています。