From 85c60f39e6f3ace59a5310be38e58b1ae02fa2ec Mon Sep 17 00:00:00 2001 From: odan Date: Thu, 13 Jun 2019 09:48:54 +0200 Subject: [PATCH] Add ImageType value object and tests --- src/ImageType.php | 64 +++++++++++++++++++++- src/ImageTypeDetector.php | 6 +-- tests/ImageTypeDetectorTest.php | 96 +++++++++++++++++++++++++++++++++ tests/ImageTypeTest.php | 70 ++++++++++++------------ tests/images/empty.png | 1 + 5 files changed, 197 insertions(+), 40 deletions(-) create mode 100644 tests/ImageTypeDetectorTest.php create mode 100644 tests/images/empty.png diff --git a/src/ImageType.php b/src/ImageType.php index db6d677..6b57805 100644 --- a/src/ImageType.php +++ b/src/ImageType.php @@ -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; + } } diff --git a/src/ImageTypeDetector.php b/src/ImageTypeDetector.php index ce52270..b33acf0 100644 --- a/src/ImageTypeDetector.php +++ b/src/ImageTypeDetector.php @@ -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(); @@ -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); } /** diff --git a/tests/ImageTypeDetectorTest.php b/tests/ImageTypeDetectorTest.php new file mode 100644 index 0000000..600b5a9 --- /dev/null +++ b/tests/ImageTypeDetectorTest.php @@ -0,0 +1,96 @@ +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); + } +} diff --git a/tests/ImageTypeTest.php b/tests/ImageTypeTest.php index 7af3e87..0bc46ad 100644 --- a/tests/ImageTypeTest.php +++ b/tests/ImageTypeTest.php @@ -3,9 +3,8 @@ namespace Selective\ImageType\Test; use PHPUnit\Framework\TestCase; +use ReflectionClass; use Selective\ImageType\ImageType; -use Selective\ImageType\ImageTypeDetector; -use SplFileInfo; /** * Test. @@ -13,54 +12,53 @@ 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(''); } } diff --git a/tests/images/empty.png b/tests/images/empty.png new file mode 100644 index 0000000..6fd199e --- /dev/null +++ b/tests/images/empty.png @@ -0,0 +1 @@ +