Skip to content

Commit be2849b

Browse files
committed
add more tests
1 parent 23c2320 commit be2849b

File tree

8 files changed

+222
-118
lines changed

8 files changed

+222
-118
lines changed

.editorconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# This file is for unifying the coding style for different editors and IDEs
2+
# editorconfig.org
3+
4+
root = true
5+
6+
[*]
7+
end_of_line = lf
8+
charset = utf-8
9+
trim_trailing_whitespace = true
10+
insert_final_newline = false
11+
indent_style = space
12+
indent_size = 4

.travis.yml

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,29 @@
11
language: php
2-
php:
3-
- 5.4
4-
- 5.5
5-
- 5.6
6-
- 7.0
7-
- 7.1
2+
matrix:
3+
include:
4+
- php: 5.3
5+
dist: precise
6+
- php: 5.4
7+
dist: trusty
8+
- php: 5.5
9+
dist: trusty
10+
- php: 5.6
11+
dist: trusty
12+
- php: 7.0
13+
dist: trusty
14+
- php: 7.1
15+
dist: trusty
16+
- php: 7.2
17+
dist: trusty
18+
- php: nightly
19+
dist: trusty
20+
21+
before_script:
22+
- composer install
23+
24+
script:
25+
- mkdir -p build/logs
26+
- ./vendor/bin/phpunit --coverage-clover build/logs/clover.xml
27+
28+
after_script:
29+
- php vendor/bin/coveralls -v

composer.json

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,26 @@
1313
],
1414
"require": {
1515
"php": ">=5.3.0",
16-
"ext-gd": "*"
16+
"ext-gd": "*",
17+
"ext-fileinfo": "*"
1718
},
1819
"suggest": {
1920
"ext-exif": "Auto-rotate jpeg files"
2021
},
2122
"autoload": {
22-
"classmap": ["lib"]
23+
"psr-4": {
24+
"Gumlet\\": "lib/"
25+
}
26+
},
27+
"autoload-dev": {
28+
"psr-4": {
29+
"Gumlet\\": "test/"
30+
}
2331
},
2432
"require-dev": {
25-
"apigen/apigen": "^4.1"
33+
"phpunit/phpunit": "^4.8",
34+
"php-coveralls/php-coveralls": "dev-master",
35+
"ext-exif": "*",
36+
"ext-gd": "*"
2637
}
2738
}

lib/ImageResize.php

Lines changed: 48 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ class ImageResize
1414
const CROPLEFT = 4;
1515
const CROPRIGHT = 5;
1616
const CROPTOPCENTER = 6;
17+
const IMG_FLIP_HORIZONTAL = 0;
18+
const IMG_FLIP_VERTICAL = 1;
19+
const IMG_FLIP_BOTH = 2;
1720

1821
public $quality_jpg = 85;
1922
public $quality_webp = 85;
@@ -79,11 +82,12 @@ public function addFilter(callable $filter)
7982
* Apply filters.
8083
*
8184
* @param $image resource an image resource identifier
85+
* @param $filterType filter type and default value is IMG_FILTER_NEGATE
8286
*/
83-
protected function applyFilter($image)
87+
protected function applyFilter($image, $filterType = IMG_FILTER_NEGATE)
8488
{
8589
foreach ($this->filters as $function) {
86-
$function($image);
90+
$function($image, $filterType);
8791
}
8892
}
8993

@@ -149,7 +153,6 @@ public function __construct($filename)
149153

150154
default:
151155
throw new ImageResizeException('Unsupported image type');
152-
break;
153156
}
154157

