UploadedFileTransformerTest.php 3.88 KiB
<?php declare(strict_types=1);
/*
 * Copyright (C) 2015-2017 IRSTEA
 * All rights reserved.
 */
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;
use Irstea\FileUploadBundle\Model\FileManagerInterface;
/**
 * 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(FileManagerInterface::class);
        $this->transformer = new UploadedFileTransformer($this->fileManager, true);
    /**
     * @dataProvider getEmptyValues
     * @param mixed $expected
     * @param mixed $method
     * @param mixed $value
    public function testEmptyValues($expected, $method, $value)
        static::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()
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
$this->transformer->transform('x'); } public function testTransformFile() { $file = $this->getMock(UploadedFileInterface::class); $file->expects(static::once())->method('toArray')->willReturn(['bla']); static::assertEquals(['bla'], $this->transformer->transformFile($file)); } /** * @expectedException \Symfony\Component\Form\Exception\TransformationFailedException */ public function testReverseTransformFileInvalidObject() { $this->transformer->reverseTransformFile('x'); } public function testReverseTransformFileNoId() { static::assertNull($this->transformer->reverseTransformFile(['desc' => ''])); } public function testReverseTransformFile() { $file = $this->getMock(UploadedFileInterface::class); $this->fileManager->expects(static::once())->method('get')->with('FOO')->willReturn($file); $file->expects(static::once())->method('isOrphelin')->willReturn(true); $file->expects(static::once())->method('setEtat')->with(UploadedFileInterface::ETAT_NORMAL); $file->expects(static::once())->method('setDescription')->with('truc'); static::assertSame($file, $this->transformer->reverseTransformFile(['id' => 'FOO', 'description' => 'truc'])); } public function testTransformEmptyEntries() { $file = $this->getMock(UploadedFileInterface::class); $file->expects(static::any())->method('toArray')->willReturn(['bla']); $result = $this->transformer->transform([$file, null, $file]); static::assertEquals([['bla'], ['bla']], $result); } public function testReverseTransformEmptyEntries() { $file = $this->getMock(UploadedFileInterface::class); $this->fileManager->expects(static::any())->method('get')->with('FOO')->willReturn($file); $result = $this->transformer->reverseTransform([['id' => 'FOO'], null, ['description' => 'muche'], ['id' => 'FOO']]); static::assertEquals([$file, $file], $result); } }