Source of file WriteCallbackTest.php
Size: 3,415 Bytes - Last Modified: 2021-12-23T10:02:11+00:00
/var/www/docs.ssmods.com/process/src/tests/WriteCallbackTest.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 | <?php namespace BatchWrite\Tests; /** * Class WriteCallbackTest * @package BatchWrite\Tests */ /** * Class WriteCallbackTest * @package BatchWrite\Tests */ class WriteCallbackTest 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', ); /** * WriteCallbackTest constructor. */ public function __construct() { $this->setUpOnce(); } /** * @throws \ValidationException * @throws null */ public function testCallback_SetOnAfterWriteCallback_CallbackCalled() { $dog = new Dog(); $dog->Name = 'Jim bob'; $owner = new Human(); $owner->Name = 'Hilly Stewart'; $owner->onAfterWriteCallback(function ($owner) use ($dog) { $dog->OwnerID = $owner->ID; $dog->write(); }); $owner->write(); $this->assertTrue($owner->exists()); $this->assertTrue($dog->exists()); $this->assertEquals(1, Human::get()->Count()); $this->assertEquals(1, Dog::get()->Count()); $this->assertEquals($owner->ID, $dog->OwnerID); } /** * @throws \ValidationException * @throws null */ public function testCallback_SetOnBeforeWriteCallback_CallbackCalled() { $dog = new Dog(); $dog->Name = 'Jim bob'; $owner = new Human(); $owner->Name = 'Hilly Stewart'; $owner->write(); $dog->onBeforeWriteCallback(function ($dog) use ($owner) { $dog->OwnerID = $owner->ID; }); $dog->write(); $this->assertTrue($owner->exists()); $this->assertTrue($dog->exists()); $this->assertEquals(1, Human::get()->Count()); $this->assertEquals(1, Dog::get()->Count()); $this->assertEquals($owner->ID, $dog->OwnerID); } /** * @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 static function tearDownAfterClass() // { // parent::tearDownAfterClass(); // \SapphireTest::delete_all_temp_dbs(); // } } |