1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 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: public function testConstructWhenFileNotExists()
49: {
50: $this->setExpectedException('Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException');
51:
52: new File(__DIR__.'/Fixtures/not_here');
53: }
54:
55: public function testMove()
56: {
57: $path = __DIR__.'/Fixtures/test.copy.gif';
58: $targetDir = __DIR__.'/Fixtures/directory';
59: $targetPath = $targetDir.'/test.copy.gif';
60: @unlink($path);
61: @unlink($targetPath);
62: copy(__DIR__.'/Fixtures/test.gif', $path);
63:
64: $file = new File($path);
65: $movedFile = $file->move($targetDir);
66: $this->assertInstanceOf('Symfony\Component\HttpFoundation\File\File', $movedFile);
67:
68: $this->assertTrue(file_exists($targetPath));
69: $this->assertFalse(file_exists($path));
70: $this->assertEquals(realpath($targetPath), $movedFile->getRealPath());
71:
72: @unlink($targetPath);
73: }
74:
75: public function testMoveWithNewName()
76: {
77: $path = __DIR__.'/Fixtures/test.copy.gif';
78: $targetDir = __DIR__.'/Fixtures/directory';
79: $targetPath = $targetDir.'/test.newname.gif';
80: @unlink($path);
81: @unlink($targetPath);
82: copy(__DIR__.'/Fixtures/test.gif', $path);
83:
84: $file = new File($path);
85: $movedFile = $file->move($targetDir, 'test.newname.gif');
86:
87: $this->assertTrue(file_exists($targetPath));
88: $this->assertFalse(file_exists($path));
89: $this->assertEquals(realpath($targetPath), $movedFile->getRealPath());
90:
91: @unlink($targetPath);
92: }
93:
94: public function getFilenameFixtures()
95: {
96: return array(
97: array('original.gif', 'original.gif'),
98: array('..\\..\\original.gif', 'original.gif'),
99: array('../../original.gif', 'original.gif'),
100: array('файлfile.gif', 'файлfile.gif'),
101: array('..\\..\\файлfile.gif', 'файлfile.gif'),
102: array('../../файлfile.gif', 'файлfile.gif'),
103: );
104: }
105:
106: 107: 108:
109: public function testMoveWithNonLatinName($filename, $sanitizedFilename)
110: {
111: $path = __DIR__.'/Fixtures/'.$sanitizedFilename;
112: $targetDir = __DIR__.'/Fixtures/directory/';
113: $targetPath = $targetDir.$sanitizedFilename;
114: @unlink($path);
115: @unlink($targetPath);
116: copy(__DIR__.'/Fixtures/test.gif', $path);
117:
118: $file = new File($path);
119: $movedFile = $file->move($targetDir, $filename);
120: $this->assertInstanceOf('Symfony\Component\HttpFoundation\File\File', $movedFile);
121:
122: $this->assertTrue(file_exists($targetPath));
123: $this->assertFalse(file_exists($path));
124: $this->assertEquals(realpath($targetPath), $movedFile->getRealPath());
125:
126: @unlink($targetPath);
127: }
128:
129: public function testMoveToAnUnexistentDirectory()
130: {
131: $sourcePath = __DIR__.'/Fixtures/test.copy.gif';
132: $targetDir = __DIR__.'/Fixtures/directory/sub';
133: $targetPath = $targetDir.'/test.copy.gif';
134: @unlink($sourcePath);
135: @unlink($targetPath);
136: @rmdir($targetDir);
137: copy(__DIR__.'/Fixtures/test.gif', $sourcePath);
138:
139: $file = new File($sourcePath);
140: $movedFile = $file->move($targetDir);
141:
142: $this->assertFileExists($targetPath);
143: $this->assertFileNotExists($sourcePath);
144: $this->assertEquals(realpath($targetPath), $movedFile->getRealPath());
145:
146: @unlink($sourcePath);
147: @unlink($targetPath);
148: @rmdir($targetDir);
149: }
150:
151: public function testGetExtension()
152: {
153: $file = new File(__DIR__.'/Fixtures/test.gif');
154: $this->assertEquals('gif', $file->getExtension());
155: }
156:
157: protected function createMockGuesser($path, $mimeType)
158: {
159: $guesser = $this->getMock('Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface');
160: $guesser
161: ->expects($this->once())
162: ->method('guess')
163: ->with($this->equalTo($path))
164: ->will($this->returnValue($mimeType))
165: ;
166:
167: return $guesser;
168: }
169: }
170: