Search code examples
symfonyphpunitsymfony-3.4

Unable to get mock container to return anything but null


I have a test that fails due to me being unable to successfully stub the get method of the Controller:

1) Tests\my-project\BackendBundle\Service\PdfGenerateServiceTest::test_getHttpPathToPtImage_should_return_the_default_avatar_when_photo_is_null
TypeError: Argument 1 passed to Mock_Pdf_d0288d34::setLogger() must implement interface Psr\Log\LoggerInterface, null given, called in /var/www/my-project/src/my-project/BackendBundle/Service/PdfGenerateService.php on line 66

The test

    public function test_getHttpPathToPtImage_should_return_the_default_avatar_when_photo_is_null()
    {
        $protocolAndHost = "http://foo.bar.com";
        $service = new PdfGenerateService($this->createFileServiceMock(), $this->createTranslatorMock(), $this->createSnappyMock(), $this->createContainerMock(), $protocolAndHost);
        $httpPathToPtImage = $service->getHttpPathToPtImage(null);

        self::assertEquals($httpPathToPtImage, $protocolAndHost . "abc/def");
    }

The constructor that is failing

    public function __construct(FileService $fileService, Translator $translator, Pdf $snappy, ContainerInterface $container, string $protocolAndHost)
    {
        $this->fileService = $fileService;
        $this->translator = $translator;
        $this->currentLocale = $this->translator->getLocale();

        /* Could reconfigure the service using `service.yml` to pass these in using DI */
        $this->twig = $container->get('twig');
        $this->logger = $container->get('logger');  // <--- should not be null

        $timeoutInSeconds = 15; // can be high, since the job is done async in a job (user does not wait)
        $snappy->setLogger($this->logger); // <--- THIS FAILS due to $this->logger being null

The stubs

    protected function createContainerMock()
    {
        $containerMock = $this->createMock('Symfony\Component\DependencyInjection\ContainerInterface');
        $loggerMock = $this->createLoggerMock();
        $containerMock->method('get')->will($this->returnValueMap([
            ['logger', $loggerMock]
        ]));
        return $containerMock;
    }

I do not really understand why the the get('logger') call just returns null when I have setup a mock to be returned using the returnValueMap call above.


By chance, I just found a SO question on this topic where someone mentioned that you need to supply all parameters, even optional ones. I then checked out the interface, which does indeed list a second parameter:

public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE);

Still, changing the map to ['logger', null, $loggerMock] made no change, so I am a bit at a loss as to what to try next.


Phpunit 6.5, PHP 7.2, Symfony 3.4


Solution

  • You were really close to the solution. When providing a value for an optional parameter in returnValueMap, you must use the value itself, not just null.

    So instead of

    ['logger', null, $loggerMock]
    

    try specifying

    ['logger', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, $loggerMock]
    

    Complete call looks like this:

    $containerMock->method('get')->will($this->returnValueMap([
       ['logger', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, $loggerMock]
    ]));