Skip to content

Commit

Permalink
Merge pull request #16 from tattersoftware/tests
Browse files Browse the repository at this point in the history
Add Tests
  • Loading branch information
MGatner committed Jan 18, 2022
2 parents 2ecd25c + 9b1cd83 commit a1f232a
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 48 deletions.
1 change: 1 addition & 0 deletions UPGRADING.md
Expand Up @@ -12,3 +12,4 @@ for that library as well.
* Thumbnail handlers have been dubbed "Thumbnailers" to differentiate them from the library and service; adjust any class extensions appropriately
* The interface method has been renamed `process()` to differentiate it from the service method
* The service and interface method now both return the string path to the new file; failed attempts should throw some version of `RuntimeException`
* `Thumbnails` now uses an existing file extension before guessing based on MIME type
5 changes: 0 additions & 5 deletions src/Thumbnailers/ImageThumbnailer.php
Expand Up @@ -44,11 +44,6 @@ public function __construct($imagesHandler = null)
: service('image', $imagesHandler);
}

public function getInterface(): string
{
return ThumbnailerInterface::class;
}

/**
* Uses a framework image handler to fit the image to its new size.
*
Expand Down
5 changes: 3 additions & 2 deletions src/Thumbnails.php
Expand Up @@ -66,7 +66,7 @@ class Thumbnails
/**
* Initializes the library with its configuration.
*/
public function __construct(?ThumbnailsConfig $config)
public function __construct(ThumbnailsConfig $config)
{
$this->setConfig($config);
$this->factory = new ThumbnailerFactory();
Expand Down Expand Up @@ -205,7 +205,8 @@ public function create(string $input): ?string
}

