An error occurred while loading the file. Please try again.
-
Guillaume Perréal authored
UploadedFileTransformer: gère proprement les conversions, et passe explicitement un tableau aux vues.
168cd78a
<?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"]);
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
$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);
}
}