fdecdc7cc1f45d4bc254dc840a72d64224ab29d6
[yaffs-website] / vendor / symfony / validator / Tests / Constraints / FileTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Validator\Tests\Constraints;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Validator\Constraints\File;
16 use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
17
18 class FileTest extends TestCase
19 {
20     /**
21      * @param mixed $maxSize
22      * @param int   $bytes
23      * @param bool  $binaryFormat
24      * @dataProvider provideValidSizes
25      */
26     public function testMaxSize($maxSize, $bytes, $binaryFormat)
27     {
28         $file = new File(array('maxSize' => $maxSize));
29
30         $this->assertSame($bytes, $file->maxSize);
31         $this->assertSame($binaryFormat, $file->binaryFormat);
32     }
33
34     /**
35      * @dataProvider provideValidSizes
36      *
37      * @param int|string $maxSize
38      * @param int        $bytes
39      * @param string     $binaryFormat
40      */
41     public function testMaxSizeCanBeSetAfterInitialization($maxSize, $bytes, $binaryFormat)
42     {
43         $file = new File();
44         $file->maxSize = $maxSize;
45
46         $this->assertSame($bytes, $file->maxSize);
47         $this->assertSame($binaryFormat, $file->binaryFormat);
48     }
49
50     /**
51      * @dataProvider provideInvalidSizes
52      * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
53      *
54      * @param int|string $maxSize
55      */
56     public function testInvalidValueForMaxSizeThrowsExceptionAfterInitialization($maxSize)
57     {
58         $file = new File(array('maxSize' => 1000));
59         $file->maxSize = $maxSize;
60     }
61
62     /**
63      * @dataProvider provideInvalidSizes
64      *
65      * @param int|string $maxSize
66      */
67     public function testMaxSizeCannotBeSetToInvalidValueAfterInitialization($maxSize)
68     {
69         $file = new File(array('maxSize' => 1000));
70
71         try {
72             $file->maxSize = $maxSize;
73         } catch (ConstraintDefinitionException $e) {
74         }
75
76         $this->assertSame(1000, $file->maxSize);
77     }
78
79     /**
80      * @param mixed $maxSize
81      * @dataProvider provideInValidSizes
82      * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
83      */
84     public function testInvalidMaxSize($maxSize)
85     {
86         new File(array('maxSize' => $maxSize));
87     }
88
89     /**
90      * @return array
91      */
92     public function provideValidSizes()
93     {
94         return array(
95             array('500', 500, false),
96             array(12300, 12300, false),
97             array('1ki', 1024, true),
98             array('1KI', 1024, true),
99             array('2k', 2000, false),
100             array('2K', 2000, false),
101             array('1mi', 1048576, true),
102             array('1MI', 1048576, true),
103             array('3m', 3000000, false),
104             array('3M', 3000000, false),
105         );
106     }
107
108     /**
109      * @return array
110      */
111     public function provideInvalidSizes()
112     {
113         return array(
114             array('+100'),
115             array('foo'),
116             array('1Ko'),
117             array('1kio'),
118             array('1G'),
119             array('1Gi'),
120         );
121     }
122
123     /**
124      * @param mixed $maxSize
125      * @param bool  $guessedFormat
126      * @param bool  $binaryFormat
127      * @dataProvider provideFormats
128      */
129     public function testBinaryFormat($maxSize, $guessedFormat, $binaryFormat)
130     {
131         $file = new File(array('maxSize' => $maxSize, 'binaryFormat' => $guessedFormat));
132
133         $this->assertSame($binaryFormat, $file->binaryFormat);
134     }
135
136     /**
137      * @return array
138      */
139     public function provideFormats()
140     {
141         return array(
142             array(100, null, false),
143             array(100, true, true),
144             array(100, false, false),
145             array('100K', null, false),
146             array('100K', true, true),
147             array('100K', false, false),
148             array('100Ki', null, true),
149             array('100Ki', true, true),
150             array('100Ki', false, false),
151         );
152     }
153 }