155158
if (!$this->source_image) {
@@ -571,7 +574,7 @@ public function crop($width, $height, $allow_enlarge = false, $position = self::
571574
*/
572575
public function freecrop($width, $height, $x = false, $y = false)
573576
{
574-
if ($x === false or $y === false) {
577+
if ($x === false || $y === false) {
575578
return $this->crop($width, $height);
576579
}
577580
$this->source_x = $x;
@@ -658,59 +661,51 @@ protected function getCropPosition($expectedSize, $position = self::CROPCENTER)
658661
}
659662
return $size;
660663
}
661-
}
662-
663-
// imageflip definition for PHP < 5.5
664-
if (!function_exists('imageflip')) {
665-
define('IMG_FLIP_HORIZONTAL', 0);
666-
define('IMG_FLIP_VERTICAL', 1);
667-
define('IMG_FLIP_BOTH', 2);
668664

669-
function imageflip($image, $mode)
665+
/**
666+
* Flips an image using a given mode if PHP version is lower than 5.5
667+
*
668+
* @param resource $image
669+
* @param integer $mode
670+
* @return integer|null
671+
*/
672+
public function imageFlip($image, $mode)
670673
{
671-
switch ($mode) {
672-
case IMG_FLIP_HORIZONTAL: {
673-
$max_x = imagesx($image) - 1;
674-
$half_x = $max_x / 2;
675-
$sy = imagesy($image);
676-
$temp_image = imageistruecolor($image)? imagecreatetruecolor(1, $sy): imagecreate(1, $sy);
677-
for ($x = 0; $x < $half_x; ++$x) {
678-
imagecopy($temp_image, $image, 0, 0, $x, 0, 1, $sy);
679-
imagecopy($image, $image, $x, 0, $max_x - $x, 0, 1, $sy);
680-
imagecopy($image, $temp_image, $max_x - $x, 0, 0, 0, 1, $sy);
674+
switch($mode) {
675+
case self::IMG_FLIP_HORIZONTAL: {
676+
$max_x = imagesx($image) - 1;
677+
$half_x = $max_x / 2;
678+
$sy = imagesy($image);
679+
$temp_image = imageistruecolor($image)? imagecreatetruecolor(1, $sy): imagecreate(1, $sy);
680+
for ($x = 0; $x < $half_x; ++$x) {
681+
imagecopy($temp_image, $image, 0, 0, $x, 0, 1, $sy);
682+
imagecopy($image, $image, $x, 0, $max_x - $x, 0, 1, $sy);
683+
imagecopy($image, $temp_image, $max_x - $x, 0, 0, 0, 1, $sy);
684+
}
685+
break;
681686
}
682-
break;
683-
}
684-
case IMG_FLIP_VERTICAL: {
685-
$sx = imagesx($image);
686-
$max_y = imagesy($image) - 1;
687-
$half_y = $max_y / 2;
688-
$temp_image = imageistruecolor($image)? imagecreatetruecolor($sx, 1): imagecreate($sx, 1);
689-
for ($y = 0; $y < $half_y; ++$y) {
690-
imagecopy($temp_image, $image, 0, 0, 0, $y, $sx, 1);
691-
imagecopy($image, $image, 0, $y, 0, $max_y - $y, $sx, 1);
692-
imagecopy($image, $temp_image, 0, $max_y - $y, 0, 0, $sx, 1);
693-
}
694-
break;
695-
}
696-
case IMG_FLIP_BOTH: {
697-
$sx = imagesx($image);
698-
$sy = imagesy($image);
699-
$temp_image = imagerotate($image, 180, 0);
700-
imagecopy($image, $temp_image, 0, 0, 0, 0, $sx, $sy);
701-
break;
702-
}
703-
default: {
704-
return;
705-
}
687+
case self::IMG_FLIP_VERTICAL: {
688+
$sx = imagesx($image);
689+
$max_y = imagesy($image) - 1;
690+
$half_y = $max_y / 2;
691+
$temp_image = imageistruecolor($image)? imagecreatetruecolor($sx, 1): imagecreate($sx, 1);
692+
for ($y = 0; $y < $half_y; ++$y) {
693+
imagecopy($temp_image, $image, 0, 0, 0, $y, $sx, 1);
694+
imagecopy($image, $image, 0, $y, 0, $max_y - $y, $sx, 1);
695+
imagecopy($image, $temp_image, 0, $max_y - $y, 0, 0, $sx, 1);
696+
}
697+
break;
698+
}
699+
case self::IMG_FLIP_BOTH: {
700+
$sx = imagesx($image);
701+
$sy = imagesy($image);
702+
$temp_image = imagerotate($image, 180, 0);
703+
imagecopy($image, $temp_image, 0, 0, 0, 0, $sx, $sy);
704+
break;
705+
}
706+
default:
707+
return null;
706708
}
707709
imagedestroy($temp_image);
708710
}
709711
}
710-
711-
/**
712-
* PHP Exception used in the ImageResize class
713-
*/
714-
class ImageResizeException extends \Exception
715-
{
716-
}

lib/ImageResizeException.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Gumlet;
4+
5+
/**
6+
* PHP Exception used in the ImageResize class
7+
*/
8+
class ImageResizeException extends \Exception
9+
{
10+
}

phpunit.xml

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
<phpunit colors="true">
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="vendor/autoload.php" colors="true" backupGlobals="false"
3+
backupStaticAttributes="false" syntaxCheck="false">
24
<testsuites>
3-
<testsuite name="php-image-resize tests">
4-
<directory>test</directory>
5+
<testsuite name="Tests">
6+
<directory suffix="Test.php">test</directory>
57
</testsuite>
68
</testsuites>
79
<filter>
8-
<whitelist processUncoveredFilesFromWhitelist="true">
9-
<directory suffix=".php">lib</directory>
10-
</whitelist>
10+
<whitelist processUncoveredFilesFromWhitelist="false">
11+
<directory suffix=".php">lib</directory>
12+
</whitelist>
1113
</filter>
12-
<logging>
13-
<log type="coverage-text" target="php://stdout"/>
14-
</logging>
1514
</phpunit>

test/ImageResizeExceptionTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
use \Gumlet\ImageResize;
4+
use \Gumlet\ImageResizeException;
5+
use \PHPUnit\Framework\TestCase;
6+
7+
class ImageResizeExceptionTest extends TestCase
8+
{
9+
public function testExceptionEmpty()
10+
{
11+
$e = new ImageResizeException();
12+
13+
$this->assertEquals("", $e->getMessage());
14+
$this->assertInstanceOf('\Gumlet\ImageResizeException', $e);
15+
}
16+
17+
public function testExceptionMessage()
18+
{
19+
$e = new ImageResizeException("General error");
20+
21+
$this->assertEquals("General error", $e->getMessage());
22+
$this->assertInstanceOf('\Gumlet\ImageResizeException', $e);
23+
}
24+
25+
public function testExceptionExtending()
26+
{
27+
$e = new ImageResizeException("General error");
28+
29+
$this->assertInstanceOf('\Exception', $e);
30+
}
31+
32+
public function testExceptionThrown()
33+
{
34+
try{
35+
throw new ImageResizeException("General error");
36+
} catch (\Exception $e) {
37+
$this->assertEquals("General error", $e->getMessage());
38+
$this->assertInstanceOf('\Gumlet\ImageResizeException', $e);
39+
return;
40+
}
41+
$this->fail();
42+
}
43+
}
44+
// It's pretty easy to get your attention these days, isn't it? :D

0 commit comments

Comments
 (0)