// Get the file extension
if (! $extension = $file->guessExtension() ?? pathinfo($input, PATHINFO_EXTENSION)) {
$extension = pathinfo($input, PATHINFO_EXTENSION) ?: $file->guessExtension();
if (empty($extension)) {
throw ThumbnailsException::forNoExtension();
}

Expand Down
@@ -1,13 +1,14 @@
<?php

use CodeIgniter\Files\File;
use CodeIgniter\Images\Handlers\GDHandler;
use Tatter\Thumbnails\Thumbnailers\ImageThumbnailer;
use Tests\Support\ThumbnailsTestCase;
use Tests\Support\TestCase;

/**
* @internal
*/
final class ImageThumbnailerTest extends ThumbnailsTestCase
final class ImageThumbnailerTest extends TestCase
{
/**
* The test file primed for use
Expand All @@ -24,12 +25,22 @@ protected function setUp(): void
$this->file = new File(SUPPORTPATH . 'assets/image.jpg');
}

public function testCreatesFile()
public function testProcessCreatesFile()
{
$handler = new ImageThumbnailer();
$result = $handler->process($this->file, $this->config->imageType, $this->config->width, $this->config->height);

$this->assertIsString($result);
$this->assertFileExists($result);
}

public function testUsesHandler()
{
$images = new GDHandler();
$handler = new ImageThumbnailer($images);

$result = $this->getPrivateProperty($handler, 'images');

$this->assertSame($images, $result);
}
}
103 changes: 103 additions & 0 deletions tests/LibraryTest.php
@@ -0,0 +1,103 @@
<?php

use CodeIgniter\Files\Exceptions\FileNotFoundException;
use Tatter\Thumbnails\Exceptions\ThumbnailsException;
use Tatter\Thumbnails\Thumbnails;
use Tests\Support\TestCase;
use Tests\Support\Thumbnailers\MockThumbnailer;

/**
* @internal
*/
final class LibraryTest extends TestCase
{
public function testSetWidth()
{
$this->thumbnails->setWidth(42);

$result = $this->getPrivateProperty($this->thumbnails, 'width');

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

public function testSetHeight()
{
$this->thumbnails->setHeight(42);

$result = $this->getPrivateProperty($this->thumbnails, 'height');

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

public function testSetImageType()
{
$this->thumbnails->setImageType(IMAGETYPE_WBMP);

$result = $this->getPrivateProperty($this->thumbnails, 'imageType');

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

public function testCreatesFile()
{
$result = $this->thumbnails->create($this->input);

$this->assertFileExists($result);
}

public function testCreateThrowsNoFile()
{
$this->expectException(FileNotFoundException::class);
$this->expectExceptionMessage('File not found: banana');

$this->thumbnails->create('banana');
}

public function testCreateThrowsNoHandler()
{
// Disable the universal MockHandler
config('Handlers')->ignoredClasses = [MockThumbnailer::class];

// Use a fresh instance to trigger factory re-discovery
$thumbnails = new Thumbnails($this->config);

$this->expectException(ThumbnailsException::class);
$this->expectExceptionMessage('No handler found for file extension: php');

$thumbnails->create(__FILE__);
}

public function testCreateGathersErrors()
{
MockThumbnailer::$shouldError = true;

try {
$this->thumbnails->create(__FILE__);
} catch (ThumbnailsException $e) {
}

$result = $this->thumbnails->getErrors();
$this->assertCount(1, $result);
$this->assertSame(['mock' => 'This error happened.'], $result);
}

public function testCreateThrowsFailure()
{
MockThumbnailer::$shouldError = true;

$this->expectException(ThumbnailsException::class);
$this->expectExceptionMessage('Thumbnail creation failed for file: ' . __FILE__);

$this->thumbnails->create(__FILE__);
}

public function testCreateUsesHandler()
{
MockThumbnailer::$didProcess = false;

$this->thumbnails->setHandler('mock');
$this->thumbnails->create($this->input);

$this->assertTrue(MockThumbnailer::$didProcess);
}
}
38 changes: 38 additions & 0 deletions tests/MimeTest.php
@@ -0,0 +1,38 @@
<?php

use Config\Mimes;
use Tatter\Thumbnails\Exceptions\ThumbnailsException;
use Tests\Support\TestCase;

/**
* @internal
*/
final class MimeTest extends TestCase
{
private $mimeBackup;

protected function setUp(): void
{
parent::setUp();

// Ensure no MIME type matches
$this->mimeBackup = Mimes::$mimes;
Mimes::$mimes = [];
}

protected function tearDown(): void
{
parent::tearDown();

// Restore MIME types
Mimes::$mimes = $this->mimeBackup;
}

public function testCreateThrowsNoExtension()
{
$this->expectException(ThumbnailsException::class);
$this->expectExceptionMessage('Unable to determine file extension for thumbnail handling');

$this->thumbnails->create(SUPPORTPATH . 'assets/noext');
}
}
Expand Up @@ -3,15 +3,14 @@
namespace Tests\Support;

use CodeIgniter\Test\CIUnitTestCase;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamDirectory;
use Tatter\Thumbnails\Config\Thumbnails as ThumbnailsConfig;
use Tatter\Thumbnails\Thumbnails;
use Tests\Support\Thumbnailers\MockThumbnailer;

/**
* @internal
*/
abstract class ThumbnailsTestCase extends CIUnitTestCase
abstract class TestCase extends CIUnitTestCase
{
/**
* @var ThumbnailsConfig
Expand All @@ -24,26 +23,24 @@ abstract class ThumbnailsTestCase extends CIUnitTestCase
protected $thumbnails;

/**
* @var vfsStreamDirectory|null
* Path to the test file
*
* @var string
*/
protected $root;
protected $input = SUPPORTPATH . 'assets/image.jpg';

protected function setUp(): void
{
parent::setUp();

// Start the virtual filesystem
$this->root = vfsStream::setup();
// Disable handler caching
config('Handlers')->cacheDuration = null;

// Create the service
// Create the library
$this->config = new ThumbnailsConfig();
$this->thumbnails = new Thumbnails($this->config);
}

protected function tearDown(): void
{
parent::tearDown();

$this->root = null;
MockThumbnailer::$shouldError = false;
MockThumbnailer::$didProcess = false;
}
}
Expand Up @@ -3,10 +3,14 @@
namespace Tests\Support\Thumbnailers;

use CodeIgniter\Files\File;
use RuntimeException;
use Tatter\Thumbnails\Interfaces\ThumbnailerInterface;

class MockThumbnail implements ThumbnailerInterface
class MockThumbnailer implements ThumbnailerInterface
{
public static $didProcess = false;
public static $shouldError = false;

public static function handlerId(): string
{
return 'mock';
Expand All @@ -25,6 +29,10 @@ public static function attributes(): array
*/
public function process(File $file, int $imageType, int $width, int $height): string
{
if (self::$shouldError) {
throw new RuntimeException('This error happened.');
}

$path = tempnam(sys_get_temp_dir(), static::handlerId());

switch ($imageType) {
Expand All @@ -36,6 +44,7 @@ public function process(File $file, int $imageType, int $width, int $height): st
}

copy(SUPPORTPATH . 'assets/thumbnail.' . $extension, $path);
self::$didProcess = true;

return $path;
}
Expand Down
1 change: 1 addition & 0 deletions tests/_support/assets/noext
@@ -0,0 +1 @@
icanhascheeseburger
23 changes: 0 additions & 23 deletions tests/unit/LibraryTest.php

This file was deleted.

0 comments on commit a1f232a

Please sign in to comment.