Source of file OnAfterExistsTest.php
Size: 3,793 Bytes - Last Modified: 2021-12-23T10:02:11+00:00
/var/www/docs.ssmods.com/process/src/tests/OnAfterExistsTest.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 | <?php namespace BatchWrite\Tests; /** * Class OnAfterExistsTest * @package BatchWrite\Tests */ /** * Class OnAfterExistsTest * @package BatchWrite\Tests */ class OnAfterExistsTest extends \SapphireTest { /** * @var bool */ protected $usesDatabase = true; /** * @var array */ protected $extraDataObjects = array( 'BatchWrite\Tests\Animal', 'BatchWrite\Tests\Batman', 'BatchWrite\Tests\Cat', 'BatchWrite\Tests\Child', 'BatchWrite\Tests\Child', 'BatchWrite\Tests\Dog', 'BatchWrite\Tests\DogPage', 'BatchWrite\Tests\Human', ); /** * OnAfterExistsTest constructor. */ public function __construct() { $this->setUpOnce(); } /** * @throws \ValidationException * @throws null */ public function testCallback_OneCondition_CalledBack() { $dog = new Dog(); $dog->Name = 'Johnny'; $owner = new Human(); $owner->Name = 'Bob'; $afterExists = new \OnAfterExists(function () use ($dog) { $dog->write(); }); $afterExists->addCondition($owner, function ($owner) use($dog) { $dog->OwnerID = $owner->ID; }); $owner->write(); $this->assertTrue($dog->exists()); $this->assertEquals($owner->ID, $dog->OwnerID); } /** * @throws \ValidationException * @throws null */ public function testCallback_ManyConditions_CalledBack() { $dog = new Dog(); $dog->Name = 'Johnny'; $owner1 = new Human(); $owner1->Name = 'Bob'; $owner2 = new Human(); $owner2->Name = 'Wot'; $cat = new Cat(); $cat->Name = 'Agnis'; $afterExists = new \OnAfterExists(function () use ($dog) { $dog->write(); }); $afterExists->addCondition($owner1, function ($owner) use($dog) { $dog->Name .= ' ' . $owner->Name; $dog->OwnerID = $owner->ID; }); $afterExists->addCondition($owner2, function ($owner) use($dog) { $dog->Name .= ' ' . $owner->Name; $dog->OwnerID = $owner->ID; }); $afterExists->addCondition($cat, function ($cat) use($dog) { $dog->Name .= ' ' . $cat->Name; }); $owner1->write(); $this->assertFalse($dog->exists()); $owner2->write(); $this->assertFalse($dog->exists()); $cat->write(); $this->assertTrue($dog->exists()); $this->assertEquals($owner2->ID, $dog->OwnerID); $this->assertEquals('Johnny Bob Wot Agnis', $dog->Name); } /** * */ 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()); } // // public static function tearDownAfterClass() // { // parent::tearDownAfterClass(); // \SapphireTest::delete_all_temp_dbs(); // } } |