JMSDiExtraBundle icon indicating copy to clipboard operation
JMSDiExtraBundle copied to clipboard

Issue#282: call setContainer on ContainerAwareInterface

Open SmeTskE opened this issue 8 years ago • 13 comments

Autowired controllers break when not calling setContainer on ContainerAwareInterface implementations.

SmeTskE avatar Oct 27 '17 11:10 SmeTskE

Honestly not really familiar with unit tests, can you give me some pointers? I'll try to write some soon.

SmeTskE avatar Oct 27 '17 20:10 SmeTskE

I'm on my phone all this week end so it won't be easy . Read .travis.yml to see how to read them, and make a deliberate mistake in the existing part of the code you modified. If it is covered, the tests should break.

greg0ire avatar Oct 27 '17 21:10 greg0ire

When I add throw new \Exception('deliberate mistake'); to the first line of the instantiateController(), the local tests fail on the following test functions:

  • Tests/Functional/AutomaticControllerInjectionsTest.php testInjections()
  • Tests/Functional/ControllerResolverTest.php testLookupMethodIsCorrectlyImplemented()
  • Tests/Functional/ControllerResolverTest.php testLookupMethodAndAopProxy()
  • Tests/Functional/ControllerResolverTest.php testAopProxyWhenNoDiMetadata()

Same effect when I add the throw right before the return.

My guess is that the code is covered, then? :) Any way I can provide proof, or should I extend the tests?

SmeTskE avatar Oct 27 '17 21:10 SmeTskE

It means the existing code is covered indeed :) you might want to try throwing inside the if statements now, and check that they are covered too, and add more tests if they are not.

greg0ire avatar Oct 27 '17 21:10 greg0ire

First and third if-statements aren't covered. I'll see what I can do with tests tomorrow.

Thanks for the feedback!

SmeTskE avatar Oct 27 '17 21:10 SmeTskE

I can't really wrap my head around the fact the tests fail for php 5.x and not for 7.x

When I try to debug the test for the autowired controller on 7.x, the code is executed properly, but whenever I debug the controller on 5.x, it seems the code isn't executed and the test fails telling me the Route could not be found.

The HttpKernel/ControllerResolver::createController() does not get executed on 5.x with the current config (and thus the instantiateController() isn't called either).

Any ideas?

SmeTskE avatar Oct 28 '17 13:10 SmeTskE

You could try generating traces with xdebug_start_trace() and compare them

greg0ire avatar Oct 28 '17 15:10 greg0ire

The third if is now covered.

The first if not yet. When would a Controller already be set on the Container, but not yet be instantiated? In a sub request a new Container would be created, no? I can't think of another scenario in which the first if ($this->container->has($class)) would validate.

Any idea?

SmeTskE avatar Oct 31 '17 14:10 SmeTskE

Maybe if a controller fowards to itself? Not sure if what I am saying is clever or utterly stupid...

greg0ire avatar Oct 31 '17 14:10 greg0ire

It seems during a forward another Container is instantiated for the sub-request while forwarding. I'll try to investigate further.

SmeTskE avatar Oct 31 '17 16:10 SmeTskE

Any news about this pull request which may fix the issue about Symfony 3.3 Auto-wiring on controllers ?

kmarques avatar Jan 25 '18 13:01 kmarques

Is this ready to merge? Or are there still some things to cover with tests?

GuilhemN avatar Jan 25 '18 13:01 GuilhemN

There's still one check to be written, but I don't know how to reproduce it with a test. https://github.com/schmittjoh/JMSDiExtraBundle/pull/293#issuecomment-340782931

Any feedback would be appreciated.

SmeTskE avatar Jan 25 '18 18:01 SmeTskE