Why not login to Qiita and try out its useful features?

We'll deliver articles that match you.

You can read useful information later.

11
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

composerでPHPUnitを使うための覚え書き (Mac)

Last updated at Posted at 2018-04-12

#導入手順

  • homebrewでcomposerをインストール
  • composerでPHPUnitをインストール
  • PHPUnitの設定

*便利かもと思い、composer・PHPUnit共に直接インストールでなく、homebrew,composerを利用しています。

#事前準備していたもの

  • MAMP (PHPバージョンは7.1.12)
  • homebrew (composerのインストール用)

#導入

##homebrewでcomposerをインストールする
インストールする前にhomebrewのアップデートの確認を念のため

$ brew update

composerのインストール

$ brew install composer

無事にインストールされたか確認

$ composer -v

こんなのが出力されればOK。

   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 1.6.3 2018-01-31 16:28:17

#composerからPHPUnitをインストール
まずはターミナルからプロジェクトディレクトリへ移動

$ cd プロジェクトディレクトリ名

phpunitのインストール。--devオプションをつけて開発用でインストール。

$ composer require phpunit/phpunit --dev

バージョンは現時点での最新が自動で選ばれます。
インストールが成功するとcomposer.jsonファイルがプロジェクトディレクト内に作成されます。

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

PHPUnitもインストールできているか一応確認

$ vendor/bin/phpunit --version

これが出てくればOK

$ PHPUnit 7.1.2 by Sebastian Bergmann and contributors.

#PHPUnitの設定

xDebugを使えるようにする

xDebugはMAMPに標準で用意されている。でも、php.iniではコメントアウトされている。

###1. php.iniの設定
php.iniの場所は
/Application/MAMP/bin/php/使用しているPHPバージョン/conf/

コードの最後にxDebugの記述がある。

php.ini
[xdebug]
#zend_extension="/Applications/MAMP/bin/php/php7.1.12/lib/php/extensions/no-debug-non-zts-xxxxxxxx/xdebug.so"

ここのコメントアウトを外して、下に記述を追加する。

php.ini
[xdebug]
zend_extension="/Applications/MAMP/bin/php/php7.1.12/lib/php/extensions/no-debug-non-zts-xxxxxxxx/xdebug.so"
xdebug.coverage_enable=1 
xdebug.default_enable=1
xdebug.profiler_enable=1
xdebug.profiler_output_dir="/tmp"
xdebug.remote_autostart=1
xdebug.remote_enable=1
xdebug.remote_host=localhost
xdebug.remote_port=9000

* "no-debug-non-zts-xxxxxxxx"のxxxxxxxx部分は各々の環境で違うと思うのでコピペ注意です。php.iniを確認してください。

###2. apacheを再起動

ここでハマったのが、localhost内でphpinfo()を出力して確認するとxDebugが有効になっているのにも関わらず、コマンドラインではxDebugが有効になっていない。
これは、コマンドライン上ではMAMPのphpにPATHが通っておらず、Macに標準インストールされているphpが実行されていたのが原因でした。

which phpusr/binが出力されているとMac標準のphpが実行されています。なので**.bash_profile**でPATHを変更してあげましょう。

.bash_profile
$ export PATH=/Applications/MAMP/bin/php/使用しているバージョン/bin:$PATH

.bash_profileの再読み込み

$ source .bash_profile

これでもう一度、which phpで確認。/Applications/MAMP/bin/php/使用しているバージョン/bin/phpが出ればOK。

phpunit.xmlの用意

  • autoload.phpの事前読み込み
  • 複数テストの実行
  • カバレッジファイルの生成
phpunit.xml
<phpunit bootstrap="vendor/autoload.php">
	<filter>
		<whitelist processUncoveredFilesFromWhitelist="true">
			<directory suffix=".php">テストディレクトリ</directory>
		</whitelist>
	</filter>
	<testsuites>
		<testsuite name="まとめて実行したいテストのグループ名">
			<directory>テストディレクトリ</directory>
		</testsuite>
	</testsuites>
</phpunit>

###composer.jsonにコマンドを用意

composer.json
{
    "require-dev": {
        "phpunit/phpunit": "7.0.3",
    },
    "scripts": {
    	"test": [
    		"vendor/bin/phpunit --testsuite グループ名"
    	],
    	"coverage": [
    		"vendor/bin/phpunit --coverage-html カバレッジファイルの出力ディレクト名 test/実行テストファイル"
    	]
    }
}

ここまで

11
16
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
11
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?