Skip to content

Commit

Permalink
Add ImageType value object and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
odan committed Jun 13, 2019
1 parent 18b5da0 commit 85c60f3
Show file tree
Hide file tree
Showing 5 changed files with 197 additions and 40 deletions.
64 changes: 63 additions & 1 deletion src/ImageType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,74 @@

namespace Selective\ImageType;

use InvalidArgumentException;

/**
* Image format constants.
*/
class ImageType
{
/**
* @var string The value
*/
private $type;

public const JPEG = 'jpeg';
public const GIF = 'gif';
public const PNG = 'png';
public const WEBP = 'webp';
public const BMP = 'bmp';
public const PSD = 'psd';
public const TIFF = 'tiff';
public const SVG = 'svg';
public const ICO = 'ico';
public const CUR = 'cur';
public const SWF = 'swf';
public const AI = 'ai';

/**
* ImageType constructor.
*
* @param string $type The image format
*/
public function __construct(string $type)
{
if (empty($type)) {
throw new InvalidArgumentException(sprintf('Invalid type: %s', $type));
}

$this->type = $type;
}

/**
* Format to string.
*
* @return string The type
*/
public function toString(): string
{
return $this->type;
}

/**
* Format to string.
*
* @return string The type
*/
public function __toString()
{
return $this->toString();
}

// add more supported formats ...
/**
* Compare with other image type.
*
* @param ImageType $other The other type
*
* @return bool Status
*/
public function equals(ImageType $other): bool
{
return $this->type === $other->type;
}
}
6 changes: 3 additions & 3 deletions src/ImageTypeDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ final class ImageTypeDetector
*
* @throws ImageTypeDetectorException
*
* @return string The image type
* @return ImageType The image type
*/
public function getImageTypeFromFile(SplFileInfo $file): string
public function getImageTypeFromFile(SplFileInfo $file): ImageType
{
$realFile = $file->getRealPath();

Expand All @@ -35,7 +35,7 @@ public function getImageTypeFromFile(SplFileInfo $file): string
throw new ImageTypeDetectorException(sprintf('Image type could not be detected: %s', $file->getRealPath()));
}

return $type;
return new ImageType($type);
}

/**
Expand Down
96 changes: 96 additions & 0 deletions tests/ImageTypeDetectorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

namespace Selective\ImageType\Test;

use PHPUnit\Framework\TestCase;
use Selective\ImageType\ImageType;
use Selective\ImageType\ImageTypeDetector;
use Selective\ImageType\ImageTypeDetectorException;
use SplFileInfo;

/**
* Test.
*/
class ImageTypeDetectorTest extends TestCase
{
/**
* Test.
*
* @dataProvider providerGetImageTypeFromFile
*
* @param string $file The file
* @param string $expected The expected value
*
* @return void
*/
public function testGetImageTypeFromFile(string $file, string $expected): void
{
$this->assertFileExists($file);
$imageTypeDetector = new ImageTypeDetector();

$file = new SplFileInfo($file);
$actual = $imageTypeDetector->getImageTypeFromFile($file);

$this->assertTrue($actual->equals(new ImageType($expected)));
}

/**
* Provider.
*
* @return array
*/
public function providerGetImageTypeFromFile(): array
{
return [
[__DIR__ . '/images/test.gif', ImageType::GIF],
[__DIR__ . '/images/test.jpg', ImageType::JPEG],
//[__DIR__ . '/images/test.wbmp', 'wbmp'],
[__DIR__ . '/images/test-animated.gif', ImageType::GIF],
[__DIR__ . '/images/test-bmp8.bmp', ImageType::BMP],
[__DIR__ . '/images/test-bmp24.bmp', ImageType::BMP],
[__DIR__ . '/images/test-png8.png', ImageType::PNG],
[__DIR__ . '/images/test-png24.png', ImageType::PNG],
[__DIR__ . '/images/test-png32.png', ImageType::PNG],
[__DIR__ . '/images/test-tiff8.tif', ImageType::TIFF],
[__DIR__ . '/images/test-tiff24.tif', ImageType::TIFF],
[__DIR__ . '/images/test-tiff32.tif', ImageType::TIFF],
[__DIR__ . '/images/test.psd', ImageType::PSD],
[__DIR__ . '/images/test.webp', ImageType::WEBP],
[__DIR__ . '/images/test2.webp', ImageType::WEBP],
[__DIR__ . '/images/test.svg', ImageType::SVG],
[__DIR__ . '/images/test.ico', ImageType::ICO],
[__DIR__ . '/images/test.cur', ImageType::CUR],
[__DIR__ . '/images/test.ai', ImageType::AI],
[__DIR__ . '/images/test.swf', ImageType::SWF],
];
}

/**
* Test.
*
* @return void
*/
public function testGetImageTypeFromFileWithError(): void
{
$this->expectException(ImageTypeDetectorException::class);

$imageTypeDetector = new ImageTypeDetector();

$file = new SplFileInfo('/nada');
$imageTypeDetector->getImageTypeFromFile($file);
}

/**
* Test.
*
* @return void
*/
public function testGetImageTypeWithUnknownFormat(): void
{
$this->expectException(ImageTypeDetectorException::class);
$imageTypeDetector = new ImageTypeDetector();

$file = new SplFileInfo(__DIR__ . '/images/empty.png');
$imageTypeDetector->getImageTypeFromFile($file);
}
}
70 changes: 34 additions & 36 deletions tests/ImageTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,64 +3,62 @@
namespace Selective\ImageType\Test;

