Quantcast
Channel: Active questions tagged symfony4 - Stack Overflow
Viewing all articles
Browse latest Browse all 3925

Phpspec with Symfony 4+ how to test Controller that uses AbstractController functions

$
0
0

Any test case that uses anything from Symfony AbstractController can not be really tested... or can it?

Simple example:

class TestController extends AbstractController{  public function index()  {    if ($this->getUser()) {      return $this->redirect($this->generateUrl('home'));    }    return $this->render('home/index.html.twig', []);  }}

Then the phpspec:

public function it_can_redirect_if_user_logged_in(    TokenInterface $token, UrlGeneratorInterface $router,     ContainerInterface $container, TokenStorageInterface $tokenStorage, UserInterface $user  ): void  {    $container->has(Argument::any())->willReturn(true);    $container->get(Argument::any())->willReturn($tokenStorage, $router);    $tokenStorage->getToken()->willReturn($token);    $token->getUser()->willReturn($user);    $router->generate('home')->willReturn('/');    // not setting here will get: err:Error("Call to a member function has() on null"    // as getUser() inside abstract is looking for this->container->has('security.token_storage')    // setting it here will then not use the mocked return from container and get the error from generate    // [err:TypeError("Double\UrlGeneratorInterface\P26::generate(): Return value must be of type string, null returned")    $this->setContainer($container);    $this->index()->shouldBeAnInstanceOf(RedirectResponse::class);  }

Tried with a concrete mock of the AbstractController, but then can't mock the ContainerInterface or anything returned by it.

Any other ideas?


Viewing all articles
Browse latest Browse all 3925

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>