示例#1
0
 /**
  * Move to trash. Validation will be performed
  * before trashing with `trash` (TrashInterface::ScenarioTrash) scenario.
  *
  *
  * @see TrashInterface::ScenarioTrash
  * @param boolean $runValidation whether to perform validation before saving the record.
  * If the validation fails, the record will not be saved to database.
  *
  * @return boolean
  * @Ignored
  */
 public function trash($runValidation = true)
 {
     ScenarioManager::setScenario($this, TrashInterface::ScenarioTrash);
     $validator = new Validator($this);
     if (!$runValidation || $validator->validate()) {
         $eventBefore = new TrashEvent($this);
         if (Event::hasHandler($this, TrashInterface::EventBeforeTrash)) {
             if (!Event::valid($this, TrashInterface::EventBeforeTrash, $eventBefore)) {
                 return false;
             }
         }
         $meta = ManganMeta::create($this);
         $trash = $eventBefore->getTrash();
         if (empty($trash)) {
             $trash = new Trash();
         }
         $trash->name = (string) $this;
         $trash->data = $this;
         $trash->type = isset($meta->type()->label) ? $meta->type()->label : get_class($this);
         if (!$trash->save()) {
             return false;
         }
         $eventAfter = new TrashEvent($this);
         $eventAfter->setTrash($trash);
         if (!Event::valid($this, TrashInterface::EventAfterTrash, $eventAfter)) {
             return false;
         }
         // Use deleteOne, to avoid beforeDelete event,
         // which should be raised only when really removing document:
         // when emtying trash
         $em = new EntityManager($this);
         return $em->deleteOne(PkManager::prepareFromModel($this));
     }
     return false;
 }
示例#2
0
 public function testIfWillValidateWithCustomValidatorValue()
 {
     $model = new ModelWithCustomValidatorCustomValue();
     $validator = new Validator($model);
     // Should fail
     $valid = $validator->validate();
     $this->assertFalse($valid);
     $model->number = 666;
     $valid2 = $validator->validate();
     $this->assertTrue($valid2);
 }
示例#3
0
 public function testIfWillValidateDbRefArrayOfDocumentsAndSkipNotUpdatableModels()
 {
     $model = new ModelWithDbRefArrayWithValidatorNotUpdatable();
     $model->addresses[] = new EmbeddedModelWithValidator();
     $model->addresses[] = new EmbeddedModelWithValidator();
     $validator = new Validator($model);
     $result = $validator->validate();
     codecept_debug($validator->getErrors());
     // Should pass even when adress is not valid
     $this->assertTrue($result, 'That validation passed, even if db refs are not valid, as they are not updatable');
 }
 public function testIfWillValidateWithModelValidator()
 {
     $model = new ModelWithModelValidator();
     $validator1 = new Validator($model);
     $result1 = $validator1->validate();
     $this->assertFalse($result1, 'That validation is not passing');
     $model = new ModelWithModelValidator();
     $model->number = \Maslosoft\ManganTest\Models\Validator\ModelValidator::ValidValue;
     $validator2 = new Validator($model);
     $result2 = $validator2->validate();
     $this->assertTrue($result2, 'That validation is passing');
 }
示例#5
0
 public function testIfWillSetErrorsWithExternalValidatorCallToDocumentInstance()
 {
     $model = new DocumentWithRequiredValidator();
     $validator = new Validator($model);
     // Should not pass validation
     $this->assertFalse($validator->validate());
     $errors = $model->getErrors();
     codecept_debug('Error message is: ' . $errors['login'][0]);
     $this->assertGreaterThan(0, count($errors));
     $this->assertSame(1, count($errors['login']));
     $this->assertTrue(is_string($errors['login'][0]));
 }
示例#6
0
 public function testIfWillFailValidationExceptInsertAndRegister()
 {
     $model = new ModelWithValidationExceptInsertAndRegister();
     $validator = new Validator($model);
     $valid = $validator->validate();
     $this->assertTrue($valid);
     ScenarioManager::setScenario($model, 'register');
     $valid2 = $validator->validate();
     $this->assertTrue($valid2);
     ScenarioManager::setScenario($model, 'update');
     $valid3 = $validator->validate();
     $this->assertFalse($valid3);
 }
