個々のsymfony 2バンドルのテスト

短い背景から始めましょう。 私はホテルの部屋の予約を書く仕事があり、お気に入りのパカギストのすべてに登り、既製のソリューションを探しましたが、残念ながら何も見つかりませんでした。 まあ、私たちはそれをしなければなりません-それをします。 コードは記述され、アプリケーションの機能テストでカバーされます。 数週間後、書かれたバンドルをgithubに投稿することにしました。 しかし、私の前に疑問が生じました。別個のバンドルをテストするとき、アプリケーション自体はありません。 彼はグーグルを始め、再び価値のあるものを見つけませんでした。 一般に、私は少しずつ情報を収集する必要がありました。そして今、あなたと私の経験を共有したいと思います。

依存関係


最初に行うことは、バンドル用の新しいリポジトリを作成し、それに関連するファイルを追加することです。 もちろん、バンドルには外部依存関係があります。 それらを解決するには、 composerを使用します。 私はそれをグローバルにインストールしていますが、これは正しいです。 始めましょう:

$ composer init #


プロジェクトを初期化しました。 composer.jsonファイルはルートディレクトリに作成されました。 私たちにとって興味深いセクションがいくつかあります: requirerequire-devsuggest 。 それぞれを見ていきましょう。

コマンドで必要なコンポーネントをインストールします
$ 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のテストの複雑さを分解しようとします。

Source: https://habr.com/ru/post/J221259/


All Articles