Skip to content

Instantly share code, notes, and snippets.

@hidenorigoto
Created May 29, 2011 06:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hidenorigoto/997514 to your computer and use it in GitHub Desktop.
Save hidenorigoto/997514 to your computer and use it in GitHub Desktop.
Install Behat (Symfony SE 2.0.1)
<?php
// for Symfony2 tests
$world->getContainer = function() use ($world) {
return $world->getSession()->getDriver()->getClient()->getKernel()->getContainer();
};
$world->getEm = function() use ($world) {
return $world->getContainer()->get('doctrine')->getEntityManager();
};

Symfony Standard Edition(2.0.1)を利用している前提 また、以下ではPHPUnitをプロジェクトディレクトリ配下に配置している

Standard Edition のルートディレクトリにある deps ファイルに以下の行を追加する

[behat]
    git=http://github.com/Behat/Behat.git
    target=/Behat/Behat
[mink]
    git=http://github.com/Behat/Mink.git
    target=/Behat/Mink
[gherkin]
    git=http://github.com/Behat/Gherkin.git
    target=/Behat/Gherkin
[BehatBundle]
    git=http://github.com/Behat/BehatBundle.git
    target=/Behat/BehatBundle
[MinkBundle]
    git=http://github.com/Behat/MinkBundle.git
    target=/Behat/MinkBundle
[phpunit]
    git=http://github.com/knplabs/phpunit-easyinstall.git

vendorインストールスクリプトを実行する

$ php bin/vendors install

PHPUnitの依存モジュールを取得する

$ cd vendor/phpunit
$ git submodule update --init

app/autoload.phpを編集する

$loader->registerNamespaces(array(
    // ・・・
    'Behat\BehatBundle' => __DIR__.'/../vendor',
    'Behat\MinkBundle' => __DIR__.'/../vendor',
    'Behat\Behat'       => __DIR__.'/../vendor/Behat/Behat/src',
    'Behat\Gherkin'     => __DIR__.'/../vendor/Behat/Gherkin/src',
    'Behat\Mink'       => __DIR__.'/../vendor/Behat/Mink/src',
));

$loader->registerPrefixFallback(array(
    // ・・・
    '/opt/local/lib/php',  // システムのPEARのディレクトリ
));

// autoload.phpの末尾にてinclude_path追加設定
set_include_path(get_include_path()
  .PATH_SEPARATOR.__DIR__.'/../vendor/phpunit'
  .PATH_SEPARATOR.__DIR__.'/../vendor/phpunit/Vendor/php-code-coverage'
  .PATH_SEPARATOR.__DIR__.'/../vendor/phpunit/Vendor/php-file-iterator'
  .PATH_SEPARATOR.__DIR__.'/../vendor/phpunit/Vendor/php-text-template'
  .PATH_SEPARATOR.__DIR__.'/../vendor/phpunit/Vendor/php-timer'
  .PATH_SEPARATOR.__DIR__.'/../vendor/phpunit/Vendor/php-token-stream'
  .PATH_SEPARATOR.__DIR__.'/../vendor/phpunit/Vendor/phpunit-mock-objects'
);

app/AppKernel.phpのregisterBundles()を編集する

        if (in_array($this->getEnvironment(), array('test'))) {
            $bundles[] = new Behat\BehatBundle\BehatBundle();
            $bundles[] = new Behat\MinkBundle\MinkBundle();
        }

app/config/config_test.ymlでテスト用の設定を追加

framework:
    test: ~

behat: ~

Behatコマンドの実行。コマンド名が「behat」のみに変わっていることに注意する。

Linux/Macの場合

$ php app/console --env=test behat --init Acme\\TestBundle

Windowsの場合

C:\xampp\htdocs\Symfony>php app/console --env=test behat --init "Acme\DemoBundle"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment