symfony - Functional tests inside a standalone Symfony2's bundle -
i need make functional tests directly in standalone bundle. don't want test controller, interaction between real services.
i know if there standard/best way that. did 1 way know if there better one.
here own solution (i summarize process testing in standalone bundle):
1. first, bundle has own composer.json
define dependencies:
{ "name": "my/own-bundle", "type": "symfony-bundle", "description": "symfony2 bundle provides ...", "keywords": ["my","own"], "license": "mit", "authors": [ { "name": "john doe", "email": "john.doe@omg.wtf" } ], "require": { "php": ">=5.3.2", "symfony/framework-bundle": ">=2.3" }, "require-dev": { "phpunit/phpunit": "3.7.*" }, "autoload": { "psr-0": { "my\\ownbundle": "" } }, "target-dir": "my/ownbundle", "minimum-stability": "dev" }
note use of dependency on symfony/framework-bundle
needed our tests on services. can lower dependencies in specifying own real dependencies on symfony core.
with file can process command (do it) build vendor directory of bundle:
$ composer update
2. then, set phpunit config file:
<!-- phpunit.xml.dist --> <?xml version="1.0" encoding="utf-8"?> <phpunit backupglobals="false" backupstaticattributes="false" colors="true" converterrorstoexceptions="true" convertnoticestoexceptions="true" convertwarningstoexceptions="true" processisolation="false" stoponfailure="false" syntaxcheck="false" bootstrap="tests/bootstrap.php" > <testsuites> <testsuite name="myownbundle test suite"> <directory>./tests/</directory> </testsuite> </testsuites> <filter> <whitelist> <directory>./</directory> <exclude> <directory>./resources</directory> <directory>./tests</directory> <directory>./vendor</directory> </exclude> </whitelist> </filter> </phpunit>
3. then, set php bootstrap autoload of class in test directory:
// tests/bootstrap.php $file = __dir__.'/../vendor/autoload.php'; if (!file_exists($file)) { $file = __dir__.'/../../../../../../vendor/autoload.php'; if (!file_exists($file)) throw new runtimeexception('install dependencies run test suite.'); } $autoload = require_once $file;
these steps standard test in standalone bundle.
4. now, want simulate application make functionnal tests on services:
i need kernel class:
// tests/appkernel.php (you can define in subdirectory /fixtures if prefer) use symfony\component\httpkernel\kernel; use symfony\component\config\loader\loaderinterface; class appkernel extends kernel { public function registerbundles() { $bundles = array(); if (in_array($this->getenvironment(), array('test'))) { $bundles[] = new symfony\bundle\frameworkbundle\frameworkbundle(); $bundles[] = new my\ownbundle\myownbundle(); } return $bundles; } public function registercontainerconfiguration(loaderinterface $loader) { $loader->load(__dir__.'/config.yml'); } }
and corresponding config.yml
:
# tests/config.yml framework: secret: test session: storage_id: session.storage.mock_file my_own: test: 2
here example mock session. don't forget specify correct framework configuration nodes if want have access services (if don't specify node session
, have no service session
instance).
5. finally, can retrieve services following in test classes:
// tests/functional/handling/handler.php namespace my\ownbundle\tests\functional\handling; use symfony\bundle\frameworkbundle\test\webtestcase; class handlertest extends webtestcase { private $handler; protected function setup() { require_once __dir__.'/../../appkernel.php'; $kernel = new \appkernel('test', true); $kernel->boot(); $container = $kernel->getcontainer(); $this->handler = $container->get('my_own.handling.handler'); } public function testhandle() { $this->assert($this->handler->handle()); } }
Comments
Post a Comment