PHPUnit在使用Symfony2成功完成测试后抛出错误消息

时间:2022-10-15 19:46:30

We just started to unit-test our application for our CI; the first tests we have written run through, but PHPUnit always throws a error message at the end:

我们刚刚开始对我们的CI应用程序进行单元测试;我们编写的第一个测试运行了一遍,但是PHPUnit总是在最后抛出一个错误消息:

user@server:~/public$ phpunit -c app/ --strict src/Acme/Bundle/SomeBundle/Tests/Controller/SomeControllerTest.php 
PHPUnit 3.6.11 by Sebastian Bergmann.

Configuration read from /var/www/user/htdocs/public/app/phpunit.xml.dist

..

Time: 5 seconds, Memory: 130.00Mb

OO (2 tests, 2 assertions)
PHP Fatal error:  Exception thrown without a stack frame in Unknown on line 0
PHP Stack trace:
PHP   1. {main}() /usr/bin/phpunit:0
PHP   2. PHPUnit_TextUI_Command::main() /usr/bin/phpunit:46
PHP   3. PHPUnit_TextUI_Command->run() /usr/share/php/PHPUnit/TextUI/Command.php:133

Fatal error: Exception thrown without a stack frame in Unknown on line 0

Call Stack:
    0.0001     629992   1. {main}() /usr/bin/phpunit:0
    0.0023    1235832   2. PHPUnit_TextUI_Command::main() /usr/bin/phpunit:46
    0.0023    1236848   3. PHPUnit_TextUI_Command->run() /usr/share/php/PHPUnit/TextUI/Command.php:133

The only hint so far is that it only occurs if one (or more) of the tests uses [see below] to make a Request on the Page, so its is probably in some way session related.

到目前为止,唯一的提示是,只有当一个(或多个)测试使用[见下文]在页面上发出请求时,才会发生这种情况,因此,它可能在某种程度上与会话相关。

$client = static::createClient();
$container = $client->getContainer();
$uri = $container->getParameter('url_frontend') . '/';
$client->request('GET', $uri);

Here is a link to our phpunit.xml.dist

这是我们的phpunit.xml.dist的链接

Update:

更新:

I managed to get a little more information, about the error itself:

我设法获得了更多关于错误本身的信息:

1) Acme\Bundle\SomeBundle\Tests\Controller\SomeControllerTest::testIndexResponseStatusCode
RuntimeException: PHP Fatal error:  Uncaught exception 'ErrorException' with message 'Warning: Unknown: Failed to write session data (user). Please verify that the current setting of session.save_path is correct (/tmp/sessions) in Unknown line 0' in /var/www/user/htdocs/public/vendor/symfony/src/Symfony/Component/HttpKernel/Debug/ErrorHandler.php:67
Stack trace:
#0 [internal function]: Symfony\Component\HttpKernel\Debug\ErrorHandler->handle(2, 'Unknown: Failed...', 'Unknown', 0, Array)
#1 {main}
  thrown in /var/www/user/htdocs/public/vendor/symfony/src/Symfony/Component/HttpKernel/Debug/ErrorHandler.php on line 67

2 个解决方案

#1


0  

I'm just guessing but I think the exception is thrown because phpunit tries to run second time (exception is throw at the beginning of run() method).

我只是猜测,但是我认为异常被抛出,因为phpunit试图运行第二次(在run()方法的开头抛出异常)。

In your phpunit.xml.dist try replacing:

在你phpunit.xml。dist试试更换:

<testsuites>
    <testsuite name="Project Test Suite">
        <directory>../src/*/*Bundle/Tests</directory>
        <directory>../src/*/Bundle/*Bundle/Tests</directory>
    </testsuite>
</testsuites>

with:

:

<testsuites>
    <testsuite name="Project Test Suite">
        <directory>../src/*/*Bundle/Tests</directory>
    </testsuite>
</testsuites>

I think the first rule matches both paths.

我认为第一条规则同时适用于两条路径。

#2


0  

One of our Developers (after much swearing and cursing) solved the Problem, at fault was the FOS\FacebookBundle\Facebook\FacebookSessionPersistence to be precise the fact that the constructor starts its own session:

我们的一个开发人员(在多次咒骂和咒骂之后)解决了这个问题,问题出在FOS\FacebookBundle\Facebook\ facebooksessionpersistent上,确切地说,构造函数开始了它自己的会话:

