LoginSignup
2
0

【初心者向け】PHPUnitをとりあえず使ってみる方法

Posted at

PHPUnitとは

PHP言語の単体テストを行うためのフレームワークです。

変更した箇所で要件を満たしていて、既存の機能に影響がないかを毎回確認することを自動化することができます。

利用方法としては、PHPUnitをComposerで入れる→テストコードを書く→コード実行で利用できます。

動作環境

  • PHP: v8.1.13
  • composer: v2.4.4
  • PHPUnit: v9.5

準備

プロジェクト作成

プロジェクト作成のコマンド

$ composer init

設定はこんな感じです。

Package name (<vendor>/<name>) [makoto/phpunit-getting-started]: app/phpunit
Description []:
Author [名前 <メールアドレス>, n to skip]:
Minimum Stability []:
Package Type (e.g. library, project, metapackage, composer-plugin) []: prpject
License []:

Define your dependencies.

Would you like to define your dependencies (require) interactively [yes]? no
Would you like to define your dev dependencies (require-dev) interactively [yes]? no
Add PSR-4 autoload mapping? Maps namespace "App\Phpunit" to the entered relative path. [src/, n to skip]:

↓実際のスクリーンショット
82aeb3e1-8b6b-c249-51cd-07a060f92e55.png

config.platformの設定

$ composer config platform.php  8.1.13

composer.json に下記が追加されます。

"config": {
    "platform": {
        "php": "8.1.13"
    }
}

PHPUnitのインストール

$ composer require --dev phpunit/phpunit

composer.jsonに下記が追加されます。

"require-dev": {
    "phpunit/phpunit": "^9.5"
}

これで一旦準備は完了です。
ここから実際に処理やコードを書いていきます。

実装

src配下に実際の処理コードを実装

src配下に「Calculator.php」ファイルを作成

$ touch src/Calculator.php

「Calculator」クラスを作成し、2つの引数を足し算する「add」メソッドと引き算する「sub」メソッドを作成

<?php

declare(strict_types=1);

namespace App\Phpunit;

class Calculator
{
    public function add(int $firstPrice, int $SecondPrice): int
    {
        return $firstPrice + $SecondPrice;
    }

    public function sub(int $firstPrice, int $SecondPrice): int
    {
        return $firstPrice - $SecondPrice;
    }
}

これで実際の処理は一旦完成です!
次にテストを書いていきます。

testsフォルダを作成しその中でテストコードを書いていく

testsフォルダの作成

$ mkdir tests

testsフォルダ配下に「CalculatorTest.php」を作成

$ touch tests/CalculatorTest.php

CalculatorTestの実装

<?php

namespace App\Phpunit;

use PHPUnit\Framework\TestCase;

class CalculatorTest extends TestCase
{
    public function test_合計金額()
    {
        $calculator = new Calculator();
        $this->assertSame(12, $calculator->add(10, 2));
    }

    public function test最初の金額から次の金額を引いた金額()
    {
        $calculator = new Calculator();
        $this->assertSame(8, $calculator->sub(10, 2));
    }
}

ここでは、実際の「Calculator」クラスをインスタンス化して足し算と引き算のメソッドのテストをそれぞれ行っています。

assertについて軽く触れておくと、今回利用しているassertSameは引数としている変数が同じ型で同じ値でない場合エラーとなります。

今回の合計金額を例とすると、最初の引数「12」と次の引数addメソッドの処理後「12」が比較されています。

実際に動かしてみる

コマンドは下記です。

$ vendor/phpunit/phpunit/phpunit tests

実行結果としては下記になります。

PHPUnit 9.5.27 by Sebastian Bergmann and contributors.

..                                                                  2 / 2 (100%)

Time: 00:00.004, Memory: 4.00 MB

OK (2 tests, 2 assertions)

「.」 は成功を表していて2つのテストがしっかり通っていることがここでわかります。

テストを失敗させてみる

「CalculatorTest.php」の合計金額を出している下記箇所を変更してみます。

- $this->assertSame(12, $calculator->add(10, 2));
+ $this->assertSame(10, $calculator->add(10, 2));

そうすると実行結果は下記になります。

PHPUnit 9.5.27 by Sebastian Bergmann and contributors.

F. 2 / 2 (100%)

Time: 00:00.007, Memory: 4.00 MB

There was 1 failure:

1) App\Phpunit\CalculatorTest::test_合計金額
Failed asserting that 12 is identical to 10.

/Users/makoto/Desktop/phpunit-getting-started/tests/CalculatorTest.php:12

FAILURES!
Tests: 2, Assertions: 2, Failures: 1.

今度は一箇所「.」ではなく「F」になりましたね。
これがテストが落ちている状態です。

エラーの内容にもあるとおり「test_合計金額」の期待値が「12」だけど「10」になっているよと言っています。

こんな感じでテストを書いていきます。

2
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
2
0