@@ -33,7 +33,13 @@ protected function setUp()
33
33
34
34
protected function tearDown ()
35
35
{
36
- fclose ($ this ->file );
36
+ if (is_resource ($ this ->file )) {
37
+ fclose ($ this ->file );
38
+ }
39
+
40
+ if (file_exists ($ this ->path )) {
41
+ unlink ($ this ->path );
42
+ }
37
43
38
44
$ this ->context = null ;
39
45
$ this ->validator = null ;
@@ -82,90 +88,98 @@ public function testValidUploadedfile()
82
88
$ this ->validator ->validate ($ file , new File ());
83
89
}
84
90
85
- public function testTooLargeBytes ()
91
+ public function provideMaxSizeExceededTests ()
86
92
{
87
- fwrite ($ this ->file , str_repeat ('0 ' , 11 ));
93
+ return array (
94
+ array (11 , 10 , '11 ' , '10 ' , 'bytes ' ),
88
95
89
- $ constraint = new File (array (
90
- 'maxSize ' => 10 ,
91
- 'maxSizeMessage ' => 'myMessage ' ,
92
- ));
96
+ array (ceil (1.005 *1000 ), ceil (1.005 *1000 ) - 1 , '1005 ' , '1004 ' , 'bytes ' ),
97
+ array (ceil (1.005 *1000 *1000 ), ceil (1.005 *1000 *1000 ) - 1 , '1005000 ' , '1004999 ' , 'bytes ' ),
93
98
94
- $ this ->context ->expects ($ this ->once ())
95
- ->method ('addViolation ' )
96
- ->with ('myMessage ' , array (
97
- '{{ limit }} ' => '10 ' ,
98
- '{{ size }} ' => '11 ' ,
99
- '{{ suffix }} ' => 'bytes ' ,
100
- '{{ file }} ' => $ this ->path ,
101
- ));
99
+ // round(size) == 1.01kB, limit == 1kB
100
+ array (ceil (1.005 *1000 ), 1000 , '1.01 ' , '1 ' , 'kB ' ),
101
+ array (ceil (1.005 *1000 ), '1k ' , '1.01 ' , '1 ' , 'kB ' ),
102
102
103
- $ this ->validator ->validate ($ this ->getFile ($ this ->path ), $ constraint );
104
- }
103
+ // round(size) == 1kB, limit == 1kB -> use bytes
104
+ array (ceil (1.004 *1000 ), 1000 , '1004 ' , '1000 ' , 'bytes ' ),
105
+ array (ceil (1.004 *1000 ), '1k ' , '1004 ' , '1000 ' , 'bytes ' ),
105
106
106
- public function testTooLargeKiloBytes ()
107
- {
108
- fwrite ($ this ->file , str_repeat ('0 ' , 1400 ));
107
+ array (1000 + 1 , 1000 , '1001 ' , '1000 ' , 'bytes ' ),
108
+ array (1000 + 1 , '1k ' , '1001 ' , '1000 ' , 'bytes ' ),
109
109
110
- $ constraint = new File ( array (
111
- ' maxSize ' => ' 1k ' ,
112
- ' maxSizeMessage ' => ' myMessage ' ,
113
- ));
110
+ // round(size) == 1.01MB, limit == 1MB
111
+ array ( ceil ( 1.005 * 1000 * 1000 ), 1000 * 1000 , ' 1.01 ' , ' 1 ' , ' MB ' ) ,
112
+ array ( ceil ( 1.005 * 1000 * 1000 ), ' 1000k ' , ' 1.01 ' , ' 1 ' , ' MB ' ) ,
113
+ array ( ceil ( 1.005 * 1000 * 1000 ), ' 1M ' , ' 1.01 ' , ' 1 ' , ' MB ' ),
114
114
115
- $ this ->context ->expects ($ this ->once ())
116
- ->method ('addViolation ' )
117
- ->with ('myMessage ' , array (
118
- '{{ limit }} ' => '1 ' ,
119
- '{{ size }} ' => '1.37 ' ,
120
- '{{ suffix }} ' => 'KiB ' ,
121
- '{{ file }} ' => $ this ->path ,
122
- ));
115
+ // round(size) == 1MB, limit == 1MB -> use kB
116
+ array (ceil (1.004 *1000 *1000 ), 1000 *1000 , '1004 ' , '1000 ' , 'kB ' ),
117
+ array (ceil (1.004 *1000 *1000 ), '1000k ' , '1004 ' , '1000 ' , 'kB ' ),
118
+ array (ceil (1.004 *1000 *1000 ), '1M ' , '1004 ' , '1000 ' , 'kB ' ),
123
119
124
- $ this ->validator ->validate ($ this ->getFile ($ this ->path ), $ constraint );
120
+ array (1000 *1000 + 1 , 1000 *1000 , '1000001 ' , '1000000 ' , 'bytes ' ),
121
+ array (1000 *1000 + 1 , '1000k ' , '1000001 ' , '1000000 ' , 'bytes ' ),
122
+ array (1000 *1000 + 1 , '1M ' , '1000001 ' , '1000000 ' , 'bytes ' ),
123
+ );
125
124
}
126
125
127
- public function testTooLargeMegaBytes ()
126
+ /**
127
+ * @dataProvider provideMaxSizeExceededTests
128
+ */
129
+ public function testMaxSizeExceeded ($ bytesWritten , $ limit , $ sizeAsString , $ limitAsString , $ suffix )
128
130
{
129
- fwrite ($ this ->file , str_repeat ('0 ' , 1400000 ));
131
+ fseek ($ this ->file , $ bytesWritten -1 , SEEK_SET );
132
+ fwrite ($ this ->file , '0 ' );
133
+ fclose ($ this ->file );
130
134
131
135
$ constraint = new File (array (
132
- 'maxSize ' => ' 1M ' ,
136
+ 'maxSize ' => $ limit ,
133
137
'maxSizeMessage ' => 'myMessage ' ,
134
138
));
135
139
136
140
$ this ->context ->expects ($ this ->once ())
137
141
->method ('addViolation ' )
138
142
->with ('myMessage ' , array (
139
- '{{ limit }} ' => ' 1 ' ,
140
- '{{ size }} ' => ' 1.34 ' ,
141
- '{{ suffix }} ' => ' MiB ' ,
143
+ '{{ limit }} ' => $ limitAsString ,
144
+ '{{ size }} ' => $ sizeAsString ,
145
+ '{{ suffix }} ' => $ suffix ,
142
146
'{{ file }} ' => $ this ->path ,
143
147
));
144
148
145
149
$ this ->validator ->validate ($ this ->getFile ($ this ->path ), $ constraint );
146
150
}
147
151
148
- public function testMaxSizeKiloBytes ()
152
+ public function provideMaxSizeNotExceededTests ()
149
153
{
150
- fwrite ($ this ->file , str_repeat ('0 ' , 1010 ));
154
+ return array (
155
+ array (10 , 10 ),
156
+ array (9 , 10 ),
151
157
152
- $ constraint = new File (array (
153
- 'maxSize ' => '1k ' ,
154
- ));
158
+ array (1000 , '1k ' ),
159
+ array (1000 - 1 , '1k ' ),
155
160
156
- $ this ->context ->expects ($ this ->never ())->method ('addViolation ' );
157
- $ this ->validator ->validate ($ this ->getFile ($ this ->path ), $ constraint );
161
+ array (1000 *1000 , '1M ' ),
162
+ array (1000 *1000 - 1 , '1M ' ),
163
+ );
158
164
}
159
165
160
- public function testMaxSizeMegaBytes ()
166
+ /**
167
+ * @dataProvider provideMaxSizeNotExceededTests
168
+ */
169
+ public function testMaxSizeNotExceeded ($ bytesWritten , $ limit )
161
170
{
162
- fwrite ($ this ->file , str_repeat ('0 ' , (1024 * 1022 )));
171
+ fseek ($ this ->file , $ bytesWritten -1 , SEEK_SET );
172
+ fwrite ($ this ->file , '0 ' );
173
+ fclose ($ this ->file );
163
174
164
175
$ constraint = new File (array (
165
- 'maxSize ' => '1M ' ,
176
+ 'maxSize ' => $ limit ,
177
+ 'maxSizeMessage ' => 'myMessage ' ,
166
178
));
167
179
168
- $ this ->context ->expects ($ this ->never ())->method ('addViolation ' );
180
+ $ this ->context ->expects ($ this ->never ())
181
+ ->method ('addViolation ' );
182
+
169
183
$ this ->validator ->validate ($ this ->getFile ($ this ->path ), $ constraint );
170
184
}
171
185
0 commit comments