| 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\File;
|
|
|
13 |
|
|
|
14 |
use Symfony\Component\HttpFoundation\File\File;
|
|
|
15 |
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser;
|
|
|
16 |
|
|
|
17 |
class FileTest extends \PHPUnit_Framework_TestCase
|
|
|
18 |
{
|
|
|
19 |
protected $file;
|
|
|
20 |
|
|
|
21 |
public function testGetMimeTypeUsesMimeTypeGuessers()
|
|
|
22 |
{
|
|
|
23 |
$file = new File(__DIR__.'/Fixtures/test.gif');
|
|
|
24 |
$guesser = $this->createMockGuesser($file->getPathname(), 'image/gif');
|
|
|
25 |
|
|
|
26 |
MimeTypeGuesser::getInstance()->register($guesser);
|
|
|
27 |
|
|
|
28 |
$this->assertEquals('image/gif', $file->getMimeType());
|
|
|
29 |
}
|
|
|
30 |
|
|
|
31 |
public function testGuessExtensionWithoutGuesser()
|
|
|
32 |
{
|
|
|
33 |
$file = new File(__DIR__.'/Fixtures/directory/.empty');
|
|
|
34 |
|
|
|
35 |
$this->assertNull($file->guessExtension());
|
|
|
36 |
}
|
|
|
37 |
|
|
|
38 |
public function testGuessExtensionIsBasedOnMimeType()
|
|
|
39 |
{
|
|
|
40 |
$file = new File(__DIR__.'/Fixtures/test');
|
|
|
41 |
$guesser = $this->createMockGuesser($file->getPathname(), 'image/gif');
|
|
|
42 |
|
|
|
43 |
MimeTypeGuesser::getInstance()->register($guesser);
|
|
|
44 |
|
|
|
45 |
$this->assertEquals('gif', $file->guessExtension());
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
/**
|
|
|
49 |
* @requires extension fileinfo
|
|
|
50 |
*/
|
|
|
51 |
public function testGuessExtensionWithReset()
|
|
|
52 |
{
|
|
|
53 |
$file = new File(__DIR__.'/Fixtures/other-file.example');
|
|
|
54 |
$guesser = $this->createMockGuesser($file->getPathname(), 'image/gif');
|
|
|
55 |
MimeTypeGuesser::getInstance()->register($guesser);
|
|
|
56 |
|
|
|
57 |
$this->assertEquals('gif', $file->guessExtension());
|
|
|
58 |
|
|
|
59 |
MimeTypeGuesser::reset();
|
|
|
60 |
|
|
|
61 |
$this->assertNull($file->guessExtension());
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
public function testConstructWhenFileNotExists()
|
|
|
65 |
{
|
|
|
66 |
$this->setExpectedException('Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException');
|
|
|
67 |
|
|
|
68 |
new File(__DIR__.'/Fixtures/not_here');
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
public function testMove()
|
|
|
72 |
{
|
|
|
73 |
$path = __DIR__.'/Fixtures/test.copy.gif';
|
|
|
74 |
$targetDir = __DIR__.'/Fixtures/directory';
|
|
|
75 |
$targetPath = $targetDir.'/test.copy.gif';
|
|
|
76 |
@unlink($path);
|
|
|
77 |
@unlink($targetPath);
|
|
|
78 |
copy(__DIR__.'/Fixtures/test.gif', $path);
|
|
|
79 |
|
|
|
80 |
$file = new File($path);
|
|
|
81 |
$movedFile = $file->move($targetDir);
|
|
|
82 |
$this->assertInstanceOf('Symfony\Component\HttpFoundation\File\File', $movedFile);
|
|
|
83 |
|
|
|
84 |
$this->assertFileExists($targetPath);
|
|
|
85 |
$this->assertFileNotExists($path);
|
|
|
86 |
$this->assertEquals(realpath($targetPath), $movedFile->getRealPath());
|
|
|
87 |
|
|
|
88 |
@unlink($targetPath);
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
public function testMoveWithNewName()
|
|
|
92 |
{
|
|
|
93 |
$path = __DIR__.'/Fixtures/test.copy.gif';
|
|
|
94 |
$targetDir = __DIR__.'/Fixtures/directory';
|
|
|
95 |
$targetPath = $targetDir.'/test.newname.gif';
|
|
|
96 |
@unlink($path);
|
|
|
97 |
@unlink($targetPath);
|
|
|
98 |
copy(__DIR__.'/Fixtures/test.gif', $path);
|
|
|
99 |
|
|
|
100 |
$file = new File($path);
|
|
|
101 |
$movedFile = $file->move($targetDir, 'test.newname.gif');
|
|
|
102 |
|
|
|
103 |
$this->assertFileExists($targetPath);
|
|
|
104 |
$this->assertFileNotExists($path);
|
|
|
105 |
$this->assertEquals(realpath($targetPath), $movedFile->getRealPath());
|
|
|
106 |
|
|
|
107 |
@unlink($targetPath);
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
public function getFilenameFixtures()
|
|
|
111 |
{
|
|
|
112 |
return array(
|
|
|
113 |
array('original.gif', 'original.gif'),
|
|
|
114 |
array('..\\..\\original.gif', 'original.gif'),
|
|
|
115 |
array('../../original.gif', 'original.gif'),
|
|
|
116 |
array('файлfile.gif', 'файлfile.gif'),
|
|
|
117 |
array('..\\..\\файлfile.gif', 'файлfile.gif'),
|
|
|
118 |
array('../../файлfile.gif', 'файлfile.gif'),
|
|
|
119 |
);
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
/**
|
|
|
123 |
* @dataProvider getFilenameFixtures
|
|
|
124 |
*/
|
|
|
125 |
public function testMoveWithNonLatinName($filename, $sanitizedFilename)
|
|
|
126 |
{
|
|
|
127 |
$path = __DIR__.'/Fixtures/'.$sanitizedFilename;
|
|
|
128 |
$targetDir = __DIR__.'/Fixtures/directory/';
|
|
|
129 |
$targetPath = $targetDir.$sanitizedFilename;
|
|
|
130 |
@unlink($path);
|
|
|
131 |
@unlink($targetPath);
|
|
|
132 |
copy(__DIR__.'/Fixtures/test.gif', $path);
|
|
|
133 |
|
|
|
134 |
$file = new File($path);
|
|
|
135 |
$movedFile = $file->move($targetDir, $filename);
|
|
|
136 |
$this->assertInstanceOf('Symfony\Component\HttpFoundation\File\File', $movedFile);
|
|
|
137 |
|
|
|
138 |
$this->assertFileExists($targetPath);
|
|
|
139 |
$this->assertFileNotExists($path);
|
|
|
140 |
$this->assertEquals(realpath($targetPath), $movedFile->getRealPath());
|
|
|
141 |
|
|
|
142 |
@unlink($targetPath);
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
public function testMoveToAnUnexistentDirectory()
|
|
|
146 |
{
|
|
|
147 |
$sourcePath = __DIR__.'/Fixtures/test.copy.gif';
|
|
|
148 |
$targetDir = __DIR__.'/Fixtures/directory/sub';
|
|
|
149 |
$targetPath = $targetDir.'/test.copy.gif';
|
|
|
150 |
@unlink($sourcePath);
|
|
|
151 |
@unlink($targetPath);
|
|
|
152 |
@rmdir($targetDir);
|
|
|
153 |
copy(__DIR__.'/Fixtures/test.gif', $sourcePath);
|
|
|
154 |
|
|
|
155 |
$file = new File($sourcePath);
|
|
|
156 |
$movedFile = $file->move($targetDir);
|
|
|
157 |
|
|
|
158 |
$this->assertFileExists($targetPath);
|
|
|
159 |
$this->assertFileNotExists($sourcePath);
|
|
|
160 |
$this->assertEquals(realpath($targetPath), $movedFile->getRealPath());
|
|
|
161 |
|
|
|
162 |
@unlink($sourcePath);
|
|
|
163 |
@unlink($targetPath);
|
|
|
164 |
@rmdir($targetDir);
|
|
|
165 |
}
|
|
|
166 |
|
|
|
167 |
protected function createMockGuesser($path, $mimeType)
|
|
|
168 |
{
|
|
|
169 |
$guesser = $this->getMock('Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface');
|
|
|
170 |
$guesser
|
|
|
171 |
->expects($this->once())
|
|
|
172 |
->method('guess')
|
|
|
173 |
->with($this->equalTo($path))
|
|
|
174 |
->will($this->returnValue($mimeType))
|
|
|
175 |
;
|
|
|
176 |
|
|
|
177 |
return $guesser;
|
|
|
178 |
}
|
|
|
179 |
}
|