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