public function __construct($config, Session $session, $prefix = self::PREFIX)
{
    $this->session = $session;
    $this->prefix  = $prefix;
    $this->session->start(); // he starts a session

    parent::__construct($config);
}

So we just wrote our own FacebookSessionPersistence Class:

我们刚写了自己的FacebookSessionPersistence类:

namespace Acme\Bundle\UserBundle\Facebook;

use FOS\FacebookBundle\Facebook\FacebookSessionPersistence as BaseFacebookSessionPersistence;
use Symfony\Component\HttpFoundation\Session;

/**
 * Quick (and dirty) fix for running tests using FosFacebookBundle.
 * only use this class, while testing the application with phpunit
 *
 * @see https://github.com/FriendsOfSymfony/FOSFacebookBundle/issues/79
 */
class FacebookSessionPersistence extends BaseFacebookSessionPersistence
{
    public function __construct($config, Session $session, $prefix = self::PREFIX)
    {
        $this->session = $session;
        $this->prefix  = $prefix;

        // don't start session or call parent constructor
        //$this->session->start();

        //parent::__construct($config);

    }
}

And added it in the test config:

并将其添加到测试配置中:

# quick workaround as the fb class fails the test as it calls session->start in the construct   
# @see https://github.com/FriendsOfSymfony/FOSFacebookBundle/issues/79
fos_facebook:
    class:
        api: Acme\Bundle\UserBundle\Facebook\FacebookSessionPersistence   

Hope that will help future generations...

希望这能帮助我们的后代……

#1


0  

I'm just guessing but I think the exception is thrown because phpunit tries to run second time (exception is throw at the beginning of run() method).

我只是猜测,但是我认为异常被抛出,因为phpunit试图运行第二次(在run()方法的开头抛出异常)。

In your phpunit.xml.dist try replacing:

在你phpunit.xml。dist试试更换:

<testsuites>
    <testsuite name="Project Test Suite">
        <directory>../src/*/*Bundle/Tests</directory>
        <directory>../src/*/Bundle/*Bundle/Tests</directory>
    </testsuite>
</testsuites>

with:

:

<testsuites>
    <testsuite name="Project Test Suite">
        <directory>../src/*/*Bundle/Tests</directory>
    </testsuite>
</testsuites>

I think the first rule matches both paths.

我认为第一条规则同时适用于两条路径。

#2


0  

One of our Developers (after much swearing and cursing) solved the Problem, at fault was the FOS\FacebookBundle\Facebook\FacebookSessionPersistence to be precise the fact that the constructor starts its own session:

我们的一个开发人员(在多次咒骂和咒骂之后)解决了这个问题,问题出在FOS\FacebookBundle\Facebook\ facebooksessionpersistent上,确切地说,构造函数开始了它自己的会话:

public function __construct($config, Session $session, $prefix = self::PREFIX)
{
    $this->session = $session;
    $this->prefix  = $prefix;
    $this->session->start(); // he starts a session

    parent::__construct($config);
}

So we just wrote our own FacebookSessionPersistence Class:

我们刚写了自己的FacebookSessionPersistence类:

namespace Acme\Bundle\UserBundle\Facebook;

use FOS\FacebookBundle\Facebook\FacebookSessionPersistence as BaseFacebookSessionPersistence;
use Symfony\Component\HttpFoundation\Session;

/**
 * Quick (and dirty) fix for running tests using FosFacebookBundle.
 * only use this class, while testing the application with phpunit
 *
 * @see https://github.com/FriendsOfSymfony/FOSFacebookBundle/issues/79
 */
class FacebookSessionPersistence extends BaseFacebookSessionPersistence
{
    public function __construct($config, Session $session, $prefix = self::PREFIX)
    {
        $this->session = $session;
        $this->prefix  = $prefix;

        // don't start session or call parent constructor
        //$this->session->start();

        //parent::__construct($config);

    }
}

And added it in the test config:

并将其添加到测试配置中:

# quick workaround as the fb class fails the test as it calls session->start in the construct   
# @see https://github.com/FriendsOfSymfony/FOSFacebookBundle/issues/79
fos_facebook:
    class:
        api: Acme\Bundle\UserBundle\Facebook\FacebookSessionPersistence   

Hope that will help future generations...

希望这能帮助我们的后代……