Added the Search API Synonym module to deal specifically with licence and license...
[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 testShouldRemoveEmptyUploadedFilesForMultiUpload()
64     {
65         $bag = new FileBag(array('files' => array(
66             'name' => array(''),
67             'type' => array(''),
68             'tmp_name' => array(''),
69             'error' => array(UPLOAD_ERR_NO_FILE),
70             'size' => array(0),
71         )));
72
73         $this->assertSame(array(), $bag->get('files'));
74     }
75
76     public function testShouldNotRemoveEmptyUploadedFilesForAssociativeArray()
77     {
78         $bag = new FileBag(array('files' => array(
79             'name' => array('file1' => ''),
80             'type' => array('file1' => ''),
81             'tmp_name' => array('file1' => ''),
82             'error' => array('file1' => UPLOAD_ERR_NO_FILE),
83             'size' => array('file1' => 0),
84         )));
85
86         $this->assertSame(array('file1' => null), $bag->get('files'));
87     }
88
89     public function testShouldConvertUploadedFilesWithPhpBug()
90     {
91         $tmpFile = $this->createTempFile();
92         $file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', 100, 0);
93
94         $bag = new FileBag(array(
95             'child' => array(
96                 'name' => array(
97                     'file' => basename($tmpFile),
98                 ),
99                 'type' => array(
100                     'file' => 'text/plain',
101                 ),
102                 'tmp_name' => array(
103                     'file' => $tmpFile,
104                 ),
105                 'error' => array(
106                     'file' => 0,
107                 ),
108                 'size' => array(
109                     'file' => 100,
110                 ),
111             ),
112         ));
113
114         $files = $bag->all();
115         $this->assertEquals($file, $files['child']['file']);
116     }
117
118     public function testShouldConvertNestedUploadedFilesWithPhpBug()
119     {
120         $tmpFile = $this->createTempFile();
121         $file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', 100, 0);
122
123         $bag = new FileBag(array(
124             'child' => array(
125                 'name' => array(
126                     'sub' => array('file' => basename($tmpFile)),
127                 ),
128                 'type' => array(
129                     'sub' => array('file' => 'text/plain'),
130                 ),
131                 'tmp_name' => array(
132                     'sub' => array('file' => $tmpFile),
133                 ),
134                 'error' => array(
135                     'sub' => array('file' => 0),
136                 ),
137                 'size' => array(
138                     'sub' => array('file' => 100),
139                 ),
140             ),
141         ));
142
143         $files = $bag->all();
144         $this->assertEquals($file, $files['child']['sub']['file']);
145     }
146
147     public function testShouldNotConvertNestedUploadedFiles()
148     {
149         $tmpFile = $this->createTempFile();
150         $file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', 100, 0);
151         $bag = new FileBag(array('image' => array('file' => $file)));
152
153         $files = $bag->all();
154         $this->assertEquals($file, $files['image']['file']);
155     }
156
157     protected function createTempFile()
158     {
159         return tempnam(sys_get_temp_dir().'/form_test', 'FormTest');
160     }
161
162     protected function setUp()
163     {
164         mkdir(sys_get_temp_dir().'/form_test', 0777, true);
165     }
166
167     protected function tearDown()
168     {
169         foreach (glob(sys_get_temp_dir().'/form_test/*') as $file) {
170             unlink($file);
171         }
172
173         rmdir(sys_get_temp_dir().'/form_test');
174     }
175 }