Symfony 2 + Doctrine 2 + PHPUnit 3.5:闭包异常的序列化 [英] Symfony 2 + Doctrine 2 + PHPUnit 3.5: Serialization of closure exception

查看:14
本文介绍了Symfony 2 + Doctrine 2 + PHPUnit 3.5:闭包异常的序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I tried to find something about this on Google but nothing came out. I have a TestCase class that inherits from WebTestCase, with some methods that I want to use in all my unit/functional tests:

<?php

namespace ApplicationFaxServerBundleTest;

use SymfonyBundleFrameworkBundleTestWebTestCase;
use DoctrineCommonDataFixturesLoader;
use DoctrineCommonDataFixturesExecutorORMExecutor;
use DoctrineCommonDataFixturesPurgerORMPurger;

use ApplicationFaxServerBundleDataFixturesORMNetworkConfigurationData;

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( 'ApplicationFaxServerBundleEntityNetworkConfiguration' );
    }

    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 ApplicationFaxServerBundleTestsEntity;

use DoctrineORMAbstractQuery;

use ApplicationFaxServerBundleEntity;
use ApplicationFaxServerBundleTestTestCase;

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

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

It was working before, but, suddenly, after I updated my vendors (doctrine included), it began to throw this exception:

3) ApplicationFaxServerBundleTestsEntityNetworkConfigurationRepositoryTest::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

我发现问题来自夹具加载.如果我删除加载装置的代码,它就可以工作.

I've found that the problem comes from the fixture loading. If I remove the code that loads fixtures, it works.

有人知道我的代码有什么问题吗?这是加载灯具的最佳方式吗?

Does anyone know what could be wrong in my code? Is this the best way of loading fixtures?

谢谢!

推荐答案

在技术上与您的问题无关.但是,在使用 PHPUnit 时,我很难解决不允许 'Closure' 序列化"的问题,而这个问题是 Google 的最高结果.

Not technically related to your issue. However, I had a really hard time trying to solve the "Serialization of 'Closure' is not allowed" issue while using PHPUnit, and this question is the top Google result.

问题来自这样一个事实,即 PHPUnit 序列化系统中的所有 $GLOBALS,以便在测试运行时对它们进行必要的备份.然后在测试完成后恢复它们.

The problem comes from the fact that PHPUnit serializes all the $GLOBALS in the system to essential back them up while the test is running. It then restores them after the test is done.

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

However, if you have any closures in your GLOBAL space, it's going to cause problems. There's two ways to solve it.

您可以使用注释完全禁用全局备份程序.

You can disable the global backup procedure totally by using an annotation.

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

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

Or, if you know which variable is causing the problem (look for a lambda in var_dump($GLOBALS)), you can just blacklist the problem variable(s).

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

这篇关于Symfony 2 + Doctrine 2 + PHPUnit 3.5:闭包异常的序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