Skip to content

Commit

Permalink
Image ImageManager instantiation signature
Browse files Browse the repository at this point in the history
  • Loading branch information
olivervogel committed Oct 21, 2023
1 parent 0bc8b7c commit f0e7abb
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
8 changes: 8 additions & 0 deletions src/Exceptions/ConfigurationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Intervention\Image\Exceptions;

class ConfigurationException extends \RuntimeException
{
//
}
13 changes: 10 additions & 3 deletions src/ImageManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,16 @@ class ImageManager
{
use CanResolveDriverClass;

public function __construct(protected string $driver = 'gd')
private static $required_options = ['driver'];

public function __construct(protected array $options)
{
//
if (count(array_intersect(array_keys($options), self::$required_options)) != count(self::$required_options)) {
throw new Exceptions\ConfigurationException(
'The following attributes are required to initialize ImageManager: ' .
implode(', ', self::$required_options)
);
}
}

/**
Expand Down Expand Up @@ -55,6 +62,6 @@ public function read($source): ImageInterface
*/
protected function getCurrentDriver(): string
{
return strtolower($this->driver);
return strtolower($this->options['driver']);
}
}
17 changes: 12 additions & 5 deletions tests/ImageManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Intervention\Image\Tests;

use Intervention\Image\Exceptions\ConfigurationException;
use Intervention\Image\ImageManager;
use Intervention\Image\Interfaces\ImageInterface;

Expand All @@ -12,38 +13,44 @@ class ImageManagerTest extends TestCase
{
public function testConstructor()
{
$manager = new ImageManager('foo');
$manager = new ImageManager(['driver' => 'gd']);
$this->assertInstanceOf(ImageManager::class, $manager);

$this->expectException(ConfigurationException::class);
$manager = new ImageManager([]);

$this->expectException(ConfigurationException::class);
$manager = new ImageManager(['foo' => 'bar']);
}

/** @requires extension gd */
public function testCreateGd()
{
$manager = new ImageManager('gd');
$manager = new ImageManager(['driver' => 'gd']);
$image = $manager->create(5, 4);
$this->assertInstanceOf(ImageInterface::class, $image);
}

/** @requires extension gd */
public function testReadGd()
{
$manager = new ImageManager('gd');
$manager = new ImageManager(['driver' => 'gd']);
$image = $manager->read(__DIR__ . '/images/red.gif');
$this->assertInstanceOf(ImageInterface::class, $image);
}

/** @requires extension imagick */
public function testCreateImagick()
{
$manager = new ImageManager('imagick');
$manager = new ImageManager(['driver' => 'imagick']);
$image = $manager->create(5, 4);
$this->assertInstanceOf(ImageInterface::class, $image);
}

/** @requires extension imagick */
public function testReadImagick()
{
$manager = new ImageManager('imagick');
$manager = new ImageManager(['driver' => 'imagick']);
$image = $manager->read(__DIR__ . '/images/red.gif');
$this->assertInstanceOf(ImageInterface::class, $image);
}
Expand Down

0 comments on commit f0e7abb

Please sign in to comment.