use PHPUnit\Framework\TestCase;
use ReflectionClass;
use Selective\ImageType\ImageType;
use Selective\ImageType\ImageTypeDetector;
use SplFileInfo;

/**
* Test.
*/
class ImageTypeTest extends TestCase
{
/**
* Test create object.
* Test.
*
* @dataProvider providerGetImageTypeFromFile
* @dataProvider providerCreateInstance
*
* @param string $file The file
* @param string $type The type
* @param string $expected The expected value
*
* @return void
*/
public function testGetImageTypeFromFile(string $file, string $expected): void
public function testCreateInstance(string $type, string $expected): void
{
$this->assertFileExists($file);
$imageTypeDetector = new ImageTypeDetector();
$imageType = new ImageType($type);

$file = new SplFileInfo($file);
$actual = $imageTypeDetector->getImageTypeFromFile($file);

$this->assertSame($expected, $actual);
$this->assertSame((string)$imageType, $expected);
$this->assertSame($imageType->__toString(), $imageType->toString());
}

/**
* Provider.
*
* @return array
* @return array Data
*/
public function providerCreateInstance(): array
{
$class = new ReflectionClass(ImageType::class);

$constants = $class->getConstants();

$data = [];
foreach ($constants as $constant) {
$data[] = [
$constant,
$constant,
];
}

return $data;
}

/**
* Test.
*
* @return void
*/
public function providerGetImageTypeFromFile(): array
public function testCreateInstanceWithError(): void
{
return [
[__DIR__ . '/images/test.gif', 'gif'],
[__DIR__ . '/images/test.jpg', ImageType::JPEG],
//[__DIR__ . '/images/test.wbmp', 'wbmp'],
[__DIR__ . '/images/test-animated.gif', 'gif'],
[__DIR__ . '/images/test-bmp8.bmp', 'bmp'],
[__DIR__ . '/images/test-bmp24.bmp', 'bmp'],
[__DIR__ . '/images/test-png8.png', 'png'],
[__DIR__ . '/images/test-png24.png', 'png'],
[__DIR__ . '/images/test-png32.png', 'png'],
[__DIR__ . '/images/test-tiff8.tif', 'tiff'],
[__DIR__ . '/images/test-tiff24.tif', 'tiff'],
[__DIR__ . '/images/test-tiff32.tif', 'tiff'],
[__DIR__ . '/images/test.psd', 'psd'],
[__DIR__ . '/images/test.webp', 'webp'],
[__DIR__ . '/images/test2.webp', 'webp'],
[__DIR__ . '/images/test.svg', 'svg'],
[__DIR__ . '/images/test.ico', 'ico'],
[__DIR__ . '/images/test.cur', 'cur'],
[__DIR__ . '/images/test.ai', 'ai'],
[__DIR__ . '/images/test.swf', 'swf'],
];
$this->expectException(\InvalidArgumentException::class);
new ImageType('');
}
}
1 change: 1 addition & 0 deletions tests/images/empty.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 85c60f3

Please sign in to comment.