LoginSignup
59
55

More than 5 years have passed since last update.

ComposerでPHPUnit一式+Mockeryをインストール

Last updated at Posted at 2012-12-26

UPDATE 2013/03/01: phpunitの本家がcomposerをサポートしているので、EHER/phpunit-all-in-oneを使わないようにした

このtutorialで作るファイル

.
├── MyProject
│   └── ExampleTest.php
├── composer.json
├── phpunit.bootstrap.php
├── phpunit.xml.dist
└── vendor

composer.json を作る

composer.json
{
    "require-dev": {
        "phpunit/phpunit": "3.7.*",
        "mockery/mockery": "0.7.*"
    }
}

phpunitの本家はなかなかcomposerに対応していませんでした。なので、今までは本家のリポジトリではなく、EHER/phpunit-all-in-oneからphpunitをインストールするのが最も手軽でした。しかし、2012年8月13日に本家がcomposerをサポートしため、本家のリポジトリからインストールすることができます。

composer.json"require-dev" は開発系の依存パッケージを指定するためのものです。一方、ここにはありませんが、通常の "require" はプロダクトの依存パッケージを指定するのものです。この2つを区別することで、開発環境やビルドマシンではComposerでPHPUnitをインストールでき、プロダクション環境ではPHPUnitをインストールしないように環境に応じて選択することができます。

ComposerでPHPUnitなどをインストールする

"require-dev" に指定した依存パッケージを含めてインストールする場合は --dev をつけます。

$ composer.phar install --dev
Loading composer repositories with package information
Installing dependencies
Nothing to install or update
Loading composer repositories with package information
Installing dev dependencies
  - Installing mockery/mockery (0.7.2)
    Loading from cache

  - Installing symfony/yaml (v2.1.8)
    Loading from cache

  - Installing phpunit/php-text-template (1.1.4)
    Loading from cache

  - Installing phpunit/phpunit-mock-objects (1.2.3)
    Loading from cache

  - Installing phpunit/php-timer (1.0.4)
    Loading from cache

  - Installing phpunit/php-token-stream (1.1.5)
    Loading from cache

  - Installing phpunit/php-file-iterator (1.3.3)
    Loading from cache

  - Installing phpunit/php-code-coverage (1.2.9)
    Loading from cache

  - Installing phpunit/phpunit (3.7.14)
    Loading from cache

mockery/mockery suggests installing Hamcrest (1.0.0)
phpunit/phpunit suggests installing phpunit/php-invoker (>=1.1.0,<1.2.0)
Writing lock file
Generating autoload files

PHPUnitが実行できるか確認する

インストールすると、実行ファイル phpunit がついてきます。実行ファイルは vendor/bin に入ります。

$ ./vendor/bin/phpunit --version
PHPUnit 3.7.14 by Sebastian Bergmann.

PHPUnit設定ファイルを作る

$ vim phpunit.xml.dist
phpunit.xml.dist
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
    bootstrap="phpunit.bootstrap.php"
    processIsolation="false"
    verbose="true"
    strict="false"
    colors="true">
    <testsuites>
        <testsuite name="PHPUnit">
            <!-- テストケースがあるディレクトリを列挙する -->
            <directory>MyProject</directory>
            <!-- <directory>MyProject2</directory> -->
            <!-- <directory>MyProject3</directory> -->
            <!-- <directory>MyProject4</directory> -->
        </testsuite>
    </testsuites>

    <logging>
        <!-- コードカバレッジ率 -->
        <log
            type="coverage-html"
            target="metrics/coverage"
            charset="UTF-8"
            yui="true"
            highlight="false"
            lowUpperBound="35"
            highLowerBound="70" />
        <!-- <log type="coverage-text" target="php://stdout" lowUpperBound="35" highLowerBound="70" /> -->
        <!-- 上行をアンコメントアウトするとカバレッジ率がプロンプトにも表示される -->
    </logging>

    <filter>
        <!-- コードカバレッジ率を計測する対象の指定 -->
        <whitelist>
            <directory suffix=".php">src</directory>
            <!-- <file>/path/to/file</file> -->
            <exclude>
                <file>public/index.php</file>
            </exclude>
        </whitelist>
        <!-- コードカバレッジ測定対象から除外 -->
        <blacklist>
            <directory suffix=".php" group="PHPUNIT">vendor</directory>
        </blacklist>
    </filter>
    <listeners>
        <!-- mockery統合のため必要 -->
        <listener class="\Mockery\Adapter\Phpunit\TestListener" file="Vendor/mockery/mockery/library/Mockery/Adapter/Phpunit/TestListener.php" />
    </listeners>
</phpunit>

phpunit.bootstrap.php を作成する

$ vim phpunit.bootstrap.php
phpunit.bootstrap.php
<?php

// For composer
require_once 'vendor/autoload.php';

// テストクラス対象のオートロードなどもここに書く

単体テストのサンプルを作って試してみる

$ mkdir MyProject
$ cd MyProject/
$ vim ExampleTest.php
ExampleTest.php
<?php

class ExampleTest extends PHPUnit_Framework_TestCase
{
        public function testSomething()
        {
                $this->assertTrue(true);
        }
}
$ cd ../
$ ./vendor/bin/phpunit 
PHPUnit 3.7.14 by Sebastian Bergmann.

Configuration read from /private/tmp/phpunit/phpunit.xml.dist

.

Time: 0 seconds, Memory: 4.75Mb

OK (1 test, 1 assertion)

Generating code coverage report in HTML format ... done

PHPに xdebug 拡張が入っていればコードカバレッジは metrics/coverage/index.html に生成されます。

以上。

59
55
2

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
59
55