Symfony 2 + Doctrine 2 + PHPUnit 3.5 : Serialization of closure exception

标签 symfony doctrine phpunit

我尝试在 Google 上查找有关此内容的信息,但一无所获。我有一个继承自 WebTestCase 的 TestCase 类,其中包含一些我想在所有单元/功能测试中使用的方法:

<?php

namespace Application\FaxServerBundle\Test;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Doctrine\Common\DataFixtures\Loader;
use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
use Doctrine\Common\DataFixtures\Purger\ORMPurger;

use Application\FaxServerBundle\DataFixtures\ORM\NetworkConfigurationData;

class TestCase extends WebTestCase
{
    protected $kernel;

    public function setUp()
    {
        parent::setUp();
    }

    public function getEm()
    {
        return $this->getService( 'doctrine.orm.entity_manager' );
    }

    public function getNetworkConfigurationRepository()
    {
        return $this->getEm()->getRepository( 'Application\FaxServerBundle\Entity\NetworkConfiguration' );
    }

    public function loadNetworkConfigurationFixtures()
    {
        $loader = new Loader();
        $loader->addFixture( new NetworkConfigurationData() );

        $this->loadFixtures( $loader );
    }

    public function loadFixtures( $loader )
    {
        $purger     = new ORMPurger();
        $executor   = new ORMExecutor( $this->getEm(), $purger );
        $executor->execute( $loader->getFixtures() );
    }

    protected function getService( $name, $kernel = null )
    {
        return $this->getBootedKernel()->getContainer()->get( $name );
    }

    protected function hasService( $name, $kernel = null )
    {

        return $this->getBootedKernel()->getContainer()->has( $name );
    }

    protected function getBootedKernel()
    {
        $this->kernel = $this->createKernel();

        if ( !$this->kernel->isBooted() ) 
        {
            $this->kernel->boot();
        }

        return $this->kernel;
    }

    public function generateUrl( $client, $route, $parameters = array() )
    {
        return $client->getContainer()->get( 'router' )->generate( $route, $parameters );
    }
}

然后,我的单元测试:

<?php

namespace Application\FaxServerBundle\Tests\Entity;

use Doctrine\ORM\AbstractQuery;

use Application\FaxServerBundle\Entity;
use Application\FaxServerBundle\Test\TestCase;

class NetworkConfigurationRepositoryTest extends TestCase
{
 public function setUp()
 {
  parent::setUp();

  $this->loadNetworkConfigurationFixtures();
 }

 public function testGetConfiguration()
 {
  $config = $this->getNetworkConfigurationRepository()->getConfigurationArray();

  $this->assertInternalType( 'array', $config );
  $this->assertEquals( 6, count( $config ) );
  $this->assertArrayHasKey( 'id', $config );
  $this->assertArrayHasKey( 'ip', $config );
  $this->assertArrayHasKey( 'gateway', $config );
  $this->assertArrayHasKey( 'subnetMask', $config );
  $this->assertArrayHasKey( 'primaryDns', $config );
  $this->assertArrayHasKey( 'secondaryDns', $config );
 }

 public function testGetConfigurationObject()
 {
  $config = $this->getNetworkConfigurationRepository()->getConfigurationObject();

  $this->assertInternalType( 'object', $config );
 }

 public function testGetConfigurationArray()
 {
  $config = $this->getNetworkConfigurationRepository()->getConfigurationArray();

  $this->assertInternalType( 'array', $config );
 }
}

它之前可以工作,但是,突然,在我更新我的供应商(包括 Doctrine )后,它开始抛出此异常:

3) Application\FaxServerBundle\Tests\Entity\NetworkConfigurationRepositoryTest::testGetConfigurationArray
RuntimeException: PHP Fatal error:  Uncaught exception 'PDOException' with message 'You cannot serialize or unserialize PDO instances' in -:32
Stack trace:
#0 [internal function]: PDO->__sleep()
#1 -(32): serialize(Array)
#2 -(113): __phpunit_run_isolated_test()
#3 {main}

Next exception 'Exception' with message 'Serialization of 'Closure' is not allowed' in -:0
Stack trace:
#0 -(0): serialize()
#1 -(113): __phpunit_run_isolated_test()
#2 {main}
  thrown in - on line 0

我发现问题出在夹具加载上。如果我删除加载灯具的代码,它就可以工作。

有谁知道我的代码可能有什么问题?这是加载装置的最佳方式吗?

谢谢!

最佳答案

技术上与您的问题无关。然而,我在使用 PHPUnit 时尝试解决“不允许‘闭包’序列化”问题非常困难,而这个问题是 Google 的最高结果。

问题源于 PHPUnit 序列化系统中的所有 $GLOBALS 以便在测试运行时对它们进行必要的备份。然后在测试完成后恢复它们。

但是,如果您的 GLOBAL 空间中有任何关闭,就会导致问题。有两种方法可以解决。

您可以通过使用注释完全禁用全局备份过程。

/**
 * @backupGlobals disabled
 */
class MyTest extends PHPUnit_Framework_TestCase
{
    // ...
}

或者,如果您知道哪个变量导致了问题(在 var_dump($GLOBALS) 中查找 lambda),则可以将问题变量列入黑名单。

class MyTest extends PHPUnit_Framework_TestCase
{
    protected $backupGlobalsBlacklist = array('application');
    // ...
}

关于Symfony 2 + Doctrine 2 + PHPUnit 3.5 : Serialization of closure exception,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4366592/

相关文章:

phpunit - 将数据提供者传递给 PHPUnit 中的 setUp()

php - Symfony 2 缓存预热

symfony - 安全 : password check removal and custom User Provider

php - 如何在使用 Doctrine 固定装置时获取数据库中的实体?

php - Symfony2 Doctrine 支持 "private/hidden"实体字段吗?

php - 如何在 Laravel PHPUnit 测试中获取响应变量?

php - 正确的单元测试

symfony - 如何在 Doctrine 2 中配置命名策略

php - 如何在私有(private)服务器中使用 gitlab-ci 自动部署 symfony 应用程序?

php - 使用 PHPUnit 和 Mockery 模拟 Symfony 实体进行单元测试