0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PHPUnit: dataProvider の使い方

Posted at

こちらと同様のことを行いました。
PHPUnit入門①

  1. プロジェクトの作成
  2. $ 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>
    
  3. ソースコードの作成
  4. 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;
        }
    }
    
  5. テストコードの作成
  6. 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);
    }
    
    // --------------------------------------------------------------------
    }
    
    // --------------------------------------------------------------------
    
  7. テストの実行
  8. ./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 の割り算がエラーになっています。

    image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?