Skip to content

Symfony2 Testing - overriding container values does not affect after redirecting / form submitting / few requests #8858

@pleerock

Description

@pleerock

I want to replace service container value with a test one to test if another implementation of the class injected by service container would work in my controller. I can set it via $client->getContainer()->set method, but it does not work well. But after redirecting / following to another page container value is reset. I think code example will show the problem more detail:

$client = static::createClient();

 // this one implements CarList interface
$carList = new CarList();
$carList->add($car1);
$carList->add($car2);
// ...

// now I replace default carlist implementation defined in the service 
// container and used by CarController with my custom $carList.
$client->getContainer()->set('car.car_list', $carList);

// try to open CarController with the list of cars
$crawler = $this->requestGET($client, '/car/list/');

// and check if $car1 added to the custom CarList exists
$this->assertGreaterThan(0,
               $crawler->filter('html:contains("'. $car1->getName() .'")')->count()); 
// well, it works fine! we replaced value defined in the container 
// by our custom car list object

// now lets try to check it again!
$crawler = $this->requestGET($client, '/car/list/');
$this->assertGreaterThan(0,
               $crawler->filter('html:contains("'. $car1->getName() .'")')->count()); 
// FAIL. container value has been reset (e.g. CarController works 
// with its default carList implmenetation and there is no way
// to set a new container value, calling $client->getContainer()->set()
// does not work anymore


// the same result we have if we try to submit a new car via form,
// container value will not be set anymore and car will be added to
// the old carList implementation

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions