Example #1
0
 public function testValidators_()
 {
     $mock1 = new \mock\Ongoo\Component\Form\Validator();
     $mock1->getMockController()->validateValue = function (\Ongoo\Component\Form\Field $field, $value) {
         return true;
     };
     $mock2 = new \mock\Ongoo\Component\Form\Validator();
     $mock2->getMockController()->validateValue = function (\Ongoo\Component\Form\Field $field, $value) {
         return true;
     };
     $field = new \Ongoo\Component\Form\Field('field1');
     $this->array($field->getSanitizers())->isEmpty()->array($field->getValidators())->isEmpty();
     $this->if($field->addValidator($mock1))->and($field->addValidator($mock2))->and($validated = $field->validate('foo'))->then->boolean($validated)->isTrue()->boolean($field->hasSuccess())->isTrue()->boolean($field->hasError())->isFalse()->boolean($field->hasWarning())->isFalse()->boolean($field->isValueSet())->isTrue()->string($field->getValue())->isEqualTo('foo')->mock($mock1)->call('validateValue')->withArguments($field, 'foo')->once()->mock($mock2)->call('validateValue')->withArguments($field, 'foo')->once();
     $this->given($this->resetMock($mock1))->given($this->resetMock($mock2));
     $mock3 = new \mock\Ongoo\Component\Form\Validator();
     $mock3->getMockController()->validateValue = function (\Ongoo\Component\Form\Field $field, $value) {
         throw new \Ongoo\Component\Form\Exceptions\ErrorException($this, $value, $value, "{value} is not valid");
     };
     $this->if($field->addValidator($mock3))->and($validated = $field->validate('foo'))->then->boolean($validated)->isFalse()->boolean($field->hasSuccess())->isFalse()->boolean($field->hasError())->isTrue()->boolean($field->hasWarning())->isFalse()->boolean($field->isValueSet())->isFalse()->mock($mock1)->call('validateValue')->withArguments($field, 'foo')->once()->mock($mock2)->call('validateValue')->withArguments($field, 'foo')->once()->mock($mock3)->call('validateValue')->withArguments($field, 'foo')->once()->and($errors = $field->getErrors())->array($errors)->hasSize(1)->object($msg = $errors[0])->isInstanceOf('\\Ongoo\\Component\\Form\\Exceptions\\ErrorException')->string($msg->getMessage())->isEqualTo('{value} is not valid')->array($msg->getContext())->hasKey('{value}')->array($msg->getContext())->contains('foo');
 }