Symfony 2 + Doctrine 2 + Phpunit 3.5:閉鎖例外のシリアル化
質問
Googleでこれについて何かを見つけようとしましたが、何も出てきませんでした。 WebtestCaseから継承するテストケースクラスがあり、すべてのユニット/機能テストで使用したい方法がいくつかあります。
<?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 );
}
}
以前は機能していましたが、突然、ベンダー(教義を含む)を更新した後、この例外を投げ始めました。
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がシステム内のすべてのグローバルをシリアル化して、テスト中にそれらをバックアップすることに不可欠であるという事実に由来しています。次に、テストが完了した後にそれらを復元します。
ただし、グローバルスペースに閉鎖がある場合、問題を引き起こすでしょう。それを解決する方法は2つあります。
注釈を使用して、グローバルバックアップ手順を完全に無効にすることができます。
/**
* @backupGlobals disabled
*/
class MyTest extends PHPUnit_Framework_TestCase
{
// ...
}
または、どの変数が問題を引き起こしているかを知っている場合(var_dump($ globals)のラムダを探します)、問題変数をブラックリストに登録できます。
class MyTest extends PHPUnit_Framework_TestCase
{
protected $backupGlobalsBlacklist = array('application');
// ...
}
他のヒント
あなたも試すことができます。
<phpunit backupGlobals="false">
<testsuites>
<testsuite name="Test">
<directory>.</directory>
</testsuite>
</testsuites>
</phpunit>
所属していません StackOverflow