【问题标题】:PHPUnit fails when using Silex SessionServiceProvider使用 Silex SessionServiceProvider 时 PHPUnit 失败
【发布时间】:2012-11-15 04:54:48
【问题描述】:

我正在尝试为我的 Silex 应用程序创建一个单元测试。单元测试类看起来像这样:

class PageTest extends WebTestCase {

    public function createApplication() {
        $app = require __DIR__ . '/../../app/app.php';
        $app['debug'] = true;

        $app['session.storage'] = $app->share(function() {
            return new MockArraySessionStorage();
        });

        $app['session.test'] = true;

        unset($app['exception_handler']);
        return $app;
    }

    public function testIndex() {
        $client = $this->createClient();
        $client->request('GET', '/');
        $this->assertTrue($client->getResponse()->isOk());
    }

}

它尝试请求的 silex 路由看起来像这样:

$app->get('/', function() use($app) {
    $user     = $app['session']->get('loginUser');

    return $app['twig']->render('views/index.twig', array(
        'user'           => $user,
    ));
});

这会导致 RuntimeException: Failed to start the session because headers have been sent. in \Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage.php:142 带有一个回溯,其中包含 $app['session']->get.

的路线中的行

看起来在 NativeSessionStorage 中会话开始尝试之前发生的输出实际上是 PHPUnit 输出信息,因为这是我在错误消息之前得到的唯一输出:

PHPUnit 3.7.8 by Sebastian Bergmann.

Configuration read from (PATH)\phpunit.xml

E.......

我有点困惑,因为 phpunit 的这个错误输出发生在实际测试方法执行之前的输出中。我没有运行任何其他测试方法,所以它必须来自这个错误。

我应该如何让 PHPUnit 在使用会话变量的 silex 路由上工作?

【问题讨论】:

  • 您是否直接在$app 的某个地方访问$app['session'],就像未封装在侦听器中一样?完整的最小可重现 app.php 和 test.php 会很好。

标签: session header phpunit silex


【解决方案1】:

在下方评论后编辑

好的,我也遇到了同样的问题,在浏览网页一个小时后,我设法通过了测试。

在 Silex 2.0-dev 上,从 WebTestCase 类调用 $app['session.test'] = true 根本不起作用,它需要在引导程序中进行。

实现方法很多,这里有两种:

1/ 与phpunit.xml.dist

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
     backupStaticAttributes="false"
     colors="true"
     convertErrorsToExceptions="true"
     convertNoticesToExceptions="true"
     convertWarningsToExceptions="true"
     processIsolation="false"
     stopOnFailure="false"
     syntaxCheck="false"
     bootstrap="./app.php"
>
    <php>
        <env name="TEST" value="true" />          //-> This is the trick
    </php>
    <testsuites>
        <testsuite name="Your app Test Suite">
            <directory>./tests/</directory>
        </testsuite>
    </testsuites>
</phpunit>

然后在引导中

$app = new \Silex\Application();

...

$app->register(new \Silex\Provider\SessionServiceProvider(), [
    'session.test' => false !== getenv('TEST')
]);

...

return $app;


2/ 通过扩展Silex\Application,您可以将环境传递给构造函数

namespace Your\Namespace;

class YourApp extends \Silex\Application
{
    public function __construct($env, array $params = array())
    {
        $this['env'] = $env;

        parent::__construct($params);
    }
}

然后在你的引导程序中

$env = // Your logic ...

$app = new \Your\Namespace\YourApp($env);

...

$app->register(new \Silex\Provider\SessionServiceProvider(), [
    'session.test' => 'test' === $app['env'],
]);

...

return $app;

希望有帮助,干杯!

【讨论】:

  • 感谢您的解决方案,我选择了第一个。但我会补充一些细节。 env 参数需要放入 php 标签中。 &lt;php&gt;&lt;env name="TEST" value="true" /&gt;&lt;/php&gt;link。您应该使用 getenv('TEST')̀ 来检索测试值(不推荐使用 $_ENV)。不过很好的解决方案
  • 你拯救了我的一天!谢谢!
【解决方案2】:

好的,我找到了答案。这似乎是 Silex 中的一个错误。

在注册标准 FormServiceProvider 之前注册树枝扩展时出现问题。这不是由 twig 扩展内部的任何东西引起的,如果我将整个扩展类剥离为空方法,仍然会发生错误。

因此,在 Silex 应用程序对象中注册 twig 扩展应始终在注册提供程序之后完成,至少在 FormServiceProvider 之后(直到修复错误)。

【讨论】:

  • 您是否在the issue tracker 上创建了问题?否则开发人员不会知道这个错误。
猜你喜欢
  • 2012-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-21
  • 2017-12-22
  • 2011-07-06
  • 2013-07-08
  • 2011-05-09
相关资源
最近更新 更多