symfony2 tdd开发

Ale*_*kin 5 tdd phpunit symfony

有谁能提供一个使用TDD表示法在Symfony2中进行开发的标准示例?或者分享有关TDD Symfony2开发的有趣材料的链接(官方文档:))?

PS是否有人为MVC模式的控制器部分编写单元测试?

Til*_*ill 10

我刚刚为silex做了这个,它是一个基于Symfony2的微框架.据我所知,它非常相似.我推荐它作为Symfony2世界的入门书.

我还使用TDD来创建这个应用程序,所以我做的是:

  1. 我写了第一个测试来验证路线/动作
  2. 然后我在我的bootstrap中实现了路由
  3. 然后我在我的测试中添加了断言,例如,应该显示什么
  4. 我在我的代码中实现了这一点,等等

示例testcase(in 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());
    }
}
Run Code Online (Sandbox Code Playgroud)

我的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;
Run Code Online (Sandbox Code Playgroud)

我的web/index.php:

<?php
$app = require './../bootstrap.php';
$app->run();
Run Code Online (Sandbox Code Playgroud)