Good,
I managed to get it to work. I had a few setup issues.
First in my app/phpunit_boostrap.php , I added:
<?php $_SERVER['env'] = 'test'; ...
Then in my web/index.php I 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 added:
... // Set dev mode for unit testing if (isset($_SERVER['env']) && 'test' === $_SERVER['env']) { $app['mode'] = 'test'; } ...
I noticed that I did not use the correct WebTestCase , Silex has its own where you need to create the application (install 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', ...);
Now all is well. I also created my own WebTestCase class that extends one of Silex , so I donβt have to constantly install the application.
Hope this helps some of you as I did not find any good help in testing modules with Silex.
Cheers, Maxime
source share