Skip to content

Commit 0e88eb2

Browse files
authored
Merge pull request #36 from Orbitale/page
[WIP] Follow-up of #33 to implement the Page option
2 parents 8f102ee + 445d253 commit 0e88eb2

19 files changed

+333
-79
lines changed

.github/workflows/php.yml

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,6 @@ jobs:
1818
steps:
1919
- uses: actions/checkout@v2
2020

21-
- name: Install ImageMagick
22-
run: |
23-
sudo apt-get update
24-
sudo apt-get upgrade -y libjpeg-dev libpng-dev libgif-dev imagemagick
25-
mkdir -p build/logs
26-
2721
- uses: shivammathur/setup-php@v2
2822
with:
2923
php-version: "${{ matrix.php-version }}"
@@ -40,6 +34,10 @@ jobs:
4034
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
4135
restore-keys: ${{ runner.os }}-composer-
4236

43-
- run: composer install
37+
- name: Setup
38+
run: |
39+
make install
4440
45-
- run: vendor/bin/phpunit
41+
- name: Test
42+
run: |
43+
make test

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@ Tests/Resources/outputs/*
99
phpunit.xml
1010
.phpunit.result.cache
1111
.phpunit.cache
12+
13+
ImageMagick*.tar.gz
14+
/ImageMagick-*/

.php_cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ return PhpCsFixer\Config::create()
3939
'heredoc_to_nowdoc' => true,
4040
'linebreak_after_opening_tag' => true,
4141
'logical_operators' => true,
42-
'mb_str_functions' => true,
4342
'native_function_invocation' => true,
4443
'no_null_property_initialization' => true,
4544
'no_php4_constructor' => true,

Makefile

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
SHELL := /bin/bash
2+
3+
IMAGEMAGICK_DOCKER_IMAGE = orbitale-imphp
4+
5+
# Helper vars
6+
_TITLE := "\033[32m[%s]\033[0m %s\n"
7+
_ERROR := "\033[31m[%s]\033[0m %s\n"
8+
9+
.DEFAULT_GOAL := help
10+
help: ## Show this help.
11+
@printf "\n Available commands:\n\n"
12+
@grep -E '(^[a-zA-Z_-]+:.*?##.*$$)|(^##)' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[32m%-25s\033[0m %s\n", $$1, $$2}' | sed -e 's/\[32m## */[33m/'
13+
.PHONY: help
14+
15+
install: vendor imagemagick-docker ## Install composer dependencies and Docker image for testing
16+
.PHONY: install
17+
18+
start: ## Start Docker image for testing
19+
@docker start $(IMAGEMAGICK_DOCKER_IMAGE)
20+
.PHONY: start
21+
22+
stop: ## Stop testing Docker image
23+
@docker stop $(IMAGEMAGICK_DOCKER_IMAGE)
24+
.PHONY: stop
25+
26+
test: start ## Start Docker image for testing
27+
export IMAGEMAGICK_PATH="docker exec $(IMAGEMAGICK_DOCKER_IMAGE) `pwd`/docker_entrypoint.sh magick" && \
28+
vendor/bin/phpunit
29+
$(MAKE) stop
30+
.PHONY: test
31+
32+
vendor:
33+
@printf $(_TITLE) "Install" "Installing Composer dependencies"
34+
@composer update
35+
.PHONY: vendor
36+
37+
imagemagick-docker:
38+
@printf $(_TITLE) "Install" "Removing existing Docker image"
39+
@docker rm --force --volumes $(IMAGEMAGICK_DOCKER_IMAGE)
40+
@printf $(_TITLE) "Install" "Creating ImageMagick Docker image for development"
41+
@docker create \
42+
--name=$(IMAGEMAGICK_DOCKER_IMAGE) \
43+
--volume `pwd`:`pwd` \
44+
--workdir=`pwd` \
45+
--entrypoint="`pwd`/docker_entrypoint.sh" \
46+
dpokidov/imagemagick:latest \
47+
sleep 99999 \
48+
>/dev/null
49+
.PHONY: imagemagick-docker

Resources/references.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,4 +732,87 @@
732732
'none',
733733
'transparent',
734734
],
735+
'paper_sizes' => [
736+
'2A0',
737+
'4A0',
738+
'4x6',
739+
'5x7',
740+
'7x9',
741+
'8x10',
742+
'9x11',
743+
'9x12',
744+
'10x13',
745+
'10x14',
746+
'11x17',
747+
'a0',
748+
'a1',
749+
'a2',
750+
'a3',
751+
'a4',
752+
'a4small',
753+
'a5',
754+
'a6',
755+
'a7',
756+
'a8',
757+
'a9',
758+
'a10',
759+
'archa',
760+
'archb',
761+
'archC',
762+
'archd',
763+
'arche',
764+
'b0',
765+
'b1',
766+
'b2',
767+
'b3',
768+
'b4',
769+
'b5',
770+
'b6',
771+
'b7',
772+
'b8',
773+
'b9',
774+
'b10',
775+
'c0',
776+
'c1',
777+
'c2',
778+
'c3',
779+
'c4',
780+
'c5',
781+
'c6',
782+
'c7',
783+
'csheet',
784+
'dsheet',
785+
'esheet',
786+
'executive',
787+
'flsa',
788+
'flse',
789+
'folio',
790+
'halfletter',
791+
'isob0',
792+
'isob1',
793+
'isob2',
794+
'isob3',
795+
'isob4',
796+
'isob5',
797+
'isob6',
798+
'isob7',
799+
'isob8',
800+
'isob9',
801+
'isob10',
802+
'jisb0',
803+
'jisb1',
804+
'jisb2',
805+
'jisb3',
806+
'jisb4',
807+
'jisb5',
808+
'jisb6',
809+
'ledger',
810+
'legal',
811+
'letter',
812+
'lettersmall',
813+
'monarch',
814+
'quarto',
815+
'statement',
816+
'tabloid',
817+
],
735818
];

