Stagehand_TestRunner 4.1.0 がリリースされたので Symfony 2.7 で使ってみた(道半ば)

Stagehand_TestRunner 4.1.0 released!

さっそく Symfony 2.7 で使ってみました。が、ちょっと設定まわりではまって、最後まで綺麗に終わってません。

リリースノート

Stagehand_TestRunner 4.1.0 (stable) ではこういった機能が追加されたようです。

結論からいうと

こんな感じでコマンドラインで使うと「自動テスト」+「通知」をしてくれます。src 以下を監視してくれているので、ファイルを修正するたびにテストが実行されテストの検証結果の通知を対話的に受け取ることができます。通知は音でもテスト失敗と成功を教えてくれるのでいい感じ。

$ bin/testrunner phpunit -amR src --preload-script app/bootstrap_test.php
PHPUnit 4.7.6 by Sebastian Bergmann and contributors.

.

AppBundle\Tests\Controller\DefaultController
 [x] Index

Time: 1.04 seconds, Memory: 20.00Mb

OK (1 test, 2 assertions)

bootstrap_test.php はこんな感じ。

<?php
$_SERVER['KERNEL_DIR'] = __DIR__;
require_once $_SERVER['KERNEL_DIR'] . '/bootstrap.php.cache';

うまくいかなかったこと

ただし、config/phpunit.xml.dist などで設定してあげたいところがあんまりうまくいけてないです(後述)。深夜なのでまた頭がまわらない・・・。また今度調査してみようと思います。知ってる人いたら、だれか教えてくださいm(__)m

では、以下、やったことの紹介。マシンは Mac OSX Yosemite

Symfony 2.7 のサンプルプロジェクトを作成

まずは、symfony-installer を使って Symfony 2.7 のプロジェクトを作成。

$ symfony new my_project
Downloading Symfony...

    4.95 MB/4.95 MB ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓  100%

 Preparing project...

 ✔  Symfony 2.7.1 was successfully installed. Now you can:

    * Change your current directory to /Users/seta/tmp/work-stagehand-testrunner/my_project

    * Configure your application in app/config/parameters.yml file.

    * Run your application:
        1. Execute the php app/console server:run command.
        2. Browse to the http://localhost:8000 URL.

    * Read the documentation at http://symfony.com/doc

Composer Install

次に Composer Install を実行。( Composer のインストールはこちらを参考に。)

$ cd my_project
$ composer install

Stagehand_Testrunner をインストール

$ composer require --dev piece/stagehand-testrunner "4.1.*"
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
  - Installing sebastian/version (1.0.6)
    Loading from cache

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

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

  - Installing piece/stagehand-testrunner (v4.1.0)
    Loading from cache

piece/stagehand-testrunner suggests installing phpunit/phpunit (For using PHPUnit)
Writing lock file
Generating autoload files
> Incenteev\ParameterHandler\ScriptHandler::buildParameters
Updating the "app/config/parameters.yml" file
> Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::buildBootstrap
> Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache
Clearing the cache for the dev environment with debug true
> Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::installAssets
Trying to install assets as symbolic links.
Installing assets for Symfony\Bundle\FrameworkBundle into web/bundles/framework
The assets were installed using symbolic links.
Installing assets for Sensio\Bundle\DistributionBundle into web/bundles/sensiodistribution
The assets were installed using symbolic links.
> Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::installRequirementsFile
> Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::removeSymfonyStandardFiles
> Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::prepareDeploymentTarget

あれ、PHPUnit は別でインストールするんだ!インストールしてみます。

$ composer require --dev phpunit/phpunit "4.7.*"
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
  - Installing sebastian/global-state (1.0.0)
    Loading from cache

  - Installing sebastian/recursion-context (1.0.0)
    Loading from cache

  - Installing sebastian/exporter (1.2.0)
    Loading from cache

  - Installing sebastian/environment (1.2.2)
    Loading from cache

  - Installing sebastian/diff (1.3.0)
    Loading from cache

  - Installing sebastian/comparator (1.1.1)
    Loading from cache

  - Installing doctrine/instantiator (1.0.5)
    Loading from cache

  - Installing phpdocumentor/reflection-docblock (2.0.4)
    Loading from cache

  - Installing phpspec/prophecy (v1.4.1)
    Loading from cache

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

  - Installing phpunit/phpunit-mock-objects (2.3.5)
    Downloading: 100%

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

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

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

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

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

