diff --git a/src/session_decorators/identity.php b/src/session_decorators/identity.php index 0cc1072..e7e49f5 100644 --- a/src/session_decorators/identity.php +++ b/src/session_decorators/identity.php @@ -684,6 +684,9 @@ public function getRelatedObjects( $object, $relatedClass, $relationName = null * * @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 +695,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_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( 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 ); diff --git a/tests/relations/one_to_many_relation_test.php b/tests/relations/one_to_many_relation_test.php index 5f37ae7..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,9 +594,9 @@ public function testDeleteEmployer2CascadePersonCascadeBirthdaySuccess() $stmt->execute(); $this->assertEquals( - 2, + 3, $stmt->fetchColumn(), - "Persons cascaded from employer not deletec correctly." + "Persons cascaded from employer not deleted correctly." ); unset( $q, $stmt );