@@ -133,6 +133,11 @@ class MediaExtensionTest extends TestCase
         return $this->template;
     }
 
+    /**
+     * @psalm-suppress InternalMethod
+     *
+     * @see https://github.com/twigphp/Twig/blob/3.x/src/TemplateWrapper.php#L24-L30
+     */
     public function getTemplateWrapper(): TemplateWrapper
     {
         if (null === $this->templateWrapper) {
<?php

declare(strict_types=1);

/*
 * This file is part of the Sonata Project package.
 *
 * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Sonata\Doctrine\Model\ManagerInterface;
use Sonata\MediaBundle\Model\Media;
use Sonata\MediaBundle\Model\MediaInterface;
use Sonata\MediaBundle\Provider\MediaProviderInterface;
use Sonata\MediaBundle\Provider\Pool;
use Sonata\MediaBundle\Twig\Extension\MediaExtension;
use Twig\Environment;
use Twig\Template;
use Twig\TemplateWrapper;

/**
 * @author Geza Buza <bghome@gmail.com>
 */
class MediaExtensionTest extends TestCase
{
    /**
     * @var MockObject&MediaProviderInterface
     */
    private $provider;

    /**
     * @var MockObject&Template
     */
    private $template;

    /**
     * @var TemplateWrapper
     */
    private $templateWrapper;

    /**
     * @var MockObject&Environment
     */
    private $environment;

    /**
     * @var MockObject&Media
     */
    private $media;

    public function testThumbnailHasAllNecessaryAttributes(): void
    {
        $mediaExtension = new MediaExtension($this->getMediaService(), $this->getMediaManager(), $this->getEnvironment());

        $media = $this->getMedia();
        $format = 'png';
        $options = [
            'title' => 'Test title',
            'alt' => 'Test title',
        ];

        $provider = $this->getProvider();
        $provider->expects($this->once())->method('generatePublicUrl')->with($media, $format)
            ->willReturn('http://some.url.com');

        $template = $this->getTemplate();
        $template->expects($this->once())
            ->method('render')
            ->with(
                $this->equalTo(
                    [
                        'media' => $media,
                        'options' => [
                            'title' => 'Test title',
                            'alt' => 'Test title',
                            'src' => 'http://some.url.com',
                        ],
                    ]
                )
            )
            ->willReturn('rendered thumbnail');

        $mediaExtension->thumbnail($media, $format, $options);
    }

    /**
     * @return MockObject&Pool
     */
    public function getMediaService(): object
    {
        $mediaService = $this->createMock(Pool::class);
        $mediaService->method('getProvider')->willReturn($this->getProvider());

        return $mediaService;
    }

    /**
     * @return MockObject&ManagerInterface
     */
    public function getMediaManager(): object
    {
        return $this->createMock(ManagerInterface::class);
    }

    /**
     * @return MockObject&MediaProviderInterface
     */
    public function getProvider(): object
    {
        if (null === $this->provider) {
            $this->provider = $this->createMock(MediaProviderInterface::class);
            $this->provider->method('getFormatName')->willReturnArgument(1);
            $this->provider->method('getFormat')->willReturn(false);
        }

        return $this->provider;
    }

    /**
     * @return MockObject&Template
     */
    public function getTemplate(): object
    {
        if (null === $this->template) {
            $this->template = $this->createMock(Template::class);
        }

        return $this->template;
    }

    public function getTemplateWrapper(): TemplateWrapper
    {
        if (null === $this->templateWrapper) {
            $this->templateWrapper = new TemplateWrapper($this->getEnvironment(), $this->getTemplate());
        }

        return $this->templateWrapper;
    }

    /**
     * @return MockObject&Environment
     */
    public function getEnvironment(): object
    {
        if (null === $this->environment) {
            $this->environment = $this->createMock(Environment::class);
            $this->environment->method('load')->willReturn($this->getTemplateWrapper());
        }

        return $this->environment;
    }

    public function getMedia(): Media
    {
        if (null === $this->media) {
            $this->media = $this->createMock(Media::class);
            $this->media->method('getProviderStatus')->willReturn(MediaInterface::STATUS_OK);
        }

        return $this->media;
    }
}

We should create an issue/PR for this in twig.