Completed
Push — master ( 8c0a67...1fafee )
by Grégoire
9s
created

MediaBlockServiceTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 73
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 14 1
A testExecute() 0 22 1
A testDefaultSettings() 0 17 1
A configureGetFormatChoices() 0 10 1
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\MediaBundle\Tests\Block;
13
14
use Sonata\BlockBundle\Test\AbstractBlockServiceTestCase;
15
use Sonata\MediaBundle\Block\MediaBlockService;
16
17
class MediaBlockServiceTest extends AbstractBlockServiceTestCase
18
{
19
    protected $container;
20
    private $galleryManager;
21
    private $blockService;
22
23
    protected function setUp()
24
    {
25
        parent::setUp();
26
27
        $this->container = $this->prophesize('Symfony\Component\DependencyInjection\ContainerInterface');
28
        $this->galleryManager = $this->prophesize('Sonata\MediaBundle\Model\GalleryManagerInterface');
29
30
        $this->blockService = new MediaBlockService(
31
            'block.service',
32
            $this->templating,
33
            $this->container->reveal(),
34
            $this->galleryManager->reveal()
35
        );
36
    }
37
38
    public function testExecute()
39
    {
40
        $block = $this->prophesize('Sonata\BlockBundle\Model\Block');
41
        $media = $this->prophesize('Sonata\MediaBundle\Model\MediaInterface');
42
        $blockContext = $this->prophesize('Sonata\BlockBundle\Block\BlockContext');
43
44
        $this->configureGetFormatChoices($media, array('format1' => 'format1'));
45
        $blockContext->getBlock()->willReturn($block->reveal());
46
        $blockContext->getSetting('format')->willReturn('format');
47
        $blockContext->setSetting('format', 'format1')->shouldBeCalled();
48
        $blockContext->getSettings()->willReturn(array());
49
        $blockContext->getTemplate()->willReturn('template');
50
        $blockContext->getSetting('mediaId')->willReturn($media->reveal());
51
        $block->getSetting('mediaId')->willReturn($media->reveal());
52
53
        $this->blockService->execute($blockContext->reveal());
54
55
        $this->assertSame('template', $this->templating->view);
56
        $this->assertInternalType('array', $this->templating->parameters['settings']);
57
        $this->assertSame($media->reveal(), $this->templating->parameters['media']);
58
        $this->assertSame($block->reveal(), $this->templating->parameters['block']);
59
    }
60
61
    public function testDefaultSettings()
62
    {
63
        $blockContext = $this->getBlockContext($this->blockService);
64
65
        $this->assertSettings(array(
66
            'attr' => array(),
67
            'context' => false,
68
            'extra_cache_keys' => array(),
69
            'format' => false,
70
            'media' => false,
71
            'mediaId' => null,
72
            'template' => 'SonataMediaBundle:Block:block_media.html.twig',
73
            'title' => false,
74
            'ttl' => 0,
75
            'use_cache' => true,
76
        ), $blockContext);
77
    }
78
79
    private function configureGetFormatChoices($media, $choices)
80
    {
81
        $mediaAdmin = $this->prophesize('Sonata\MediaBundle\Admin\BaseMediaAdmin');
82
        $pool = $this->prophesize('Sonata\MediaBundle\Provider\Pool');
83
84
        $this->container->get('sonata.media.admin.media')->willReturn($mediaAdmin->reveal());
85
        $mediaAdmin->getPool()->willReturn($pool->reveal());
86
        $media->getContext()->willReturn('context');
87
        $pool->getFormatNamesByContext('context')->willReturn($choices);
88
    }
89
}
90