From 21b10cc57d198c6fbdc971fe38fc6df5a7375bfb Mon Sep 17 00:00:00 2001 From: Corentin Larose Date: Fri, 9 Aug 2013 15:22:26 +0200 Subject: [PATCH 01/13] Added Bitwise validator unit test --- test/BitwiseTest.php | 132 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 test/BitwiseTest.php diff --git a/test/BitwiseTest.php b/test/BitwiseTest.php new file mode 100644 index 000000000..0271d4651 --- /dev/null +++ b/test/BitwiseTest.php @@ -0,0 +1,132 @@ +validator = new Bitwise(); + } + + /** + * @covers Bitwise::isvalid() + */ + public function testBitwiseAndNotStrict() + { + $controlSum = 0x7; // (0x1 | 0x2 | 0x4) === 0x7 + + $validator = new Bitwise(); + $validator->setControl($controlSum); + $validator->setOperator(Bitwise::OP_AND); + + $this->assertTrue($validator->isValid(0x1)); + $this->assertTrue($validator->isValid(0x2)); + $this->assertTrue($validator->isValid(0x4)); + $this->assertFalse($validator->isValid(0x8)); + + $this->assertTrue($validator->isValid(0x1 | 0x2)); + $this->assertTrue($validator->isValid(0x1 | 0x2 | 0x4)); + $this->assertTrue($validator->isValid(0x1 | 0x8)); + } + + /** + * @covers Bitwise::isvalid() + */ + public function testBitwiseAndStrict() + { + $controlSum = 0x7; // (0x1 | 0x2 | 0x4) === 0x7 + + $validator = new Bitwise(); + $validator->setControl($controlSum); + $validator->setOperator(Bitwise::OP_AND); + $validator->setStrict(true); + + $this->assertTrue($validator->isValid(0x1)); + $this->assertTrue($validator->isValid(0x2)); + $this->assertTrue($validator->isValid(0x4)); + $this->assertFalse($validator->isValid(0x8)); + + $this->assertTrue($validator->isValid(0x1 | 0x2)); + $this->assertTrue($validator->isValid(0x1 | 0x2 | 0x4)); + $this->assertFalse($validator->isValid(0x1 | 0x8)); + } + + /** + * @covers Bitwise::isvalid() + */ + public function testBitwiseXor() + { + $controlSum = 0x5; // (0x1 | 0x4) === 0x5 + + $validator = new Bitwise(); + $validator->setControl($controlSum); + $validator->setOperator(Bitwise::OP_XOR); + + $this->assertTrue($validator->isValid(0x2)); + $this->assertTrue($validator->isValid(0x8)); + $this->assertTrue($validator->isValid(0x10)); + $this->assertFalse($validator->isValid(0x1)); + $this->assertFalse($validator->isValid(0x4)); + + $this->assertTrue($validator->isValid(0x8 | 0x10)); + $this->assertFalse($validator->isValid(0x1 | 0x4)); + $this->assertFalse($validator->isValid(0x1 | 0x8)); + $this->assertFalse($validator->isValid(0x4 | 0x8)); + } + + /** + * @covers Bitwise::setOperator() + */ + public function testSetOperator() + { + $validator = new Bitwise(); + + $validator->setOperator(Bitwise::OP_AND); + $this->assertSame(Bitwise::OP_AND, $validator->getOperator()); + + $validator->setOperator(Bitwise::OP_XOR); + $this->assertSame(Bitwise::OP_XOR, $validator->getOperator()); + } + + /** + * @covers Bitwise::setStrict() + */ + public function testSetStrict() + { + $validator = new Bitwise(); + + $this->assertFalse($validator->getStrict(), 'Strict false by default'); + + $validator->setStrict(false); + $this->assertFalse($validator->getStrict()); + + $validator->setStrict(true); + $this->assertTrue($validator->getStrict()); + + $validator = new Bitwise(0x1, Bitwise::OP_AND, false); + $this->assertFalse($validator->getStrict()); + + $validator = new Bitwise(0x1, Bitwise::OP_AND, true); + $this->assertTrue($validator->getStrict()); + } +} From 211b426b70bf2f6ddecaca085c09e70ef342bbf8 Mon Sep 17 00:00:00 2001 From: Corentin Larose Date: Wed, 14 Aug 2013 07:28:35 +0200 Subject: [PATCH 02/13] Docblock cleanup --- test/BitwiseTest.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/test/BitwiseTest.php b/test/BitwiseTest.php index 0271d4651..e7cdc603e 100644 --- a/test/BitwiseTest.php +++ b/test/BitwiseTest.php @@ -12,12 +12,6 @@ use Zend\Validator\Bitwise; -/** - * @category Zend - * @package Zend - * @subpackage UnitTests - * @group Zend_Validator - */ class BitwiseTest extends \PHPUnit_Framework_TestCase { /** @var Bitwise */ From 4afabd68c02a4eb725f61e670e58ae339d275eb4 Mon Sep 17 00:00:00 2001 From: Corentin Larose Date: Wed, 21 Aug 2013 18:41:34 +0200 Subject: [PATCH 03/13] Misc changes requested by Matthew --- src/Bitwise.php | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/Bitwise.php b/src/Bitwise.php index 319514cc1..1b1210f07 100644 --- a/src/Bitwise.php +++ b/src/Bitwise.php @@ -65,7 +65,7 @@ class Bitwise extends AbstractValidator public function __construct($options = null) { if ($options instanceof Traversable) { - $options = ArrayUtils::iteratorToArray($options); + $options = iterator_to_array($options); } if (!is_array($options)) { $options = func_get_args(); @@ -126,21 +126,19 @@ public function isValid($value) { $this->setValue($value); - $ret = false; - if (self::OP_AND === $this->operator) { if ($this->strict) { // All the bits set in value must be set in control - $ret = (boolean) (($this->control & $value) == $value); + return (bool) (($this->control & $value) == $value); } else { // At least one of the bits must be common between value and control - $ret = (boolean) ($this->control & $value); + return (bool) ($this->control & $value); } } elseif (self::OP_XOR === $this->operator) { - $ret = (boolean) (($this->control ^ $value) === ($this->control | $value)); + return (bool) (($this->control ^ $value) === ($this->control | $value)); } - return $ret; + return false; } /** From 3b2b4b2f93ef9213a4518d5bbacbe08f3d6f7335 Mon Sep 17 00:00:00 2001 From: Corentin Larose Date: Wed, 21 Aug 2013 18:45:46 +0200 Subject: [PATCH 04/13] Misc fixes --- src/Bitwise.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Bitwise.php b/src/Bitwise.php index 1b1210f07..c216bfe68 100644 --- a/src/Bitwise.php +++ b/src/Bitwise.php @@ -9,6 +9,8 @@ namespace Zend\Validator; +use Traversable; + class Bitwise extends AbstractValidator { const OP_AND = 'and'; @@ -149,7 +151,7 @@ public function isValid($value) */ public function setControl($control) { - $this->control = (integer) $control; + $this->control = (int) $control; return $this; } From 8bfa4a125e0916b0e07db4d5bda830cdcf448b5b Mon Sep 17 00:00:00 2001 From: Corentin Larose Date: Thu, 22 Aug 2013 16:26:07 +0200 Subject: [PATCH 05/13] Added unit test to check errors messages --- src/Bitwise.php | 5 ++++- test/BitwiseTest.php | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/Bitwise.php b/src/Bitwise.php index c216bfe68..ca62dd25d 100644 --- a/src/Bitwise.php +++ b/src/Bitwise.php @@ -42,7 +42,7 @@ class Bitwise extends AbstractValidator * @var array */ protected $messageVariables = array( - 'control' => array('options' => 'control'), + 'control' => 'control', ); /** @@ -131,12 +131,15 @@ public function isValid($value) if (self::OP_AND === $this->operator) { if ($this->strict) { // All the bits set in value must be set in control + $this->error(self::NOT_AND_STRICT); return (bool) (($this->control & $value) == $value); } else { // At least one of the bits must be common between value and control + $this->error(self::NOT_AND); return (bool) ($this->control & $value); } } elseif (self::OP_XOR === $this->operator) { + $this->error(self::NOT_XOR); return (bool) (($this->control ^ $value) === ($this->control | $value)); } diff --git a/test/BitwiseTest.php b/test/BitwiseTest.php index e7cdc603e..933db8110 100644 --- a/test/BitwiseTest.php +++ b/test/BitwiseTest.php @@ -38,6 +38,11 @@ public function testBitwiseAndNotStrict() $this->assertTrue($validator->isValid(0x4)); $this->assertFalse($validator->isValid(0x8)); + $validator->isValid(0x8); + $messages = $validator->getMessages(); + $this->assertArrayHasKey($validator::NOT_AND, $messages); + $this->assertSame("The input has no common bit set with '$controlSum'", $messages[$validator::NOT_AND]); + $this->assertTrue($validator->isValid(0x1 | 0x2)); $this->assertTrue($validator->isValid(0x1 | 0x2 | 0x4)); $this->assertTrue($validator->isValid(0x1 | 0x8)); @@ -60,6 +65,11 @@ public function testBitwiseAndStrict() $this->assertTrue($validator->isValid(0x4)); $this->assertFalse($validator->isValid(0x8)); + $validator->isValid(0x8); + $messages = $validator->getMessages(); + $this->assertArrayHasKey($validator::NOT_AND_STRICT, $messages); + $this->assertSame("The input doesn't have the same bits set as '$controlSum'", $messages[$validator::NOT_AND_STRICT]); + $this->assertTrue($validator->isValid(0x1 | 0x2)); $this->assertTrue($validator->isValid(0x1 | 0x2 | 0x4)); $this->assertFalse($validator->isValid(0x1 | 0x8)); @@ -82,6 +92,11 @@ public function testBitwiseXor() $this->assertFalse($validator->isValid(0x1)); $this->assertFalse($validator->isValid(0x4)); + $validator->isValid(0x4); + $messages = $validator->getMessages(); + $this->assertArrayHasKey($validator::NOT_XOR, $messages); + $this->assertSame("The input has common bit set with '$controlSum'", $messages[$validator::NOT_XOR]); + $this->assertTrue($validator->isValid(0x8 | 0x10)); $this->assertFalse($validator->isValid(0x1 | 0x4)); $this->assertFalse($validator->isValid(0x1 | 0x8)); From 798ad6c7b002bad7e1a16407b8c7ffb03928fb6a Mon Sep 17 00:00:00 2001 From: Corentin Larose Date: Wed, 4 Sep 2013 09:42:23 +0200 Subject: [PATCH 06/13] Removed @package annotation --- test/BitwiseTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/test/BitwiseTest.php b/test/BitwiseTest.php index 933db8110..fc914ede3 100644 --- a/test/BitwiseTest.php +++ b/test/BitwiseTest.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Validator */ namespace ZendTest\Validator; From 0c855d5882e629967a448bb36acfbf91c96800ed Mon Sep 17 00:00:00 2001 From: Corentin Larose Date: Thu, 19 Sep 2013 15:58:51 +0200 Subject: [PATCH 07/13] Added unit tests for constructor --- src/Bitwise.php | 21 +++++++++++-------- test/BitwiseTest.php | 49 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 8 deletions(-) diff --git a/src/Bitwise.php b/src/Bitwise.php index ca62dd25d..c46d535e0 100644 --- a/src/Bitwise.php +++ b/src/Bitwise.php @@ -69,21 +69,23 @@ public function __construct($options = null) if ($options instanceof Traversable) { $options = iterator_to_array($options); } + if (!is_array($options)) { $options = func_get_args(); - $temp['control'] = array_shift($options); + } - if (!empty($options)) { - $temp['operator'] = array_shift($options); - } + $temp['control'] = array_shift($options); - if (!empty($options)) { - $temp['strict'] = array_shift($options); - } + if (!empty($options)) { + $temp['operator'] = array_shift($options); + } - $options = $temp; + if (!empty($options)) { + $temp['strict'] = array_shift($options); } + $options = $temp; + parent::__construct($options); } @@ -132,14 +134,17 @@ public function isValid($value) if ($this->strict) { // All the bits set in value must be set in control $this->error(self::NOT_AND_STRICT); + return (bool) (($this->control & $value) == $value); } else { // At least one of the bits must be common between value and control $this->error(self::NOT_AND); + return (bool) ($this->control & $value); } } elseif (self::OP_XOR === $this->operator) { $this->error(self::NOT_XOR); + return (bool) (($this->control ^ $value) === ($this->control | $value)); } diff --git a/test/BitwiseTest.php b/test/BitwiseTest.php index fc914ede3..1e7195711 100644 --- a/test/BitwiseTest.php +++ b/test/BitwiseTest.php @@ -21,6 +21,55 @@ public function setUp() $this->validator = new Bitwise(); } + /** + * @covers Bitwise::isvalid() + * @dataProvider constructDataProvider + */ + public function testConstruct(array $args, array $options) + { + $validator = new Bitwise($args); + + $this->assertSame($options['control'], $validator->getControl()); + $this->assertSame($options['operator'], $validator->getOperator()); + $this->assertSame($options['strict'], $validator->getStrict()); + } + + public function constructDataProvider() + { + return array( + array( + array(), + array('control' => 0, 'operator' => null, 'strict' => false), + ), + array( + array(0x1), + array('control' => 0x1, 'operator' => null, 'strict' => false), + ), + array( + array(0x1, Bitwise::OP_AND), + array('control' => 0x1, 'operator' => Bitwise::OP_AND, 'strict' => false), + ), + array( + array(0x1, Bitwise::OP_AND, true), + array('control' => 0x1, 'operator' => Bitwise::OP_AND, 'strict' => true), + ), + + array( + array('control' => 0x1), + array('control' => 0x1, 'operator' => null, 'strict' => false), + ), + array( + array('control' => 0x1, 'operator' => Bitwise::OP_AND), + array('control' => 0x1, 'operator' => Bitwise::OP_AND, 'strict' => false), + ), + array( + array('control' => 0x1, 'operator' => Bitwise::OP_AND, 'strict' => true), + array('control' => 0x1, 'operator' => Bitwise::OP_AND, 'strict' => true), + ), + ); + + } + /** * @covers Bitwise::isvalid() */ From f34eba63eac5b9471112880c56d1b815d6632381 Mon Sep 17 00:00:00 2001 From: Corentin Larose Date: Tue, 24 Sep 2013 19:18:12 +0200 Subject: [PATCH 08/13] Fixed constructor --- src/Bitwise.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Bitwise.php b/src/Bitwise.php index c46d535e0..7ccdc5c1d 100644 --- a/src/Bitwise.php +++ b/src/Bitwise.php @@ -72,19 +72,19 @@ public function __construct($options = null) if (!is_array($options)) { $options = func_get_args(); - } - $temp['control'] = array_shift($options); + $temp['control'] = array_shift($options); - if (!empty($options)) { - $temp['operator'] = array_shift($options); - } + if (!empty($options)) { + $temp['operator'] = array_shift($options); + } - if (!empty($options)) { - $temp['strict'] = array_shift($options); - } + if (!empty($options)) { + $temp['strict'] = array_shift($options); + } - $options = $temp; + $options = $temp; + } parent::__construct($options); } From 137d668bcebc6308b75fe9fc98829dff346d2c2b Mon Sep 17 00:00:00 2001 From: Corentin Larose Date: Mon, 21 Oct 2013 16:08:46 +0200 Subject: [PATCH 09/13] Fixed U.T --- test/BitwiseTest.php | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/test/BitwiseTest.php b/test/BitwiseTest.php index 1e7195711..2cea75e6b 100644 --- a/test/BitwiseTest.php +++ b/test/BitwiseTest.php @@ -39,21 +39,8 @@ public function constructDataProvider() return array( array( array(), - array('control' => 0, 'operator' => null, 'strict' => false), + array('control' => null, 'operator' => null, 'strict' => false), ), - array( - array(0x1), - array('control' => 0x1, 'operator' => null, 'strict' => false), - ), - array( - array(0x1, Bitwise::OP_AND), - array('control' => 0x1, 'operator' => Bitwise::OP_AND, 'strict' => false), - ), - array( - array(0x1, Bitwise::OP_AND, true), - array('control' => 0x1, 'operator' => Bitwise::OP_AND, 'strict' => true), - ), - array( array('control' => 0x1), array('control' => 0x1, 'operator' => null, 'strict' => false), From 56b876a3f81f6bc7eab83dfc9c748fec19633549 Mon Sep 17 00:00:00 2001 From: Corentin Larose Date: Mon, 21 Oct 2013 16:39:38 +0200 Subject: [PATCH 10/13] DockBlock typo fix --- test/BitwiseTest.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/BitwiseTest.php b/test/BitwiseTest.php index 2cea75e6b..f66e24e22 100644 --- a/test/BitwiseTest.php +++ b/test/BitwiseTest.php @@ -22,8 +22,11 @@ public function setUp() } /** - * @covers Bitwise::isvalid() + * @covers Bitwise::__construct() * @dataProvider constructDataProvider + * + * @param array $args + * @param array $options */ public function testConstruct(array $args, array $options) { From f6b91d5c7cf001e7ed93dddf18c969548725cbc8 Mon Sep 17 00:00:00 2001 From: Corentin Larose Date: Tue, 22 Oct 2013 10:58:05 +0200 Subject: [PATCH 11/13] Fixed docblocks --- test/BitwiseTest.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/test/BitwiseTest.php b/test/BitwiseTest.php index f66e24e22..283043bb8 100644 --- a/test/BitwiseTest.php +++ b/test/BitwiseTest.php @@ -13,7 +13,9 @@ class BitwiseTest extends \PHPUnit_Framework_TestCase { - /** @var Bitwise */ + /** + * @var \ZendTest\Validator\Bitwise + */ public $validator; public function setUp() @@ -61,7 +63,7 @@ public function constructDataProvider() } /** - * @covers Bitwise::isvalid() + * @covers \ZendTest\Validator\Bitwise::isvalid() */ public function testBitwiseAndNotStrict() { @@ -87,7 +89,7 @@ public function testBitwiseAndNotStrict() } /** - * @covers Bitwise::isvalid() + * @covers \ZendTest\Validator\Bitwise::isvalid() */ public function testBitwiseAndStrict() { @@ -114,7 +116,7 @@ public function testBitwiseAndStrict() } /** - * @covers Bitwise::isvalid() + * @covers \ZendTest\Validator\Bitwise::isvalid() */ public function testBitwiseXor() { @@ -142,7 +144,7 @@ public function testBitwiseXor() } /** - * @covers Bitwise::setOperator() + * @covers \ZendTest\Validator\Bitwise::setOperator() */ public function testSetOperator() { @@ -156,7 +158,7 @@ public function testSetOperator() } /** - * @covers Bitwise::setStrict() + * @covers \ZendTest\Validator\Bitwise::setStrict() */ public function testSetStrict() { From 529e4035d803eaffc271730d1591103d726b03d8 Mon Sep 17 00:00:00 2001 From: Corentin Larose Date: Tue, 22 Oct 2013 11:20:20 +0200 Subject: [PATCH 12/13] Fixed docblocks --- test/BitwiseTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/BitwiseTest.php b/test/BitwiseTest.php index 283043bb8..9076f70e6 100644 --- a/test/BitwiseTest.php +++ b/test/BitwiseTest.php @@ -24,7 +24,7 @@ public function setUp() } /** - * @covers Bitwise::__construct() + * @covers \ZendTest\Validator\Bitwise::__construct() * @dataProvider constructDataProvider * * @param array $args From 4d5e3d35c6620199b1a5e648f40b13b1b2ba1f3b Mon Sep 17 00:00:00 2001 From: Corentin Larose Date: Tue, 22 Oct 2013 11:31:59 +0200 Subject: [PATCH 13/13] Fixed docblocks --- test/BitwiseTest.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/test/BitwiseTest.php b/test/BitwiseTest.php index 9076f70e6..da93ef82b 100644 --- a/test/BitwiseTest.php +++ b/test/BitwiseTest.php @@ -14,7 +14,7 @@ class BitwiseTest extends \PHPUnit_Framework_TestCase { /** - * @var \ZendTest\Validator\Bitwise + * @var \Zend\Validator\Bitwise */ public $validator; @@ -24,7 +24,7 @@ public function setUp() } /** - * @covers \ZendTest\Validator\Bitwise::__construct() + * @covers \Zend\Validator\Bitwise::__construct() * @dataProvider constructDataProvider * * @param array $args @@ -63,7 +63,7 @@ public function constructDataProvider() } /** - * @covers \ZendTest\Validator\Bitwise::isvalid() + * @covers \Zend\Validator\Bitwise::isvalid() */ public function testBitwiseAndNotStrict() { @@ -89,7 +89,7 @@ public function testBitwiseAndNotStrict() } /** - * @covers \ZendTest\Validator\Bitwise::isvalid() + * @covers \Zend\Validator\Bitwise::isvalid() */ public function testBitwiseAndStrict() { @@ -116,7 +116,7 @@ public function testBitwiseAndStrict() } /** - * @covers \ZendTest\Validator\Bitwise::isvalid() + * @covers \Zend\Validator\Bitwise::isvalid() */ public function testBitwiseXor() { @@ -144,7 +144,7 @@ public function testBitwiseXor() } /** - * @covers \ZendTest\Validator\Bitwise::setOperator() + * @covers \Zend\Validator\Bitwise::setOperator() */ public function testSetOperator() { @@ -158,7 +158,7 @@ public function testSetOperator() } /** - * @covers \ZendTest\Validator\Bitwise::setStrict() + * @covers \Zend\Validator\Bitwise::setStrict() */ public function testSetStrict() {