sebastian/global-state suggests installing ext-uopz (*)
phpdocumentor/reflection-docblock suggests installing dflydev/markdown (~1.0)
phpdocumentor/reflection-docblock suggests installing erusev/parsedown (~1.0)
phpunit/phpunit-mock-objects suggests installing ext-soap (*)
phpunit/phpunit suggests installing phpunit/php-invoker (~1.1)
Writing lock file
Generating autoload files
> Incenteev\ParameterHandler\ScriptHandler::buildParameters
Updating the "app/config/parameters.yml" file
> Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::buildBootstrap
> Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache
Clearing the cache for the dev environment with debug true
> Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::installAssets
Trying to install assets as symbolic links.
Installing assets for Symfony\Bundle\FrameworkBundle into web/bundles/framework
The assets were installed using symbolic links.
Installing assets for Sensio\Bundle\DistributionBundle into web/bundles/sensiodistribution
The assets were installed using symbolic links.
> Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::installRequirementsFile
> Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::removeSymfonyStandardFiles
> Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::prepareDeploymentTarget

さぁ、テスト!

まずは、PHPUnit で。

$ bin/phpunit -c app
PHPUnit 4.7.6 by Sebastian Bergmann and contributors.

.

Time: 739 ms, Memory: 16.25Mb

OK (1 test, 2 assertions)

はい。次は Stagehand_Testrunnerで!

$ bin/testrunner phpunit -R ./src
PHPUnit 4.7.6 by Sebastian Bergmann and contributors.

E

AppBundle\Tests\Controller\DefaultController
 [ ] Index

Time: 403 ms, Memory: 8.75Mb

There was 1 error:

1) AppBundle\Tests\Controller\DefaultControllerTest::testIndex
RuntimeException: You must override the KernelTestCase::createKernel() method.

/Users/seta/tmp/work-stagehand-testrunner/my_project/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Test/KernelTestCase.php:44
/Users/seta/tmp/work-stagehand-testrunner/my_project/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Test/KernelTestCase.php:113
/Users/seta/tmp/work-stagehand-testrunner/my_project/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Test/KernelTestCase.php:159
/Users/seta/tmp/work-stagehand-testrunner/my_project/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Test/KernelTestCase.php:140
/Users/seta/tmp/work-stagehand-testrunner/my_project/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Test/WebTestCase.php:33
/Users/seta/tmp/work-stagehand-testrunner/my_project/src/AppBundle/Tests/Controller/DefaultControllerTest.php:11
/Users/seta/tmp/work-stagehand-testrunner/my_project/vendor/piece/stagehand-testrunner/src/Runner/PHPUnitRunner.php:81
/Users/seta/tmp/work-stagehand-testrunner/my_project/vendor/piece/stagehand-testrunner/src/Process/TestRunner.php:106
/Users/seta/tmp/work-stagehand-testrunner/my_project/vendor/piece/stagehand-testrunner/src/CLI/TestRunnerApplication/Command/PluginCommand.php:150
/Users/seta/tmp/work-stagehand-testrunner/my_project/vendor/symfony/symfony/src/Symfony/Component/Console/Command/Command.php:259
/Users/seta/tmp/work-stagehand-testrunner/my_project/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:878
/Users/seta/tmp/work-stagehand-testrunner/my_project/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:195
/Users/seta/tmp/work-stagehand-testrunner/my_project/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:126

FAILURES!
Tests: 1, Assertions: 0, Errors: 1.

エラー・・・orz。

テスト用の bootstrap ファイルを作成してあげる

  • app/bootstrap_test.php
<?php
$_SERVER['KERNEL_DIR'] = __DIR__;
require_once $_SERVER['KERNEL_DIR'] . '/bootstrap.php.cache';

このファイルを --preload-script で通してあげてテストすると通った。

$ bin/testrunner phpunit -R ./src --preload-script app/bootstrap_test.php
PHPUnit 4.7.6 by Sebastian Bergmann and contributors.

.

AppBundle\Tests\Controller\DefaultController
 [x] Index

Time: 985 ms, Memory: 19.75Mb

OK (1 test, 2 assertions)

ほんとは PHPUnit の設定ファイルで設定してあげたい・・・(?)

  • app/phpunit.xml.dist で bootstra_test.php を読み込むようにする
<?xml version="1.0" encoding="UTF-8"?>

<!-- http://phpunit.de/manual/4.1/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.1/phpunit.xsd"
         backupGlobals="false"
         colors="true"
         bootstrap="bootstrap_test.php"
>

(省略)

</phpunit>

実行するとエラー。

$ bin/testrunner phpunit -R src --phpunit-config app/phpunit.xml.dist
Fatal error: Cannot redeclare class Symfony\Component\DependencyInjection\ContainerInterface in /Users/seta/tmp/work-stagehand-testrunner/my_project/app/bootstrap.php.cache on line 1980