Tests/AbstractTestCase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ public function __construct($name = null, array $data = [], $dataName = '')
2828
$this->resourcesDir = TEST_RESOURCES_DIR;
2929
}
3030

31-
public function setUp(): void
31+
protected function setUp(): void
3232
{
3333
$dir = TEST_RESOURCES_DIR.'/outputs';
34-
foreach (\scandir($dir, SCANDIR_SORT_NONE) as $file) {
34+
foreach (\scandir($dir, \SCANDIR_SORT_NONE) as $file) {
3535
if ('.' !== $file && '..' !== $file && '.gitkeep' !== $file) {
3636
\unlink($dir.'/'.$file);
3737
}

Tests/CommandTest.php

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,18 @@ class CommandTest extends AbstractTestCase
2323
*/
2424
public function testWrongConvertDirs($path, $expectedMessage, $expectedException): void
2525
{
26-
$exception = '';
27-
$exceptionClass = '';
28-
2926
$path = \str_replace('\\', '/', $path);
3027
$expectedMessage = \str_replace('\\', '/', $expectedMessage);
31-
try {
32-
new Command($path);
33-
} catch (\Exception $e) {
34-
$exception = $e->getMessage();
35-
$exceptionClass = \get_class($e);
36-
}
37-
static::assertStringContainsString($expectedMessage, $exception);
38-
static::assertEquals($expectedException, $exceptionClass);
28+
$this->expectExceptionMessage($expectedMessage);
29+
$this->expectException($expectedException);
30+
31+
new Command($path);
3932
}
4033

4134
public function provideWrongConvertDirs(): ?\Generator
4235
{
43-
yield ['/this/is/a/dummy/dir', 'The specified path (/this/is/a/dummy/dir) is not a file.', MagickBinaryNotFoundException::class];
44-
yield ['./', 'The specified path (.) is not a file.', MagickBinaryNotFoundException::class];
36+
yield ['/this/is/a/dummy/dir', "ImageMagick does not seem to work well, the test command resulted in an error.\nExecution returned message: \"Command not found\"\nTo solve this issue, please run this command and check your error messages to see if ImageMagick was correctly installed:\n/this/is/a/dummy/dir -version", MagickBinaryNotFoundException::class];
37+
yield ['./', "The specified path (\"ImageMagick does not seem to work well, the test command resulted in an error.\nExecution returned message: \"Misuse of shell builtins\"\nTo solve this issue, please run this command and check your error messages to see if ImageMagick was correctly installed:\n. -version\") is not a file.\nYou must set the \"magickBinaryPath\" parameter as the main \"magick\" binary installed by ImageMagick.", MagickBinaryNotFoundException::class];
4538
}
4639

4740
/**
@@ -324,14 +317,15 @@ public function testMogrifyResizeImage(): void
324317
/**
325318
* @dataProvider provideTestCommandString
326319
*/
327-
public function testCommandString($source, $output, $geometry, $quality): void
320+
public function testCommandString($source, $output, $geometry, $quality, $format): void
328321
{
329322
$command = new Command(IMAGEMAGICK_DIR);
330323

331324
$commandString = $command
332325
->convert($source)
333326
->thumbnail($geometry)
334327
->quality($quality)
328+
->page($format)
335329
->file($output, false)
336330
->getCommand()
337331
;
@@ -340,6 +334,7 @@ public function testCommandString($source, $output, $geometry, $quality): void
340334
' '.$source.
341335
' -thumbnail "'.$geometry.'"'.
342336
' -quality '.$quality.
337+
' -page "'.$format.'"'.
343338
' '.$output;
344339

345340
$expected = \str_replace('\\', '/', $expected);
@@ -349,10 +344,10 @@ public function testCommandString($source, $output, $geometry, $quality): void
349344

350345
public function provideTestCommandString(): ?\Generator
351346
{
352-
yield [$this->resourcesDir.'/moon_180.jpg', $this->resourcesDir.'/outputs/moon_10_forced.jpg', '10x10!', 10];
353-
yield [$this->resourcesDir.'/moon_180.jpg', $this->resourcesDir.'/outputs/moon_1000.jpg', '1000x1000', 100];
354-
yield [$this->resourcesDir.'/moon_180.jpg', $this->resourcesDir.'/outputs/moon_half.jpg', '50%', 50];
355-
yield [$this->resourcesDir.'/moon_180.jpg', $this->resourcesDir.'/outputs/moon_geometry.jpg', '30x30+20+20', 50];
347+
yield 0 => [$this->resourcesDir.'/moon_180.jpg', $this->resourcesDir.'/outputs/moon_10_forced.jpg', '10x10!', 10, 'a4'];
348+
yield 1 => [$this->resourcesDir.'/moon_180.jpg', $this->resourcesDir.'/outputs/moon_1000.jpg', '1000x1000', 100, '9x11'];
349+
yield 2 => [$this->resourcesDir.'/moon_180.jpg', $this->resourcesDir.'/outputs/moon_half.jpg', '50%', 50, 'halfletter'];
350+
yield 3 => [$this->resourcesDir.'/moon_180.jpg', $this->resourcesDir.'/outputs/moon_geometry.jpg', '30x30+20+20', 50, 'Letter+43+43'];
356351
}
357352

358353
public function testWrongExecutable(): void
Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,18 @@
1111
* file that was distributed with this source code.
1212
*/
1313

14-
namespace Orbitale\Component\ImageMagick\Tests;
14+
namespace Orbitale\Component\ImageMagick\Tests\References;
1515

1616
use Orbitale\Component\ImageMagick\Command;
1717
use Orbitale\Component\ImageMagick\ReferenceClasses\Geometry;
18+
use Orbitale\Component\ImageMagick\Tests\AbstractTestCase;
1819

1920
class GeometryTest extends AbstractTestCase
2021
{
2122
/**
22-
* @param int $width
23-
* @param int $height
24-
* @param int $x
25-
* @param int $y
26-
* @param string $aspectRatio
27-
*
2823
* @dataProvider provideValidGeometries
2924
*/
30-
public function testGeometry($width, $height, $x, $y, $aspectRatio): void
25+
public function testGeometry(?int $width, ?int $height, ?int $x, ?int $y, ?string $aspectRatio): void
3126
{
3227
$geometry = new Geometry($width, $height, $x, $y, $aspectRatio);
3328

@@ -333,15 +328,9 @@ public function provideValidGeometries(): ?\Generator
333328
}
334329

335330
/**
336-
* @param int $width
337-
* @param int $height
338-
* @param int $x
339-
* @param int $y
340-
* @param string $aspectRatio
341-
*
342331
* @dataProvider provideWrongGeometries
343332
*/
344-
public function testWrongGeometry($width, $height, $x, $y, $aspectRatio): void
333+
public function testWrongGeometry(?int $width, ?int $height, ?int $x, ?int $y, ?string $aspectRatio): void
345334
{
346335
$geometry = new Geometry($width, $height, $x, $y, $aspectRatio);
347336

@@ -826,4 +815,27 @@ public function provideWrongGeometries(): ?\Generator
826815
yield 462 => [100, 0, 1, 1, '^'];
827816
yield 463 => [100, 0, 1, 1, '>'];
828817
}
818+
819+
public function testInvalidAspectRatio(): void
820+
{
821+
$this->expectException(\InvalidArgumentException::class);
822+
$this->expectExceptionMessage(\sprintf("Invalid aspect ratio value to generate geometry, \"%s\" given.\nAvailable: <, !, ^, >", 'invalid_ratio'));
823+
824+
new Geometry(null, null, null, null, 'invalid_ratio');
825+
}
826+
827+
public function testInvalidWidthHeightSeparator(): void
828+
{
829+
$geometryString = '120+1+1';
830+
831+
$this->expectException(\InvalidArgumentException::class);
832+
$this->expectExceptionMessage(\sprintf(
833+
"The specified geometry (%s) is invalid.\n%s\n"."Please refer to ImageMagick command line documentation about geometry:\nhttp://www.imagemagick.org/script/command-line-processing.php#geometry\n",
834+
'120+1+1',
835+
'When using offsets and only width, you must specify the "x" separator like this: 120x+1+1'
836+
));
837+
838+
$geometry = new Geometry($geometryString);
839+
$geometry->validate();
840+
}
829841
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
* file that was distributed with this source code.
1212
*/
1313

14-
namespace Orbitale\Component\ImageMagick\Tests;
14+
namespace Orbitale\Component\ImageMagick\Tests\References;
1515

1616
use Orbitale\Component\ImageMagick\Command;
1717
use Orbitale\Component\ImageMagick\ReferenceClasses\Gravity;
18+
use Orbitale\Component\ImageMagick\Tests\AbstractTestCase;
1819

1920
class GravityTest extends AbstractTestCase
2021
{

Tests/References/InterlaceTypesTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function testInvalidInterlaceTypes($interlaceType): void
7171
$msg = $e->getMessage();
7272
}
7373
static::assertStringContainsString(
74-
\sprintf('The specified interlace type (%s) is invalid', \mb_strtolower(\trim($interlaceType))),
74+
\sprintf('The specified interlace type (%s) is invalid', \strtolower(\trim($interlaceType))),
7575
$msg
7676
);
7777
}

0 commit comments

Comments
 (0)