示例#7
0
 public function testIfWillValidateModelUsingCustomValidator()
 {
     $model = new ModelWithCustomValidatorProxy();
     $validator = new Validator($model);
     $result = $validator->validate();
     // Should not pass validation
     $this->assertFalse($result);
     // Should pass validation
     $model->login = '******';
     $result2 = $validator->validate();
     $this->assertTrue($result2);
     // Get validators meta
     $validatorsMeta = ManganMeta::create($model)->login->validators;
     // should have one
     $this->assertSame(1, count($validatorsMeta));
     $validatorInstance = Factory::create($model, $validatorsMeta[0]);
     // Check type of validator
     $this->assertInstanceof(RequiredValidator::class, $validatorInstance);
 }
示例#8
0
 public function testIfWillValidateEmbeddedArrayOfDocuments()
 {
     $model = new ModelWithEmbedArrayWithValidator();
     $model->addresses[] = new EmbeddedModelWithValidator();
     $model->addresses[] = new EmbeddedModelWithValidator();
     $validator = new Validator($model);
     $result = $validator->validate();
     codecept_debug($validator->getErrors());
     // Should fail
     $this->assertFalse($result, 'That model is not valid');
     // Should fail too - one of two models has empty street
     $model->addresses[0]->street = 'Franklin St.';
     $result2 = $validator->validate();
     $this->assertFalse($result2, 'That model is not valid');
     // Should pass
     $model->addresses[1]->street = 'Wall St.';
     $result3 = $validator->validate();
     $this->assertTrue($result3, 'That model is valid');
 }
示例#9
0
 /**
  * Updates or inserts the current document. This will try to update existing fields.
  * Will keep already stored data if present in document.
  *
  * If document does not exist, a new one will be inserted.
  *
  * @param boolean $runValidation
  * @return boolean
  * @throws ManganException
  */
 public function upsert($runValidation = true)
 {
     if (!$runValidation || $this->validator->validate()) {
         $model = $this->model;
         if ($this->_beforeSave($model)) {
             $criteria = PkManager::prepareFromModel($this->model);
             foreach ($criteria->getConditions() as $field => $value) {
                 if (empty($this->model->{$field})) {
                     $this->model->{$field} = $value;
                 }
             }
             $result = $this->updateOne($criteria);
             if ($result) {
                 $this->_afterSave($model);
                 return true;
             }
             throw new ManganException("Can't save the document to disk, or attempting to save an empty document");
         }
         return false;
     } else {
         return false;
     }
 }
示例#10
0
 /**
  * Validate model, optionally only selected fields
  * @param string[] $fields
  * @return boolean
  */
 public function validate($fields = [])
 {
     $valid = [];
     if (empty($fields)) {
         $fields = array_keys($this->meta->fields());
     }
     foreach ($fields as $name) {
         $fieldMeta = $this->meta->field($name);
         // Reset errors
         $this->errors[$name] = [];
         // Check if meta for field exists
         if (empty($fieldMeta)) {
             throw new InvalidArgumentException(sprintf("Unknown field `%s` in model `%s`", $name, get_class($this->model)));
         }
         // Validate sub documents
         // Skip fields that are not updatable
         if ($fieldMeta->owned && $fieldMeta->updatable) {
             if (is_array($this->model->{$name})) {
                 // Handle arrays of documents
                 foreach ($this->model->{$name} as $model) {
                     $validator = new Validator($model);
                     $isValid = $validator->validate();
                     $valid[] = (int) $isValid;
                     if (!$isValid) {
                         $errors = $validator->getErrors();
                         $this->setErrors($errors);
                     }
                 }
             } elseif (!empty($this->model->{$name})) {
                 // Handle single documents
                 $validator = new Validator($this->model->{$name});
                 $isValid = $validator->validate();
                 $valid[] = (int) $isValid;
                 if (!$isValid) {
                     $errors = $validator->getErrors();
                     $this->setErrors($errors);
                 }
             }
         }
         // Skip field without validators
         if (empty($fieldMeta->validators)) {
             continue;
         }
         $valid[] = (int) $this->validateEntity($name, $fieldMeta->validators);
     }
     // Model validators
     $typeValidators = $this->meta->type()->validators;
     if (!empty($typeValidators)) {
         $typeName = $this->meta->type()->name;
         // Reset errors
         $this->errors[$typeName] = [];
         $valid[] = (int) $this->validateEntity($typeName, $typeValidators);
     }
     return count($valid) === array_sum($valid);
 }