Skip to content

Commit

Permalink
Merge 3.x into master
Browse files Browse the repository at this point in the history
  • Loading branch information
SonataCI committed Oct 31, 2019
2 parents 906add4 + 987e4be commit e6fdb68
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 5 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Expand Up @@ -64,6 +64,10 @@ matrix:
env: SONATA_ADMIN='dev-master as 3.x-dev'
- php: '7.3'
env: SYMFONY_DEPRECATIONS_HELPER=0
- php: '7.3'
env: IMAGINE='^1.0'
- php: '7.3'
env: IMAGINE='^0.7'
allow_failures:
- php: nightly
- env: SYMFONY_DEPRECATIONS_HELPER=0
Expand Down
3 changes: 2 additions & 1 deletion .travis/before_install_test.sh
Expand Up @@ -18,4 +18,5 @@ sed --in-place "s/\"dev-master\":/\"dev-${TRAVIS_COMMIT}\":/" composer.json
if [ "$DOCTRINE_ODM" != "" ]; then composer require "doctrine/mongodb-odm:$DOCTRINE_ODM" --no-update; fi;
if [ "$SONATA_CORE" != "" ]; then composer require "sonata-project/core-bundle:$SONATA_CORE" --no-update; fi;
if [ "$SONATA_ADMIN" != "" ]; then composer require "sonata-project/admin-bundle:$SONATA_ADMIN" --no-update; fi;

if [ "$SONATA_ADMIN" != "" ]; then composer require "sonata-project/admin-bundle:$SONATA_ADMIN" --no-update; fi;
if [ "$IMAGINE" != "" ]; then composer require "imagine/imagine:$IMAGINE" --no-update; fi;
4 changes: 2 additions & 2 deletions composer.json
Expand Up @@ -28,7 +28,7 @@
"require": {
"php": "^7.1",
"guzzlehttp/psr7": "^1.0",
"imagine/imagine": "^0.6 || ^0.7",
"imagine/imagine": "^0.6 || ^0.7 || ^1.0",
"jms/serializer-bundle": "^1.0 || ^2.0 || ^3.0",
"knplabs/gaufrette": "^0.8",
"kriswallsmith/buzz": "^0.15 || ^0.16",
Expand Down Expand Up @@ -73,7 +73,7 @@
"aws/aws-sdk-php": "^2.8",
"friendsofsymfony/rest-bundle": "^2.1",
"jackalope/jackalope-doctrine-dbal": "^1.1",
"liip/imagine-bundle": "^1.9",
"liip/imagine-bundle": "^1.9 || ^2.0",
"matthiasnoback/symfony-dependency-injection-test": "^3.1",
"nelmio/api-doc-bundle": "^2.11",
"sonata-project/admin-bundle": "^3.31",
Expand Down
45 changes: 45 additions & 0 deletions src/Resizer/ImagineCompatibleResizerTrait.php
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\MediaBundle\Resizer;

use Imagine\Image\ImageInterface;
use Imagine\Image\ImagineInterface;

/**
* This trait is used to provide compatibility with Imagine >= 1.0.0.
*/
trait ImagineCompatibleResizerTrait
{
/**
* Convert mode for compatibility with imagine >= 1.0.0.
*
* @param int|string $mode
*
* @return int|string
*/
final protected function convertMode($mode)
{
if (\is_string($mode) && version_compare(ImagineInterface::VERSION, '1.0.0', '>=')) {
if ('inset' === $mode) {
$mode = ImageInterface::THUMBNAIL_INSET;
} elseif ('outbound' === $mode) {
$mode = ImageInterface::THUMBNAIL_OUTBOUND;
} elseif (is_numeric($mode)) {
$mode = (int) $mode;
}
}

return $mode;
}
}
4 changes: 3 additions & 1 deletion src/Resizer/SimpleResizer.php
Expand Up @@ -26,6 +26,8 @@
*/
class SimpleResizer implements ResizerInterface
{
use ImagineCompatibleResizerTrait;

/**
* @var ImagineInterface
*/
Expand All @@ -47,7 +49,7 @@ class SimpleResizer implements ResizerInterface
public function __construct(ImagineInterface $adapter, $mode, MetadataBuilderInterface $metadata)
{
$this->adapter = $adapter;
$this->mode = $mode;
$this->mode = $this->convertMode($mode);
$this->metadata = $metadata;
}

Expand Down
4 changes: 3 additions & 1 deletion src/Resizer/SquareResizer.php
Expand Up @@ -32,6 +32,8 @@
*/
class SquareResizer implements ResizerInterface
{
use ImagineCompatibleResizerTrait;

/**
* @var ImagineInterface
*/
Expand All @@ -53,7 +55,7 @@ class SquareResizer implements ResizerInterface
public function __construct(ImagineInterface $adapter, $mode, MetadataBuilderInterface $metadata)
{
$this->adapter = $adapter;
$this->mode = $mode;
$this->mode = $this->convertMode($mode);
$this->metadata = $metadata;
}

Expand Down
44 changes: 44 additions & 0 deletions tests/Resizer/ImagineCompatibleResizerTraitTest.php
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\MediaBundle\Tests\Resizer;

use Imagine\Image\ImageInterface;
use PHPUnit\Framework\TestCase;
use Sonata\MediaBundle\Resizer\ImagineCompatibleResizerTrait;

class ImagineCompatibleResizerTraitTest extends TestCase
{
/**
* @dataProvider getModes
*/
public function testModeConversion($mode, $result): void
{
$objectWithTrait = $this->getObjectForTrait(ImagineCompatibleResizerTrait::class);
$reflection = new \ReflectionObject($objectWithTrait);
$method = $reflection->getMethod('convertMode');
$method->setAccessible(true);

$convertedMode = $method->invokeArgs($objectWithTrait, [$mode]);

$this->assertSame($result, $convertedMode);
}

public static function getModes()
{
return [
['inset', ImageInterface::THUMBNAIL_INSET],
['outbound', ImageInterface::THUMBNAIL_OUTBOUND],
];
}
}

0 comments on commit e6fdb68

Please sign in to comment.