Inheritance: extends Webiny\Component\Entity\AbstractEntity
Example #1
0
 /**
  * Test default value of the attribute
  */
 public function testDefaultValue()
 {
     $entity = new Lib\NoValidation\Entity();
     $entity->getAttribute('char')->setDefaultValue('Default Title');
     $entity->save();
     Entity::getInstance()->reset();
     $entity = Lib\NoValidation\Entity::findOne(['char' => 'Default Title']);
     $this->assertInstanceOf('Webiny\\Component\\Entity\\AbstractEntity', $entity);
 }
Example #2
0
 public function testMany2Many()
 {
     $entity = new Lib\NoValidation\Entity();
     $entity->char = 'Webiny Test';
     // Set initial value
     $entity->many2many = [['char' => 'many1']];
     // Append another value
     $entity->many2many[] = ['char' => 'many2'];
     $this->assertCount(2, $entity->many2many);
     $entity->save();
     $id = $entity->id;
     // Reload entity and assert values are saved
     Entity::getInstance()->reset();
     $entity = Lib\NoValidation\Entity::findById($id);
     $this->assertCount(2, $entity->many2many);
     // Test simple "unset"
     unset($entity->many2many[0]);
     $this->assertCount(1, $entity->many2many);
     // Assert that the correct index was unset
     $this->assertEquals('many2', $entity->many2many[1]->char);
     // Test appending of entity instances
     $many3 = new Many2Many();
     $many3->populate(['char' => 'many3']);
     $entity->many2many[] = ['char' => 'many2'];
     $entity->many2many[] = $many3;
     $entity->save();
     // Reload and test removal of single and all values
     Entity::getInstance()->reset();
     $entity = Lib\NoValidation\Entity::findById($id);
     $this->assertCount(3, $entity->many2many);
     $entity->getAttribute('many2many')->unlink($many3);
     $this->assertCount(2, $entity->many2many);
     $entity->getAttribute('many2many')->unlinkAll();
     $this->assertCount(0, $entity->many2many);
     $entity->save();
     // Reload and assert no values are saved
     Entity::getInstance()->reset();
     $entity = Lib\NoValidation\Entity::findById($id);
     $this->assertCount(0, $entity->many2many);
     // Append 2 values, save, reload and try setting an empty array to remove all values
     $entity->many2many[] = ['char' => 'many1'];
     $entity->many2many[] = ['char' => 'many2'];
     $this->assertCount(2, $entity->many2many);
     $entity->save();
     Entity::getInstance()->reset();
     $entity = Lib\NoValidation\Entity::findById($id);
     // Remove all items by setting an empty array
     $entity->many2many = [];
     $this->assertCount(0, $entity->many2many);
     $entity->save();
     // Reload and make sure nothing was loaded from DB
     Entity::getInstance()->reset();
     $entity = Lib\NoValidation\Entity::findById($id);
     $this->assertCount(0, $entity->many2many);
 }