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.distcli-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/Servicedirectory.In
tests/Setup/Entitydirectory, you will find entities that are going to be created during the tests.In
tests/Setup/Factorydirectory, you will findzendstruck/foundrymodel factories describing the related entities (one factory per entity).In
tests/Setup/Storydirectory, you shall definitely look at the way entities are created and persisted using services from VoltelExtraFoundryBundle.Note
Inspect classes in
tests/Setup/Storydirectory to see examples of suggestedVoltel\ExtraFoundryBundle\Service\FixtureEntity\EntityProxyPersistServiceusage.In
tests/Setup/Servicedirectory, you will find a faux service that is used inafterPersistcallback inProductFactoryclass. It is needed in order to to change theslugproperty onProductentity and assert during the test that theafterPersistcallback was indeed invoked.The
tests/Setup/Kerneldirectory contains the only file withVoltel\ExtraFoundryBundle\Tests\Setup\Kernel\VoltelExtraFoundryTestingKernelclass where all services used during the tests are configured, including the services provided by VoltelExtraFoundryBundle and Doctrine.In
tests/Setup/MySqlDumpdirectory,mysql_dump_for_tests.sqlfile contains a MySQL dump that is asserted during the tests to be properly loaded/imported into the database.File
bootstrap.phpwas modified to retrieve the value ofDATABASE_URLfromphpunit.xml.distand set it in the super global$_ENVarray 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\VoltelExtraFoundryTestingKernelclass.// 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_URLdefinition inphpunit.xml.distto reflect your MySQL test user’s credentials and test database/schema name.Note
The configuration in
phpunit.xml.distalso 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\HelperSetfor 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