Skip to content

Commit 0775050

Browse files
committed
Merge pull request #72 from skwi/format-on-replace
Provides ability to specify formats for `replaceFile` method in `ImageManagerTrait`
2 parents 12f0cb7 + 5b2f119 commit 0775050

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

Manager/Traits/ImageManagerTrait.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,11 @@ public function removeFiles(BaseEntityWithFile $entity, $properties = array(), $
129129
* @param string $sourceFilepath The image source folder
130130
* @param string|null $destFilepath The image destination folder
131131
* @param string $operation 'copy' or 'rename'
132+
* @param array $formatList Format to override, all if left null (recommended)
132133
*
133134
* @return array|null An array containing informations about the copied file
134135
*/
135-
public function replaceFile(BaseEntityWithFile $entity, $propertyName, $sourceFilepath, $destFilepath = null, $operation = self::OPERATION_COPY)
136+
public function replaceFile(BaseEntityWithFile $entity, $propertyName, $sourceFilepath, $destFilepath = null, $operation = self::OPERATION_COPY, array $formatList = null)
136137
{
137138
if (!in_array($operation, array(self::OPERATION_COPY, self::OPERATION_RENAME))) {
138139
throw new \InvalidArgumentException(sprintf('$operation only accept "%s" or "%s" value', self::OPERATION_COPY, self::OPERATION_RENAME));
@@ -149,7 +150,11 @@ public function replaceFile(BaseEntityWithFile $entity, $propertyName, $sourceFi
149150
$entity->$propertyFileNameSetter($this->buildDestination($entity, $propertyName, $sourceFilepath, null));
150151
}
151152

152-
foreach ($this->imageFormatChoices[$propertyName] as $format) {
153+
if (null === $formatList) {
154+
$formatList = $this->imageFormatChoices[$propertyName];
155+
}
156+
157+
foreach ($formatList as $format) {
153158

154159
$oldDestPath = $this->transformPathWithFormat($oldDestPathPattern, $format);
155160
if (is_file($oldDestPath)) {

Tests/Units/Manager/BaseEntityWithImageManager.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,36 @@ public function testReplaceFile()
308308
->string($entity->getUserPhotoFilename())->isEqualTo($this->workspace.'/dest/replacement_{-imgformat-}.png')
309309
->boolean(file_exists($this->workspace.'/image.png'))->isFalse()
310310

311+
->assert('BaseEntityWithImageManager::replaceFile for specific format')
312+
->given(
313+
touch($this->workspace.'/my-photo_original.png'),
314+
touch($this->workspace.'/my-photo_format1Definition.png'),
315+
touch($this->workspace.'/my-photo_format2Definition.png'),
316+
$entity = $this->createMockEntity(array(
317+
'userPhoto' => $this->createMockFileType(__DIR__.'/../../data/image.png'),
318+
'userPhotoFilename' => 'my-photo_{-imgformat-}.png',
319+
),
320+
$entityManager = $this->createMockEntityManager())
321+
)
322+
->if(
323+
$testedClass = $this->createTestedClassInstance(array(
324+
'bundle.web' => $this->workspace.'/',
325+
'bundle.root' => $this->workspace.'/',
326+
'userPhoto' => '/{-origin-}_{-imgformat-}.{-ext-}',
327+
), $entityManager)
328+
)
329+
->when($fileProperties = $testedClass->replaceFile($entity, 'userPhoto', __DIR__.'/../../data/image.png', null, Manager\BaseEntityWithImageManager::OPERATION_COPY, ['format1Definition']))
330+
->then
331+
->array($fileProperties)
332+
->hasKeys(array('extension', 'original', 'size', 'mime'))
333+
->string['extension']->isEqualTo('png')
334+
->string['original']->isEqualTo('image.png')
335+
->integer['size']->isGreaterThan(0)
336+
->string($entity->getUserPhotoFilename())->isEqualTo('/image_{-imgformat-}.png')
337+
->boolean(file_exists($this->workspace.'/image_original.png'))->isTrue()
338+
->boolean(file_exists($this->workspace.'/image_format1Definition.png'))->isTrue()
339+
->boolean(file_exists($this->workspace.'/image_format2Definition.png'))->isTrue()
340+
311341
->assert('BaseEntityWithImageManager::replaceFile with invalid file')
312342
->if(
313343
$testedClass = $this->createTestedClassInstance(array(

0 commit comments

Comments
 (0)