Completed
Pull Request — master (#1163)
by Grégoire
02:51
created

SonataMediaExtensionTest::getConfigs()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 34
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 20
nc 1
nop 0
dl 0
loc 34
rs 8.8571
c 0
b 0
f 0
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\DependencyInjection;
13
14
use Sonata\MediaBundle\DependencyInjection\SonataMediaExtension;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
use Symfony\Component\DependencyInjection\Definition;
17
18
class SonataMediaExtensionTest extends \PHPUnit_Framework_TestCase
19
{
20
    /**
21
     * @var SonataMediaExtension
22
     */
23
    private $extension;
24
25
    /**
26
     * Root name of the configuration.
27
     *
28
     * @var ContainerBuilder
29
     */
30
    private $container;
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function setUp()
36
    {
37
        $configs = $this->getConfigs();
38
39
        $this->container = new ContainerBuilder();
40
        $this->container->setParameter('kernel.bundles', array());
41
42
        $this->extension = $this->getExtension();
43
        $this->extension->load($configs, $this->container);
44
    }
45
46
    public function testLoadWithDefaultAndCustomCategoryManager()
47
    {
48
        $container = $this->getContainer(
49
            array(array(
50
            'class' => array('category' => '\stdClass'),
51
            'category_manager' => 'dummy.service.name',
52
        )));
53
54
        $this->assertTrue($container->hasAlias('sonata.media.manager.category'));
55
        $this->assertSame($container->getAlias('sonata.media.manager.category')->__toString(), 'dummy.service.name');
56
    }
57
58
    public function testLoadWithForceDisableTrueAndWithCategoryManager()
59
    {
60
        $container = $this->getContainer(
61
            array(array(
62
            'class' => array('category' => '\stdClass'),
63
            'category_manager' => 'dummy.service.name',
64
            'force_disable_category' => true,
65
        )));
66
67
        $this->assertFalse($container->hasDefinition('sonata.media.manager.category'));
68
    }
69
70
    public function testLoadWithDefaultAndClassificationBundleEnable()
71
    {
72
        $container = $this->getContainer();
73
        $this->assertTrue($container->hasAlias('sonata.media.manager.category'));
74
        $this->assertSame($container->getDefinition('sonata.media.manager.category.default')->getClass(), 'Sonata\MediaBundle\Model\CategoryManager');
75
    }
76
77
    public function testLoadWithDefaultAndClassificationBundleEnableAndForceDisableCategory()
78
    {
79
        $container = $this->getContainer(array(array('force_disable_category' => true)));
80
81
        $this->assertFalse($container->hasDefinition('sonata.media.manager.category'));
82
    }
83
84
    public function testLoadWithDefaultAndClassificationBundleEnableAndCustomCategoryManager()
85
    {
86
        $container = $this->getContainer(
87
            array(array(
88
                'class' => array('category' => '\stdClass'),
89
                'category_manager' => 'dummy.service.name',
90
            )));
91
92
        $this->assertTrue($container->hasAlias('sonata.media.manager.category'));
93
        $this->assertSame($container->getAlias('sonata.media.manager.category')->__toString(), 'dummy.service.name');
94
    }
95
96
    public function testDefaultAdapter()
97
    {
98
        $this->assertTrue($this->container->hasAlias('sonata.media.adapter.image.default'));
99
        $this->assertEquals('sonata.media.adapter.image.gd', $this->container->getAlias('sonata.media.adapter.image.default'));
100
    }
101
102
    /**
103
     * @param string $serviceId
104
     * @param string $extension
105
     * @param string $type
106
     *
107
     * @dataProvider dataAdapter
108
     */
109
    public function testAdapter($serviceId, $extension, $type)
110
    {
111
        $this->assertTrue($this->container->has($serviceId));
112
113
        if (extension_loaded($extension)) {
114
            $this->isInstanceOf($type, $this->container->get($serviceId));
0 ignored issues
show
Unused Code introduced by
The call to SonataMediaExtensionTest::isInstanceOf() has too many arguments starting with $this->container->get($serviceId).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
115
        }
116
    }
117
118
    public function dataAdapter()
119
    {
120
        return array(
121
            array('sonata.media.adapter.image.gd', 'gd', 'Imagine\\Gd\\Imagine'),
122
            array('sonata.media.adapter.image.gmagick', 'gmagick', 'Imagine\\Gmagick\\Imagine'),
123
            array('sonata.media.adapter.image.imagick', 'imagick', 'Imagine\\Imagick\\Imagine'),
124
        );
125
    }
126
127
    public function testDefaultResizer()
128
    {
129
        $this->assertTrue($this->container->hasAlias('sonata.media.resizer.default'));
130
        $this->assertEquals('sonata.media.resizer.simple', $this->container->getAlias('sonata.media.resizer.default'));
131
        if (extension_loaded('gd')) {
132
            $this->isInstanceOf('Sonata\\MediaBundle\\Resizer\\SimpleResizer', $this->container->get('sonata.media.resizer.default'));
0 ignored issues
show
Unused Code introduced by
The call to SonataMediaExtensionTest::isInstanceOf() has too many arguments starting with $this->container->get('s...media.resizer.default').

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
133
        }
134
    }
135
136
    /**
137
     * @param $serviceId
138
     * @param $type
139
     *
140
     * @dataProvider dataResizer
141
     */
142
    public function testResizer($serviceId, $type)
143
    {
144
        $this->assertTrue($this->container->has($serviceId));
145
        if (extension_loaded('gd')) {
146
            $this->isInstanceOf($type, $this->container->get($serviceId));
0 ignored issues
show
Unused Code introduced by
The call to SonataMediaExtensionTest::isInstanceOf() has too many arguments starting with $this->container->get($serviceId).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
147
        }
148
    }
149
150
    public function dataResizer()
151
    {
152
        return array(
153
            array('sonata.media.resizer.simple', 'Sonata\\MediaBundle\\Resizer\\SimpleResizer'),
154
            array('sonata.media.resizer.square', 'Sonata\\MediaBundle\\Resizer\\SquareResizer'),
155
        );
156
    }
157
158
    /**
159
     * @return SonataMediaExtension
160
     */
161
    protected function getExtension()
162
    {
163
        return new SonataMediaExtension();
164
    }
165
166
    /**
167
     * @return array
168
     */
169
    protected function getConfigs()
170
    {
171
        $configs = array(
172
            'sonata_media' => array(
173
                'db_driver' => 'doctrine_orm',
174
                'default_context' => 'default',
175
                'contexts' => array(
176
                    'default' => array(
177
                        'providers' => array(
178
                            'sonata.media.provider.image',
179
                        ),
180
                        'formats' => array(
181
                            'default' => array(
182
                                'width' => 100,
183
                                'quality' => 100,
184
                            ),
185
                        ),
186
                    ),
187
                ),
188
                'cdn' => array(
189
                    'server' => array(
190
                        'path' => '/uploads/media',
191
                    ),
192
                ),
193
                'filesystem' => array(
194
                    'local' => array(
195
                        'directory' => '%kernel.root_dir%/../web/uploads/media',
196
                    ),
197
                ),
198
            ),
199
        );
200
201
        return $configs;
202
    }
203
204
    private function getContainer(array $config = array())
205
    {
206
        $defaults = array(array(
207
            'default_context' => 'default',
208
            'db_driver' => 'doctrine_orm',
209
            'contexts' => array('default' => array('formats' => array('small' => array('width' => 100, 'quality' => 50)))),
210
            'filesystem' => array('local' => array('directory' => '/tmp/')),
211
        ));
212
213
        $container = new ContainerBuilder();
214
        $container->setParameter('kernel.bundles', array('SonataAdminBundle' => true));
215
        $container->setDefinition('translator', new Definition('\stdClass'));
216
        $container->setDefinition('security.context', new Definition('\stdClass'));
217
        $container->setDefinition('doctrine', new Definition('\stdClass'));
218
        $container->setDefinition('session', new Definition('\stdClass'));
219
220
        if (isset($config[0]['category_manager'])) {
221
            $container->setDefinition($config[0]['category_manager'], new Definition('\stdClass'));
222
        }
223
224
        $container->setDefinition('sonata.classification.manager.category', new Definition('Sonata\ClassificationBundle\Model\CategoryManager'));
225
226
        $loader = new SonataMediaExtension();
227
        $loader->load(array_merge($defaults, $config), $container);
228
        $container->compile();
229
230
        return $container;
231
    }
232
}
233