Example #1
0
 public function testIfWillSkipUnsafeAttributeOnSaveWhenUpdatingModel()
 {
     $model = new ModelWithUnsafeAttribute();
     $model->active = true;
     $em = new EntityManager($model);
     $em->save();
     $finder = new Finder($model, $em);
     $found = $finder->findByPk($model->_id);
     $this->assertNotNull($found, 'That model was saved');
     $this->assertTrue($found->active, 'That value was set');
     // Update model from external data
     // NOTE: Creating model from external data will not work, as there is no way to take value from
     $data = ['active' => false];
     $model2 = SafeArray::toModel($data, null, $found);
     $this->assertTrue($model2->active, 'That value was ignored on mass set, as it is unsafe');
     $em2 = new EntityManager($model2);
     $em2->save();
     $found2 = $finder->findByPk($found->_id);
     $this->assertTrue($found2->active, 'That value was not updated in db');
     $this->assertSame(1, $finder->count(), 'That only one model was saved');
 }
 public function testIfWillProperlyUpdateInstanceWithReorderedClassesAndOneOfDifferentTypeWithEmbedRefsFromPostData()
 {
     $model = new PageCells();
     $firstPost = ['_id' => '56336cccc79fda857b8b4b0d', '_class' => PageCells::class, 'widgets' => [['_id' => '56336ccbc79fda857b8b4afe', '_class' => FirstWidget::class], ['_id' => '56336ccbc79fda857b8b4aff', '_class' => SecondWidget::class]]];
     SafeArray::toModel($firstPost, null, $model);
     $this->assertInstanceOf(PageCells::class, $model);
     $this->assertInstanceOf(FirstWidget::class, $model->widgets[0]);
     $this->assertInstanceOf(SecondWidget::class, $model->widgets[1]);
     $secondPost = ['_id' => '56336cccc79fda857b8b4b0d', '_class' => PageCells::class, 'widgets' => [['_id' => '56336ccbc79fda857b8b4aff', '_class' => ThirdWidget::class], ['_id' => '56336ccbc79fda857b8b4afe', '_class' => FirstWidget::class]]];
     SafeArray::toModel($secondPost, null, $model);
     $this->assertInstanceOf(PageCells::class, $model);
     $this->assertInstanceOf(ThirdWidget::class, $model->widgets[0]);
     $this->assertInstanceOf(FirstWidget::class, $model->widgets[1]);
 }
Example #3
0
 /**
  * Set attributes en masse.
  * Attributes will be filtered according to SafeAnnotation.
  * Only attributes marked as safe will be set, other will be ignored.
  *
  * @param mixed[] $atributes
  */
 public function setAttributes($atributes)
 {
     SafeArray::toModel($atributes, $this->model, $this->model);
 }