Attachment: Does anyone write unit tests for the controller part of the MVC pattern?
I also used TDD to create this application, so what I did was:
> I wrote the first test to verify the route/action
> Then I implemented routing in my bootstrap
> Then I added assertions in my test, for example, what should be displayed
> I implemented this in my code, and so on
The example test case (in tests/ExampleTestCase.php) looks like this:
use Silex\WebTestCase;
use Symfony\Component\HttpFoundation\SessionStorage\ArraySessionStorage;
class ExampleTestCase extends WebTestCase
{
/* *
* Necessary to make our application testable.
*
* @return Silex\Application
*/
public function createApplication()
{
return require __DIR__.'/../bootstrap.php';
}
/**
* Override NativeSessionStorage
*
* @ return void
*/
public function setUp()
{
parent::setUp();
$this->app['session.storag e'] = $this->app->share(function () {
return new ArraySessionStorage();
});
}
/**
* Test / (home)
*
* @return void
*/
public function testHome()
{
$client = $this->createClient();
$crawler = $client->request('GET','/');
$this->assertTrue($client->getResponse( )->isOk());
}
}
My bootstrap.php:
require_once __DIR__.'/vendor/silex.phar';
$app = new Silex\Application();
// load session extensions
$ app->register(new Silex\Extension\SessionExtension());
$app->get('/home', function() use ($app) {
return "Hello World";
});
return $app;
My website/index.php:
$app = require'./../bootstrap.php';
$app->run();
Can anyone provide A standard example of developing in Symfony2 using TDD notation? Or share links to interesting materials about TDD Symfony2 development (official documentation:))?
Attachment: Does anyone write unit tests for the controller part of the MVC pattern?
I just made this for silex, which is a micro-framework based on Symfony2. As far as I know, it is very similar. I recommend it as Symfony2 World
I also used TDD to create this application, so what I did was:
>I wrote the first test to verify the route/action
> Then I implemented routing in my bootstrap
> Then I added assertions in my test, for example, what should be displayed
> I implemented this in my code, etc. Etc.
The example test case (in tests/ExampleTestCase.php) is as follows:
use Silex\WebTestCase;
use Symfony\Component\HttpFoundation\SessionStorage\ArraySessionStorage;
class ExampleTestCase extends WebTestCase
{
/**
* Necessary to make our application testable .
*
* @return Silex\Application
*/
public function createApplication()
{
return require __DIR__.'/../bootstrap .php';
}
/**
* Override NativeSessionStorage
*
* @return void
*/
public function setUp()
{
parent::setUp();
$this->app['session.storage'] = $this->app->share(function () {
re turn new ArraySessionStorage();
});
}
/**
* Test / (home)
*
* @ return void
*/
public function testHome()
{
$client = $this->createClient();
$crawler = $client->request( 'GET','/');
$this->assertTrue($client->getResponse()->isOk());
}
}
My bootstrap.php:
require_once __DIR__.'/vendor/silex.phar';
$app = new Silex\Application();
// load session extensions
$app->register(new Silex\Extension\SessionExtension());
$app->get('/home', function() use ($app) {
return "Hello World";
});
return $app;
My website/index.php:
$app = require'./../bootstrap.php';
$app->run();