Esempio n. 1
0
 public function testCreatedRules()
 {
     $rules = RulesBuilder::create()->addRule('a', 'required')->addRule('b', 'min', 5)->getRules();
     $data = ['a' => 'abc', 'b' => 6];
     $validator = new Validator();
     $read = $validator->isValid($rules, $data);
     $expected = ['valid' => true, 'errors' => []];
     $this->assertSame($read, $expected);
 }
Esempio n. 2
0
 public function testAddValidator()
 {
     $validator = new Validator();
     $rules = ['a' => [['test']]];
     try {
         $validator->isValid($rules, ['a' => 1]);
         $this->fail();
     } catch (ValidatorException $e) {
         $read = $e->getMessage();
         $expected = '"test" not found as available validator.';
         $this->assertSame($read, $expected);
     } catch (\Exception $e) {
         $this->fail();
     }
     $validator->addValidator('test', new TestValidator());
     $read = $validator->isValid($rules, ['a' => 1]);
     $expected = ['valid' => false, 'errors' => ['a' => ['test']]];
     $this->assertSame($read, $expected);
     $read = $validator->isValid($rules, ['a' => 2]);
     $expected = ['valid' => true, 'errors' => []];
     $this->assertSame($read, $expected);
 }
Esempio n. 3
0
 /**
  * Validates the entity against the definition.
  *
  * @param AbstractData $data
  * the data access instance used for counting things
  * @param integer $expectedVersion
  * the version to perform the optimistic locking check on
  *
  * @return array
  * an array with the fields "valid" and "errors"; valid provides a quick
  * check whether the given entity passes the validation and errors is an
  * array with all errored fields as keys and arrays as values; this field arrays
  * contains the actual errors on the field: "boolean", "floating", "integer",
  * "dateTime" (for dates and datetime fields), "inSet", "reference", "required",
  * "unique", "value" (only for the version field, set if the optimistic locking
  * failed).
  */
 public function validate(AbstractData $data, $expectedVersion)
 {
     $validator = new Validator();
     $validator->addValidator('unique', new UniqueValidator());
     $validator->addValidator('reference', new ReferenceValidator());
     $validator->addValidator('many', new ManyValidator());
     $rules = $this->buildUpRules($data, $validator);
     $toValidate = $this->buildUpData();
     $rules['version'] = [['value', $expectedVersion]];
     $toValidate['version'] = $this->entity->get('version');
     $validation = $validator->isValid($rules, $toValidate);
     return $validation;
 }