有人可以提供一个使用TDD表示法在Symfony2中进行开发的标准示例吗?或者分享有关TDD Symfony2开发的有趣材料的链接(官方文档除外))?
P.S。是否有人为MVC模式的控制器部分编写单元测试?
答案 0 :(得分:10)
我刚为silex做了这个,这是一个基于Symfony2的微框架。据我所知,它非常相似。我推荐它作为Symfony2世界的入门书。
我还使用TDD来创建这个应用程序,所以我做的是:
示例测试用例(在tests/ExampleTestCase.php
中)如下所示:
<?php
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 () {
return new ArraySessionStorage();
});
}
/**
* Test / (home)
*
* @return void
*/
public function testHome()
{
$client = $this->createClient();
$crawler = $client->request('GET', '/');
$this->assertTrue($client->getResponse()->isOk());
}
}
我的bootstrap.php
:
<?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;
我的web/index.php
:
<?php
$app = require './../bootstrap.php';
$app->run();