【问题标题】:PHPUnit throws error message after successful completion of tests using Symfony2使用 Symfony2 成功完成测试后 PHPUnit 抛出错误消息
【发布时间】:2012-06-28 15:52:01
【问题描述】:

我们刚刚开始对我们的 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

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

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

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

更新

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

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

【问题讨论】:

    标签: php symfony continuous-integration jenkins phpunit


    【解决方案1】:

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

    在您的 phpunit.xml.dist 中尝试替换:

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

    与:

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

    我认为第一条规则匹配两条路径。

    【讨论】:

      【解决方案2】:

      我们的一位开发人员(经过多次咒骂和诅咒)解决了这个问题,错误在于 FOS\FacebookBundle\Facebook\FacebookSessionPersistence,确切地说是构造函数启动了自己的会话:

      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);
      }
      

      所以我们只是编写了自己的 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);
      
          }
      }
      

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

      # 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   
      

      希望对后代有所帮助...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-06-18
        • 2015-06-08
        • 1970-01-01
        • 1970-01-01
        • 2014-04-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多