composerでPHPUnitをインストールしてどんなにマクロなプログラムを書いた時もテストする


マクロなプログラムを書いたときでもテストを書く

「プロジェクトごとにPHPUnitをインストールするのはめんどくさいがテストは書いておきたい~」
ということでcomposerのglobalにPHPUnitをインストールしてそれを使うようにしていきます。
(※基本Windowsユーザー向けで書いてます)

composerのglobalにPHPUnitをインストール

composerがインストールされている想定で書いていきます。
まず、composerのglobalがあるとこに移動

$ cd  C:/Users/User/AppData/Roaming/Composer

このディレクトリにcomposer.jsonファイルを作成します。

ここではPHPUnitの安定板の6.5系をインストールすることにします。(※バージョンによりPHPの互換性が異なるので注意)
composer.jsonファイルを作成し以下のように記述(composer initでも生成可)

composer.json
{
    "require-dev": {
        "phpunit/phpunit": "6.5.*"
    }
}

composer.jsonファイルを作成したらcomposer installでインストール

User@**** MINGW64 ~/AppData/Roaming/Composer
$ composer install
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 28 installs, 0 updates, 0 removals
  - Installing sebastian/version (2.0.1): Loading from cache
  - Installing sebastian/resource-operations (1.0.0): Loading from cache
  - Installing sebastian/recursion-context (3.0.0): Loading from cache
  - Installing sebastian/object-reflector (1.1.1): Loading from cache
  - Installing sebastian/object-enumerator (3.0.3): Loading from cache
  - Installing sebastian/global-state (2.0.0): Loading from cache
  - Installing sebastian/exporter (3.1.0): Loading from cache
  - Installing sebastian/environment (3.1.0): Loading from cache
  - Installing sebastian/diff (2.0.1): Loading from cache
  - Installing sebastian/comparator (2.1.3): Loading from cache
  - Installing doctrine/instantiator (1.1.0): Loading from cache
  - Installing phpunit/php-text-template (1.2.1): Loading from cache
  - Installing phpunit/phpunit-mock-objects (5.0.6): Loading from cache
  - Installing phpunit/php-timer (1.0.9): Loading from cache
  - Installing phpunit/php-file-iterator (1.4.5): Loading from cache
  - Installing theseer/tokenizer (1.1.0): Loading from cache
  - Installing sebastian/code-unit-reverse-lookup (1.0.1): Loading from cache
  - Installing phpunit/php-token-stream (2.0.2): Loading from cache
  - Installing phpunit/php-code-coverage (5.3.0): Loading from cache
  - Installing webmozart/assert (1.3.0): Loading from cache
  - Installing phpdocumentor/reflection-common (1.0.1): Loading from cache
  - Installing phpdocumentor/type-resolver (0.4.0): Loading from cache
  - Installing phpdocumentor/reflection-docblock (4.3.0): Loading from cache
  - Installing phpspec/prophecy (1.7.5): Loading from cache
  - Installing phar-io/version (1.0.1): Loading from cache
  - Installing phar-io/manifest (1.0.1): Loading from cache
  - Installing myclabs/deep-copy (1.7.0): Loading from cache
  - Installing phpunit/phpunit (6.5.7): Loading from cache
sebastian/global-state suggests installing ext-uopz (*)
phpunit/phpunit-mock-objects suggests installing ext-soap (*)
phpunit/php-code-coverage suggests installing ext-xdebug (^2.5.5)
phpunit/phpunit suggests installing phpunit/php-invoker (^1.1)
phpunit/phpunit suggests installing ext-xdebug (*)
Writing lock file
Generating autoload files

User@**** MINGW64 ~/AppData/Roaming/Composer

インストールが終わったらPHPUnitのversionが指定したversionなのかを確認しておく

User@**** MINGW64 ~
$ phpunit --version
PHPUnit 6.5.7 by Sebastian Bergmann and contributors.


User@**** MINGW64 ~

(もしかして:PHPUnitのバージョンがおかしい

テストを書く

PHPUnitのインストール完了後、対象のプロジェクトディレクトリに移動してテストを書いていきます。
ドキュメントルートにtestsディレクトリを作成し、その中に テスト対象クラス+Test という名前でテストファイルを作成。
(この際、テスト対象のファイルを読み込む必要があります。)

↓テストを書いた例

HogeTest.php
<?php
use PHPUnit\Framework\TestCase;

// テスト対象のファイルを読み込む
require('../../programDir/Hoge.php');

class HogeTest extends TestCase
{
    /**
     * Hoge Object
     *
     * @var Object
     */
    protected $hoge;

    /**
     * 各テストメソッドが実行される前に、setUp() という名前のテンプレートメソッドが実行される。
     * (テスト対象のオブジェクトを生成するような処理に使用する。)
     */
    protected function setUp() {
        $this->hoge = new Hoge();
    }

    public function test_TrueはTrueであること()
    {
        $this->assertTrue(True);
    }

    public function test_値がhogehogeの場合ほげほげが返ること()
    {
        // HogeクラスにhogeMethodがある想定のテスト例
        $this->assertEquals('ほげほげ', $this->hoge->hogeMethod('hogehoge'));
    }

}

setUp()メソッドのほかにもtearDown()メソッドやらいろいろあります。詳しくはPHPUnitのマニュアルを見てください。

テストを書き終わったら、テストファイルのあるディレクトリに移動して

phpunit ****Test.php

を実行。(phpunitにパスが通ってるか要確認)

↓実行例

$ phpunit HogeTest.php
PHPUnit 6.5.7 by Sebastian Bergmann and contributors.

..                                                                  2 / 2 (100%)

Time: 167 ms, Memory: 4.00MB

OK (2 tests, 2 assertions)

これでどんなにマクロなプログラムを書いたときでもテストをかける生活ができます(?)

おまけ:PHPUnitのバージョンがおかしい

6.5系をインストールしたはずなのになぜかPHPUnit 3.7.**とか表示されている。。。

ってなった人はもしかしてXAMPPをインストールしてる人ですかね。
Windowsの環境変数にC:\xampp\phpが書いてあると、XAMPPの方の古いPHPUnitを見に行ってしまう(っぽい)。
なので、(都合が悪くない場合のみ)このパスを消せばcomposerでglobalにインストールした方のPHPUnitを見に行くようになります。
(※ただし環境によりけり)

他に調べてみると↑とは異なる解決方法が書かれてあったので参考にするといいかも(ちなみに自分はこれできませんでした)

ついでにコマンドプロンプトからパスの確認するコマンドも書いておきます
echo %path:;= / %

おわり

  • おまけ~とか書いてるけどぶっちゃけめっちゃ詰まったとこです
  • 本当はプロジェクトごとにインストールしたかったんですが、わからなかったが本音のところがある
  • なんか間違えてるところとかいい方法があれば教えていただきたく

参考

以下は個人的なURLメモ