|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Liip\ImagineBundle\Tests\Binary\Loader; |
|
4
|
|
|
|
|
5
|
|
|
use Liip\ImagineBundle\Binary\Loader\FileSystemLoader; |
|
6
|
|
|
use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesser; |
|
7
|
|
|
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @covers Liip\ImagineBundle\Binary\Loader\FileSystemLoader |
|
11
|
|
|
*/ |
|
12
|
|
|
class FileSystemLoaderTest extends \PHPUnit_Framework_TestCase |
|
13
|
|
|
{ |
|
14
|
|
|
public static function provideLoadCases() |
|
15
|
|
|
{ |
|
16
|
|
|
$fileName = pathinfo(__FILE__, PATHINFO_BASENAME); |
|
17
|
|
|
|
|
18
|
|
|
return array( |
|
19
|
|
|
array(__DIR__, $fileName), |
|
20
|
|
|
array(__DIR__.'/', $fileName), |
|
21
|
|
|
array(__DIR__, '/'.$fileName), |
|
22
|
|
|
array(__DIR__.'/', '/'.$fileName), |
|
23
|
|
|
array(realpath(__DIR__.'/..'), 'Loader/'.$fileName), |
|
24
|
|
|
array(realpath(__DIR__.'/../'), '/Loader/'.$fileName), |
|
25
|
|
|
); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function testShouldImplementLoaderInterface() |
|
29
|
|
|
{ |
|
30
|
|
|
$rc = new \ReflectionClass('Liip\ImagineBundle\Binary\Loader\FileSystemLoader'); |
|
31
|
|
|
|
|
32
|
|
|
$this->assertTrue($rc->implementsInterface('Liip\ImagineBundle\Binary\Loader\LoaderInterface')); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function testCouldBeConstructedWithExpectedArguments() |
|
36
|
|
|
{ |
|
37
|
|
|
new FileSystemLoader( |
|
38
|
|
|
MimeTypeGuesser::getInstance(), |
|
39
|
|
|
ExtensionGuesser::getInstance(), |
|
40
|
|
|
__DIR__ |
|
41
|
|
|
); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
View Code Duplication |
public function testThrowExceptionIfPathHasDoublePointSlashAtBegging() |
|
|
|
|
|
|
45
|
|
|
{ |
|
46
|
|
|
$loader = new FileSystemLoader( |
|
47
|
|
|
MimeTypeGuesser::getInstance(), |
|
48
|
|
|
ExtensionGuesser::getInstance(), |
|
49
|
|
|
__DIR__ |
|
50
|
|
|
); |
|
51
|
|
|
|
|
52
|
|
|
$this->setExpectedException( |
|
53
|
|
|
'Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException', |
|
54
|
|
|
'Source image was searched with' |
|
55
|
|
|
); |
|
56
|
|
|
|
|
57
|
|
|
$loader->find('../foo/bar'); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
View Code Duplication |
public function testThrowExceptionIfPathHasDoublePointSlashInTheMiddle() |
|
|
|
|
|
|
61
|
|
|
{ |
|
62
|
|
|
$loader = new FileSystemLoader( |
|
63
|
|
|
MimeTypeGuesser::getInstance(), |
|
64
|
|
|
ExtensionGuesser::getInstance(), |
|
65
|
|
|
__DIR__ |
|
66
|
|
|
); |
|
67
|
|
|
|
|
68
|
|
|
$this->setExpectedException( |
|
69
|
|
|
'Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException', |
|
70
|
|
|
'Source image was searched with' |
|
71
|
|
|
); |
|
72
|
|
|
|
|
73
|
|
|
$loader->find('foo/../bar'); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
View Code Duplication |
public function testThrowExceptionIfFileNotExist() |
|
|
|
|
|
|
77
|
|
|
{ |
|
78
|
|
|
$loader = new FileSystemLoader( |
|
79
|
|
|
MimeTypeGuesser::getInstance(), |
|
80
|
|
|
ExtensionGuesser::getInstance(), |
|
81
|
|
|
__DIR__ |
|
82
|
|
|
); |
|
83
|
|
|
|
|
84
|
|
|
$this->setExpectedException( |
|
85
|
|
|
'Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException', |
|
86
|
|
|
'Source image not found' |
|
87
|
|
|
); |
|
88
|
|
|
|
|
89
|
|
|
$loader->find('fileNotExist'); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @dataProvider provideLoadCases |
|
94
|
|
|
*/ |
|
95
|
|
|
public function testLoad($rootDir, $path) |
|
96
|
|
|
{ |
|
97
|
|
|
$loader = new FileSystemLoader( |
|
98
|
|
|
MimeTypeGuesser::getInstance(), |
|
99
|
|
|
ExtensionGuesser::getInstance(), |
|
100
|
|
|
$rootDir |
|
101
|
|
|
); |
|
102
|
|
|
|
|
103
|
|
|
$binary = $loader->find($path); |
|
104
|
|
|
|
|
105
|
|
|
$this->assertInstanceOf('Liip\ImagineBundle\Model\FileBinary', $binary); |
|
106
|
|
|
$this->assertStringStartsWith('text/', $binary->getMimeType()); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.