i22 Functional Test Bundle
A Symfony bundle to simplify functional testing with the help of
AliceBundle., (*1)
Table of contents:
- Installation
- Basic usage
- Seperate fixture files
- Authorize User
- Fake Translator
- Disabling Csrf Form Protection, (*2)
Installation
-
Download the Bundle, (*3)
$ composer require --dev i22/functional-test-bundle
-
Enable the Bundle, (*4)
With Symfony 3.x adding the Bundle to app/AppKernel.php
, (*5)
<?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), (*6)
<?php
return [
// ...
I22\FunctionalTestBundle\I22FunctionalTestBundle::class => ['test' => true],
];
Basic usage
Loading fixtures for each test class
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., (*7)
Setup your Test Class to extend from WebTestCase, (*8)
```php
<?php, (*9)
use I22\FunctionalTestBundle\Test\WebTestCase;, (*10)
class MyControllerTest extends WebTestCase
{, (*11)
}
```, (*12)
In order to load your customized fixtures automatically place a fixture folder with your fixtures beneath
your test file, (*13)
.
โโโ tests/
โโโ ...
โโโ Controller/
โโโ MyControllerTest/
โโโ fixtures/
โ โโโ users.yaml
โ โโโ ...
โโโ MyControllerTest.php
, (*14)
If you want do define from where to load your fixtures, you can override the method getFixtureFilePaths()
, (*15)
```php
<?php, (*16)
use I22\FunctionalTestBundle\Test\WebTestCase;
class MyControllerTest extends WebTestCase
{
protected function getFixtureFilePaths() : array
{
return [
__DIR__.'/../../global-fixtures/users.yaml',
];
}
}
```, (*17)
Authorize User
in case u need to authorize a user for your functional test, you can use the UserAuthorizationTrait
to login the user., (*18)
```php
<?php, (*19)
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' => 'test@i22.de']);
$this->login($user);
}
}
```, (*20)
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., (*21)
No Translator - working with translation keys
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., (*22)
for testing purpose it is usefull to test against the key instead of changing translations., (*23)
if you want to disable this feature, change the default configuration as follows:, (*24)
```yaml
#config/packages/test/i22_functional_test.yaml, (*25)
i22_functional_test:
use_fake_translator: false
```, (*26)
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, (*27)
if you want to disable this feature, change the default configuration as follows:, (*28)
```yaml
#config/packages/test/i22_functional_test.yaml, (*29)
i22_functional_test:
disable_csrf_form_protection: false
```, (*30)