2016-06-02 6 views
2

私はSF2.8アプリに簡単な機能テストを実行しよう:実行SF2機能テスト:「スコープがアクティブである場合、コンテナをリセットすると、許可されていません」

  • のPHPUnit 5.3.4
  • コマンドライン:PHPUnitの-cアプリのsrc/LCH/MultisiteBundle /テスト/コントローラ/ SiteControllerTest

SiteControllerTest:

class SiteControllerTest extends WebTestCase 
{ 

    /** 
    * {@inheritDoc} 
    */ 
    protected function setUp() 
    { 
     $this->superadmin = static::createClient(); 
    } 

    /* 
    * @group multisite 
    */ 
    public function testList() 
    { 
     // Nothing here yet. 
    } 

    protected function tearDown() { 
     parent::tearDown(); 
    } 
} 

PHPUnitのリターン:

There was 1 error:

1) LCH\MultisiteBundle\Tests\Controller\SiteControllerTest::testList Symfony\Component\DependencyInjection\Exception\LogicException: Resetting the container is not allowed when a scope is active.

/var/www/html/sites/lch/loyalty/app/bootstrap.php.cache:2231 /var/www/html/sites/lch/loyalty/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Test/KernelTestCase.php:182 /var/www/html/sites/lch/loyalty/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Test/KernelTestCase.php:192 /var/www/html/sites/lch/loyalty/src/LCH/MultisiteBundle/Tests/Controller/SiteControllerTest.php:29

これはリセット時のコンテナクラス自体()メソッドによってthrowedさ:

/** 
    * {@inheritdoc} 
    */ 
    public function reset() 
    { 
     if (!empty($this->scopedServices)) { 
      throw new LogicException('Resetting the container is not allowed when a scope is active.'); 
     } 

     $this->services = array(); 
    } 

しかし、私は、なぜ見つけることができません。これまでのサービス登録でスコープを使用していなかったので、デフォルトのself :: SCOPE_CONTAINERのいずれかにする必要があります。

ヒント?

ありがとうございます!

答えて

1

解決策を見つけたMatmouthのおかげで。 AppKernel.php:我々はいくつかのCLI呼び出しでリクエストオブジェクトを必要に応じて開始時に

は、我々はのみCLIのための容器内の要求注入を実施し

protected function initializeContainer() { 
     parent::initializeContainer(); 

     if (PHP_SAPI == 'cli') { 
      $this->getContainer()->enterScope('request'); 
      $this->getContainer()->set('request', new \Symfony\Component\HttpFoundation\Request(), 'request'); 
     } 
    } 

これは依存、すなわちテスト環境ではなかったですケースが "テスト"環境でロードされ、空のリクエストが注入され、上記の例外が作成されました。

そこで我々は、テスト環境の除外を追加し、すべてが正常である:

protected function initializeContainer() { 
     parent::initializeContainer(); 

     if (PHP_SAPI == 'cli' && $this->getEnvironment() != 'test') { 
      $this->getContainer()->enterScope('request'); 
      $this->getContainer()->set('request', new \Symfony\Component\HttpFoundation\Request(), 'request'); 
     } 
    } 
+0

どこあなたは 'initializeContainer'方法を置くのですか? – con

+0

AppKernelでは、Kernel :: initializeContainer()メソッドがオーバーライドされます。もちろん、 – nbonniot

+0

はい。ありがとうございました :) – con

関連する問題