Skip to content

Commit d4c41f4

Browse files
Merge pull request #78 from TheM1984/master
Added the ImageResizeException to the Readme and php unit test.
2 parents 67a692b + 2a6ba38 commit d4c41f4

File tree

2 files changed

+62
-8
lines changed

2 files changed

+62
-8
lines changed

README.md

+16
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,22 @@ $image
233233
;
234234
```
235235

236+
Exceptions
237+
--------
238+
239+
ImageResize throws ImageResizeException for it's own for errors. You can catch that or catch the general \Exception which it's extending.
240+
241+
It is not to be expected, but should anything go horribly wrong mid way then notice or warning Errors could be shown from the PHP GD and Image Functions (http://php.net/manual/en/ref.image.php)
242+
243+
```php
244+
try{
245+
$image = new ImageResize(null);
246+
echo "This line will not be printed";
247+
} catch (ImageResizeException $e) {
248+
echo "Something went wrong" . $e->getMessage();
249+
}
250+
```
251+
236252
API Doc
237253
-------
238254

test/Test.php

+46-8
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
include __DIR__.'/../lib/ImageResize.php';
44

55
use \Eventviva\ImageResize;
6+
use \Eventviva\ImageResizeException;
67

7-
if (version_compare(PHP_VERSION, '7.0.0') >= 0) {
8+
if (version_compare(PHP_VERSION, '7.0.0') >= 0 && !class_exists('PHPUnit_Framework_TestCase')) {
89
class_alias('PHPUnit\Framework\TestCase', 'PHPUnit_Framework_TestCase');
910
}
1011

@@ -60,13 +61,12 @@ public function testLoadString()
6061
$this->assertInstanceOf('\Eventviva\ImageResize', $resize);
6162
}
6263

63-
6464
/**
6565
* Bad load tests
6666
*/
6767

6868
/**
69-
* @expectedException Exception
69+
* @expectedException \Eventviva\ImageResizeException
7070
* @expectedExceptionMessage Could not read file
7171
*/
7272
public function testLoadNoFile()
@@ -75,7 +75,7 @@ public function testLoadNoFile()
7575
}
7676

7777
/**
78-
* @expectedException Exception
78+
* @expectedException \Eventviva\ImageResizeException
7979
* @expectedExceptionMessage Could not read file
8080
*/
8181
public function testLoadUnsupportedFile()
@@ -84,7 +84,7 @@ public function testLoadUnsupportedFile()
8484
}
8585

8686
/**
87-
* @expectedException Exception
87+
* @expectedException \Eventviva\ImageResizeException
8888
* @expectedExceptionMessage Could not read file
8989
*/
9090
public function testLoadUnsupportedFileString()
@@ -93,7 +93,7 @@ public function testLoadUnsupportedFileString()
9393
}
9494

9595
/**
96-
* @expectedException Exception
96+
* @expectedException \Eventviva\ImageResizeException
9797
* @expectedExceptionMessage Unsupported image type
9898
*/
9999
public function testLoadUnsupportedImage()
@@ -108,7 +108,7 @@ public function testLoadUnsupportedImage()
108108
}
109109

110110
/**
111-
* @expectedException Exception
111+
* @expectedException \Eventviva\ImageResizeException
112112
* @expectedExceptionMessage Unsupported image type
113113
*/
114114
public function testInvalidString()
@@ -398,7 +398,7 @@ public function testOutputPng()
398398
private function createImage($width, $height, $type)
399399
{
400400
if (!in_array($type, $this->image_types)) {
401-
throw new \Exception('Unsupported image type');
401+
throw new ImageResizeException('Unsupported image type');
402402
}
403403

404404
$image = imagecreatetruecolor($width, $height);
@@ -417,4 +417,42 @@ private function getTempFile()
417417
}
418418

419419
}
420+
421+
class ImageResizeExceptionTest extends PHPUnit_Framework_TestCase
422+
{
423+
public function testExceptionEmpty()
424+
{
425+
$e = new ImageResizeException();
426+
427+
$this->assertEquals("", $e->getMessage());
428+
$this->assertInstanceOf('\Eventviva\ImageResizeException', $e);
429+
}
430+
431+
public function testExceptionMessage()
432+
{
433+
$e = new ImageResizeException("General error");
434+
435+
$this->assertEquals("General error", $e->getMessage());
436+
$this->assertInstanceOf('\Eventviva\ImageResizeException', $e);
437+
}
438+
439+
public function testExceptionExtending()
440+
{
441+
$e = new ImageResizeException("General error");
442+
443+
$this->assertInstanceOf('\Exception', $e);
444+
}
445+
446+
public function testExceptionThrown()
447+
{
448+
try{
449+
throw new ImageResizeException("General error");
450+
} catch (\Exception $e) {
451+
$this->assertEquals("General error", $e->getMessage());
452+
$this->assertInstanceOf('\Eventviva\ImageResizeException', $e);
453+
return;
454+
}
455+
$this->fail();
456+
}
457+
}
420458
// It's pretty easy to get your attention these days, isn't it? :D

0 commit comments

Comments
 (0)