Pathologic was missing because of a .git folder inside.
[yaffs-website] / vendor / symfony / http-foundation / Tests / FileBagTest.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\HttpFoundation\Tests;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\HttpFoundation\File\UploadedFile;
16 use Symfony\Component\HttpFoundation\FileBag;
17
18 /**
19  * FileBagTest.
20  *
21  * @author Fabien Potencier <fabien@symfony.com>
22  * @author Bulat Shakirzyanov <mallluhuct@gmail.com>
23  */
24 class FileBagTest extends TestCase
25 {
26     /**
27      * @expectedException \InvalidArgumentException
28      */
29     public function testFileMustBeAnArrayOrUploadedFile()
30     {
31         new FileBag(array('file' => 'foo'));
32     }
33
34     public function testShouldConvertsUploadedFiles()
35     {
36         $tmpFile = $this->createTempFile();
37         $file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', 100, 0);
38
39         $bag = new FileBag(array('file' => array(
40             'name' => basename($tmpFile),
41             'type' => 'text/plain',
42             'tmp_name' => $tmpFile,
43             'error' => 0,
44             'size' => 100,
45         )));
46
47         $this->assertEquals($file, $bag->get('file'));
48     }
49
50     public function testShouldSetEmptyUploadedFilesToNull()
51     {
52         $bag = new FileBag(array('file' => array(
53             'name' => '',
54             'type' => '',
55             'tmp_name' => '',
56             'error' => UPLOAD_ERR_NO_FILE,
57             'size' => 0,
58         )));
59
60         $this->assertNull($bag->get('file'));
61     }
62
63     public function testShouldConvertUploadedFilesWithPhpBug()
64     {
65         $tmpFile = $this->createTempFile();
66         $file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', 100, 0);
67
68         $bag = new FileBag(array(
69             'child' => array(
70                 'name' => array(
71                     'file' => basename($tmpFile),
72                 ),
73                 'type' => array(
74                     'file' => 'text/plain',
75                 ),
76                 'tmp_name' => array(
77                     'file' => $tmpFile,
78                 ),
79                 'error' => array(
80                     'file' => 0,
81                 ),
82                 'size' => array(
83                     'file' => 100,
84                 ),
85             ),
86         ));
87
88         $files = $bag->all();
89         $this->assertEquals($file, $files['child']['file']);
90     }
91
92     public function testShouldConvertNestedUploadedFilesWithPhpBug()
93     {
94         $tmpFile = $this->createTempFile();
95         $file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', 100, 0);
96
97         $bag = new FileBag(array(
98             'child' => array(
99                 'name' => array(
100                     'sub' => array('file' => basename($tmpFile)),
101                 ),
102                 'type' => array(
103                     'sub' => array('file' => 'text/plain'),
104                 ),
105                 'tmp_name' => array(
106                     'sub' => array('file' => $tmpFile),
107                 ),
108                 'error' => array(
109                     'sub' => array('file' => 0),
110                 ),
111                 'size' => array(
112                     'sub' => array('file' => 100),
113                 ),
114             ),
115         ));
116
117         $files = $bag->all();
118         $this->assertEquals($file, $files['child']['sub']['file']);
119     }
120
121     public function testShouldNotConvertNestedUploadedFiles()
122     {
123         $tmpFile = $this->createTempFile();
124         $file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', 100, 0);
125         $bag = new FileBag(array('image' => array('file' => $file)));
126
127         $files = $bag->all();
128         $this->assertEquals($file, $files['image']['file']);
129     }
130
131     protected function createTempFile()
132     {
133         return tempnam(sys_get_temp_dir().'/form_test', 'FormTest');
134     }
135
136     protected function setUp()
137     {
138         mkdir(sys_get_temp_dir().'/form_test', 0777, true);
139     }
140
141     protected function tearDown()
142     {
143         foreach (glob(sys_get_temp_dir().'/form_test/*') as $file) {
144             unlink($file);
145         }
146
147         rmdir(sys_get_temp_dir().'/form_test');
148     }
149 }