|
13 | 13 |
|
14 | 14 | use Liip\ImagineBundle\Binary\Loader\FileSystemLoader;
|
15 | 15 | use Liip\ImagineBundle\Binary\Locator\FileSystemLocator;
|
| 16 | +use Liip\ImagineBundle\Binary\Locator\LocatorInterface; |
| 17 | +use Liip\ImagineBundle\Model\FileBinary; |
16 | 18 | use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesser;
|
17 | 19 | use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser;
|
18 | 20 |
|
|
21 | 23 | */
|
22 | 24 | class FileSystemLoaderTest extends \PHPUnit_Framework_TestCase
|
23 | 25 | {
|
24 |
| - /** |
25 |
| - * @return \PHPUnit_Framework_MockObject_MockObject|FileSystemLocator |
26 |
| - */ |
27 |
| - private function getLocator() |
28 |
| - { |
29 |
| - return new FileSystemLocator(); |
30 |
| - } |
31 |
| - |
32 |
| - public function testShouldImplementLoaderInterface() |
| 26 | + public function testImplementsLoaderInterface() |
33 | 27 | {
|
34 |
| - $rc = new \ReflectionClass('Liip\ImagineBundle\Binary\Loader\FileSystemLoader'); |
| 28 | + $r = new \ReflectionClass('Liip\ImagineBundle\Binary\Loader\FileSystemLoader'); |
35 | 29 |
|
36 |
| - $this->assertTrue($rc->implementsInterface('Liip\ImagineBundle\Binary\Loader\LoaderInterface')); |
| 30 | + $this->assertTrue($r->implementsInterface('Liip\ImagineBundle\Binary\Loader\LoaderInterface')); |
37 | 31 | }
|
38 | 32 |
|
39 |
| - public function testCouldBeConstructedWithExpectedArguments() |
| 33 | + public function testConstruction() |
40 | 34 | {
|
41 |
| - new FileSystemLoader( |
42 |
| - MimeTypeGuesser::getInstance(), |
43 |
| - ExtensionGuesser::getInstance(), |
44 |
| - __DIR__, |
45 |
| - $this->getLocator() |
46 |
| - ); |
47 |
| - } |
48 |
| - |
49 |
| - public function testThrowExceptionIfNoRootPathsProvided() |
50 |
| - { |
51 |
| - $this->setExpectedException( |
52 |
| - 'Liip\ImagineBundle\Exception\InvalidArgumentException', |
53 |
| - 'One or more data root paths must be specified.' |
54 |
| - ); |
55 |
| - |
56 |
| - new FileSystemLoader( |
57 |
| - MimeTypeGuesser::getInstance(), |
58 |
| - ExtensionGuesser::getInstance(), |
59 |
| - array(), |
60 |
| - $this->getLocator() |
61 |
| - ); |
| 35 | + $this->getFileSystemLoader(); |
62 | 36 | }
|
63 | 37 |
|
64 |
| - public function testThrowExceptionIfRootPathIsEmpty() |
| 38 | + /** |
| 39 | + * @return array[] |
| 40 | + */ |
| 41 | + public static function provideLoadCases() |
65 | 42 | {
|
66 |
| - $this->setExpectedException( |
67 |
| - 'Liip\ImagineBundle\Exception\InvalidArgumentException', |
68 |
| - 'Root image path not resolvable' |
69 |
| - ); |
| 43 | + $file = pathinfo(__FILE__, PATHINFO_BASENAME); |
70 | 44 |
|
71 |
| - new FileSystemLoader( |
72 |
| - MimeTypeGuesser::getInstance(), |
73 |
| - ExtensionGuesser::getInstance(), |
74 |
| - '', |
75 |
| - $this->getLocator() |
| 45 | + return array( |
| 46 | + array( |
| 47 | + __DIR__, |
| 48 | + $file, |
| 49 | + ), |
| 50 | + array( |
| 51 | + __DIR__.'/', |
| 52 | + $file, |
| 53 | + ), |
| 54 | + array( |
| 55 | + __DIR__, '/'. |
| 56 | + $file, |
| 57 | + ), |
| 58 | + array( |
| 59 | + __DIR__.'/../../Binary/Loader', |
| 60 | + '/'.$file, |
| 61 | + ), |
| 62 | + array( |
| 63 | + realpath(__DIR__.'/..'), |
| 64 | + 'Loader/'.$file, |
| 65 | + ), |
| 66 | + array( |
| 67 | + __DIR__.'/../', |
| 68 | + '/Loader/../../Binary/Loader/'.$file, |
| 69 | + ), |
76 | 70 | );
|
77 | 71 | }
|
78 | 72 |
|
79 |
| - public function testThrowExceptionIfRootPathDoesNotExist() |
| 73 | + /** |
| 74 | + * @dataProvider provideLoadCases |
| 75 | + * |
| 76 | + * @param string $root |
| 77 | + * @param string $path |
| 78 | + */ |
| 79 | + public function testLoad($root, $path) |
80 | 80 | {
|
81 |
| - $this->setExpectedException( |
82 |
| - 'Liip\ImagineBundle\Exception\InvalidArgumentException', |
83 |
| - 'Root image path not resolvable' |
84 |
| - ); |
85 |
| - |
86 |
| - new FileSystemLoader( |
87 |
| - MimeTypeGuesser::getInstance(), |
88 |
| - ExtensionGuesser::getInstance(), |
89 |
| - '/a/bad/root/path', |
90 |
| - $this->getLocator() |
91 |
| - ); |
| 81 | + $this->assertValidLoaderFindReturn($this->getFileSystemLoader(array($root))->find($path)); |
92 | 82 | }
|
93 | 83 |
|
94 |
| - public function testThrowExceptionIfRealPathIsOutsideRootPath1() |
| 84 | + /** |
| 85 | + * @dataProvider provideLoadCases |
| 86 | + * |
| 87 | + * @param string $root |
| 88 | + * @param string $path |
| 89 | + */ |
| 90 | + public function testDeprecatedConstruction($root, $path) |
95 | 91 | {
|
96 | 92 | $loader = new FileSystemLoader(
|
97 | 93 | MimeTypeGuesser::getInstance(),
|
98 | 94 | ExtensionGuesser::getInstance(),
|
99 |
| - __DIR__, |
100 |
| - $this->getLocator() |
101 |
| - ); |
102 |
| - |
103 |
| - $this->setExpectedException( |
104 |
| - 'Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException', |
105 |
| - 'Source image invalid' |
| 95 | + array($root) |
106 | 96 | );
|
107 | 97 |
|
108 |
| - $loader->find('../Loader/../../Binary/Loader/../../../Resources/config/routing.xml'); |
| 98 | + $this->assertValidLoaderFindReturn($loader->find($path)); |
109 | 99 | }
|
110 | 100 |
|
111 |
| - public function testThrowExceptionIfRealPathIsOutsideRootPath2() |
| 101 | + /** |
| 102 | + * @return array[] |
| 103 | + */ |
| 104 | + public static function provideMultipleRootLoadCases() |
112 | 105 | {
|
113 |
| - $loader = new FileSystemLoader( |
114 |
| - MimeTypeGuesser::getInstance(), |
115 |
| - ExtensionGuesser::getInstance(), |
116 |
| - __DIR__, |
117 |
| - $this->getLocator() |
118 |
| - ); |
119 |
| - |
120 |
| - $this->setExpectedException( |
121 |
| - 'Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException', |
122 |
| - 'Source image invalid' |
| 106 | + $pathsPrepended = array( |
| 107 | + realpath(__DIR__.'/../'), |
| 108 | + realpath(__DIR__.'/../../'), |
| 109 | + realpath(__DIR__.'/../../../'), |
123 | 110 | );
|
124 | 111 |
|
125 |
| - $loader->find('../../Binary/'); |
| 112 | + return array_map(function ($parameters) use ($pathsPrepended) { |
| 113 | + return array(array($pathsPrepended[mt_rand(0, count($pathsPrepended) - 1)], $parameters[0]), $parameters[1]); |
| 114 | + }, static::provideLoadCases()); |
126 | 115 | }
|
127 | 116 |
|
128 |
| - public function testThrowExceptionIfPathHasDoublePointSlashInTheMiddle() |
| 117 | + /** |
| 118 | + * @dataProvider provideMultipleRootLoadCases |
| 119 | + * |
| 120 | + * @param string $root |
| 121 | + * @param string $path |
| 122 | + */ |
| 123 | + public function testMultipleRootLoadCases($root, $path) |
129 | 124 | {
|
130 |
| - $loader = new FileSystemLoader( |
131 |
| - MimeTypeGuesser::getInstance(), |
132 |
| - ExtensionGuesser::getInstance(), |
133 |
| - __DIR__, |
134 |
| - $this->getLocator() |
135 |
| - ); |
136 |
| - |
137 |
| - $loader->find('/../../Binary/Loader/'.pathinfo(__FILE__, PATHINFO_BASENAME)); |
| 125 | + $this->assertValidLoaderFindReturn($this->getFileSystemLoader($root)->find($path)); |
138 | 126 | }
|
139 | 127 |
|
140 |
| - public function testThrowExceptionIfFileNotExist() |
| 128 | + /** |
| 129 | + * @expectedException \InvalidArgumentException |
| 130 | + * @expectedExceptionMessageRegExp {Method .+ expects a LocatorInterface for the forth argument} |
| 131 | + */ |
| 132 | + public function testThrowsIfConstructionArgumentsInvalid() |
141 | 133 | {
|
142 |
| - $loader = new FileSystemLoader( |
| 134 | + new FileSystemLoader( |
143 | 135 | MimeTypeGuesser::getInstance(),
|
144 | 136 | ExtensionGuesser::getInstance(),
|
145 |
| - __DIR__, |
146 |
| - $this->getLocator() |
| 137 | + array(__DIR__), |
| 138 | + 'not-instance-of-locator' |
147 | 139 | );
|
| 140 | + } |
148 | 141 |
|
149 |
| - $this->setExpectedException( |
150 |
| - 'Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException', |
151 |
| - 'Source image not resolvable' |
152 |
| - ); |
| 142 | + /** |
| 143 | + * @expectedException \Liip\ImagineBundle\Exception\InvalidArgumentException |
| 144 | + * @expectedExceptionMessage One or more data root paths must be specified |
| 145 | + */ |
| 146 | + public function testThrowsIfZeroCountRootPathArray() |
| 147 | + { |
| 148 | + $this->getFileSystemLoader(array()); |
| 149 | + } |
153 | 150 |
|
154 |
| - $loader->find('fileNotExist'); |
| 151 | + /** |
| 152 | + * @expectedException \Liip\ImagineBundle\Exception\InvalidArgumentException |
| 153 | + * @expectedExceptionMessage Root image path not resolvable |
| 154 | + */ |
| 155 | + public function testThrowsIfEmptyRootPath() |
| 156 | + { |
| 157 | + $this->getFileSystemLoader(''); |
155 | 158 | }
|
156 | 159 |
|
157 |
| - public static function provideLoadCases() |
| 160 | + /** |
| 161 | + * @expectedException \Liip\ImagineBundle\Exception\InvalidArgumentException |
| 162 | + * @expectedExceptionMessage Root image path not resolvable |
| 163 | + */ |
| 164 | + public function testThrowsIfRootPathDoesNotExist() |
158 | 165 | {
|
159 |
| - $fileName = pathinfo(__FILE__, PATHINFO_BASENAME); |
| 166 | + $this->getFileSystemLoader('/a/bad/root/path'); |
| 167 | + } |
160 | 168 |
|
| 169 | + /** |
| 170 | + * @return array[] |
| 171 | + */ |
| 172 | + public function provideOutsideRootPathsData() |
| 173 | + { |
161 | 174 | return array(
|
162 |
| - array(__DIR__, $fileName), |
163 |
| - array(__DIR__.'/', $fileName), |
164 |
| - array(__DIR__, '/'.$fileName), |
165 |
| - array(__DIR__.'/../../Binary/Loader', '/'.$fileName), |
166 |
| - array(realpath(__DIR__.'/..'), 'Loader/'.$fileName), |
167 |
| - array(__DIR__.'/../', '/Loader/../../Binary/Loader/'.$fileName), |
| 175 | + array('../Loader/../../Binary/Loader/../../../Resources/config/routing.xml'), |
| 176 | + array('../../Binary/'), |
168 | 177 | );
|
169 | 178 | }
|
170 | 179 |
|
171 | 180 | /**
|
172 |
| - * @dataProvider provideLoadCases |
| 181 | + * @dataProvider provideOutsideRootPathsData |
| 182 | + * |
| 183 | + * @expectedException \Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException |
| 184 | + * @expectedExceptionMessage Source image invalid |
173 | 185 | */
|
174 |
| - public function testLoad($rootDir, $path) |
| 186 | + public function testThrowsIfRealPathOutsideRootPath($path) |
175 | 187 | {
|
176 |
| - $loader = new FileSystemLoader( |
177 |
| - MimeTypeGuesser::getInstance(), |
178 |
| - ExtensionGuesser::getInstance(), |
179 |
| - $rootDir, |
180 |
| - $this->getLocator() |
181 |
| - ); |
182 |
| - |
183 |
| - $binary = $loader->find($path); |
| 188 | + $this->getFileSystemLoader()->find($path); |
| 189 | + } |
184 | 190 |
|
185 |
| - $this->assertInstanceOf('Liip\ImagineBundle\Model\FileBinary', $binary); |
186 |
| - $this->assertStringStartsWith('text/', $binary->getMimeType()); |
| 191 | + public function testPathWithDoublePeriodBackStep() |
| 192 | + { |
| 193 | + $this->assertValidLoaderFindReturn($this->getFileSystemLoader()->find('/../../Binary/Loader/'.pathinfo(__FILE__, PATHINFO_BASENAME))); |
187 | 194 | }
|
188 | 195 |
|
189 | 196 | /**
|
190 |
| - * @dataProvider provideLoadCases |
| 197 | + * @expectedException \Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException |
| 198 | + * @expectedExceptionMessage Source image not resolvable |
191 | 199 | */
|
192 |
| - public function testLoadUsingDeprecatedConstruction($rootDir, $path) |
| 200 | + public function testThrowsIfFileDoesNotExist() |
193 | 201 | {
|
194 |
| - $loader = new FileSystemLoader( |
195 |
| - MimeTypeGuesser::getInstance(), |
196 |
| - ExtensionGuesser::getInstance(), |
197 |
| - $rootDir |
198 |
| - ); |
199 |
| - |
200 |
| - $binary = $loader->find($path); |
201 |
| - |
202 |
| - $this->assertInstanceOf('Liip\ImagineBundle\Model\FileBinary', $binary); |
203 |
| - $this->assertStringStartsWith('text/', $binary->getMimeType()); |
| 202 | + $this->getFileSystemLoader()->find('fileNotExist'); |
204 | 203 | }
|
205 | 204 |
|
206 |
| - public function testThrowsExceptionWhenFourthConstructorArgumentNotLoaderInterface() |
| 205 | + /** |
| 206 | + * @return FileSystemLocator |
| 207 | + */ |
| 208 | + private function getFileSystemLocator() |
207 | 209 | {
|
208 |
| - $this->setExpectedExceptionRegExp('\InvalidArgumentException', '{Method .+ expects a LocatorInterface for the forth argument}'); |
209 |
| - |
210 |
| - new FileSystemLoader( |
211 |
| - MimeTypeGuesser::getInstance(), |
212 |
| - ExtensionGuesser::getInstance(), |
213 |
| - __DIR__, |
214 |
| - null |
215 |
| - ); |
| 210 | + return new FileSystemLocator(); |
216 | 211 | }
|
217 | 212 |
|
218 |
| - public static function provideMultipleRootLoadCases() |
| 213 | + /** |
| 214 | + * @return string[] |
| 215 | + */ |
| 216 | + private function getDefaultDataRoots() |
219 | 217 | {
|
220 |
| - $prepend = array( |
221 |
| - realpath(__DIR__.'/../'), |
222 |
| - realpath(__DIR__.'/../../'), |
223 |
| - realpath(__DIR__.'/../../../'), |
224 |
| - ); |
225 |
| - |
226 |
| - return array_map(function ($params) use ($prepend) { |
227 |
| - return array(array($prepend[mt_rand(0, count($prepend) - 1)], $params[0]), $params[1]); |
228 |
| - }, static::provideLoadCases()); |
| 218 | + return array(__DIR__); |
229 | 219 | }
|
230 | 220 |
|
231 | 221 | /**
|
232 |
| - * @dataProvider provideMultipleRootLoadCases |
| 222 | + * @param string|array|null $root |
| 223 | + * @param LocatorInterface|null $locator |
| 224 | + * |
| 225 | + * @return FileSystemLoader |
233 | 226 | */
|
234 |
| - public function testMultipleRootLoadCases($rootDirs, $path) |
| 227 | + private function getFileSystemLoader($root = null, LocatorInterface $locator = null) |
235 | 228 | {
|
236 |
| - $loader = new FileSystemLoader( |
| 229 | + return new FileSystemLoader( |
237 | 230 | MimeTypeGuesser::getInstance(),
|
238 | 231 | ExtensionGuesser::getInstance(),
|
239 |
| - $rootDirs, |
240 |
| - $this->getLocator() |
| 232 | + null !== $root ? $root : $this->getDefaultDataRoots(), |
| 233 | + null !== $locator ? $locator : $this->getFileSystemLocator() |
241 | 234 | );
|
| 235 | + } |
242 | 236 |
|
243 |
| - $binary = $loader->find($path); |
244 |
| - |
245 |
| - $this->assertInstanceOf('Liip\ImagineBundle\Model\FileBinary', $binary); |
246 |
| - $this->assertStringStartsWith('text/', $binary->getMimeType()); |
| 237 | + /** |
| 238 | + * @param FileBinary|mixed $return |
| 239 | + * @param string|null $message |
| 240 | + */ |
| 241 | + private function assertValidLoaderFindReturn($return, $message = null) |
| 242 | + { |
| 243 | + $this->assertInstanceOf('\Liip\ImagineBundle\Model\FileBinary', $return, $message); |
| 244 | + $this->assertStringStartsWith('text/', $return->getMimeType(), $message); |
247 | 245 | }
|
248 | 246 | }
|
0 commit comments