Completed
Push — master ( aba493...5356ed )
by Ruud
315:38 queued 305:00
created

MediaBundle/Tests/unit/Helper/MediaManagerTest.php (8 issues)

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
namespace Kunstmaan\MediaBundle\Tests\Helper;
4
5
use Kunstmaan\MediaBundle\Entity\Media;
6
use Kunstmaan\MediaBundle\Helper\MediaManager;
7
use PHPUnit\Framework\TestCase;
8
9
class MediaManagerTest extends TestCase
10
{
11
    /**
12
     * @var MediaManager
13
     */
14
    protected $object;
15
16
    private $defaultHandler;
17
18
    /**
19
     * Sets up the fixture, for example, opens a network connection.
20
     * This method is called before a test is executed.
21
     */
22
    protected function setUp()
23
    {
24
        $this->defaultHandler = $this->getMockForAbstractClass('Kunstmaan\MediaBundle\Helper\Media\AbstractMediaHandler', array(0));
25
        $this->defaultHandler
26
            ->expects($this->any())
27
            ->method('canHandle')
28
            ->willReturn(true);
29
        $this->defaultHandler
30
            ->expects($this->any())
31
            ->method('getName')
32
            ->willReturn('DefaultHandler');
33
        $this->defaultHandler
34
            ->expects($this->any())
35
            ->method('getType')
36
            ->willReturn('any/type');
37
        $this->object = new MediaManager();
38
        $this->object->setDefaultHandler($this->defaultHandler);
39
    }
40
41 View Code Duplication
    public function testAddHandler()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
42
    {
43
        $media = new Media();
44
        $handler = $this->getCustomHandler($media);
45
        $this->object->addHandler($handler);
46
        $this->assertEquals($handler, $this->object->getHandler($media));
47
    }
48
49
    public function testGetHandlerForType()
50
    {
51
        $handler = $this->getCustomHandler();
52
        $this->object->addHandler($handler);
53
        $this->assertEquals($handler, $this->object->getHandlerForType('custom/type'));
54
        $this->assertEquals($this->defaultHandler, $this->object->getHandlerForType('unknown/type'));
55
    }
56
57
    public function testGetHandlers()
58
    {
59
        $handler = $this->getCustomHandler();
60
        $this->object->addHandler($handler);
61
        $handlers = $this->object->getHandlers();
62
        $this->assertCount(1, $handlers);
63
        $this->assertEquals($handler, current($handlers));
64
    }
65
66 View Code Duplication
    public function testPrepareMediaWithDefaultHandler()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
    {
68
        $media = new Media();
69
        $this->defaultHandler
70
            ->expects($this->once())
71
            ->method('prepareMedia')
72
            ->with($this->equalTo($media));
73
        $this->object->prepareMedia($media);
74
    }
75
76 View Code Duplication
    public function testPrepareMediaWithCustomHandler()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
77
    {
78
        $media = new Media();
79
        $handler = $this->getCustomHandler($media);
80
        $handler
81
            ->expects($this->once())
82
            ->method('prepareMedia')
83
            ->with($this->equalTo($media));
84
        $this->object->addHandler($handler);
85
        $this->object->prepareMedia($media);
86
    }
87
88
    public function testSaveMediaWithDefaultHandler()
89
    {
90
        $media = new Media();
91
        $this->defaultHandler
92
            ->expects($this->once())
93
            ->method('saveMedia')
94
            ->with($this->equalTo($media));
95
        $this->object->saveMedia($media, true);
96
97
        $this->defaultHandler
98
            ->expects($this->once())
99
            ->method('updateMedia')
100
            ->with($this->equalTo($media));
101
        $this->object->saveMedia($media);
102
    }
103
104 View Code Duplication
    public function testCreateMediaWithCustomHandler()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
105
    {
106
        $media = new Media();
107
        $handler = $this->getCustomHandler($media);
108
        $handler
109
            ->expects($this->once())
110
            ->method('saveMedia')
111
            ->with($this->equalTo($media));
112
        $this->object->addHandler($handler);
113
        $this->object->saveMedia($media, true);
114
    }
115
116 View Code Duplication
    public function testUpdateMediaWithCustomHandler()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
117
    {
118
        $media = new Media();
119
        $handler = $this->getCustomHandler($media);
120
        $handler
121
            ->expects($this->once())
122
            ->method('updateMedia')
123
            ->with($this->equalTo($media));
124
        $this->object->addHandler($handler);
125
        $this->object->saveMedia($media);
126
    }
127
128 View Code Duplication
    public function testRemoveMediaWithDefaultHandler()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
129
    {
130
        $media = new Media();
131
        $this->defaultHandler
132
            ->expects($this->once())
133
            ->method('removeMedia')
134
            ->with($this->equalTo($media));
135
        $this->object->removeMedia($media);
136
    }
137
138 View Code Duplication
    public function testRemoveMediaWithCustomHandler()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
139
    {
140
        $media = new Media();
141
        $handler = $this->getCustomHandler($media);
142
        $handler
143
            ->expects($this->once())
144
            ->method('removeMedia')
145
            ->with($this->equalTo($media));
146
        $this->object->addHandler($handler);
147
        $this->object->removeMedia($media);
148
    }
149
150
    public function testGetHandlerWithDefaultHandler()
151
    {
152
        $media = new Media();
153
        $this->assertEquals($this->defaultHandler, $this->object->getHandler($media));
154
    }
155
156 View Code Duplication
    public function testGetHandlerWithCustomHandler()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
157
    {
158
        $media = new Media();
159
        $handler = $this->getCustomHandler($media);
160
        $this->object->addHandler($handler);
161
        $this->assertEquals($handler, $this->object->getHandler($media));
162
    }
163
164
    public function testCreateNew()
165
    {
166
        $media = new Media();
167
        $data = new \StdClass();
168
        $this->assertNull($this->object->createNew($data));
169
170
        $handler1 = $this->getCustomHandler(null, 'CustomHandler1');
171
        $handler1
172
            ->expects($this->once())
173
            ->method('createNew')
174
            ->with($this->equalTo($data))
175
            ->willReturn(false);
176
        $this->object->addHandler($handler1);
177
178
        $handler2 = $this->getCustomHandler(null, 'CustomHandler2');
179
        $handler2
180
            ->expects($this->once())
181
            ->method('createNew')
182
            ->with($this->equalTo($data))
183
            ->willReturn($media);
184
        $this->object->addHandler($handler2);
185
186
        $this->assertEquals($media, $this->object->createNew($data));
187
    }
188
189
    public function testGetFolderAddActions()
190
    {
191
        $actions = array();
192
        $this->assertEquals($actions, $this->object->getFolderAddActions());
193
194
        $actions = array('action1', 'action2');
195
        $handler = $this->getCustomHandler();
196
        $handler
197
            ->expects($this->once())
198
            ->method('getAddFolderActions')
199
            ->willReturn($actions);
200
        $this->object->addHandler($handler);
201
        $this->assertEquals($actions, $this->object->getFolderAddActions());
202
    }
203
204
    /**
205
     * @param object $media
206
     * @param string $name
207
     *
208
     * @return \PHPUnit_Framework_MockObject_MockObject
209
     */
210
    protected function getCustomHandler($media = null, $name = null)
211
    {
212
        $handler = $this->getMockForAbstractClass('Kunstmaan\MediaBundle\Helper\Media\AbstractMediaHandler', array(1));
213
        if (empty($name)) {
214
            $name = 'CustomHandler';
215
        }
216
        $handler
217
            ->expects($this->any())
218
            ->method('getName')
219
            ->willReturn($name);
220
        $handler
221
            ->expects($this->any())
222
            ->method('getType')
223
            ->willReturn('custom/type');
224
        if (!\is_null($media)) {
225
            $handler
226
                ->expects($this->any())
227
                ->method('canHandle')
228
                ->with($this->equalTo($media))
229
                ->willReturn(true);
230
        }
231
232
        return $handler;
233
    }
234
}
235