This Bundle provides base classes for unit and integration tests in order to assist in setting test databases and data fixtures.
Install SeegnoTestBundle by running the command:
$ composer require --dev seegno/test-bundle
Enable the bundle in the kernel:
<?php
// app/AppKernel.php
public function registerBundles()
{
// ...
if (in_array($this->getEnvironment(), array('test'))) {
// ...
$bundles[] = new Seegno\TestBundle\SeegnoTestBundle();
}
}
Integration (functional)
Please add the following configuration in config_test.yml
:
seegno_test:
database:
driver: ORM # Types available: ORM (SQL) and ODM (MongoDB)
Warning!!
It's very important that you configure a different database for tests in your config_test.yml
file since all data is purged from the database in integration tests.
use Seegno\TestBundle\TestCase\BaseTestCase;
class SomeClassTest extends BaseTestCase
{
//...
}
Available features:
$this->getFaker(); // Get a faker instance.
$this->setReflectionProperty($class, $propertyName, $propertyValue); // Set a class property using reflection.
use Seegno\TestBundle\TestCase\IntegrationTestCase;
class SomeClassTest extends IntegrationTestCase
{
//...
}
Available features:
$this->getContainer(); // Get an instance of the dependency injection container.
$this->getSession(); // Get session.
$this->get('SERVICE'); // Get services.
$this->initializeDatabase(); // Initialize test database (SQL or MongoDB).
use Seegno\TestBundle\TestCase\WebTestCase;
class SomeClassTest extends WebTestCase
{
//...
}
Available features:
$this->authenticateUser($client, $user, $credentials, $roles, $firewall); // Authenticate a user.
$this->getClient(); // Get client that simulates a browser and makes requests to a Kernel object.
$this->assertResponseFields($response, $object, $groups); // Assert that object properties keys are in the response.
Before running the tests, make sure you have the test database updated.
php app/console doctrine:database:drop --env=test --force
php app/console doctrine:database:create --env=test
php app/console doctrine:schema:create --env=test
To run the tests on your local machine, just use the phpunit command:
phpunit