依存関係
最初に行うことは、バンドル用の新しいリポジトリを作成し、それに関連するファイルを追加することです。 もちろん、バンドルには外部依存関係があります。 それらを解決するには、 composerを使用します。 私はそれをグローバルにインストールしていますが、これは正しいです。 始めましょう:
$ composer init #
プロジェクトを初期化しました。
composer.json
ファイルはルートディレクトリに作成されました。 私たちにとって興味深いセクションがいくつかあります:
require
、
require-dev
、
suggest
。 それぞれを見ていきましょう。
-
require
は、それなしではプロジェクトが機能しないものです -
require-dev
は、開発とテストに使用するものrequire-dev
-
suggest
-ここでは、ORMだけでなくODMでもバンドルが機能すると言うことができます
コマンドで必要なコンポーネントをインストールします
$ composer install
さて、依存関係が整理されています。
コア
テストサービスと機能テストには、ほぼフル機能のアプリケーションが必要です。
クラスのスタートアップ
テスト用のディレクトリに
bootstrap.php
を作成し、phpunitがそれを使用する必要があることを示す必要があります。
<?php use Doctrine\Common\Annotations\AnnotationRegistry; // Doctrine use Composer\Autoload\ClassLoader; /** * @var ClassLoader $loader */ $loader = require __DIR__.'/../vendor/autoload.php'; AnnotationRegistry::registerLoader(array($loader, 'loadClass')); // Doctrine return $loader;
もう一度、AnnotationRegistry::registerLoader(array($loader, 'loadClass'));
の行に注意を向けたいと思いますAnnotationRegistry::registerLoader(array($loader, 'loadClass'));
。 私のバンドルはDoctrineとアノテーションを最大限に活用しており、「アノテーションをロードできません」というテキストで繰り返し例外を受け取ったとき、それは私にとって大きな驚きでした。
phpunit.xml.dist
を開き、
phpunit.xml.dist
を指定します
<phpunit bootstrap="./Tests/bootstrap.php">
AppKernelとコンソール
次のステップは、アプリケーションを初期化することです。 テストアプリケーションに関連するファイルが置かれる
Tests/fixtures
フォルダーを作成しました。 symfonyアプリケーションのキークラスは
AppKernel
で、
Tests/fixtures/app
フォルダーに作成します
<?php use Symfony\Component\HttpKernel\Kernel; use Symfony\Component\Config\Loader\LoaderInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\HttpKernelInterface; class AppKernel extends Kernel { /** * @return array */ public function registerBundles() { $bundles = array( // ); return $bundles; } /** * @param \Symfony\Component\Config\Loader\LoaderInterface $loader */ public function registerContainerConfiguration(LoaderInterface $loader) { $loader->load(__DIR__ . '/config/config.yml'); } }
次に、コンテナを設定する必要があります。そのために、ファイル
Tests/fixtures/app/config/config.yml
を作成します。
コンソールが必要な場合は、次の内容の
Tests/fixtures/app/console
ファイルを作成するだけ
Tests/fixtures/app/console
。
#!/usr/bin/env php <?php // if you don't want to setup permissions the proper way, just uncomment the following PHP line // read http://symfony.com/doc/current/book/installation.html#configuration-and-setup for more information //umask(0000); set_time_limit(0); require_once __DIR__.'/../../bootstrap.php'; require_once __DIR__.'/AppKernel.php'; use Symfony\Bundle\FrameworkBundle\Console\Application; use Symfony\Component\Console\Input\ArgvInput; use Symfony\Component\Debug\Debug; $input = new ArgvInput(); $env = $input->getParameterOption(array('--env', '-e'), getenv('SYMFONY_ENV') ?: 'dev'); $debug = getenv('SYMFONY_DEBUG') !== '0' && !$input->hasParameterOption(array('--no-debug', '')) && $env !== 'prod'; if ($debug) { Debug::enable(); } $kernel = new AppKernel($env, $debug); $application = new Application($kernel); $application->run($input);
アプリケーションのコアがどこにあるかを示し、
phpunit.xml.dist
次のディレクティブを追加します。
<php> <server name="KERNEL_DIR" value="Tests/Fixtures/app/" /> </php>
これらの簡単な操作の後、テストアプリケーションを取得し、
Symfony\Bundle\FrameworkBundle\Test\WebTestCase
を使用する方法を
Symfony\Bundle\FrameworkBundle\Test\WebTestCase
しました。これにより、Symfony 2アプリケーションのコンテキストでサービスなどをテストできます。
ここで完全なコードを読む
PS:次の記事では、バンドル内のDoctrineのテストの複雑さを分解しようとします。