Skip to content

Commit

Permalink
Improved code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
jamielsharief committed Jul 25, 2020
1 parent 8e69255 commit 82a12f0
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions tests/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,36 @@ public function testValidate()
$this->assertNotEmpty($validator->validate(['email' => 'foo']));
}

// unreachable
public function testValidateInvalidRule()
{
$this->expectException(InvalidArgumentException::class);
$validator = new Validator();
$validator->add('foo', 'foo');
$this->assertEmpty($validator->validate(['foo'=>'bar']));
$this->assertNotEmpty($validator->validate(['email' => 'foo']));
}

public function testRules()
{
$validator = new Validator();

$validator->add('name', 'notEmpty');
$expected = [
'rule' => 'notEmpty',
'message' => 'This field cannot be empty',
'on' => null,
'present' => false,
'allowEmpty' => false,
'stopOnFail' => false
];

$this->assertArrayHasKey('name', $validator->rules());
$this->assertNotEmpty($validator->rules()['name']);
$this->assertEquals(['notEmpty'=>$expected], $validator->rules()['name']);
$this->assertEquals(['notEmpty'=>$expected], $validator->rules('name'));
}

/**
* @depends testValidate
*/
Expand Down Expand Up @@ -207,6 +237,8 @@ public function testValidateRequired()

$this->assertArrayHasKey('email', $errors);
$this->assertEquals('This field is required', $errors['email'][0]);

$this->assertEmpty($validator->validate(['email' => 'phpunit@originphp.com']));
}

public function testValidateOptional()
Expand All @@ -218,6 +250,8 @@ public function testValidateOptional()
]);

$this->assertEmpty($validator->validate(['email' => '']));

$this->assertEmpty($validator->validate(['email' => 'phpunit@originphp.com']));
}

public function testValidatePresent()
Expand All @@ -231,6 +265,8 @@ public function testValidatePresent()

$this->assertArrayHasKey('email', $errors);
$this->assertEquals('This field is must be present', $errors['email'][0]);

$this->assertEmpty($validator->validate(['email' => 'phpunit@originphp.com']));
}

public function testValidatePresentOption()
Expand All @@ -246,6 +282,22 @@ public function testValidatePresentOption()

$this->assertArrayHasKey('email', $errors);
$this->assertEquals('This field is must be present', $errors['email'][0]);

$validator = new Validator();
$validator->add('email', [
'notEmpty' => [
'rule' => 'notEmpty',
'present' => true,
'stopOnFail' => true,
],
'email' => [
'rule' => 'email'
]
]);
$errors = $validator->validate(['foo' => 'bar']);
$this->assertArrayHasKey('email', $errors);
$this->assertEquals('This field is must be present', $errors['email'][0]);
$this->assertCount(1, $errors['email']);
}

public function testValidateAllowEmptyOption()
Expand Down

0 comments on commit 82a12f0

Please sign in to comment.