From d58bb116856319e123d82256b576095e42231a55 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Sat, 2 Jan 2016 17:29:46 -0500 Subject: [PATCH] Increase test coverage. Cover off previously uncovered code paths. --- tests/TestCase/Validation/ValidatorTest.php | 75 +++++++++++++++++++-- 1 file changed, 68 insertions(+), 7 deletions(-) diff --git a/tests/TestCase/Validation/ValidatorTest.php b/tests/TestCase/Validation/ValidatorTest.php index fa2f5bd472b..3ecdd11ff80 100644 --- a/tests/TestCase/Validation/ValidatorTest.php +++ b/tests/TestCase/Validation/ValidatorTest.php @@ -1084,6 +1084,18 @@ public function testLengthBetween() $this->assertNotEmpty($validator->errors(['username' => 'foo'])); } + /** + * Tests the lengthBetween proxy method + * + * @expectedException InvalidArgumentException + * @return void + */ + public function testLengthBetweenFailure() + { + $validator = new Validator(); + $validator->lengthBetween('username', [7]); + } + /** * Tests the creditCard proxy method * @@ -1272,6 +1284,18 @@ public function testIps() $this->assertNotEmpty($validator->errors(['username' => 'not ip'])); } + /** + * Tests the minLength proxy method + * + * @return void + */ + public function testMinLength() + { + $validator = new Validator(); + $this->assertProxyMethod($validator, 'minLength', 2, [2]); + $this->assertNotEmpty($validator->errors(['username' => 'a'])); + } + /** * Tests the maxLength proxy method * @@ -1284,6 +1308,19 @@ public function testMaxLength() $this->assertNotEmpty($validator->errors(['username' => 'aaa'])); } + /** + * Tests the numeric proxy method + * + * @return void + */ + public function testNumeric() + { + $validator = new Validator(); + $this->assertProxyMethod($validator, 'numeric'); + $this->assertEmpty($validator->errors(['username' => '22'])); + $this->assertNotEmpty($validator->errors(['username' => 'a'])); + } + /** * Tests the naturalNumber proxy method * @@ -1320,6 +1357,17 @@ public function testRange() $this->assertNotEmpty($validator->errors(['username' => 5])); } + /** + * Tests the range failure case + * + * @expectedException InvalidArgumentException + * @return void + */ + public function testRangeFailure() + { + $validator = new Validator(); + $validator->range('username', [1]); + } /** * Tests the url proxy method * @@ -1442,6 +1490,19 @@ public function testUtf8Extended() $this->assertEmpty($validator->errors(['username' => $extended])); } + /** + * Tests the email proxy method + * + * @return void + */ + public function testEmail() + { + $validator = new Validator(); + $validator->email('username'); + $this->assertEmpty($validator->errors(['username' => 'test@example.com'])); + $this->assertNotEmpty($validator->errors(['username' => 'not an email'])); + } + /** * Tests the integer proxy method * @@ -1464,11 +1525,11 @@ protected function assertProxyMethod($validator, $method, $extra = null, $pass = } $rule = $validator->field('username')->rule($method); - $this->assertNull($rule->get('message')); - $this->assertNull($rule->get('on')); - $this->assertEquals($name, $rule->get('rule')); - $this->assertEquals($pass, $rule->get('pass')); - $this->assertEquals('default', $rule->get('provider')); + $this->assertNull($rule->get('message'), 'Message is present when it should not be'); + $this->assertNull($rule->get('on'), 'On clause is present when it should not be'); + $this->assertEquals($name, $rule->get('rule'), 'Rule name does not match'); + $this->assertEquals($pass, $rule->get('pass'), 'Passed options are different'); + $this->assertEquals('default', $rule->get('provider'), 'Provider does not match'); if ($extra !== null) { $validator->{$method}('username', $extra, 'the message', 'create'); @@ -1477,7 +1538,7 @@ protected function assertProxyMethod($validator, $method, $extra = null, $pass = } $rule = $validator->field('username')->rule($method); - $this->assertEquals('the message', $rule->get('message')); - $this->assertEquals('create', $rule->get('on')); + $this->assertEquals('the message', $rule->get('message'), 'Error messages are not the same'); + $this->assertEquals('create', $rule->get('on'), 'On clause is wrong'); } }