【问题标题】:symfony2 tdd developingsymfony2 tdd 开发中
【发布时间】:2011-05-09 02:27:57
【问题描述】:

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

附:有人为 MVC 模式的控制器部分编写单元测试吗?

【问题讨论】:

    标签: tdd phpunit symfony


    【解决方案1】:

    我只是为silex 做的,这是一个基于 Symfony2 的微框架。据我了解,非常相似。我推荐它作为 Symfony2 世界的入门。

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

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

    一个示例测试用例(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();
    

    【讨论】:

      猜你喜欢
      • 2011-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-10
      相关资源
      最近更新 更多