Skip to content

Commit

Permalink
Migrate Crop filter loader away from using list() w/ regression test.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexwilson committed Sep 8, 2016
1 parent f601ee9 commit 079a0a5
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 4 deletions.
7 changes: 5 additions & 2 deletions Imagine/Filter/Loader/CropFilterLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ class CropFilterLoader implements LoaderInterface
*/
public function load(ImageInterface $image, array $options = array())
{
list($x, $y) = $options['start'];
list($width, $height) = $options['size'];
$x = isset($options['start'][0]) ? $options['start'][0] : null;
$y = isset($options['start'][1]) ? $options['start'][1] : null;

$width = isset($options['size'][0]) ? $options['size'][0] : null;
$height = isset($options['size'][1]) ? $options['size'][1] : null;

$filter = new Crop(new Point($x, $y), new Box($width, $height));
$image = $filter->apply($image);
Expand Down
2 changes: 1 addition & 1 deletion Imagine/Filter/Loader/ThumbnailFilterLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function load(ImageInterface $image, array $options = array())
$filter = ImageInterface::FILTER_UNDEFINED;
}

$width = isset($options['size'][0]) ? $options['size'][0] : null;
$width = isset($options['size'][0]) ? $options['size'][0] : null;
$height = isset($options['size'][1]) ? $options['size'][1] : null;

$size = $image->getSize();
Expand Down
72 changes: 72 additions & 0 deletions Tests/Imagine/Filter/Loader/CropFilterLoaderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace Liip\ImagineBundle\Tests\Filter;

use Liip\ImagineBundle\Imagine\Filter\Loader\CropFilterLoader;
use Liip\ImagineBundle\Tests\AbstractTest;
use Imagine\Image\Palette\Grayscale;
use Imagine\Image\Box;
use Imagine\Image\Point;

/**
* Test cases for CropFilterLoader class.
*
* @covers Liip\ImagineBundle\Imagine\Filter\Loader\CropFilterLoader
*/
class CropFilterLoaderTest extends AbstractTest
{
/**
* @var int
*/
const DUMMY_IMAGE_WIDTH = 500;

/**
* @var int
*/
const DUMMY_IMAGE_HEIGHT = 600;

/**
* @param int $width
* @param int $height
*
* @dataProvider cropDataProvider
*/
public function testLoad($coordinates, $area)
{
$x = $coordinates[0];
$y = $coordinates[1];

$width = $area[0];
$height = $area[1];

$loader = new CropFilterLoader();

$mockImageSize = new Box(
self::DUMMY_IMAGE_WIDTH,
self::DUMMY_IMAGE_HEIGHT
);
$image = $this->getMockImage();
$image->expects($this->once())
->method('crop')
->with(new Point($x, $y), new Box($width, $height))
->willReturn($image);

$options = array();
$options['start'] = $coordinates;
$options['size'] = $area;

$result = $loader->load($image, $options);
}

/**
* @returns array Array containing coordinate and width/height pairs.
*/
public function cropDataProvider()
{
return array(
array(array(140, 130), array(200, 129)),
array(array(30, 60), array(50, 50)),
array(array(400, 500), array(1, 30)),
);
}
}
2 changes: 1 addition & 1 deletion Tests/Imagine/Filter/Loader/ThumbnailFilterLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function testLoad($width, $height, $expected)
$result = $loader->load($image, $options);
}

/**
/**
* @returns array Array containing width/height pairs and an expected size.
*/
public function heightWidthProvider()
Expand Down

0 comments on commit 079a0a5

Please sign in to comment.