Skip to content

Commit

Permalink
Detect mime type in gaufrette image loader
Browse files Browse the repository at this point in the history
  • Loading branch information
ojhaujjwal committed Jul 10, 2014
1 parent 946e4b4 commit 627e7cd
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 3 deletions.
23 changes: 22 additions & 1 deletion src/Imagine/Loader/GaufretteLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
namespace HtImgModule\Imagine\Loader;

use Gaufrette\Filesystem;
use Gaufrette\Exception\FileNotFound as FileNotFoundException;
use HtImgModule\Exception;
use HtImgModule\Binary\Binary;

/**
* Image Loader for Library, Gaufrette
Expand Down Expand Up @@ -29,6 +32,24 @@ public function __construct(Filesystem $filesystem)
*/
public function load($path)
{
return $this->filesystem->read($path);
try {
$contents = $this->filesystem->read($path);
} catch (FileNotFoundException $e) {
throw new Exception\ImageNotFoundException(sprintf('Source image not found in "%s"', $path));
}

try {
$mimeType = $this->filesystem->mimeType($path);
} catch (\LogicException $e) {
// Mime Type could not be detected
return $contents;
}

if (!$mimeType) {
// Mime Type could not be detected
return $contents;
}

return new Binary($contents, $mimeType);
}
}
2 changes: 1 addition & 1 deletion src/Imagine/Loader/LoaderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface LoaderInterface
* The path may be a file path on a filesystem, or any unique identifier among the storage engine implemented by this Loader.
*
* @param mixed $path
*
* @throws \HtImgModule\Exception\ImageNotFoundException
* @return Binary|string An image binary content
*/
public function load($path);
Expand Down
51 changes: 50 additions & 1 deletion tests/HtImgModuleTest/Imagine/Loader/GaufretteLoaderTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
<?php
namespace HtImgModuleTest\Imagine\Loader;

use Gaufrette\Exception\FileNotFound as FileNotFoundException;
use HtImgModule\Imagine\Loader\GaufretteLoader;

class GaufretteLoaderTest extends \PHPUnit_Framework_TestCase
{
public function testLoad()
public function testGetExceptionWhenImageCouldNotBeFound()
{
$filesystem = $this->getMockBuilder('Gaufrette\Filesystem')
->disableOriginalConstructor()
->getMock();
$filesystem->expects($this->exactly(1))
->method('read')
->with('my/special/image')
->will($this->throwException(new FileNotFoundException('my/special/image')));
$loader = new GaufretteLoader($filesystem);

$this->setExpectedException('HtImgModule\Exception\ImageNotFoundException');
$loader->load('my/special/image');
}

public function testLoadOnlyContents()
{
$filesystem = $this->getMockBuilder('Gaufrette\Filesystem')
->disableOriginalConstructor()
Expand All @@ -16,4 +32,37 @@ public function testLoad()
$loader = new GaufretteLoader($filesystem);
$this->assertEquals('some/image/content', $loader->load('asdfasdf'));
}

public function testLoadOnlyContentsWhenLogicException()
{
$filesystem = $this->getMockBuilder('Gaufrette\Filesystem')
->disableOriginalConstructor()
->getMock();
$filesystem->expects($this->exactly(1))
->method('read')
->will($this->returnValue('some/image/content'));
$filesystem->expects($this->exactly(1))
->method('mimeType')
->will($this->throwException(new \LogicException));
$loader = new GaufretteLoader($filesystem);
$this->assertEquals('some/image/content', $loader->load('asdfasdf'));
}

public function testGetBinary()
{
$filesystem = $this->getMockBuilder('Gaufrette\Filesystem')
->disableOriginalConstructor()
->getMock();
$filesystem->expects($this->exactly(1))
->method('read')
->will($this->returnValue('some/image/content'));
$filesystem->expects($this->exactly(1))
->method('mimeType')
->will($this->returnValue('image/jpeg'));
$loader = new GaufretteLoader($filesystem);
$binary = $loader->load('asdfasdf');
$this->assertInstanceOf('HtImgModule\Binary\Binary', $binary);
$this->assertEquals('some/image/content', $binary->getContent());
$this->assertEquals('image/jpeg', $binary->getMimeType());
}
}

0 comments on commit 627e7cd

Please sign in to comment.