Ok,
I've managed to make it work. I had several configuration issues.
The first one in my app/phpunit_boostrap.php
, I've added:
<?php
$_SERVER['env'] = 'test';
...
Then in my web/index.php
I've added:
// Return the kernel instead to run it if we are unit testing
if ('test' == $app['mode']) {
return $app;
}
$app->run();
Then in my app/application.php
, I've added:
...
// Set dev mode for unit testing
if (isset($_SERVER['env']) && 'test' === $_SERVER['env']) {
$app['mode'] = 'test';
}
...
I've noticed that I was not using the correct WebTestCase
, Silex
has its own where you need to create the application (set the kernel):
<?php
namespace Acme\User\Tests\Controller;
// Notice the Silex class for the WebTestCase
use Silex\WebTestCase;
class UserControllerTest extends WebTestCase
{
protected $headers;
public function createApplication()
{
// index.php should return the $app instead to run() it
return require __DIR__ . '/../../../../../web/index.php';
}
protected function setUp()
{
// Don't forget to call the parent setup that is setting the kernel
parent::setUp();
$this->headers = array(
'CONTENT_TYPE' => 'application/json',
);
}
public function testAuthUser()
{
// Create a client this way
$client = $this->createClient();
$client->request('POST', ...);
Everything is working nicely now. Also I've create my own WebTestCase
class extending the one from Silex
so that I don't have to setup the application all the time.
I hope this will help some of you as I didn't find any good help about unit testing with Silex.
Cheers, Maxime