Call Stack:
    0.0059     264160   1. {main}() /Users/seta/tmp/work-stagehand-testrunner/my_project/vendor/piece/stagehand-testrunner/bin/testrunner:0
    0.0873    2695080   2. Symfony\Component\Console\Application->run() /Users/seta/tmp/work-stagehand-testrunner/my_project/vendor/piece/stagehand-testrunner/bin/testrunner:55
    0.1007    3031784   3. Symfony\Component\Console\Application->doRun() /Users/seta/tmp/work-stagehand-testrunner/my_project/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:126
    0.1213    3032904   4. Symfony\Component\Console\Application->doRunCommand() /Users/seta/tmp/work-stagehand-testrunner/my_project/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:195
    0.1214    3033424   5. Symfony\Component\Console\Command\Command->run() /Users/seta/tmp/work-stagehand-testrunner/my_project/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:878
    0.1245    3039264   6. Stagehand\TestRunner\CLI\TestRunnerApplication\Command\PluginCommand->execute() /Users/seta/tmp/work-stagehand-testrunner/my_project/vendor/symfony/symfony/src/Symfony/Component/Console/Command/Command.php:259
    0.1703    4127864   7. Stagehand\TestRunner\CLI\TestRunnerApplication\Command\PluginCommand->createTestRunner() /Users/seta/tmp/work-stagehand-testrunner/my_project/vendor/piece/stagehand-testrunner/src/CLI/TestRunnerApplication/Command/PluginCommand.php:150
    0.1703    4127960   8. Stagehand\TestRunner\Core\ApplicationContext->createComponent() /Users/seta/tmp/work-stagehand-testrunner/my_project/vendor/piece/stagehand-testrunner/src/CLI/TestRunnerApplication/Command/PluginCommand.php:298
    0.1703    4128024   9. Stagehand\ComponentFactory\ComponentFactory->create() /Users/seta/tmp/work-stagehand-testrunner/my_project/vendor/piece/stagehand-testrunner/src/Core/ApplicationContext.php:106
    0.1703    4128520  10. Symfony\Component\DependencyInjection\Container->get() /Users/seta/tmp/work-stagehand-testrunner/my_project/vendor/piece/stagehand-componentfactory/src/Stagehand/ComponentFactory/ComponentFactory.php:76
    0.1703    4129160  11. Stagehand\TestRunner\DependencyInjection\PHPUnitContainer->getTestRunnerService() /Users/seta/tmp/work-stagehand-testrunner/my_project/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Container.php:327
    0.1749    4182432  12. Stagehand\TestRunner\Process\TestRunner->__construct() /Users/seta/tmp/work-stagehand-testrunner/my_project/vendor/piece/stagehand-testrunner/src/DependencyInjection/PHPUnitContainer.php:449
    0.1749    4182504  13. Stagehand\TestRunner\Preparer\PHPUnitPreparer->prepare() /Users/seta/tmp/work-stagehand-testrunner/my_project/vendor/piece/stagehand-testrunner/src/Process/TestRunner.php:95
    0.1828    4515680  14. Stagehand\TestRunner\Preparer\PHPUnitPreparer->earlyConfigure() /Users/seta/tmp/work-stagehand-testrunner/my_project/vendor/piece/stagehand-testrunner/src/Preparer/PHPUnitPreparer.php:71
    0.1874    4672272  15. Stagehand\TestRunner\Preparer\PHPUnitPreparer->handleBootstrap() /Users/seta/tmp/work-stagehand-testrunner/my_project/vendor/piece/stagehand-testrunner/src/Preparer/PHPUnitPreparer.php:127
    0.1885    4680656  16. PHPUnit_Util_Fileloader::checkAndLoad() /Users/seta/tmp/work-stagehand-testrunner/my_project/vendor/piece/stagehand-testrunner/src/Preparer/PHPUnitPreparer.php:107
    0.1885    4680856  17. PHPUnit_Util_Fileloader::load() /Users/seta/tmp/work-stagehand-testrunner/my_project/vendor/phpunit/phpunit/src/Util/Fileloader.php:36
    0.2001    5822360  18. include_once('/Users/seta/tmp/work-stagehand-testrunner/my_project/app/bootstrap.php.cache') /Users/seta/tmp/work-stagehand-testrunner/my_project/vendor/phpunit/phpunit/src/Util/Fileloader.php:52

もちろん、これは通る。

$ bin/testrunner phpunit -R src --phpunit-config app/phpunit.xml.dist --preload-script app/bootstrap_test.php
PHPUnit 4.7.6 by Sebastian Bergmann and contributors.

.

AppBundle\Tests\Controller\DefaultController
 [x] Index

Time: 1.26 seconds, Memory: 19.75Mb

OK (1 test, 2 assertions)

あれ?これどうするんだ・・・っけ(?)。何か勘違いしている・・・!?深夜で頭がまわってないので、また今度調べてみます。

参考 URL