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
12namespace Symfony\Component\HttpFoundation\Tests\File;
13
14use PHPUnit\Framework\TestCase;
15use Symfony\Component\HttpFoundation\File\File;
16
17/**
18 * @requires extension fileinfo
19 */
20class FileTest extends TestCase
21{
22    protected $file;
23
24    public function testGetMimeTypeUsesMimeTypeGuessers()
25    {
26        $file = new File(__DIR__.'/Fixtures/test.gif');
27        $this->assertEquals('image/gif', $file->getMimeType());
28    }
29
30    public function testGuessExtensionWithoutGuesser()
31    {
32        $file = new File(__DIR__.'/Fixtures/directory/.empty');
33        $this->assertNull($file->guessExtension());
34    }
35
36    public function testGuessExtensionIsBasedOnMimeType()
37    {
38        $file = new File(__DIR__.'/Fixtures/test');
39        $this->assertEquals('gif', $file->guessExtension());
40    }
41
42    public function testConstructWhenFileNotExists()
43    {
44        $this->expectException('Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException');
45
46        new File(__DIR__.'/Fixtures/not_here');
47    }
48
49    public function testMove()
50    {
51        $path = __DIR__.'/Fixtures/test.copy.gif';
52        $targetDir = __DIR__.'/Fixtures/directory';
53        $targetPath = $targetDir.'/test.copy.gif';
54        @unlink($path);
55        @unlink($targetPath);
56        copy(__DIR__.'/Fixtures/test.gif', $path);
57
58        $file = new File($path);
59        $movedFile = $file->move($targetDir);
60        $this->assertInstanceOf('Symfony\Component\HttpFoundation\File\File', $movedFile);
61
62        $this->assertFileExists($targetPath);
63        $this->assertFileNotExists($path);
64        $this->assertEquals(realpath($targetPath), $movedFile->getRealPath());
65
66        @unlink($targetPath);
67    }
68
69    public function testMoveWithNewName()
70    {
71        $path = __DIR__.'/Fixtures/test.copy.gif';
72        $targetDir = __DIR__.'/Fixtures/directory';
73        $targetPath = $targetDir.'/test.newname.gif';
74        @unlink($path);
75        @unlink($targetPath);
76        copy(__DIR__.'/Fixtures/test.gif', $path);
77
78        $file = new File($path);
79        $movedFile = $file->move($targetDir, 'test.newname.gif');
80
81        $this->assertFileExists($targetPath);
82        $this->assertFileNotExists($path);
83        $this->assertEquals(realpath($targetPath), $movedFile->getRealPath());
84
85        @unlink($targetPath);
86    }
87
88    public function getFilenameFixtures()
89    {
90        return [
91            ['original.gif', 'original.gif'],
92            ['..\\..\\original.gif', 'original.gif'],
93            ['../../original.gif', 'original.gif'],
94            ['файлfile.gif', 'файлfile.gif'],
95            ['..\\..\\файлfile.gif', 'файлfile.gif'],
96            ['../../файлfile.gif', 'файлfile.gif'],
97        ];
98    }
99
100    /**
101     * @dataProvider getFilenameFixtures
102     */
103    public function testMoveWithNonLatinName($filename, $sanitizedFilename)
104    {
105        $path = __DIR__.'/Fixtures/'.$sanitizedFilename;
106        $targetDir = __DIR__.'/Fixtures/directory/';
107        $targetPath = $targetDir.$sanitizedFilename;
108        @unlink($path);
109        @unlink($targetPath);
110        copy(__DIR__.'/Fixtures/test.gif', $path);
111
112        $file = new File($path);
113        $movedFile = $file->move($targetDir, $filename);
114        $this->assertInstanceOf('Symfony\Component\HttpFoundation\File\File', $movedFile);
115
116        $this->assertFileExists($targetPath);
117        $this->assertFileNotExists($path);
118        $this->assertEquals(realpath($targetPath), $movedFile->getRealPath());
119
120        @unlink($targetPath);
121    }
122
123    public function testMoveToAnUnexistentDirectory()
124    {
125        $sourcePath = __DIR__.'/Fixtures/test.copy.gif';
126        $targetDir = __DIR__.'/Fixtures/directory/sub';
127        $targetPath = $targetDir.'/test.copy.gif';
128        @unlink($sourcePath);
129        @unlink($targetPath);
130        @rmdir($targetDir);
131        copy(__DIR__.'/Fixtures/test.gif', $sourcePath);
132
133        $file = new File($sourcePath);
134        $movedFile = $file->move($targetDir);
135
136        $this->assertFileExists($targetPath);
137        $this->assertFileNotExists($sourcePath);
138        $this->assertEquals(realpath($targetPath), $movedFile->getRealPath());
139
140        @unlink($sourcePath);
141        @unlink($targetPath);
142        @rmdir($targetDir);
143    }
144}
145