/** * Test public properties. * @covers Rentalhost\VanillaValidation\Result\Result */ public function testPublicProperties() { static::assertClassHasAttribute('field', Success::class); static::assertClassHasAttribute('value', Success::class); static::assertClassHasAttribute('ruleIndex', Success::class); static::assertClassHasAttribute('rule', Success::class); $validation = new ValidationField('name', 'value'); $validation->required(); $validationResult = $validation->validate(); $validationResultFirst = $validationResult->getResults()[0]; static::assertInstanceOf(Result::class, $validationResultFirst); static::assertInstanceOf(ValidationField::class, $validationResultFirst->field); static::assertInternalType('string', $validationResultFirst->field->name); static::assertInternalType('string', $validationResultFirst->field->value); static::assertInternalType('string', $validationResultFirst->value); static::assertInternalType('integer', $validationResultFirst->ruleIndex); static::assertEquals(new ValidationFieldRule('required'), $validationResultFirst->rule); }
/** * Test validate method. * @covers Rentalhost\VanillaValidation\ValidationField::validate */ public function testValidate() { $field = new ValidationField('name', ' '); $field->notEmpty()->trim()->notEmpty(); $fieldResult = $field->validate(); $resultSuccess = new Success(); $resultSuccess->field = $field; $resultSuccess->value = ' '; $resultSuccess->ruleIndex = 0; $resultSuccess->rule = new ValidationFieldRule('notEmpty'); $resultFail = new Fail('fail:notEmpty'); $resultFail->field = $field; $resultFail->value = ''; $resultFail->ruleIndex = 2; $resultFail->rule = new ValidationFieldRule('notEmpty'); static::assertFalse($fieldResult->isSuccess()); static::assertSame('fail', $fieldResult->getMessage()); static::assertEquals([$resultSuccess], $fieldResult->getSuccesses()); static::assertEquals([$resultSuccess, $resultFail], $fieldResult->getResults()); static::assertEquals([$resultFail], $fieldResult->getFails()); }