diff --git a/Form/DataTranformer/UploadedFileTransformer.php b/Form/DataTranformer/UploadedFileTransformer.php index 790bcd86ecdf44a2f7de62aed0019e22a46663a4..d4eab3a19e0008b1c1087ad621b2d954ca5312d5 100644 --- a/Form/DataTranformer/UploadedFileTransformer.php +++ b/Form/DataTranformer/UploadedFileTransformer.php @@ -45,7 +45,7 @@ class UploadedFileTransformer implements DataTransformerInterface return $this->reverseTransformFile($value); } - if(null === $value || '' === $value) { + if(empty($value)) { return []; } @@ -54,9 +54,9 @@ class UploadedFileTransformer implements DataTransformerInterface } $result = []; - foreach($value as $index => $identifier) { + foreach($value as $identifier) { if(null !== $file = $this->reverseTransformFile($identifier)) { - $result[$index] = $file; + $result[] = $file; } } return $result; @@ -80,11 +80,12 @@ class UploadedFileTransformer implements DataTransformerInterface } $result = []; - foreach($value as $index => $file) { + foreach($value as $file) { if(null !== $item = $this->transformFile($file)) { - $result[$index] = $item; + $result[] = $item; } } + return $result; } @@ -93,9 +94,9 @@ class UploadedFileTransformer implements DataTransformerInterface * @param mixed $value * @return mixed */ - protected function transformFile($value) + public function transformFile($value) { - if(null === $value) { + if(empty($value)) { return null; } @@ -103,7 +104,7 @@ class UploadedFileTransformer implements DataTransformerInterface throw new TransformationFailedException('Expected an UploadedFileInterface.'); } - return $value; + return $value->toArray(); } /** @@ -111,23 +112,32 @@ class UploadedFileTransformer implements DataTransformerInterface * @param mixed $value * @return UploadedFileInterface */ - protected function reverseTransformFile($value) + public function reverseTransformFile($value) { - if(is_string($value)) { - $value = $this->fileManager->get($value); + if(empty($value)) { + return null; + } + + if(!is_array($value)) { + throw new TransformationFailedException(sprintf('Expected an array, not a %s.', is_object($value) ? get_class($value) : gettype($value))); } - if(null === $value || '' === $value) { + if(empty($value['id'])) { return null; } - if($value instanceof UploadedFileInterface) { - if($value->isOrphelin()) { - $value->setEtat(UploadedFileInterface::ETAT_NORMAL); - } - return $value; + $identifier = $value['id']; + + $file = $this->fileManager->get($identifier); + if(null === $file) { + return null; + } + + if($file->isOrphelin()) { + $file->setEtat(UploadedFileInterface::ETAT_NORMAL); } + $file->setDescription(!empty($value['description']) ? $value['description'] : null); - throw new TransformationFailedException('Expected an UploadedFileInterface or a valid file identifier.'); + return $file; } } diff --git a/Tests/Form/DataTranformer/UploadedFileTransformerTest.php b/Tests/Form/DataTranformer/UploadedFileTransformerTest.php new file mode 100644 index 0000000000000000000000000000000000000000..80286d54d1ca848d767a683515097fe22bb8a285 --- /dev/null +++ b/Tests/Form/DataTranformer/UploadedFileTransformerTest.php @@ -0,0 +1,118 @@ +<?php + +namespace Irstea\FileUploadBundle\Tests\Form\DataTranformer; + +use Irstea\FileUploadBundle\Form\DataTranformer\UploadedFileTransformer; +use Irstea\FileUploadBundle\Model\UploadedFileInterface; +use PHPUnit_Framework_MockObject_MockObject; +use PHPUnit_Framework_TestCase; + +/** + * Generated by PHPUnit_SkeletonGenerator on 2015-01-27 at 11:36:06. + */ +class UploadedFileTransformerTest extends PHPUnit_Framework_TestCase +{ + /** + * + * @var PHPUnit_Framework_MockObject_MockObject + */ + protected $fileManager; + + /** + * @var UploadedFileTransformer + */ + protected $transformer; + + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + */ + protected function setUp() + { + $this->fileManager = $this->getMock('\Irstea\FileUploadBundle\Model\FileManagerInterface'); + + $this->transformer = new UploadedFileTransformer($this->fileManager, true); + } + + /** + * @dataProvider getEmptyValues + */ + public function testEmptyValues($expected, $method, $value) + { + $this->assertEquals($expected, $this->transformer->$method($value)); + } + + public function getEmptyValues() + { + $values = []; + foreach([null, '', []] as $value) { + foreach(['transform', 'reverseTransform'] as $method) { + $values[] = [[], $method, $value]; + $values[] = [null, $method."File", $value]; + } + } + return $values; + } + + /** + * @expectedException Symfony\Component\Form\Exception\TransformationFailedException + */ + public function testTransformFileInvalidObject() + { + $this->transformer->transform("x"); + } + + public function testTransformFile() + { + $file = $this->getMock('\Irstea\FileUploadBundle\Model\UploadedFileInterface'); + + $file->expects($this->once())->method('toArray')->willReturn(["bla"]); + + $this->assertEquals(["bla"], $this->transformer->transformFile($file)); + } + + /** + * @expectedException Symfony\Component\Form\Exception\TransformationFailedException + */ + public function testReverseTransformFileInvalidObject() + { + $this->transformer->reverseTransformFile("x"); + } + + public function testReverseTransformFileNoId() + { + $this->assertNull($this->transformer->reverseTransformFile(['desc' => ''])); + } + + public function testReverseTransformFile() + { + $file = $this->getMock('\Irstea\FileUploadBundle\Model\UploadedFileInterface'); + + $this->fileManager->expects($this->once())->method("get")->with("FOO")->willReturn($file); + $file->expects($this->once())->method('isOrphelin')->willReturn(true); + $file->expects($this->once())->method('setEtat')->with(UploadedFileInterface::ETAT_NORMAL); + $file->expects($this->once())->method('setDescription')->with('truc'); + + $this->assertSame($file, $this->transformer->reverseTransformFile(['id' => 'FOO', 'description' => 'truc'])); + } + + public function testTransformEmptyEntries() + { + $file = $this->getMock('\Irstea\FileUploadBundle\Model\UploadedFileInterface'); + $file->expects($this->any())->method('toArray')->willReturn(["bla"]); + + $result = $this->transformer->transform([$file, null, $file]); + + $this->assertEquals([['bla'], ['bla']], $result); + } + + public function testReverseTransformEmptyEntries() + { + $file = $this->getMock('\Irstea\FileUploadBundle\Model\UploadedFileInterface'); + $this->fileManager->expects($this->any())->method("get")->with("FOO")->willReturn($file); + + $result = $this->transformer->reverseTransform([['id' => 'FOO'], null, ['description' => 'muche'], ['id' => 'FOO']]); + + $this->assertEquals([$file, $file], $result); + } +}