From 5b50e7b5067a61be510e4784732bdd506ce8e6f4 Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Sat, 26 Jul 2014 10:04:49 +0200 Subject: [PATCH] added "digit" as argument type --- src/Intervention/Image/Commands/Argument.php | 10 ++++ tests/ArgumentTest.php | 62 ++++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/src/Intervention/Image/Commands/Argument.php b/src/Intervention/Image/Commands/Argument.php index 7d6aa35a..9d30e9ea 100644 --- a/src/Intervention/Image/Commands/Argument.php +++ b/src/Intervention/Image/Commands/Argument.php @@ -117,6 +117,11 @@ class Argument $fail = ! is_a($value, '\Closure'); $message = sprintf('%s accepts only Closure as argument %d.', $this->getCommandName(), $this->key + 1); break; + + case 'digit': + $fail = ! $this->isDigit($value); + $message = sprintf('%s accepts only digit values as argument %d.', $this->getCommandName(), $this->key + 1); + break; } if ($fail) { @@ -199,4 +204,9 @@ class Argument return $this; } + + private function isDigit($value) + { + return is_numeric($value) ? intval($value) == $value : false; + } } diff --git a/tests/ArgumentTest.php b/tests/ArgumentTest.php index 7181db89..6c212f21 100644 --- a/tests/ArgumentTest.php +++ b/tests/ArgumentTest.php @@ -69,6 +69,68 @@ class ArgumentTest extends PHPUnit_Framework_TestCase $arg->type('integer'); } + public function testTypeDigitPass() + { + $arg = new Argument($this->getMockedCommand(array())); + $arg->type('digit'); + $this->validateArgument($arg, null); + + $arg = new Argument($this->getMockedCommand(array(0))); + $arg->type('digit'); + $this->validateArgument($arg, 0); + + $arg = new Argument($this->getMockedCommand(array(123))); + $arg->type('digit'); + $this->validateArgument($arg, 123); + + $arg = new Argument($this->getMockedCommand(array(5.0))); + $arg->type('digit'); + $this->validateArgument($arg, 5.0); + + $arg = new Argument($this->getMockedCommand(array(-10))); + $arg->type('digit'); + $this->validateArgument($arg, -10); + + $arg = new Argument($this->getMockedCommand(array(-10.0))); + $arg->type('digit'); + $this->validateArgument($arg, -10.0); + + $arg = new Argument($this->getMockedCommand(array('12'))); + $arg->type('digit'); + $this->validateArgument($arg, '12'); + + $arg = new Argument($this->getMockedCommand(array('12.0'))); + $arg->type('digit'); + $this->validateArgument($arg, '12.0'); + } + + /** + * @expectedException \Intervention\Image\Exception\InvalidArgumentException + */ + public function testTypeDigitFailString() + { + $arg = new Argument($this->getMockedCommand(array('foo'))); + $arg->type('digit'); + } + + /** + * @expectedException \Intervention\Image\Exception\InvalidArgumentException + */ + public function testTypeDigitFailFloat() + { + $arg = new Argument($this->getMockedCommand(array(12.5))); + $arg->type('digit'); + } + + /** + * @expectedException \Intervention\Image\Exception\InvalidArgumentException + */ + public function testTypeDigitFailBool() + { + $arg = new Argument($this->getMockedCommand(array(true))); + $arg->type('digit'); + } + public function testTypeNumericPass() { $arg = new Argument($this->getMockedCommand(array()));