Skip to content

Commit 4e8d5bd

Browse files
committed
cleanup file system loader/locator/functional tests
1 parent b8b481d commit 4e8d5bd

File tree

6 files changed

+285
-287
lines changed

6 files changed

+285
-287
lines changed

Tests/Binary/Loader/FileSystemLoaderTest.php

+152-154
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
use Liip\ImagineBundle\Binary\Loader\FileSystemLoader;
1515
use Liip\ImagineBundle\Binary\Locator\FileSystemLocator;
16+
use Liip\ImagineBundle\Binary\Locator\LocatorInterface;
17+
use Liip\ImagineBundle\Model\FileBinary;
1618
use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesser;
1719
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser;
1820

@@ -21,228 +23,224 @@
2123
*/
2224
class FileSystemLoaderTest extends \PHPUnit_Framework_TestCase
2325
{
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()
3327
{
34-
$rc = new \ReflectionClass('Liip\ImagineBundle\Binary\Loader\FileSystemLoader');
28+
$r = new \ReflectionClass('Liip\ImagineBundle\Binary\Loader\FileSystemLoader');
3529

36-
$this->assertTrue($rc->implementsInterface('Liip\ImagineBundle\Binary\Loader\LoaderInterface'));
30+
$this->assertTrue($r->implementsInterface('Liip\ImagineBundle\Binary\Loader\LoaderInterface'));
3731
}
3832

39-
public function testCouldBeConstructedWithExpectedArguments()
33+
public function testConstruction()
4034
{
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();
6236
}
6337

64-
public function testThrowExceptionIfRootPathIsEmpty()
38+
/**
39+
* @return array[]
40+
*/
41+
public static function provideLoadCases()
6542
{
66-
$this->setExpectedException(
67-
'Liip\ImagineBundle\Exception\InvalidArgumentException',
68-
'Root image path not resolvable'
69-
);
43+
$file = pathinfo(__FILE__, PATHINFO_BASENAME);
7044

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+
),
7670
);
7771
}
7872

79-
public function testThrowExceptionIfRootPathDoesNotExist()
73+
/**
74+
* @dataProvider provideLoadCases
75+
*
76+
* @param string $root
77+
* @param string $path
78+
*/
79+
public function testLoad($root, $path)
8080
{
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));
9282
}
9383

94-
public function testThrowExceptionIfRealPathIsOutsideRootPath1()
84+
/**
85+
* @dataProvider provideLoadCases
86+
*
87+
* @param string $root
88+
* @param string $path
89+
*/
90+
public function testDeprecatedConstruction($root, $path)
9591
{
9692
$loader = new FileSystemLoader(
9793
MimeTypeGuesser::getInstance(),
9894
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)
10696
);
10797

108-
$loader->find('../Loader/../../Binary/Loader/../../../Resources/config/routing.xml');
98+
$this->assertValidLoaderFindReturn($loader->find($path));
10999
}
110100

111-
public function testThrowExceptionIfRealPathIsOutsideRootPath2()
101+
/**
102+
* @return array[]
103+
*/
104+
public static function provideMultipleRootLoadCases()
112105
{
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__.'/../../../'),
123110
);
124111

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());
126115
}
127116

128-
public function testThrowExceptionIfPathHasDoublePointSlashInTheMiddle()
117+
/**
118+
* @dataProvider provideMultipleRootLoadCases
119+
*
120+
* @param string $root
121+
* @param string $path
122+
*/
123+
public function testMultipleRootLoadCases($root, $path)
129124
{
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));
138126
}
139127

140-
public function testThrowExceptionIfFileNotExist()
128+
/**
129+
* @expectedException \InvalidArgumentException
130+
* @expectedExceptionMessageRegExp {Method .+ expects a LocatorInterface for the forth argument}
131+
*/
132+
public function testThrowsIfConstructionArgumentsInvalid()
141133
{
142-
$loader = new FileSystemLoader(
134+
new FileSystemLoader(
143135
MimeTypeGuesser::getInstance(),
144136
ExtensionGuesser::getInstance(),
145-
__DIR__,
146-
$this->getLocator()
137+
array(__DIR__),
138+
'not-instance-of-locator'
147139
);
140+
}
148141

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+
}
153150

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('');
155158
}
156159

157-
public static function provideLoadCases()
160+
/**
161+
* @expectedException \Liip\ImagineBundle\Exception\InvalidArgumentException
162+
* @expectedExceptionMessage Root image path not resolvable
163+
*/
164+
public function testThrowsIfRootPathDoesNotExist()
158165
{
159-
$fileName = pathinfo(__FILE__, PATHINFO_BASENAME);
166+
$this->getFileSystemLoader('/a/bad/root/path');
167+
}
160168

169+
/**
170+
* @return array[]
171+
*/
172+
public function provideOutsideRootPathsData()
173+
{
161174
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/'),
168177
);
169178
}
170179

171180
/**
172-
* @dataProvider provideLoadCases
181+
* @dataProvider provideOutsideRootPathsData
182+
*
183+
* @expectedException \Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException
184+
* @expectedExceptionMessage Source image invalid
173185
*/
174-
public function testLoad($rootDir, $path)
186+
public function testThrowsIfRealPathOutsideRootPath($path)
175187
{
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+
}
184190

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)));
187194
}
188195

189196
/**
190-
* @dataProvider provideLoadCases
197+
* @expectedException \Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException
198+
* @expectedExceptionMessage Source image not resolvable
191199
*/
192-
public function testLoadUsingDeprecatedConstruction($rootDir, $path)
200+
public function testThrowsIfFileDoesNotExist()
193201
{
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');
204203
}
205204

206-
public function testThrowsExceptionWhenFourthConstructorArgumentNotLoaderInterface()
205+
/**
206+
* @return FileSystemLocator
207+
*/
208+
private function getFileSystemLocator()
207209
{
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();
216211
}
217212

218-
public static function provideMultipleRootLoadCases()
213+
/**
214+
* @return string[]
215+
*/
216+
private function getDefaultDataRoots()
219217
{
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__);
229219
}
230220

231221
/**
232-
* @dataProvider provideMultipleRootLoadCases
222+
* @param string|array|null $root
223+
* @param LocatorInterface|null $locator
224+
*
225+
* @return FileSystemLoader
233226
*/
234-
public function testMultipleRootLoadCases($rootDirs, $path)
227+
private function getFileSystemLoader($root = null, LocatorInterface $locator = null)
235228
{
236-
$loader = new FileSystemLoader(
229+
return new FileSystemLoader(
237230
MimeTypeGuesser::getInstance(),
238231
ExtensionGuesser::getInstance(),
239-
$rootDirs,
240-
$this->getLocator()
232+
null !== $root ? $root : $this->getDefaultDataRoots(),
233+
null !== $locator ? $locator : $this->getFileSystemLocator()
241234
);
235+
}
242236

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);
247245
}
248246
}

0 commit comments

Comments
 (0)