Skip to content

Commit

Permalink
Add validation
Browse files Browse the repository at this point in the history
  • Loading branch information
joppuyo committed Sep 2, 2021
1 parent 7d47742 commit c287fe0
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# SPDX-FileCopyrightText: 2021 Johannes Siipola
# SPDX-License-Identifier: CC0-1.0

composer.lock
/vendor/
/tests/_output/*
!/tests/_output/.gitkeep
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"require": {
"php": ">=7.2.5",
"symfony/process": "^5.3",
"rosell-dk/image-mime-type-guesser": "^0.3.1"
"rosell-dk/image-mime-type-guesser": "^0.3.1",
"respect/validation": "^2.0.17"
},
"require-dev": {
"codeception/codeception": "^4.1",
Expand Down
19 changes: 19 additions & 0 deletions src/JpegXlEncode/Encoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
namespace Joppuyo\JpegXlEncode;

use ImageMimeTypeGuesser\ImageMimeTypeGuesser;
use Joppuyo\JpegXlEncode\Exception\InvalidArgumentException;
use Symfony\Component\Process\Process;
use Respect\Validation\Validator as v;

class Encoder {
/*
Expand Down Expand Up @@ -67,6 +69,8 @@ public static function encode(string $source, string $destination, array $option
$options['quality'] = 100;
}

self::validateOptions($options);

$flags = [];

// TODO: validate is numeric and in range
Expand Down Expand Up @@ -134,4 +138,19 @@ public static function ensure_permissions($path)
chmod($path, 0755);
}
}

private static function validateOptions(array $options)
{
$optionValidator = v::key('quality', v::intType()->between(1, 100))
->key('effort', v::intType()->between(1, 9))
->key('progressive', v::boolType())
->key('encoding', v::stringType()->in(['lossless', 'lossy']));

try {
$optionValidator->check($options);
} catch (\Exception $exception) {
throw new InvalidArgumentException($exception->getMessage());
}

}
}
7 changes: 7 additions & 0 deletions src/JpegXlEncode/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Joppuyo\JpegXlEncode\Exception;

class InvalidArgumentException extends \Exception {

}
2 changes: 1 addition & 1 deletion tests/unit/ExampleTest.php → tests/unit/EncodingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// SPDX-FileCopyrightText: 2021 Johannes Siipola
// SPDX-License-Identifier: MIT

class ExampleTest extends \Codeception\Test\Unit
class EncodingTest extends \Codeception\Test\Unit
{
/**
* @var \UnitTester
Expand Down
95 changes: 95 additions & 0 deletions tests/unit/ValidationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

// SPDX-FileCopyrightText: 2021 Johannes Siipola
// SPDX-License-Identifier: MIT

class ValidationTest extends \Codeception\Test\Unit
{
/**
* @var \UnitTester
*/
protected $tester;

protected function _before()
{
$binary = \Joppuyo\JpegXlEncode\Encoder::getBinaryPath();
\Joppuyo\JpegXlEncode\Encoder::ensure_permissions($binary);
}

protected function _after()
{
}

public function testCantSetInvalidQuality()
{
$this->tester->expectThrowable(\Joppuyo\JpegXlEncode\Exception\InvalidArgumentException::class, function () {
$source = __DIR__ . '/../_data/jpeg-xl-logo.png';
$destination = __DIR__ . '/../_output/testCantSetInvalidQuality.jxl';
$options = [
'encoding' => 'lossy',
'quality' => 'asdf',
];
\Joppuyo\JpegXlEncode\Encoder::encode($source, $destination, $options);
});

$this->tester->expectThrowable(\Joppuyo\JpegXlEncode\Exception\InvalidArgumentException::class, function () {
$source = __DIR__ . '/../_data/jpeg-xl-logo.png';
$destination = __DIR__ . '/../_output/testCantSetInvalidQuality.jxl';
$options = [
'encoding' => 'lossy',
'quality' => 123,
];
\Joppuyo\JpegXlEncode\Encoder::encode($source, $destination, $options);
});

$this->tester->expectThrowable(\Joppuyo\JpegXlEncode\Exception\InvalidArgumentException::class, function () {
$source = __DIR__ . '/../_data/jpeg-xl-logo.png';
$destination = __DIR__ . '/../_output/testCantSetInvalidQuality.jxl';
$options = [
'encoding' => 'lossy',
'quality' => -1,
];
\Joppuyo\JpegXlEncode\Encoder::encode($source, $destination, $options);
});
}

public function testCantSetInvalidEncoding()
{
$this->tester->expectThrowable(\Joppuyo\JpegXlEncode\Exception\InvalidArgumentException::class, function () {
$source = __DIR__ . '/../_data/jpeg-xl-logo.png';
$destination = __DIR__ . '/../_output/testCantSetInvalidQuality.jxl';
$options = [
'encoding' => 'asdf',
];
\Joppuyo\JpegXlEncode\Encoder::encode($source, $destination, $options);
});
}

public function testCantSetInvalidEffort()
{
$this->tester->expectThrowable(\Joppuyo\JpegXlEncode\Exception\InvalidArgumentException::class, function () {
$source = __DIR__ . '/../_data/jpeg-xl-logo.png';
$destination = __DIR__ . '/../_output/testCantSetInvalidQuality.jxl';
$options = [
'effort' => 'asdf',
];
\Joppuyo\JpegXlEncode\Encoder::encode($source, $destination, $options);
});
$this->tester->expectThrowable(\Joppuyo\JpegXlEncode\Exception\InvalidArgumentException::class, function () {
$source = __DIR__ . '/../_data/jpeg-xl-logo.png';
$destination = __DIR__ . '/../_output/testCantSetInvalidQuality.jxl';
$options = [
'effort' => 10,
];
\Joppuyo\JpegXlEncode\Encoder::encode($source, $destination, $options);
});
$this->tester->expectThrowable(\Joppuyo\JpegXlEncode\Exception\InvalidArgumentException::class, function () {
$source = __DIR__ . '/../_data/jpeg-xl-logo.png';
$destination = __DIR__ . '/../_output/testCantSetInvalidQuality.jxl';
$options = [
'effort' => -1,
];
\Joppuyo\JpegXlEncode\Encoder::encode($source, $destination, $options);
});
}
}

0 comments on commit c287fe0

Please sign in to comment.