Completed
Push — master ( bb6905...021686 )
by Grégoire
18s queued 12s
created

tests/Block/MediaBlockServiceTest.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\MediaBundle\Tests\Block;
15
16
use Prophecy\Prophecy\ObjectProphecy;
17
use Sonata\BlockBundle\Block\BlockContext;
18
use Sonata\BlockBundle\Model\Block;
19
use Sonata\BlockBundle\Test\AbstractBlockServiceTestCase;
20
use Sonata\MediaBundle\Admin\BaseMediaAdmin;
21
use Sonata\MediaBundle\Block\MediaBlockService;
22
use Sonata\MediaBundle\Model\GalleryManagerInterface;
23
use Sonata\MediaBundle\Model\MediaInterface;
24
use Sonata\MediaBundle\Provider\Pool;
25
use Symfony\Component\DependencyInjection\ContainerInterface;
26
27
class MediaBlockServiceTest extends AbstractBlockServiceTestCase
28
{
29
    protected $container;
30
    private $galleryManager;
31
    private $blockService;
32
33
    protected function setUp(): void
34
    {
35
        parent::setUp();
36
37
        $this->container = $this->prophesize(ContainerInterface::class);
38
        $this->galleryManager = $this->prophesize(GalleryManagerInterface::class);
39
40
        $this->blockService = new MediaBlockService(
41
            'block.service',
42
            $this->templating,
43
            $this->container->reveal(),
44
            $this->galleryManager->reveal()
45
        );
46
    }
47
48
    public function testExecute(): void
49
    {
50
        $block = $this->prophesize(Block::class);
51
        $media = $this->prophesize(MediaInterface::class);
52
        $blockContext = $this->prophesize(BlockContext::class);
53
54
        $this->configureGetFormatChoices($media, ['format1' => 'format1']);
55
        $blockContext->getBlock()->willReturn($block->reveal());
56
        $blockContext->getSetting('format')->willReturn('format');
57
        $blockContext->setSetting('format', 'format1')->shouldBeCalled();
58
        $blockContext->getSettings()->willReturn([]);
59
        $blockContext->getTemplate()->willReturn('template');
60
        $blockContext->getSetting('mediaId')->willReturn($media->reveal());
61
        $block->getSetting('mediaId')->willReturn($media->reveal());
62
63
        $this->blockService->execute($blockContext->reveal());
64
65
        $this->assertSame('template', $this->templating->view);
66
        $this->assertInternalType('array', $this->templating->parameters['settings']);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit\Framework\Assert::assertInternalType() has been deprecated with message: https://github.com/sebastianbergmann/phpunit/issues/3369

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
67
        $this->assertSame($media->reveal(), $this->templating->parameters['media']);
68
        $this->assertSame($block->reveal(), $this->templating->parameters['block']);
69
    }
70
71
    public function testDefaultSettings(): void
72
    {
73
        $blockContext = $this->getBlockContext($this->blockService);
74
75
        $this->assertSettings([
76
            'attr' => [],
77
            'context' => false,
78
            'extra_cache_keys' => [],
79
            'format' => false,
80
            'media' => false,
81
            'mediaId' => null,
82
            'template' => '@SonataMedia/Block/block_media.html.twig',
83
            'title' => null,
84
            'translation_domain' => null,
85
            'icon' => null,
86
            'class' => null,
87
            'ttl' => 0,
88
            'use_cache' => true,
89
        ], $blockContext);
90
    }
91
92
    private function configureGetFormatChoices(ObjectProphecy $media, array $choices): void
93
    {
94
        $mediaAdmin = $this->prophesize(BaseMediaAdmin::class);
95
        $pool = $this->prophesize(Pool::class);
96
97
        $this->container->get('sonata.media.admin.media')->willReturn($mediaAdmin->reveal());
98
        $mediaAdmin->getPool()->willReturn($pool->reveal());
99
        $media->getContext()->willReturn('context');
100
        $pool->getFormatNamesByContext('context')->willReturn($choices);
101
    }
102
}
103