/** * Test real nullable case. * @coversNothing */ public function testNullable() { // Null before validation. static::assertTrue(Validation::nullable()->cpf()->validate('')->isSuccess()); static::assertTrue(Validation::nullable()->cpf()->validate(' ')->isSuccess()); // Null will pass and will fail in validation. static::assertFalse(Validation::nullable()->cpf()->validate('fail')->isSuccess()); // Fail in validation before null check. static::assertFalse(Validation::cpf()->nullable()->validate('')->isSuccess()); }
/** * Test nullable rule. * @covers Rentalhost\VanillaValidation\ValidationFieldRuleList::validate * @covers Rentalhost\VanillaValidation\Result\Nullable::__construct * @covers Rentalhost\VanillaValidation\Rule\NullableRule::validate */ public function testNullable() { // Both are invalid, because both is not nullable ('hello' != empty). $validation = Validation::nullable()->minLength(8)->validate('hello'); static::assertCount(1, $validation->getFails()); $validation = Validation::minLength(8)->nullable()->validate('hello'); static::assertCount(1, $validation->getFails()); // But here, one first will works, because it check by nullable first, // then the next rule is ignored ('' == empty). $validation = Validation::nullable()->minLength(8)->validate(''); static::assertCount(0, $validation->getFails()); $validation = Validation::minLength(8)->nullable()->validate(''); static::assertCount(1, $validation->getFails()); }