From 7a03c21089cfad81398aa72888301a2dece00f0b Mon Sep 17 00:00:00 2001 From: Sebastian Schaum Date: Thu, 28 Nov 2013 00:55:38 +0100 Subject: [PATCH 1/6] fixed bug in identity map decorator if no object is found The normal ezcPersistentSession object throws an ezcPersistentRelatedObjectNotFoundException if getRelatedObject() did not found any related object, but the Decorator does not. This problem was fixed here. --- src/session_decorators/identity.php | 12 ++++++++++-- tests/data/relation_test.php | 1 + .../relation_test.php | 9 +++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/session_decorators/identity.php b/src/session_decorators/identity.php index 0cc1072..fe44e7a 100644 --- a/src/session_decorators/identity.php +++ b/src/session_decorators/identity.php @@ -682,8 +682,8 @@ public function getRelatedObjects( $object, $relatedClass, $relationName = null * * @return object($relatedClass) * - * @throws ezcPersistentRelationNotFoundException - * if the given $object does not have a relation to $relatedClass. + * @throws ezcPersistentRelatedObjectNotFoundException + * if there is no object of $relatedClass found for $object */ public function getRelatedObject( $object, $relatedClass, $relationName = null ) { @@ -692,6 +692,14 @@ public function getRelatedObject( $object, $relatedClass, $relationName = null ) $relatedClass, $relationName ); + if ( empty( $relObjs ) ) { + // no object found, so throw same exception like it is thrown by + // ezcPersistentSession + throw new ezcPersistentRelatedObjectNotFoundException( + $object, + $relatedClass + ); + } return reset( $relObjs ); } diff --git a/tests/data/relation_test.php b/tests/data/relation_test.php index 5863e77..dbed8fe 100644 --- a/tests/data/relation_test.php +++ b/tests/data/relation_test.php @@ -45,6 +45,7 @@ public static function insertData( $db = null ) $db->exec( "INSERT INTO " . $db->quoteIdentifier( "PO_persons" ) . " (" . $db->quoteIdentifier( "firstname" ) . ", " . $db->quoteIdentifier( "surname" ) . ", " . $db->quoteIdentifier( "employer" ) . ") VALUES (" . $db->quote( "Theodor" ) . ", " . $db->quote( "Gopher" ) . ", 2)" ); $db->exec( "INSERT INTO " . $db->quoteIdentifier( "PO_persons" ) . " (" . $db->quoteIdentifier( "firstname" ) . ", " . $db->quoteIdentifier( "surname" ) . ", " . $db->quoteIdentifier( "employer" ) . ") VALUES (" . $db->quote( "Frederick" ) . ", " . $db->quote( "Ajax" ) . ", 1)" ); $db->exec( "INSERT INTO " . $db->quoteIdentifier( "PO_persons" ) . " (" . $db->quoteIdentifier( "firstname" ) . ", " . $db->quoteIdentifier( "surname" ) . ", " . $db->quoteIdentifier( "employer" ) . ") VALUES (" . $db->quote( "Raymond" ) . ", " . $db->quote( "Socialweb" ) . ", 1)" ); + $db->exec( "INSERT INTO " . $db->quoteIdentifier( "PO_persons" ) . " (" . $db->quoteIdentifier( "firstname" ) . ", " . $db->quoteIdentifier( "surname" ) . ", " . $db->quoteIdentifier( "employer" ) . ") VALUES (" . $db->quote( "Pete" ) . ", " . $db->quote( "Javascript" ) . ", 0)" ); $db->exec( "INSERT INTO " . $db->quoteIdentifier( "PO_persons_addresses" ) . " ( " . $db->quoteIdentifier( "person_id" ) . ", " . $db->quoteIdentifier( "address_id" ) . ") VALUES ( 1, 1)" ); $db->exec( "INSERT INTO " . $db->quoteIdentifier( "PO_persons_addresses" ) . " ( " . $db->quoteIdentifier( "person_id" ) . ", " . $db->quoteIdentifier( "address_id" ) . ") VALUES ( 1, 2)" ); diff --git a/tests/persistent_session_identity_decorator/relation_test.php b/tests/persistent_session_identity_decorator/relation_test.php index 0456602..011fb59 100644 --- a/tests/persistent_session_identity_decorator/relation_test.php +++ b/tests/persistent_session_identity_decorator/relation_test.php @@ -183,6 +183,15 @@ public function testGetRelatedObjectEmployer1Refetch() $this->assertEquals( $employer1, $employer2 ); } + /** + * @expectedException ezcPersistentRelatedObjectNotFoundException + */ + public function testGetRelatedObjectNotFound() + { + $person = $this->idSession->load( "RelationTestPerson", 4 ); + $this->idSession->getRelatedObject( $person, "RelationTestEmployer" ); + } + public function testAddRelatedObjectEmployerFailureReverse() { $person = $this->idSession->load( "RelationTestPerson", 2 ); From 8d524a11d1216371f54aeb2afdfe427d4b2307c7 Mon Sep 17 00:00:00 2001 From: Sebastian Schaum Date: Thu, 28 Nov 2013 12:11:46 +0100 Subject: [PATCH 2/6] added falsely removed @throws documentation --- src/session_decorators/identity.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/session_decorators/identity.php b/src/session_decorators/identity.php index fe44e7a..e7e49f5 100644 --- a/src/session_decorators/identity.php +++ b/src/session_decorators/identity.php @@ -682,6 +682,9 @@ public function getRelatedObjects( $object, $relatedClass, $relationName = null * * @return object($relatedClass) * + * @throws ezcPersistentRelationNotFoundException + * if the given $object does not have a relation to $relatedClass. + * * @throws ezcPersistentRelatedObjectNotFoundException * if there is no object of $relatedClass found for $object */ From 42cc50aac0b185c8dd21f91d9d3ccd6cea7a6bd2 Mon Sep 17 00:00:00 2001 From: Sebastian Schaum Date: Thu, 28 Nov 2013 16:57:50 +0100 Subject: [PATCH 3/6] fixed typo in test-fail message --- tests/relations/one_to_many_relation_test.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/relations/one_to_many_relation_test.php b/tests/relations/one_to_many_relation_test.php index 5f37ae7..528f2ef 100644 --- a/tests/relations/one_to_many_relation_test.php +++ b/tests/relations/one_to_many_relation_test.php @@ -596,7 +596,7 @@ public function testDeleteEmployer2CascadePersonCascadeBirthdaySuccess() $this->assertEquals( 2, $stmt->fetchColumn(), - "Persons cascaded from employer not deletec correctly." + "Persons cascaded from employer not deleted correctly." ); unset( $q, $stmt ); From f7b3bdd2b44b23855cd28d3218157273f6ed522a Mon Sep 17 00:00:00 2001 From: Sebastian Schaum Date: Thu, 28 Nov 2013 17:05:42 +0100 Subject: [PATCH 4/6] adjusted expected id and count of persons to consider the recently added person in test-enviroment --- tests/relations/one_to_many_relation_test.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/relations/one_to_many_relation_test.php b/tests/relations/one_to_many_relation_test.php index 528f2ef..6800191 100644 --- a/tests/relations/one_to_many_relation_test.php +++ b/tests/relations/one_to_many_relation_test.php @@ -393,7 +393,7 @@ public function testAddRelatedPersonToEmployer2SaveSuccess() $this->session->save( $person ); $res = RelationTestPerson::__set_state(array( - 'id' => 4, + 'id' => 5, 'firstname' => 'Jan', 'surname' => 'Soap', 'employer' => 2, @@ -457,14 +457,14 @@ public function testAddRelatedPersonsToEmployer2SaveSuccess() $res = array( 0 => RelationTestPerson::__set_state(array( - 'id' => 4, + 'id' => 5, 'firstname' => 'Tobias', 'surname' => 'Preprocess', 'employer' => 2, )), 1 => RelationTestPerson::__set_state(array( - 'id' => 5, + 'id' => 6, 'firstname' => 'Jan', 'surname' => 'Soap', 'employer' => 2, @@ -594,7 +594,7 @@ public function testDeleteEmployer2CascadePersonCascadeBirthdaySuccess() $stmt->execute(); $this->assertEquals( - 2, + 3, $stmt->fetchColumn(), "Persons cascaded from employer not deleted correctly." ); From da8156fc2f630e9cd33d8324db4564b6b7613734 Mon Sep 17 00:00:00 2001 From: Sebastian Schaum Date: Thu, 28 Nov 2013 17:06:38 +0100 Subject: [PATCH 5/6] ignoring persons which does not have an employer set in fake find query this is needed because we do not expect them to be found --- .../relation_object_extractor_test.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/persistent_session_identity_decorator/relation_object_extractor_test.php b/tests/persistent_session_identity_decorator/relation_object_extractor_test.php index e4d103d..41b4588 100644 --- a/tests/persistent_session_identity_decorator/relation_object_extractor_test.php +++ b/tests/persistent_session_identity_decorator/relation_object_extractor_test.php @@ -164,6 +164,10 @@ public function testFindOneLevelOneRelationRestrictions() ); $fakeFind = $this->session->createFindQuery( 'RelationTestPerson' ); + // ignore Persons where employer is not set + $fakeFind->where( + $fakeFind->expr->neq('employer', 0) + ); $fakeRes = $this->session->find( $fakeFind ); $this->assertEquals( @@ -488,6 +492,10 @@ public function testFindOneLevelMultiRelationRestrictions() ); $fakeFind = $this->session->createFindQuery( 'RelationTestPerson' ); + // ignore Persons where employer is not set + $fakeFind->where( + $fakeFind->expr->neq('employer', 0) + ); $fakeRes = $this->session->find( $fakeFind ); $this->assertEquals( @@ -622,6 +630,10 @@ public function testFindMultiLevelSingleRelationRestrictions() ); $fakeFind = $this->session->createFindQuery( 'RelationTestPerson' ); + // ignore Persons where employer is not set + $fakeFind->where( + $fakeFind->expr->neq('employer', 0) + ); $fakeRes = $this->session->find( $fakeFind ); $this->assertEquals( From 42a59d4090a097e8220d07df84ed49573c0ae90c Mon Sep 17 00:00:00 2001 From: Sebastian Schaum Date: Thu, 28 Nov 2013 17:15:58 +0100 Subject: [PATCH 6/6] made require statement independently from include path This class relies on the fact that zetaComponents were installed by PEAR or are directly accessible from the include path. This was fixed and the implementation in this directories are used now. --- tests/persistent_session_identity_decorator/test_case.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/persistent_session_identity_decorator/test_case.php b/tests/persistent_session_identity_decorator/test_case.php index e499496..29e428c 100644 --- a/tests/persistent_session_identity_decorator/test_case.php +++ b/tests/persistent_session_identity_decorator/test_case.php @@ -25,7 +25,7 @@ * @subpackage Tests */ -require_once 'PersistentObject/tests/persistent_session/test_case.php'; +require_once dirname(__FILE__) . '/../persistent_session/test_case.php'; /** * Tests the code manager.