|
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\Provider; |
|
15
|
|
|
|
|
16
|
|
|
use Gaufrette\Adapter; |
|
17
|
|
|
use Gaufrette\File; |
|
18
|
|
|
use Gaufrette\Filesystem; |
|
19
|
|
|
use Sonata\AdminBundle\Form\FormMapper; |
|
20
|
|
|
use Sonata\MediaBundle\CDN\CDNInterface; |
|
21
|
|
|
use Sonata\MediaBundle\CDN\Server; |
|
22
|
|
|
use Sonata\MediaBundle\Generator\DefaultGenerator; |
|
23
|
|
|
use Sonata\MediaBundle\Metadata\MetadataBuilderInterface; |
|
24
|
|
|
use Sonata\MediaBundle\Model\MediaInterface; |
|
25
|
|
|
use Sonata\MediaBundle\Provider\BaseProvider; |
|
26
|
|
|
use Sonata\MediaBundle\Tests\Entity\Media; |
|
27
|
|
|
use Sonata\MediaBundle\Thumbnail\ThumbnailInterface; |
|
28
|
|
|
use Symfony\Component\Form\FormBuilder; |
|
29
|
|
|
|
|
30
|
|
|
class BaseProviderTest extends AbstractProviderTest |
|
31
|
|
|
{ |
|
32
|
|
|
public function getProvider() |
|
33
|
|
|
{ |
|
34
|
|
|
$adapter = $this->createMock(Adapter::class); |
|
35
|
|
|
|
|
36
|
|
|
$filesystem = $this->getMockBuilder(Filesystem::class) |
|
37
|
|
|
->setMethods(['get']) |
|
38
|
|
|
->setConstructorArgs([$adapter]) |
|
39
|
|
|
->getMock(); |
|
40
|
|
|
$file = $this->getMockBuilder(File::class) |
|
|
|
|
|
|
41
|
|
|
->setConstructorArgs(['foo', $filesystem]) |
|
42
|
|
|
->getMock(); |
|
43
|
|
|
|
|
44
|
|
|
$cdn = new Server('/uploads/media'); |
|
45
|
|
|
|
|
46
|
|
|
$generator = new DefaultGenerator(); |
|
47
|
|
|
|
|
48
|
|
|
$thumbnail = $this->createMock(ThumbnailInterface::class); |
|
49
|
|
|
|
|
50
|
|
|
$metadata = $this->createMock(MetadataBuilderInterface::class); |
|
51
|
|
|
|
|
52
|
|
|
$provider = new TestProvider('test', $filesystem, $cdn, $generator, $thumbnail, $metadata); |
|
|
|
|
|
|
53
|
|
|
$this->assertInstanceOf(BaseProvider::class, $provider); |
|
54
|
|
|
|
|
55
|
|
|
return $provider; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function testBaseProvider(): void |
|
59
|
|
|
{ |
|
60
|
|
|
$provider = $this->getProvider(); |
|
61
|
|
|
$provider->setTemplates([ |
|
62
|
|
|
'edit' => 'edit.twig', |
|
63
|
|
|
]); |
|
64
|
|
|
|
|
65
|
|
|
$this->assertInternalType('array', $provider->getTemplates()); |
|
66
|
|
|
$this->assertSame('edit.twig', $provider->getTemplate('edit')); |
|
67
|
|
|
|
|
68
|
|
|
$this->assertInstanceOf(CDNInterface::class, $provider->getCdn()); |
|
69
|
|
|
|
|
70
|
|
|
$provider->addFormat('small', []); |
|
71
|
|
|
|
|
72
|
|
|
$this->assertInternalType('array', $provider->getFormat('small')); |
|
73
|
|
|
|
|
74
|
|
|
$media = new Media(); |
|
75
|
|
|
$media->setContext('test'); |
|
76
|
|
|
|
|
77
|
|
|
$this->assertSame('admin', $provider->getFormatName($media, 'admin')); |
|
78
|
|
|
$this->assertSame('reference', $provider->getFormatName($media, 'reference')); |
|
79
|
|
|
$this->assertSame('test_small', $provider->getFormatName($media, 'small')); |
|
80
|
|
|
$this->assertSame('test_small', $provider->getFormatName($media, 'test_small')); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function testGetCdnPath(): void |
|
84
|
|
|
{ |
|
85
|
|
|
$provider = $this->getProvider(); |
|
86
|
|
|
$this->assertSame('/uploads/media/my_file.txt', $provider->getCdnPath('my_file.txt', false)); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function testMetadata(): void |
|
90
|
|
|
{ |
|
91
|
|
|
$provider = $this->getProvider(); |
|
92
|
|
|
|
|
93
|
|
|
$this->assertSame('test', $provider->getProviderMetadata()->getTitle()); |
|
94
|
|
|
$this->assertSame('test.description', $provider->getProviderMetadata()->getDescription()); |
|
95
|
|
|
$this->assertNotNull($provider->getProviderMetadata()->getImage()); |
|
96
|
|
|
$this->assertSame('fa fa-file', $provider->getProviderMetadata()->getOption('class')); |
|
97
|
|
|
$this->assertSame('SonataMediaBundle', $provider->getProviderMetadata()->getDomain()); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function testPostRemove(): void |
|
101
|
|
|
{ |
|
102
|
|
|
$reflect = new \ReflectionClass(BaseProvider::class); |
|
103
|
|
|
$prop = $reflect->getProperty('clones'); |
|
104
|
|
|
$prop->setAccessible(true); |
|
105
|
|
|
|
|
106
|
|
|
$provider = $this->getProvider(); |
|
107
|
|
|
$media = new Media(); |
|
108
|
|
|
$media->setId(1399); |
|
109
|
|
|
$media->setProviderReference('1f981a048e7d8b671415d17e9633abc0059df394.png'); |
|
110
|
|
|
$hash = spl_object_hash($media); |
|
111
|
|
|
|
|
112
|
|
|
$provider->preRemove($media); |
|
113
|
|
|
|
|
114
|
|
|
$this->assertArrayHasKey($hash, $prop->getValue($provider)); |
|
115
|
|
|
|
|
116
|
|
|
$media->setId(null); // Emulate an object detached from the EntityManager. |
|
117
|
|
|
$provider->postRemove($media); |
|
118
|
|
|
|
|
119
|
|
|
$this->assertArrayNotHasKey($hash, $prop->getValue($provider)); |
|
120
|
|
|
$this->assertSame('/0001/02/1f981a048e7d8b671415d17e9633abc0059df394.png', $provider->prevReferenceImage); |
|
121
|
|
|
|
|
122
|
|
|
$prop->setAccessible(false); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
class TestProvider extends BaseProvider |
|
127
|
|
|
{ |
|
128
|
|
|
/** |
|
129
|
|
|
* @var string |
|
130
|
|
|
*/ |
|
131
|
|
|
public $prevReferenceImage; |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* {@inheritdoc} |
|
135
|
|
|
*/ |
|
136
|
|
|
public function getHelperProperties(MediaInterface $media, $format, $options = []): void |
|
137
|
|
|
{ |
|
138
|
|
|
// TODO: Implement getHelperProperties() method. |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* {@inheritdoc} |
|
143
|
|
|
*/ |
|
144
|
|
|
public function postPersist(MediaInterface $media): void |
|
145
|
|
|
{ |
|
146
|
|
|
// TODO: Implement postPersist() method. |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* {@inheritdoc} |
|
151
|
|
|
*/ |
|
152
|
|
|
public function buildEditForm(FormMapper $form): void |
|
153
|
|
|
{ |
|
154
|
|
|
// TODO: Implement buildEditForm() method. |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* {@inheritdoc} |
|
159
|
|
|
*/ |
|
160
|
|
|
public function buildCreateForm(FormMapper $form): void |
|
161
|
|
|
{ |
|
162
|
|
|
// TODO: Implement buildCreateForm() method. |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* {@inheritdoc} |
|
167
|
|
|
*/ |
|
168
|
|
|
public function postUpdate(MediaInterface $media): void |
|
169
|
|
|
{ |
|
170
|
|
|
// TODO: Implement postUpdate() method. |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* {@inheritdoc} |
|
175
|
|
|
*/ |
|
176
|
|
|
public function getAbsolutePath(MediaInterface $media): void |
|
|
|
|
|
|
177
|
|
|
{ |
|
178
|
|
|
// TODO: Implement getAbsolutePath() method. |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* {@inheritdoc} |
|
183
|
|
|
*/ |
|
184
|
|
|
public function getReferenceImage(MediaInterface $media): string |
|
185
|
|
|
{ |
|
186
|
|
|
// A copy of the code from \Sonata\MediaBundle\Provider\FileProvider::getReferenceImage() |
|
187
|
|
|
$this->prevReferenceImage = sprintf('%s/%s', |
|
188
|
|
|
$this->generatePath($media), |
|
189
|
|
|
$media->getProviderReference() |
|
190
|
|
|
); |
|
191
|
|
|
|
|
192
|
|
|
return $this->prevReferenceImage; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* {@inheritdoc} |
|
197
|
|
|
*/ |
|
198
|
|
|
public function generatePrivateUrl(MediaInterface $media, $format): void |
|
199
|
|
|
{ |
|
200
|
|
|
// TODO: Implement generatePrivateUrl() method. |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* {@inheritdoc} |
|
205
|
|
|
*/ |
|
206
|
|
|
public function generatePublicUrl(MediaInterface $media, $format): void |
|
207
|
|
|
{ |
|
208
|
|
|
// TODO: Implement generatePublicUrl() method. |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* {@inheritdoc} |
|
213
|
|
|
*/ |
|
214
|
|
|
public function getReferenceFile(MediaInterface $media): void |
|
215
|
|
|
{ |
|
216
|
|
|
// TODO: Implement getReferenceFile() method. |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* {@inheritdoc} |
|
221
|
|
|
*/ |
|
222
|
|
|
public function preUpdate(MediaInterface $media): void |
|
223
|
|
|
{ |
|
224
|
|
|
// TODO: Implement preUpdate() method. |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* {@inheritdoc} |
|
229
|
|
|
*/ |
|
230
|
|
|
public function prePersist(MediaInterface $media): void |
|
231
|
|
|
{ |
|
232
|
|
|
// TODO: Implement prePersist() method. |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* {@inheritdoc} |
|
237
|
|
|
*/ |
|
238
|
|
|
public function getDownloadResponse(MediaInterface $media, $format, $mode, array $headers = []): void |
|
239
|
|
|
{ |
|
240
|
|
|
// TODO: Implement getDownloadResponse() method. |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* {@inheritdoc} |
|
245
|
|
|
*/ |
|
246
|
|
|
public function buildMediaType(FormBuilder $formBuilder): void |
|
247
|
|
|
{ |
|
248
|
|
|
// TODO: Implement buildMediaType() method. |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* {@inheritdoc} |
|
253
|
|
|
*/ |
|
254
|
|
|
public function updateMetadata(MediaInterface $media, $force = false): void |
|
255
|
|
|
{ |
|
256
|
|
|
// TODO: Implement updateMetadata() method. |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* {@inheritdoc} |
|
261
|
|
|
*/ |
|
262
|
|
|
protected function doTransform(MediaInterface $media): void |
|
263
|
|
|
{ |
|
264
|
|
|
// TODO: Implement doTransform() method. |
|
265
|
|
|
} |
|
266
|
|
|
} |
|
267
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.