Subversion Repositories php-qbpwcf

Rev

Details | Last modification | View Log | RSS feed

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