Testing VoltelExtraFoundryBundle¶
If you want to see examples of using VoltelExtraFoundryBundle and/or test it,
you should look into the tests
directory.
Two files in the root of the bundle are also important for testing set-up:
phpunit.xml.dist
cli-config.php
Bundle’s tests
directory structure¶
your_project/
└─ tests/
├─ Service/
│ ├─ FixtureEntity/
│ └─ FixtureLoad/
├─ Setup/
│ ├─ Entity/
│ ├─ Factory/
│ ├─ Kernel/
│ ├─ MySqlDump/
│ ├─ Service/
│ └─ Story/
└─ bootstrap.php
All tests are located in
tests/Service
directory.In
tests/Setup/Entity
directory, you will find entities that are going to be created during the tests.In
tests/Setup/Factory
directory, you will findzendstruck/foundry
model factories describing the related entities (one factory per entity).In
tests/Setup/Story
directory, you shall definitely look at the way entities are created and persisted using services from VoltelExtraFoundryBundle.Note
Inspect classes in
tests/Setup/Story
directory to see examples of suggestedVoltel\ExtraFoundryBundle\Service\FixtureEntity\EntityProxyPersistService
usage.In
tests/Setup/Service
directory, you will find a faux service that is used inafterPersist
callback inProductFactory
class. It is needed in order to to change theslug
property onProduct
entity and assert during the test that theafterPersist
callback was indeed invoked.The
tests/Setup/Kernel
directory contains the only file withVoltel\ExtraFoundryBundle\Tests\Setup\Kernel\VoltelExtraFoundryTestingKernel
class where all services used during the tests are configured, including the services provided by VoltelExtraFoundryBundle and Doctrine.In
tests/Setup/MySqlDump
directory,mysql_dump_for_tests.sql
file contains a MySQL dump that is asserted during the tests to be properly loaded/imported into the database.File
bootstrap.php
was modified to retrieve the value ofDATABASE_URL
fromphpunit.xml.dist
and set it in the super global$_ENV
array to be later used when running Doctrine CLI commands (read below).
How to set up bundle tests¶
Overview¶
To set up testing with PhpUnit using a MySQL test database, several steps need to be done:
Configure the kernel class that is used by the testing suite;
Create a test MySQL database (e.g. “voltel_extra_foundry_test”);
Step 1: Configure the kernel class¶
The testing kernel is configured in
Voltel\ExtraFoundryBundle\Tests\Setup\Kernel\VoltelExtraFoundryTestingKernel
class.// in VoltelExtraFoundryTestingKernel.php class VoltelExtraFoundryTestingKernel extends Kernel { // ... public function registerContainerConfiguration(LoaderInterface $loader) { $loader->load(function (ContainerBuilder $container) use ($loader) { // Services that are used in tests // ... // Configure Doctrine $container->loadFromExtension('doctrine', [ 'dbal' => [ 'default_connection' => 'default', 'connections' => [ 'default' => [ 'url' => $_ENV['DATABASE_URL'], 'logging' => false, 'override_url' => true, ] ], ], 'orm' => [/* ... */] ]); }); } }//end of classThe connection URL in the code snippet above depends on the environmental variable DATABASE_URL which must be configured in
phpunit.xml.dist
:<!-- in phpunit.xml.dist --> <php> <!-- ... --> <env name="DATABASE_URL" value="mysql://testuser:password@127.0.0.1:3306/voltel_extra_foundry_test?serverVersion=5.7" /> <env name="KERNEL_CLASS" value="Voltel\ExtraFoundryBundle\Tests\Setup\Kernel\VoltelExtraFoundryTestingKernel" /> </php>Important
Change
DATABASE_URL
definition inphpunit.xml.dist
to reflect your MySQL test user’s credentials and test database/schema name.Note
The configuration in
phpunit.xml.dist
also contains a definition for another environmental variable,KERNEL_CLASS
, which is internally used bySymfony\Bundle\FrameworkBundle\Test\KernelTestCase
.
Step 2: Create a test MySQL database¶
In MySQL, create a new database, e.g. “voltel_extra_foundry_test”. Configure your test user to have appropriate privileges for this database.
> mysql --user=root --password
mysql> CREATE USER IF NOT EXISTS 'testuser'@'localhost' IDENTIFIED BY 'password';
mysql> CREATE DATABASE IF NOT EXISTS voltel_extra_foundry_test;
mysql> GRANT ALL ON voltel_extra_foundry_test.* TO 'testuser'@'localhost';
mysql> quit;
Note
Database name, host URL, username, user password must match those
configured in phpunit.xml.dist
for DATABASE_URL
environmental variable (see above).
Step 3: Set up MySQL schema¶
The cli-config.php
in the root of the project is required for Doctrine bundle CLI tool. The file is quite short; it has only a few things to do:
boot our custom kernel (
Voltel\ExtraFoundryBundle\Tests\Setup\Kernel\VoltelExtraFoundryTestingKernel
);retrieve entity manager from the kernel;
return an instance of
Symfony\Component\Console\Helper\HelperSet
for the provided entity manager.
With Doctrine CLI configured, run in the terminal:
$ vendor/bin/doctrine orm:schema-tool:create
or, in Windows command prompt:
> "vendor/bin/doctrine" orm:schema-tool:create
Run bundle tests¶
Run the tests with this command:
> "vendor/bin/simple-phpunit"
Note
The following exact command was run under Windows
to obtain the MySQL test database state and produce the dump
that is located in tests/Setup/MySqlDump/mysql_dump_for_tests.sql
.
> "vendor/bin/simple-phpunit" tests/Service/FixtureEntity/EntityProxyPersistServiceTest.php --filter=testStories