k-holyのPHPとか諸々メモ

Webで働くk-holyがPHP(スクリプト言語)とか諸々のことをメモしていきます。ソースコードはだいたいWindowsで動かしてます。

Composerでプロジェクトグローバルに Stagehand_TestRunner + PHPUnit をインストール(Windows7 + NYAOS編)

今度はローカル開発環境の Windows7 + NYAOS で composer global install してみました。

以下、シェルは NYAOS を使っています。

Composerをインストール

当然すでにcomposerは導入済みなのですが、Linuxの場合と同じ手順を踏んでやってみます。

PHPのインストールディレクトリ C:\php に composer.phar をインストールします。

$ cd C:\php
$ curl -sS https://getcomposer.org/installer | php
#!/usr/bin/env php
All settings correct for using Composer
Downloading...

Composer successfully installed to: C:\php\composer.phar
Use it: php composer.phar

ここからがWindows限定の作業ですが、以下のようなバッチファイルを作成して、composer.phar を代行させます。

中身はStagehand_TestRunnerの testrunner.bat を参考にしました。

(その testrunner.bat では symfony.bat を参考にされてるみたいです)

@echo off

REM This script will do the following:
REM - check for PHP_COMMAND env, if found, use it.
REM   - if not found detect php, if found use it, otherwise err and terminate

IF "%OS%"=="Windows_NT" @SETLOCAL

REM %~dp0 is expanded pathname of the current script under NT
SET SCRIPT_DIR=%~dp0

GOTO INIT

:INIT

IF "%PHP_COMMAND%" == "" GOTO NO_PHPCOMMAND

IF EXIST ".\composer" (
  "%PHP_COMMAND%" ".\composer.phar" %1 %2 %3 %4 %5 %6 %7 %8 %9
) ELSE (
  "%PHP_COMMAND%" "%SCRIPT_DIR%composer.phar" %1 %2 %3 %4 %5 %6 %7 %8 %9
)
GOTO CLEANUP

:NO_PHPCOMMAND
REM ECHO ------------------------------------------------------------------------
REM ECHO WARNING: Set environment var PHP_COMMAND to the location of your php.exe
REM ECHO          executable (e.g. C:\PHP\php.exe).  (assuming php.exe on PATH)
REM ECHO ------------------------------------------------------------------------
SET PHP_COMMAND=php.exe
GOTO INIT

:CLEANUP
IF "%OS%"=="Windows_NT" @ENDLOCAL
REM PAUSE

REM Local Variables:
REM mode: conf
REM coding: iso-8859-1
REM indent-tabs-mode: nil
REM End:

composerコマンド打ってみる。

$ composer
   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 64ac32fca9e64eb38e50abfadc6eb6f2d0470039 2014-05-24 20:57:50

OKです。

ComposerでStagehand_TestRunnerをインストール

再びこちらを参考

$ composer global require piece/stagehand-testrunner:3.6.*
Changed current directory to C:/Users/k_horii/AppData/Roaming/Composer
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
  - Installing symfony/yaml (v2.4.5)
    Downloading: 100%

  - Installing symfony/process (v2.4.5)
    Downloading: 100%

  - Installing symfony/finder (v2.4.5)
    Downloading: 100%

  - Installing symfony/dependency-injection (v2.4.5)
    Downloading: 100%

  - Installing symfony/console (v2.4.5)
    Downloading: 100%

  - Installing symfony/filesystem (v2.4.5)
    Downloading: 100%

  - Installing symfony/config (v2.4.5)
    Downloading: 100%

  - Installing symfony/class-loader (v2.4.5)
    Downloading: 100%

  - Installing piece/stagehand-componentfactory (v1.0.1)
    Loading from cache

  - Installing piece/stagehand-alterationmonitor (2.0.0)
    Loading from cache

  - Installing piece/stagehand-testrunner (v3.6.2)
    Downloading: 100%

symfony/dependency-injection suggests installing symfony/proxy-manager-bridge (Generate service proxies to lazy load them)
symfony/console suggests installing symfony/event-dispatcher ()
piece/stagehand-testrunner suggests installing phpunit/phpunit (>=3.6.0)
Writing lock file
Generating autoload files

Windowsの場合、composer global でインストールしたファイルは %APPDATA%\Composer 以下に配置されるようです。

私の環境では具体的には C:\Users\k_horii\AppData\Roaming\Composer になりました。

composerでインストールしたライブラリの実行ファイルは C:\Users\k_horii\AppData\Roaming\Composer\vendor\bin 以下に配置されました。

testrunner コマンドを実行してみると…

