Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I make some functional test with Symfony 2 and phpunit.

But i've some trouble with a Service. Let me explain. During my run test, i want to use some service used by the application. So i juste set my setUp function for setting the kernel :

    static::$kernel = static::createKernel();
    static::$kernel->boot();
    $this->objectFactory = static::$kernel->getContainer()->get('some.application.objectfactory');

So i've this and in my function i need to used a service that return an object so i call my service like that

$var = $this->objectFactory->getObject($id);

and obviously in my tearDown function i just :

    protected function tearDown()
{
    $this->client->restart();
    unset($this->client, $this->objectFactory);
}

So my problem is when i run a test i've this message :

Symfony\Component\DependencyInjection\Exception\InactiveScopeException: You cannot create a service ("request") of an inactive scope ("request").

And i can't find a way to solve this. Did someone have any idea ??

My version of Symfony is 2.2.1 and my version of phpunit is 3.7.19

If someone can help me, i could be very happy.

I'm sorry if my English isn't so good.


EDIT

Maybe it could help someone, in the service i used that : :

        $request = $this->container->get('request');

It seems to be the reason why it dosen't work, when i remove it, it doesn't say the error, but they still doesn't work.


EDIT
@Cyprian According to you have change my code for what i want. So i just add to my service, in the function that i want, the client (Client web test case), and then inside the function i just add this :

if (isset($client)) {
        $request = $client->getRequest();
    } else {
        $request = $this->container->get('request');
    }

So in my function where i call the service i've just this :

    public function getObject($id)
{
    //Get the service from the kernel
    $service = static::$kernel->getContainer()->get('service');
    $object = $service->getObject($id, $this->client);
}

and it works fine like this

@nifr Your idea doesn't work for me, but i think your idea wasn't wrong, they just not works in my case

However Thanks for your help, i'm happy i works now, and i expect that post could help someone else

share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

Try get request from the client, not service container:

$request = $this->client->getRequest();

In that way you can also get kernel and/or container:

$kernel = $this->client->getKernel();
$container = $this->client->getContainer();

One more useful tip: kernel from the client is rebooted between each two requests. So, for example if you pass your mock to client's container and do some request, in next request (after the first one) the container will not contain your mock.

share|improve this answer
    
according to your idea with some update, that's works for me –  Babou34090 May 29 '13 at 7:59
add comment

There is no request available in phpUnit as long as you don't construct one.

If you want to test a request. create it like this:

use Symfony\Component\HttpFoundation\Request; 

protected $request;

public function setUp()
{
    // ...
    $this->request = new Request();
    // ... modify your request acccording to your needs
}

and add/call a setter in your Service using the request.

$service = $this->kernel->getContainer()->get('your_service')
$service->setRequest($this->request);

or create a Functional Test with WebtestCase.

share|improve this answer
    
In fact i'm already use for a functional Test. I mean i need to use it for give the value from a Web Service to check with my test. And i'm already extends WebTestCase it's why i've some client I'll check for your idea to setRequest. Did you mean like this symfony.com/doc/2.0/components/http_foundation/… ??? –  Babou34090 May 28 '13 at 17:36
    
=> Like i say in the edit, doesn't work for me, but not necesseraly wrong thanks for your help –  Babou34090 May 29 '13 at 7:58
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.