/
FileValidatorTest.php
166 lines (136 loc) · 5.25 KB
/
FileValidatorTest.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
<?php
namespace Symfony\Tests\Component\Validator;
use Symfony\Component\Validator\Constraints\File;
use Symfony\Component\Validator\Constraints\FileValidator;
class FileValidatorTest extends \PHPUnit_Framework_TestCase
{
protected $validator;
protected $path;
protected $file;
protected function setUp()
{
$this->validator = new FileValidator();
$this->path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'FileValidatorTest';
$this->file = fopen($this->path, 'w');
}
protected function tearDown()
{
fclose($this->file);
}
public function testNullIsValid()
{
$this->assertTrue($this->validator->isValid(null, new File()));
}
public function testEmptyStringIsValid()
{
$this->assertTrue($this->validator->isValid('', new File()));
}
public function testExpectsStringCompatibleTypeOrFile()
{
$this->setExpectedException('Symfony\Component\Validator\Exception\UnexpectedTypeException');
$this->validator->isValid(new \stdClass(), new File());
}
public function testValidFile()
{
$this->assertTrue($this->validator->isValid($this->path, new File()));
}
public function testTooLargeBytes()
{
fwrite($this->file, str_repeat('0', 11));
$constraint = new File(array(
'maxSize' => 10,
'maxSizeMessage' => 'myMessage',
));
$this->assertFalse($this->validator->isValid($this->path, $constraint));
$this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
$this->assertEquals($this->validator->getMessageParameters(), array(
'{{ limit }}' => '10 bytes',
'{{ size }}' => '11 bytes',
'{{ file }}' => $this->path,
));
}
public function testTooLargeKiloBytes()
{
fwrite($this->file, str_repeat('0', 1400));
$constraint = new File(array(
'maxSize' => '1k',
'maxSizeMessage' => 'myMessage',
));
$this->assertFalse($this->validator->isValid($this->path, $constraint));
$this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
$this->assertEquals($this->validator->getMessageParameters(), array(
'{{ limit }}' => '1 kB',
'{{ size }}' => '1.4 kB',
'{{ file }}' => $this->path,
));
}
public function testTooLargeMegaBytes()
{
fwrite($this->file, str_repeat('0', 1400000));
$constraint = new File(array(
'maxSize' => '1M',
'maxSizeMessage' => 'myMessage',
));
$this->assertFalse($this->validator->isValid($this->path, $constraint));
$this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
$this->assertEquals($this->validator->getMessageParameters(), array(
'{{ limit }}' => '1 MB',
'{{ size }}' => '1.4 MB',
'{{ file }}' => $this->path,
));
}
public function testInvalidMaxSize()
{
$constraint = new File(array(
'maxSize' => '1abc',
));
$this->setExpectedException('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
$this->validator->isValid($this->path, $constraint);
}
public function testFileNotFound()
{
$constraint = new File(array(
'notFoundMessage' => 'myMessage',
));
$this->assertFalse($this->validator->isValid('foobar', $constraint));
$this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
$this->assertEquals($this->validator->getMessageParameters(), array(
'{{ file }}' => 'foobar',
));
}
public function testValidMimeType()
{
$file = $this->getMock('Symfony\Component\HttpFoundation\File\File', array(), array(), '', false);
$file->expects($this->any())
->method('getPath')
->will($this->returnValue($this->path));
$file->expects($this->any())
->method('getMimeType')
->will($this->returnValue('image/jpg'));
$constraint = new File(array(
'mimeTypes' => array('image/png', 'image/jpg'),
));
$this->assertTrue($this->validator->isValid($file, $constraint));
}
public function testInvalidMimeType()
{
$file = $this->getMock('Symfony\Component\HttpFoundation\File\File', array(), array(), '', false);
$file->expects($this->any())
->method('getPath')
->will($this->returnValue($this->path));
$file->expects($this->any())
->method('getMimeType')
->will($this->returnValue('application/pdf'));
$constraint = new File(array(
'mimeTypes' => array('image/png', 'image/jpg'),
'mimeTypesMessage' => 'myMessage',
));
$this->assertFalse($this->validator->isValid($file, $constraint));
$this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
$this->assertEquals($this->validator->getMessageParameters(), array(
'{{ type }}' => '"application/pdf"',
'{{ types }}' => '"image/png", "image/jpg"',
'{{ file }}' => $this->path,
));
}
}