Permalink
Browse files

ID#122: added event method beforeDelete() to generic domain object im…

…plementation.
  • Loading branch information...
Christian Achatz
Christian Achatz committed Aug 26, 2018
1 parent 3f25cb9 commit 634133c47481bd4832ecb2b3e6d335d667947689
@@ -546,7 +546,6 @@ public function __sleep() {
* Version 0.1, 15.01.2011<br />
*/
public function beforeSave() {
}
/**
@@ -557,7 +556,6 @@ public function beforeSave() {
* Version 0.1, 15.01.2011<br />
*/
public function afterSave() {
}
/**
@@ -568,7 +566,16 @@ public function afterSave() {
* Version 0.1, 15.01.2011<br />
*/
public function afterLoad() {
}
/**
* Implement event functions
*
* @author Christian Achatz
* @version
* Version 0.1, 26.09.2018<br />
*/
public function beforeDelete() {
}
/**
@@ -80,7 +80,23 @@ public function deleteAssociations(string $relationName);
public function &getRelatedObjects(string $relationName);
/**
* @return GenericORMapperDataObject[]
* Returns an associative list of all related objects grouped by relation name.
* <p/>
* Example:
* <code>
* [
* 'Relation1' => [
* $object1,
* $object2,
* $object3
* ],
* 'Relation2' => [
* $object4,
* ]
* ]
* </code>
*
* @return GenericORMapperDataObject[][]
*/
public function &getAllRelatedObjects();
@@ -119,4 +135,9 @@ public function afterSave();
*/
public function afterLoad();
/**
* ID#122: Will be called by GORM before an object is deleted to i.e. clean up relations.
*/
public function beforeDelete();
}
@@ -969,6 +969,10 @@ public function deleteObject(GenericORMapperDataObject $object) {
return null;
}
// ID#122: call event method beforeDelete() to allow dependency/relation management.
$object->setDataComponent($this);
$object->beforeDelete();
// 1. get compositions, where source or target object is the current object
$objectName = $object->getObjectName();
$objectID = $this->mappingTable[$objectName]['ID'];
@@ -31,15 +31,8 @@
class GenericORRelationMapperTest extends TestCase {
/**
* Clean-up generated files.
* ID#337: Tests relation timestamp loading.
*/
public function tearDown() {
unlink(__DIR__ . '/Dad.php');
unlink(__DIR__ . '/DadBase.php');
unlink(__DIR__ . '/Son.php');
unlink(__DIR__ . '/SonBase.php');
}
public function testRelationTimeStamps() {
// Generate domain objects ///////////////////////////////////////////////////////////////////////////////////////
@@ -153,6 +146,12 @@ public function testRelationTimeStamps() {
$this->assertEquals($objectId, $children[0]->getObjectId());
$this->assertEquals($timestamp, $children[0]->getRelationCreationTimestamp());
// Clean up generated domain object files.
unlink(__DIR__ . '/Dad.php');
unlink(__DIR__ . '/DadBase.php');
unlink(__DIR__ . '/Son.php');
unlink(__DIR__ . '/SonBase.php');
}
/**
@@ -193,4 +192,46 @@ protected function getConfigReturnMap() {
];
}
/**
* ID#122: test whether event method beforeDelete() is called according to timing model.
*/
public function testEventMethodBeforeDelete() {
/* @var $object GenericDomainObject|MockObject */
$object = $this->getMockBuilder(GenericDomainObject::class)
->setMethods(['beforeDelete'])
->setConstructorArgs(['Dad'])
->getMock();
$object->expects($this->once())
->method('beforeDelete');
$id = 42;
$object->setObjectId($id);
/* @var $mapper GenericORRelationMapper|MockObject */
$mapper = $this->getMockBuilder(GenericORRelationMapper::class)
->setMethods(['getConfiguration'])
->getMock();
$mapper->method('getConfiguration')
->willReturnMap($this->getConfigReturnMap());
$mapper->addMappingConfiguration('foo', 'bar');
$mapper->addRelationConfiguration('foo', 'bar');
/* @var $db MySQLiHandler|MockObject */
$db = $this->getMockBuilder(MySQLiHandler::class)
->setMethods(['connect', 'fetchData', 'escapeValue', 'executeTextStatement', 'getNumRows'])
->getMock();
$db->method('escapeValue')
->willReturnArgument(0);
$mapper->setDbDriver($db);
$this->assertEquals($id, $mapper->deleteObject($object));
}
}

0 comments on commit 634133c

Please sign in to comment.