/**
  * @throws \ValidationException
  * @throws null
  */
 public function testBranchDelete_DeleteManyObjects_ObjectsDeleted()
 {
     $objects = array();
     for ($i = 0; $i < 100; $i++) {
         $human = new Human();
         $human->Name = 'Proud Owner ' . $i;
         $human->write();
         $dog = new Dog();
         $dog->Name = 'Pup ' . $i;
         $dog->Color = 'Fifty Shade No. ' . $i;
         $dog->Owner($human);
         $dog->write();
         $objects[] = $human;
         $objects[] = $dog;
     }
     $batch = new \Batch();
     $batch->delete($objects);
     $this->assertEquals(0, Dog::get()->Count());
     $this->assertEquals(0, Human::get()->Count());
 }
 /**
  *
  */
 public function testWriteManyMany_CreateParentAndChildren_WritesManyMany()
 {
     $parent = new Batman();
     $parent->Name = 'Bruce Wayne';
     $parent->Car = 'Bat mobile';
     $children = array();
     for ($i = 0; $i < 5; $i++) {
         $child = new Child();
         $child->Name = 'Soldier #' . $i;
         $children[] = $child;
     }
     $batch = new \Batch();
     $batch->write(array($parent));
     $batch->write($children);
     $sets = array();
     foreach ($children as $child) {
         $sets[] = array($parent, 'Children', $child);
     }
     $batch->writeManyMany($sets);
     $parent = Human::get()->first();
     $this->assertEquals(5, $parent->Children()->Count());
 }
 /**
  *
  */
 public function testWriteManyMany_SetChildrenForParent_RelationWritten()
 {
     $parent = new Human();
     $parent->Name = 'Bob';
     $children = array();
     for ($i = 0; $i < 5; $i++) {
         $child = new Child();
         $child->Name = 'Soldier #' . $i;
         $children[] = $child;
     }
     $writer = new \BatchedWriter();
     $afterExists = new \OnAfterExists(function () use($writer, $parent, $children) {
         $writer->writeManyMany($parent, 'Children', $children);
     });
     $afterExists->addCondition($parent);
     $afterExists->addCondition($children);
     $writer->write(array($parent));
     $writer->write($children);
     $writer->finish();
     $parent = Human::get()->first();
     $this->assertEquals(5, $parent->Children()->Count());
 }
 /**
  * @throws \ValidationException
  * @throws null
  */
 public function testCallback_SetOnAfterExistsCallback_CallbackCalled()
 {
     $dog1 = new Dog();
     $dog1->Name = 'Jim bob';
     $dog2 = new Dog();
     $dog2->Name = 'Super Dog';
     $owner = new Human();
     $owner->Name = 'Hilly Stewart';
     $owner->write();
     $owner->onAfterExistsCallback(function ($owner) use($dog1) {
         $dog1->OwnerID = $owner->ID;
         $dog1->write();
     });
     $owner->write();
     $owner->onAfterExistsCallback(function ($owner) use($dog2) {
         $dog2->OwnerID = $owner->ID;
         $dog2->write();
     });
     $this->assertTrue($owner->exists());
     $this->assertTrue($dog1->exists());
     $this->assertTrue($dog2->exists());
     $this->assertEquals(1, Human::get()->Count());
     $this->assertEquals(2, Dog::get()->Count());
     $this->assertEquals($owner->ID, $dog1->OwnerID);
     $this->assertEquals($owner->ID, $dog2->OwnerID);
 }
 /**
  *
  */
 public function testOnAfterExists_ArrayCondition_CalledBack()
 {
     $parent = new Human();
     $parent->Name = 'Bob';
     $children = array();
     for ($i = 0; $i < 5; $i++) {
         $child = new Child();
         $child->Name = 'Soldier #' . $i;
         $children[] = $child;
     }
     $batch = new \Batch();
     $afterExists = new \OnAfterExists(function () use($batch, $parent, $children) {
         $sets = array();
         foreach ($children as $child) {
             $sets[] = array($parent, 'Children', $child);
         }
         $batch->writeManyMany($sets);
     });
     $afterExists->addCondition($parent);
     $afterExists->addCondition($children);
     $batch->write(array($parent));
     $batch->write($children);
     $parent = Human::get()->first();
     $this->assertEquals(5, $parent->Children()->Count());
 }