/
Base64FileExtensionTest.php
228 lines (191 loc) · 6.57 KB
/
Base64FileExtensionTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
/*
* This file is part of the Ivory Base64 File package.
*
* (c) Eric GELOEN <geloen.eric@gmail.com>
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code.
*/
namespace Ivory\Base64FileBundle\Tests\Form\Extension;
use Ivory\Base64FileBundle\Form\Extension\Base64FileExtension;
use Ivory\Base64FileBundle\Model\Base64File;
use Ivory\Base64FileBundle\Model\UploadedBase64File;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Validator\ValidatorExtension;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\Forms;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Validator\Validation;
/**
* @author GeLo <geloen.eric@gmail.com>
*/
class Base64FileExtensionTest extends \PHPUnit_Framework_TestCase
{
/**
* @var FormFactoryInterface
*/
private $factory;
/**
* @var string
*/
private $formType;
/**
* {@inheritdoc}
*/
protected function setUp()
{
$this->factory = Forms::createFormFactoryBuilder()
->addTypeExtension(new Base64FileExtension(false))
->addExtension(new ValidatorExtension(Validation::createValidator()))
->getFormFactory();
$this->formType = method_exists(AbstractType::class, 'getBlockPrefix') ? FileType::class : 'file';
}
public function testSubmitFile()
{
$form = $this->factory
->create($this->formType)
->submit($file = new File(__DIR__.'/../../Fixtures/Model/binary'));
$this->assertTrue($form->isValid());
$this->assertSame($file, $form->getData());
}
public function testSubmitNull()
{
$form = $this->factory
->create($this->formType, null, ['base64' => true])
->submit(null);
$this->assertTrue($form->isValid());
$this->assertNull($form->getData());
}
public function testSubmitMinimalValidBase64()
{
$form = $this->factory
->create($this->formType, null, ['base64' => true])
->submit($submitData = $this->getMinimalSubmitData());
$this->assertTrue($form->isValid());
$this->assertInstanceOf(UploadedBase64File::class, $data = $form->getData());
$this->assertSame($submitData['value'], $data->getData(true, false));
$this->assertSame($submitData['name'], $data->getClientOriginalName());
$this->assertSame('application/octet-stream', $data->getClientMimeType());
$this->assertNull($data->getClientSize());
}
public function testSubmitMaximalValidBase64()
{
$form = $this->factory
->create($this->formType, null, ['base64' => true])
->submit($submitData = $this->getMaximalSubmitData());
$this->assertTrue($form->isValid());
$this->assertInstanceOf(UploadedBase64File::class, $data = $form->getData());
$this->assertSame($submitData['value'], $data->getData(true, false));
$this->assertSame($submitData['name'], $data->getClientOriginalName());
$this->assertSame($submitData['mimeType'], $data->getClientMimeType());
$this->assertSame($submitData['size'], $data->getClientSize());
}
public function testSubmitInvalidStructure()
{
$form = $this->factory
->create($this->formType, null, ['base64' => true])
->submit('foo');
$this->assertFalse($form->isValid());
$this->assertNull($form->getData());
}
public function testSubmitMissingName()
{
$submitData = $this->getMinimalSubmitData();
unset($submitData['name']);
$form = $this->factory
->create($this->formType, null, ['base64' => true])
->submit($submitData);
$this->assertFalse($form->isValid());
$this->assertNull($form->getData());
}
public function testSubmitMissingValue()
{
$submitData = $this->getMinimalSubmitData();
unset($submitData['value']);
$form = $this->factory
->create($this->formType, null, ['base64' => true])
->submit($submitData);
$this->assertFalse($form->isValid());
$this->assertNull($form->getData());
}
public function testSubmitInvalidBase64Value()
{
$submitData = $this->getMinimalSubmitData();
$submitData['value'] = $this->getBinaryData();
$form = $this->factory
->create($this->formType, null, ['base64' => true])
->submit($submitData);
$this->assertFalse($form->isValid());
$this->assertNull($form->getData());
}
public function testValidInitialData()
{
$form = $this->factory->create(
$this->formType,
$data = new Base64File($this->getBase64Data()),
['base64' => true]
);
$this->assertSame($data, $form->getData());
}
/**
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
* @expectedExceptionMessage Expected an "Ivory\Base64FileBundle\Model\Base64FileInterface", got "stdClass".
*/
public function testInvalidInitialData()
{
$this->factory->create($this->formType, new \stdClass(), ['base64' => true]);
}
/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
* @expectedExceptionMessage The option "base64" with value "foo" is expected to be of type "bool"
*/
public function testInvalidBase64Option()
{
$this->factory->create($this->formType, null, ['base64' => 'foo']);
}
/**
* @return string[]
*/
private function getMinimalSubmitData()
{
return [
'value' => $this->getBase64Data(),
'name' => 'filename.png',
];
}
/**
* @return mixed[]
*/
private function getMaximalSubmitData()
{
return array_merge($this->getMinimalSubmitData(), [
'size' => strlen($this->getBinaryData()),
'mimeType' => 'image/png',
]);
}
/**
* @return string
*/
private function getBase64Data()
{
return $this->getFileData(__DIR__.'/../../Fixtures/Model/base64');
}
/**
* @return string
*/
private function getBinaryData()
{
return $this->getFileData(__DIR__.'/../../Fixtures/Model/binary');
}
/**
* @param string $file
*
* @return string
*/
private function getFileData($file)
{
return file_get_contents($file);
}
}