$ source ~/_nya
$ %APPDATA%\Composer\vendor\bin\testrunner
Warning: require_once(Stagehand/TestRunner/Core/Bootstrap.php): failed to open stream: No such file or directory in C:\Users\k_horii\AppData\Roaming\Composer\vendor\piece\stagehand-testrunner\bin\testrunner on line 52

案の定、前回と同じ警告で詰まりましたので、同じように -p オプションでComposerのオートロードスクリプトを読ませてみます。

$ %APPDATA%\Composer\vendor\bin\testrunner -p %APPDATA%\Composer\vendor\autoload.php
Stagehand_TestRunner version @package_version@

Copyright (c) 2005-2013 KUBO Atsuhiro and contributors,
All rights reserved.

Usage:
  [options] command [arguments]

Options:
  --help           -h Prints help and exit.
  --version        -V Prints version information and exit.
  --ansi              Enables ANSI output.
  --no-ansi           Disables ANSI output.

Testing Framework Commands:
  cakephp               Runs tests with CakePHP.
  ciunit                Runs tests with CIUnit.
  phpspec               Runs tests with PHPSpec.
  phpunit               Runs tests with PHPUnit.
  simpletest            Runs tests with SimpleTest.
Other Commands:
  compile               Compiles the DIC for the production environment.
  help                  Prints the help for a command.
  list                  Lists commands.
  phpunit:passthrough   Runs the phpunit command via the testrunner command.

無事にコマンドを実行できました。

ComposerでPHPUnit(3.7系)をインストール

Windowsの方でも3.7系を入れることにします。

$ composer global require phpunit/phpunit:3.7.*
Changed current directory to C:/Users/k_horii/AppData/Roaming/Composer
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
  - Installing phpunit/php-text-template (1.2.0)
    Loading from cache

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

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

  - Installing phpunit/php-token-stream (1.2.2)
    Downloading: 100%

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

  - Installing phpunit/php-code-coverage (1.2.17)
    Downloading: 100%

  - Installing phpunit/phpunit (3.7.37)
    Downloading: 100%

phpunit/phpunit-mock-objects suggests installing ext-soap (*)
phpunit/php-code-coverage suggests installing ext-xdebug (>=2.0.5)
phpunit/phpunit suggests installing phpunit/php-invoker (~1.1)
Writing lock file
Generating autoload files

3.7.37が入りました。

$ ls -l %APPDATA%\Composer\vendor\bin
-a--rw-      151 May 27 17:05               phpunit
-a--rw-       96 May 27 17:05               phpunit.bat
-a--rw-      156 May 27 16:31 testru~1      testrunner
-a--rwx      101 May 27 16:31 testru~1.bat  testrunner.bat

こんな感じで、testrunner.batに続いてphpunit.batが配置されてます。

プロジェクトルートでテスト実行

プロジェクトルートに移動してtestrunnerコマンドでPHPUnitのテストを実行してみます。

-c オプションでStagehand_TestRunnerの設定ファイルを読み込ませます。

$ %APPDATA%\Composer\vendor\bin\testrunner phpunit -c testrunner.yml
Please run the following command before running the phpunit command:

  testrunner compile

phpunit command の前に testrunner compile しろって言われたので、その通りにします。

$ %APPDATA%\Composer\vendor\bin\testrunner compile -p %APPDATA%\Composer\vendor\autoload.php
$ %APPDATA%\Composer\vendor\bin\testrunner phpunit -p %APPDATA%\Composer\vendor\autoload.php -c testrunner.yml

テストを実行できましたが、例のごとくエスケープシーケンスによる色付けが効かないので、msysGitのcatにテスト結果を渡します。

$ %APPDATA%\Composer\vendor\bin\testrunner phpunit -p %APPDATA%\Composer\vendor\autoload.php -c testrunner.yml | cat

色付けもできました。

こちらではGrowl経由の通知もされています。

NYAOSにコマンドのエイリアスを定義

最後に、NYAOSでは初期設定ファイル _nya にコマンドのエイリアスを定義できますので、以下を追加します。

alias testrunner-phpunit "%APPDATA%\Composer\vendor\bin\testrunner phpunit -p %APPDATA%\Composer\vendor\autoload.php -c testrunner.yml | cat"
$ source ~/_nya
$ testrunner-phpunit

これで、testrunner-phpunitコマンドでテストが可能になりました。

これまで歴史的な経緯により(?) PEARは C:\xampp\php に入れて動かしていたのですが、今ではローカル開発時はPHPは C:\php でビルトインWebサーバーを動かしており、XAMPPの機能は使っていなかったため、この機会にPEARと同時にXAMPP離れも断行しました。