The library contains improved classes of WebTestCase and Container for convenient testing.
Web Client is caching in WebTestCase. If you want to get a client you should use:
$client = static::getClient();
If you want to get a new client you should use:
$client = static::getClient(true);
Full example:
<?php
namespace Foo\BarBundle\Tests\Controller;
use Intaro\SymfonyTestingTools\WebTestCase;
class SomeControllerTest extends WebTestCase
{
public function testIndex()
{
$client = static::getClient(true);
//...
}
}
You can simply get EntityManager or Container in the current context:
<?php
namespace Foo\BarBundle\Tests\Controller;
use Intaro\SymfonyTestingTools\WebTestCase;
class SomeControllerTest extends WebTestCase
{
public function testIndex()
{
$client = static::getClient(true);
$em = static::getEntityManager();
$service = static::getContainer()->get('some_service');
//...
}
}
You can check response result with the following methods:
<?php
namespace Foo\BarBundle\Tests\Controller;
use Intaro\SymfonyTestingTools\WebTestCase;
class SomeControllerTest extends WebTestCase
{
public function testIndex()
{
$client = static::getClient();
$client->request('GET', '/foo/bar/index');
$this->assertResponseOk($client->getResponse(), 'Page opens');
$this->assertResponseRedirect($client->getResponse(), 'Page redirects to other page');
$this->assertResponseNotFound($client->getResponse(), 'Page not found');
$this->assertResponseForbidden($client->getResponse(), 'Page forbidden');
$this->assertResponseCode(201, $client->getResponse(), 'JSON returned', 'application/json');
}
}
You can add fixtures before test running:
<?php
namespace Foo\BarBundle\Tests\Controller;
use Foo\BarBundle\DataFixtures\ORM\Test\ActionRecordData;
use Intaro\SymfonyTestingTools\WebTestCase;
class SomeControllerTest extends WebTestCase
{
public static function setUpBeforeClass()
{
static::appendFixture(new ActionRecordData, [
'purge' => true,
]);
}
public function testIndex()
{
//...
}
}