Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix all test fails in master (just to check) #658

Merged
merged 3 commits into from
Nov 1, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions Imagine/Filter/Loader/AutoRotateFilterLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Liip\ImagineBundle\Imagine\Filter\Loader;

use Imagine\Image\ImageInterface;
use Imagine\Exception\InvalidArgumentException;

/**
* AutoRotateFilterLoader - rotates an Image based on its EXIF Data.
Expand All @@ -22,6 +23,12 @@ class AutoRotateFilterLoader implements LoaderInterface
public function load(ImageInterface $image, array $options = array())
{
if ($orientation = $this->getOrientation($image)) {
if ($orientation < 1 || $orientation > 8) {
throw new InvalidArgumentException(
sprintf('The image has wrong EXIF orientation tag (%d)', $orientation)
);
}

// Rotates if necessary.
$degree = $this->calculateRotation($orientation);
if ($degree !== 0) {
Expand Down Expand Up @@ -59,8 +66,6 @@ private function calculateRotation($orientation)
case 7:
case 8:
return -90;
default:
throw new Exception('Unhandled orientation');
}
}

Expand Down Expand Up @@ -112,9 +117,6 @@ private function isFlipped($orientation)
case 5:
case 7:
return true;

default:
throw new Exception('Unhandled orientation');
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 24 additions & 8 deletions Tests/Imagine/Cache/Resolver/CacheResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,16 @@ public function testRemoveSinglePathCacheOnRemove()
$cacheResolver->resolve($this->path, $this->filter);

/*
* Three items:
* Checking 2 items:
* * The result of one resolve execution.
* * The index of entity.
* * The array cache meta info
*/
$this->assertCount(3, $this->readAttribute($cache, 'data'));
$this->assertCount(2, $this->getCacheEntries($cache));

$cacheResolver->remove(array($this->path), array($this->filter));

// Cache including index has been removed.
$this->assertCount(1, $this->readAttribute($cache, 'data'));
$this->assertCount(0, $this->getCacheEntries($cache));
}

public function testRemoveAllFilterCacheOnRemove()
Expand All @@ -167,16 +166,33 @@ public function testRemoveAllFilterCacheOnRemove()
$cacheResolver->resolve('aPathBar', 'thumbnail_100x100');

/*
* Seven items:
* Checking 6 items:
* * The result of four resolve execution.
* * The index of two entities.
* * The array cache meta info
*/
$this->assertCount(7, $this->readAttribute($cache, 'data'));
$this->assertCount(6, $this->getCacheEntries($cache));

$cacheResolver->remove(array(), array('thumbnail_233x233'));

// Cache including index has been removed.
$this->assertCount(4, $this->readAttribute($cache, 'data'));
$this->assertCount(3, $this->getCacheEntries($cache));
}

/**
* There's an intermittent cache entry which is a cache namespace
* version, it may or may not be there depending on doctrine-cache
* version. There's no point in checking it anyway since it's a detail
* of doctrine cache implementation.
*
* @param ArrayCache $cache
*
* @return array
*/
private function getCacheEntries(ArrayCache $cache)
{
$cacheEntries = $this->readAttribute($cache, 'data');
unset($cacheEntries['DoctrineNamespaceCacheKey[]']);

return $cacheEntries;
}
}
61 changes: 42 additions & 19 deletions Tests/Imagine/Filter/Loader/AutoRotateFilterLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,42 @@ private function loadExif($exifValue, $expectCallRotateValue, $expectCallFlip)
{
$loader = new AutoRotateFilterLoader();

// Mocks the metadata and makes it return the expected exif value for the rotation.
// If $exifValue is null, it means the image doesn't contain any metadata.
$metaData = $this->getMockMetaData();
// Mocks the image and makes it use the fake meta data.
$image = $this->getMockImage();

$metaData
->expects($this->atLeastOnce())
->method('offsetGet')
->willReturn($exifValue);
if (method_exists('Imagine\Image\ImageInterface', 'metadata')) {
// Mocks the metadata and makes it return the expected exif value for the rotation.
// If $exifValue is null, it means the image doesn't contain any metadata.
$metaData = $this->getMockMetaData();

if ($exifValue && $exifValue !== '1') {
$metaData
->expects($this->atLeastOnce())
->method('offsetGet')
->willReturn($exifValue);

if ($exifValue && $exifValue > '1' && $exifValue <= 8) {
$metaData
->expects($this->once())
->method('offsetSet')
->with($this->orientationKey, '1');
}

$image
->expects($this->atLeastOnce())
->method('metadata')
->willReturn($metaData);
} else {
$jpg = file_get_contents(__DIR__.'/../../../Fixtures/assets/pixel_1x1_orientation_at_0x30.jpg');
// The byte with orientation is at offset 0x30 for this image
$jpg[0x30] = chr((int) $exifValue);

$image
->expects($this->once())
->method('offsetSet')
->with($this->orientationKey, '1');
->method('get')
->with('jpg')
->will($this->returnValue($jpg));
}

// Mocks the image and makes it use the fake meta data.
$image = $this->getMockImage();

$image
->expects($this->atLeastOnce())
->method('metadata')
->willReturn($metaData);

// Checks that rotate is called with $expectCallRotateValue, or not called at all if $expectCallRotateValue is null.
$image
->expects($expectCallRotateValue !== null ? $this->once() : $this->never())
Expand Down Expand Up @@ -138,7 +150,18 @@ public function testLoadExif7()
*/
public function testLoadExif8()
{
$this->loadExif('8', null - 90, false);
$this->loadExif('8', -90, false);
}

/**
* Theoretically Orientation is `short` (uint16), so it could be anything
* from [0; 65535].
*
* @expectedException \Imagine\Exception\InvalidArgumentException
*/
public function testLoadExifInvalid()
{
$this->loadExif('10', null, false);
}

/**
Expand Down