How to fix "You cannot create a service (“request”) of an inactive scope (“request”)" error when using php-unit for testing

Viewed 614

When I run my php-unit test, I get the following error:

You cannot create a service (“request”) of an inactive scope (“request”).

It's stopping me from testing my code properly, and I'd like to be able to test my code. I'm on Symfony 2.8 with PHP 5.6.

I have found some answers to on StackO which suggest adding these lines in:

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

I have that added in to AppKernel.php but I'm still receiving that error on my test. Here's the setUp() and first test.

class HelperTest extends WebTestCase
{

  private $helper;

  public function setUp() {
    $this->client = static::createClient(array(), array(
      'PHP_AUTH_USER' => 'email@address.com',
      'PHP_AUTH_PW'   => 'not_the_real_password_obvi',
    ));

    $container = $this->client->getContainer();
    $this->helper = $container->get('Helper');
  }

  /*
    Tests for daysToBirthdayFromNow() function;
  */

public function testDaysUntilBirthdayIs270() {

    $birthdayTimestamp = 1514764800; // 1 January 2018
    $currentTimestamp = 1538132400; // 28 September 2018

    $daysToBirthday = intval($this->helper->daysToBirthdayFromNow($birthdayTimestamp, $currentTimestamp));

    $this->assertTrue($daysToBirthday == 270);
  }

This calls daysToBirthdayFromNow(), inside of which I call another Helper function called convertTimestampToTimezone(). The test falls over in that function at this line of code:

$timezoneSetting = $this->getClub()->s('timezone');

I have dumped out $this->getClub() and it crashes out with the error message that is in the title of this post. Any tests that don't require $this-> work fine.

Has anyone overcome this problem? As far as I know, it's a problem that my the dev team I work in has been unable to solve since they started using Symfony.

1 Answers

I fixed this after consulting several other sources. Here is the fix for anyone interested:

Add this function into the AppKernel class:

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

And at the top of my test file:

class HelperTest extends WebTestCase
{

  private $helper;

  public function setUp() {
    $this->client = static::createClient(array(), array(
      'PHP_AUTH_USER' => 'your@email.com',
      'PHP_AUTH_PW'   => 'your_password',
    ));

    $container = $this->client->getContainer();
    $container->enterScope('request');
    $container->set('request', new Request(), 'request');

    $this->helper = $container->get('Helper');
  }

  public function tearDown() {
    $container = $this->client->getContainer();
    $container->leaveScope('request');
  }
  // Your tests go below here

YMMV but this works for me (finally!)

Related