UploadedFileTransformerTest.php 3.87 KB
Newer Older
Guillaume Perréal's avatar
Guillaume Perréal committed
1
<?php declare(strict_types=1);
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
2
/*
3
 * Copyright (C) 2015-2018 IRSTEA
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
4
5
 * All rights reserved.
 */
6
7
8
9

namespace Irstea\FileUploadBundle\Tests\Form\DataTranformer;

use Irstea\FileUploadBundle\Form\DataTranformer\UploadedFileTransformer;
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
10
use Irstea\FileUploadBundle\Model\FileManagerInterface;
11
use Irstea\FileUploadBundle\Model\UploadedFileInterface;
12
use PHPUnit\Framework\TestCase;
13
14
15
16
17
use PHPUnit_Framework_MockObject_MockObject;

/**
 * Generated by PHPUnit_SkeletonGenerator on 2015-01-27 at 11:36:06.
 */
18
class UploadedFileTransformerTest extends TestCase
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
{
    /**
     * @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()
    {
36
        $this->fileManager = $this->createMock(FileManagerInterface::class);
37
38
39
40
41
42

        $this->transformer = new UploadedFileTransformer($this->fileManager, true);
    }

    /**
     * @dataProvider getEmptyValues
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
43
44
45
46
     *
     * @param mixed $expected
     * @param mixed $method
     * @param mixed $value
47
48
49
     */
    public function testEmptyValues($expected, $method, $value)
    {
50
        static::assertEquals($expected, $this->transformer->$method($value));
51
52
53
54
55
    }

    public function getEmptyValues()
    {
        $values = [];
Raidelet Nicolas's avatar
CS    
Raidelet Nicolas committed
56
57
        foreach ([null, '', []] as $value) {
            foreach (['transform', 'reverseTransform'] as $method) {
58
                $values[] = [[], $method, $value];
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
59
                $values[] = [null, $method . 'File', $value];
60
61
            }
        }
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
62

63
64
65
66
        return $values;
    }

    /**
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
67
     * @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
68
69
70
     */
    public function testTransformFileInvalidObject()
    {
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
71
        $this->transformer->transform('x');
72
73
74
75
    }

    public function testTransformFile()
    {
76
        $file = $this->createMock(UploadedFileInterface::class);
77

78
        $file->expects(static::once())->method('toArray')->willReturn(['bla']);
79

80
        static::assertEquals(['bla'], $this->transformer->transformFile($file));
81
82
83
    }

    /**
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
84
     * @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
85
86
87
     */
    public function testReverseTransformFileInvalidObject()
    {
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
88
        $this->transformer->reverseTransformFile('x');
89
90
91
92
    }

    public function testReverseTransformFileNoId()
    {
93
        static::assertNull($this->transformer->reverseTransformFile(['desc' => '']));
94
95
96
97
    }

    public function testReverseTransformFile()
    {
98
        $file = $this->createMock(UploadedFileInterface::class);
99

100
101
102
103
        $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');
104

105
        static::assertSame($file, $this->transformer->reverseTransformFile(['id' => 'FOO', 'description' => 'truc']));
106
107
108
109
    }

    public function testTransformEmptyEntries()
    {
110
        $file = $this->createMock(UploadedFileInterface::class);
111
        $file->expects(static::any())->method('toArray')->willReturn(['bla']);
112
113
114

        $result = $this->transformer->transform([$file, null, $file]);

115
        static::assertEquals([['bla'], ['bla']], $result);
116
117
118
119
    }

    public function testReverseTransformEmptyEntries()
    {
120
        $file = $this->createMock(UploadedFileInterface::class);
121
        $this->fileManager->expects(static::any())->method('get')->with('FOO')->willReturn($file);
122
123
124

        $result = $this->transformer->reverseTransform([['id' => 'FOO'], null, ['description' => 'muche'], ['id' => 'FOO']]);

125
        static::assertEquals([$file, $file], $result);
126
127
    }
}