/** * */ 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 testBatchWrite_DifferentClasses_WritesObjects() { $dog = new Dog(); $dog->Name = 'Snuffins'; $dog->Color = 'Red'; $cat = new Cat(); $cat->Name = 'Puff daddy'; $cat->HasClaws = true; $batch = new \Batch(); $batch->write(array($dog, $cat)); $this->assertTrue($dog->exists()); $this->assertTrue($cat->exists()); $dog = Dog::get()->first(); $this->assertEquals('Snuffins', $dog->Name); $this->assertEquals('Red', $dog->Color); $cat = Cat::get()->first(); $this->assertEquals('Puff daddy', $cat->Name); $this->assertEquals(1, $cat->HasClaws); }
/** * */ 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()); }