A Symfony bundle to simplify functional testing with the help of AliceBundle.
Table of contents:
- Download the Bundle
$ composer require --dev i22/functional-test-bundle
- Enable the Bundle
With Symfony 3.x adding the Bundle to app/AppKernel.php
<?php
class AppKernel extends Kernel
{
public function registerBundles()
{
// ...
if (in_array($this->getEnvironment(), array('dev', 'test'), true)) {
// ...
if ('test' === $this->getEnvironment()) {
$bundles[] = new I22\FunctionalTestBundle\I22FunctionalTestBundle();
}
}
return $bundles;
}
// ...
}
With Symfony 4.x adding the Bundle to config/bundles.php
(Should be done with auto-configuration, but activated for all environments)
<?php
return [
// ...
I22\FunctionalTestBundle\I22FunctionalTestBundle::class => ['test' => true],
];
Instead of setting up the database with one big fixture file for all tests you can define seperate small fixture-sets customized for your test case.
Setup your Test Class to extend from WebTestCase
<?php
use I22\FunctionalTestBundle\Test\WebTestCase;
class MyControllerTest extends WebTestCase
{
}
In order to load your customized fixtures automatically place a fixture folder with your fixtures beneath your test file
.
└── tests/
├── ...
└── Controller/
└── MyControllerTest/
├── fixtures/
├ ├── users.yaml
├ ├── ...
└── MyControllerTest.php
If you want do define from where to load your fixtures, you can override the method getFixtureFilePaths()
<?php
use I22\FunctionalTestBundle\Test\WebTestCase;
class MyControllerTest extends WebTestCase
{
protected function getFixtureFilePaths() : array
{
return [
__DIR__.'/../../global-fixtures/users.yaml',
];
}
}
in case u need to authorize a user for your functional test, you can use the UserAuthorizationTrait to login the user.
<?php
use I22\FunctionalTestBundle\Security\Authorization\UserAuthorizationTrait;
use I22\FunctionalTestBundle\Test\WebTestCase;
class MyControllerTest extends WebTestCase
{
use UserAuthorizationTrait;
public function setUp()
{
parent::setUp();
$user = $this->getDoctrine()->getRepository('App:User')->findOneBy(['email' => '[email protected]']);
$this->login($user);
}
}
the login method assumes that your firewall is named 'default'. in other cases use the second argument of the login method to specify how your firewall is named.
the bundle autoconfigures a FakeTranslator in test environment as the @default.translator service, so that instead of translating your translation keys, the translator will respond the original key.
for testing purpose it is usefull to test against the key instead of changing translations.
if you want to disable this feature, change the default configuration as follows:
#config/packages/test/i22_functional_test.yaml
i22_functional_test:
use_fake_translator: false
the bundle autoconfigures the symfony form with disabling the auto protection of forms instead of disabling the whole csrf protection services. so you are able to still use the csrf token manager to generate and use tokens for validation, but it simplifies the form post handling, because you dont need to add the _token value
if you want to disable this feature, change the default configuration as follows:
#config/packages/test/i22_functional_test.yaml
i22_functional_test:
disable_csrf_form_protection: false