2

I'm writing functional tests for my Symfony3 application. I have a test which looks like this:

public function testList()
{
    $client = static::createClient();
    $client->getCookieJar()->set($this->cookie);

    $this->sender->method('isSuccessfull')->will($this->returnValue(true));

    $container = $client->getContainer();
    $container->set('app.service1', $this->object1);
    $container->set('app.service2', $this->object2);

    $crawler = $client->request('GET', '/list/1');
    $form = $crawler->selectButton('Save')->form();
    $client->submit($form);

}

Everything is good until submitting form. Kernel losing the setted container services while submitting a form. How can I these services into container also after submitting a form? Maybe there is other option to resolve my problem?

1 Answer 1

1

If you check the source code for Symfony\Component\HttpKernel\Client::doRequest() class you can see that it terminates the kernel which is then started again later and that's why you loose all service you created manually.

I guess you have an app which you're testing so you could add the services to its services.yml. Another way could be extending the Client class with your own and overriding getContainer() method to always add these extra services (then you'd have to update the service definition for test.client in a compile pass with your customized class).

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you for response. My services aren't "real" services. They are mock of real services. Do you have an idea how to pass the mock of the service in the services_test.yml or add these mocks in the extended Client class?
Adding them into test_service.yml that imports the regular service.yml is even easier that extending Client so I'd go with just ymls unless you need also some extra functionality.
Ok I know that :) But how to pass mocks as the argument of service? In my code above mocks are $this->object1 and $this->object2.
I don't know what's object1 nor object2. Just define them as services I guess.
It's not so simple, because object1 and object2 are phpunit mocks. Example of defined object: $this->object1 = $this->getMockBuilder('AppBundle\Object\Object1') ->disableOriginalConstructor() ->setMethods(['method1', 'method2']) ->getMock()
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.