LoginSignup
12
10

More than 3 years have passed since last update.

【PHP】PHPUnitインストールからテスト実行まで

Last updated at Posted at 2020-03-25

環境
PHP 7.4.3
Composer 1.10.1

PHPUnitインストール

既にcomposerを入れてるので、コマンドからインストールするだけでした。

C:\php>composer require phpunit/phpunit --dev
word description
composer PHPのパッケージ管理ツール
require npmでいうところのinstall
dev ローカルにインストールするオプションコマンド

コマンド実行すると、composer.jsonの"require-dev"に追加されます。

composer.json
{
    "require": {},
    "require-dev": {
        "phpunit/phpunit": "^9.0"
    }
}

バージョンを確認してみます。

C:\php> vendor\bin\phpunit --version

conmoserでインストールするとphpunitコマンドはvendor/bin/phpunitに生えるので、上記のような実行方法になります。

バッチ作成

毎回vendor\bin\phpunit と打つのは面倒なので、バッチを作成してphpunit だけでテストが行えるようにします。

C:\php> echo @php "%~dp0vendor\phpunit\phpunit\phpunit" %* > phpunit.cmd

上記を実行すると、カレントディレクトリにphpunit.cmdができます。

ファイルをオートロードする

プロジェクトの構成は以下のとおりです。

C:.
└─PHP プロジェクト
  ├─src ソース
  ├─test テストコード
  └─vendor

テストコードにテストクラスのrequireを書きたくない場合、src配下をオートロードしておきます。
composer.jsonにautoload部分にpsr-4を追加します。

{
    "require": {},
    "require-dev": {
        "phpunit/phpunit": "^9.0"
    },
    "autoload": {
        "psr-4": {
            "App\\": "src/"
        }
    }
}

次に、composer dumpコマンドでautoloadを反映させます。

C:\php>composer dump
Generating autoload files
Generated autoload files

コマンドの初期設定

composer.jsonがある場所に、phpunit.xmlを作成します。
xmlにphounitのコマンドオプションを書くことで、指定するオプションを省略することが出来ます。
以下は色付けのオプションを書いてます。

<?xml version="1.0" encoding="UTF-8" ?>
<phpunit colors="true">
  <testsuites>
    <testsuite name="Test suite">
      <directory>test</directory>
    </testsuite>
  </testsuites>
</phpunit>

これでphpunit だけでテストコードが実行できるようになります。

テスト対象の作成

四則演算をするだけのプログラムでテストをします。

arithmetic.php
<?php
namespace App;
class Arithmetic {

    public function add($x,$y) {
        return ($x+$y);
    }
    public function substract($x,$y) {
        return ($x-$y);
    }
    public function multiply($x,$y) {
        return ($x*$y);
    }
    public function divide($x,$y) {
        return ($x/$y);
    }
}
?>

namespaceは先ほどpackage.jsonのpsr-4に指定したものです。

テストコード作成

以下テストコードです。

arithmeticTest.php
<?php

use PHPUnit\Framework\TestCase;
use App\Arithmetic;

class arithmeticTest extends TestCase{

    protected $obj;
    protected function setUp() :void {
        $this->object = new Arithmetic();
    }

    public function testAdd() {
        $this->assertEquals(5,$this->object->add(2,3));
    }
    public function testSub() {
        $this->assertEquals(5,$this->object->substract(8,3));
    }
    public function testMulti() {
        $this->assertEquals(6,$this->object->multiply(2,3));
    }
    public function testDiv() {
        $this->assertEquals(5,$this->object->divide(10,2));
    }
}
?>

setUp()は:voidを付けないとエラーになりました。理由はよくわからない。

PHP Fatal error:  Declaration of arithmeticTest::setUp() must be compatible with 
PHPUnit\Framework\TestCase::setUp(): void in C:\php\arithmeticTest.php on line 8

assertメソッドについては以下の記事にまとまっていたので参考にさせて頂きました。
https://qiita.com/rev84/items/12fbd16d210d6a86eff9

実行時のphpunitだけでOKです。

C:php>phpunit
PHPUnit 9.0.1 by Sebastian Bergmann and contributors.

....
  4 / 4 (100%)

Time: 63 ms, Memory: 4.00 MB

OK (4 tests, 4 assertions)

失敗時は以下のように表示されます。

C:php>phpunit
PHPUnit 9.0.1 by Sebastian Bergmann and contributors.

F...
Time: 64 ms, Memory: 4.00 MB

There was 1 failure:

1) arithmeticTest::testAdd
Failed asserting that 5 matches expected 10.

C:\php\test\arithmeticTest.php:16        

FAILURES!
Tests: 4, Assertions: 4, Failures: 1.
12
10
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
12
10