Commit 168cd78a authored by Guillaume Perréal's avatar Guillaume Perréal
Browse files

UploadedFileTransformer: gère proprement les conversions, et passe...

UploadedFileTransformer: gère proprement les conversions, et passe explicitement un tableau aux vues.
Showing with 146 additions and 18 deletions
+146 -18
......@@ -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;
}
}
<?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);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment