session = new ezcPersistentSession( ezcDbInstance::get(), new ezcPersistentCodeManager( dirname( __FILE__ ) . "/data/" ) ); } public function tearDown() { PersistentTestObject::cleanup(); } public static function suite() { return new ezcTestSuite( 'ezcPersistentSessionTest' ); } /// // loadIfExists // public function testLoadIfExistsValid() { $object = $this->session->loadIfExists( 'PersistentTestObject', 1 ); $this->assertEquals( 'PersistentTestObject', get_class( $object ) ); } // class name is not a persistent object public function testLoadIfExistsInvalid() { $object = $this->session->loadIfExists( 'NoSuchClass', 1 ); $this->assertEquals( null, $object ); } // no such object id public function testLoadIfExistsNoSuchObject() { $object = $this->session->loadIfExists( 'PersistentTestObject', 999 ); $this->assertEquals( null, $object ); } // // load // // class name is not a persistent object public function testLoadValid() { $object = $this->session->load( 'PersistentTestObject', "1" ); $this->assertEquals( 'PersistentTestObject', get_class( $object ) ); } public function testLoadInvalid() { try { $object = $this->session->load( 'NoSuchClass', 1 ); $this->fail( "load() called with invalid class. Did not get an exception" ); } catch ( ezcPersistentObjectException $e ) {} } // no such object id public function testLoadNoSuchObject() { try { $object = $this->session->load( 'PersistentTestObject', 999 ); $this->fail( "load() called with invalid object id. Did not get an exception" ); } catch ( ezcPersistentObjectException $e ) { return; } } // // loadIntoObject // public function testLoadIntoObjectValid() { $object = new PersistentTestObject(); $object->varchar = 'Finland'; $object->integer = 42; $object->decimal = 1.42; $object->text = "Finland has Nokia!"; $this->session->save( $object ); $this->assertEquals( 5, $object->id ); $object2 = new PersistentTestObject(); $this->session->loadIntoObject( $object2, 5 ); $this->assertEquals( 'Finland', $object2->varchar ); $this->assertEquals( 42, (int)$object2->integer ); $this->assertEquals( 1.42, (float)$object2->decimal ); $this->assertEquals( 'Finland has Nokia!', $object2->text ); } // wrong class type public function testLoadIntoObjectInvalid() { try { $object = $this->session->loadIntoObject( new Exception(), 1 ); $this->fail( "loadIntoObject() called with invalid class. Did not get an exception" ); } catch ( ezcPersistentObjectException $e ) {} } // no such object id public function testLoadIntoObjectNoSuchObject() { try { $object = $this->session->loadIntoObject( new PersistentTestObject(), 999 ); $this->fail( "loadIntoObject() called with invalid class. Did not get an exception" ); } catch ( ezcPersistentObjectException $e ) {} } // // update // public function testUpdateValid() { $object = $this->session->loadIfExists( 'PersistentTestObject', 1 ); $this->assertEquals( 'PersistentTestObject', get_class( $object ) ); $object->varchar = 'Finland'; $object->integer = 42; $object->decimal = 1.42; $object->text = "Finland has Nokia!"; $this->session->update( $object ); // check that we got the correct values $object2 = $this->session->loadIfExists( 'PersistentTestObject', 1 ); $this->assertEquals( 'Finland', $object2->varchar ); $this->assertEquals( 42, (int)$object2->integer ); $this->assertEquals( 1.42, (float)$object2->decimal ); $this->assertEquals( 'Finland has Nokia!', $object2->text ); } // object is not a persistent object public function testUpdateInvalidObject() { try { $this->session->update( new Exception() ); $this->fail( "Update of non-persistent object did not throw exception" ); } catch ( ezcPersistentObjectException $e ) {} } // object is a new persistent object public function testUpdateNotInDatabase() { try { $this->session->update( new PersistentTestObject() ); $this->fail( "Update of object not in database did not fail." ); } catch ( ezcPersistentObjectException $e ) {} } // // save /// public function testSaveValid() { $object = new PersistentTestObject(); $object->varchar = 'Finland'; $object->integer = 42; $object->decimal = 1.42; $object->text = "Finland has Nokia!"; $this->session->save( $object ); $this->assertEquals( 5, $object->id ); $object2 = $this->session->loadIfExists( 'PersistentTestObject', 5 ); $this->assertEquals( 'Finland', $object2->varchar ); $this->assertEquals( 42, (int)$object2->integer ); $this->assertEquals( 1.42, (float)$object2->decimal ); $this->assertEquals( 'Finland has Nokia!', $object2->text ); } // object is not a persistent object public function testSaveInvalidObject() { try { $this->session->save( new Exception() ); $this->fail( "Save of non-persistent object did not throw exception" ); } catch ( ezcPersistentObjectException $e ) {} } // the object is already persistent public function testSaveAlreadyInDatabase() { $object = new PersistentTestObject(); $object->varchar = 'Finland'; $object->integer = 42; $object->decimal = 1.42; $object->text = "Finland has Nokia!"; $this->session->save( $object ); try { $this->session->save( $object ); $this->fail( "Save of object already saved did not fail." ); } catch ( ezcPersistentObjectException $e ) {} } // // Save or update // public function testSaveOrUpdateSave() { $object = new PersistentTestObject(); $object->varchar = 'Finland'; $object->integer = 42; $object->decimal = 1.42; $object->text = "Finland has Nokia!"; $this->session->saveOrUpdate( $object ); $this->assertEquals( 5, $object->id ); $object2 = $this->session->loadIfExists( 'PersistentTestObject', 5 ); $this->assertEquals( 'Finland', $object2->varchar ); $this->assertEquals( 42, (int)$object2->integer ); $this->assertEquals( 1.42, (float)$object2->decimal ); $this->assertEquals( 'Finland has Nokia!', $object2->text ); } public function testSaveOrUpdateUpdate() { $object = $this->session->loadIfExists( 'PersistentTestObject', 1 ); $this->assertEquals( 'PersistentTestObject', get_class( $object ) ); $object->varchar = 'Finland'; $object->integer = 42; $object->decimal = 1.42; $object->text = "Finland has Nokia!"; $this->session->saveOrUpdate( $object ); // check that we got the correct values $object2 = $this->session->loadIfExists( 'PersistentTestObject', 1 ); $this->assertEquals( 'Finland', $object2->varchar ); $this->assertEquals( 42, (int)$object2->integer ); $this->assertEquals( 1.42, (float)$object2->decimal ); $this->assertEquals( 'Finland has Nokia!', $object2->text ); } // object is not a persistent object public function testSaveOrUpdateInvalidObject() { try { $this->session->saveOrUpdate( new Exception() ); $this->fail( "SaveorUpdate of non-persistent object did not throw exception" ); } catch ( ezcPersistentObjectException $e ) {} } // // refresh // public function testRefreshValid() { $object = new PersistentTestObject(); $object->varchar = 'Finland'; $object->integer = 42; $object->decimal = 1.42; $object->text = "Finland has Nokia!"; $this->session->save( $object ); $object->integer = 101; $this->session->refresh( $object ); $this->assertEquals( 42, (int)$object->integer ); } public function testRefreshInvalid() { try { $this->session->refresh( new Exception() ); $this->fail( "refresh of non-persistent object did not throw exception" ); } catch ( ezcPersistentObjectException $e ) {} } public function testRefreshNotPersistent() { try { $this->session->refresh( new PersistentTestObject() ); $this->fail( "refresh of non-persistent object did not throw exception" ); } catch ( ezcPersistentObjectException $e ) {} } // // delete // public function testDeleteValid() { $object = new PersistentTestObject(); $object->varchar = 'Finland'; $object->integer = 42; $object->decimal = 1.42; $object->text = "Finland has Nokia!"; $this->session->save( $object ); $this->assertEquals( 5, $object->id ); $this->session->delete( $object ); try { $this->session->load( 'PersistentTestObject', 5 ); $this->fail( "Fetching a deleted object did not throw exception." ); } catch ( ezcPersistentObjectException $e ) {} } // not persistent class public function testDeleteInvalid() { try { $this->session->delete( new Exception() ); $this->fail( "Deleting a non persistent object did not throw exception." ); } catch ( ezcPersistentObjectException $e ) {} } // deleting an object that is not persistent yet public function testDeleteNotPersistent() { try { $this->session->delete( new PersistentTestObject() ); $this->fail( "Deleting an object that is not yet persistent did not throw exception." ); } catch ( ezcPersistentObjectException $e ) {} } // // find // public function testFindNoResult() { $q = $this->session->createFindQuery( 'PersistentTestObject' ); $q->where( $q->expr->eq( 'id', 999 ) ); $objects = $this->session->find( $q, 'PersistentTestObject' ); $this->assertEquals( 0, count( $objects ) ); } public function testFindSingleResult() { $q = $this->session->createFindQuery( 'PersistentTestObject' ); $q->where( $q->expr->eq( 'id', 1 ) ); $objects = $this->session->find( $q, 'PersistentTestObject' ); $this->assertEquals( 1, count( $objects ) ); } public function testFindMultipleResult() { $q = $this->session->createFindQuery( 'PersistentTestObject' ); $q->where( $q->expr->gt( 'id', 2 ) ); $objects = $this->session->find( $q, 'PersistentTestObject' ); $this->assertEquals( 2, count( $objects ) ); // check that the data is correct $this->assertEquals( 'Ukraine', $objects[0]->varchar ); $this->assertEquals( 47732079, (int)$objects[0]->integer ); $this->assertEquals( 603.70, (float)$objects[0]->decimal ); $this->assertEquals( 'Ukraine has a long coastline to the black see.', $objects[0]->text ); $this->assertEquals( 'Germany', $objects[1]->varchar ); $this->assertEquals( 82443000, (int)$objects[1]->integer ); $this->assertEquals( 357.02, (float)$objects[1]->decimal ); $this->assertEquals( 'Home of the lederhosen!.', $objects[1]->text ); } // // findIterator // public function testFindIteratorNoResult() { $q = $this->session->createFindQuery( 'PersistentTestObject' ); $q->where( $q->expr->eq( 'id', 999 ) ); $it = $this->session->findIterator( $q, 'PersistentTestObject' ); $this->assertEquals( null, $it->next() ); } public function testFindIteratorSingleResult() { $q = $this->session->createFindQuery( 'PersistentTestObject' ); $q->where( $q->expr->eq( 'id', 1 ) ); $it = $this->session->findIterator( $q, 'PersistentTestObject' ); $i = 0; foreach ( $it as $object ) { ++$i; } $this->assertEquals( 1, $i ); } public function testFindIteratorMultipleResult() { $q = $this->session->createFindQuery( 'PersistentTestObject' ); $q->where( $q->expr->gt( 'id', 2 ) ); $objects = $this->session->find( $q, 'PersistentTestObject' ); $this->assertEquals( 2, count( $objects ) ); $it = $this->session->findIterator( $q, 'PersistentTestObject' ); $i = 0; foreach ( $it as $object ) { ++$i; } $this->assertEquals( 2, $i ); } // // deleteFromQuery // public function testDeleteFromQuery() { $q = $this->session->createDeleteQuery( 'PersistentTestObject' ); $q->where( $q->expr->neq( 'integer', 0 ) ); $this->session->deleteFromQuery( $q ); $q = $this->session->createFindQuery( 'PersistentTestObject' ); $objects = $this->session->find( $q, 'PersistentTestObject' ); $this->assertEquals( 0, count( $objects ) ); } // public funciton testDeleteFrom with forced error // // updateFromQuery // public function testUpdateFromQuery() { $q = $this->session->createUpdateQuery( 'PersistentTestObject' ); $q->set( 'integer', 50 ); $this->session->updateFromQuery( $q ); // check that each value got 'integer' set to 50 $q = $this->session->createFindQuery( 'PersistentTestObject' ); $it = $this->session->findIterator( $q, 'PersistentTestObject' ); foreach ( $it as $object ) { $this->assertEquals( 50, (int)$object->integer ); } } // public funciton testDeleteFrom with forced error // // Test aliases // public function testFindUsingAliases() { $q = $this->session->createFindQuery( 'PersistentTestObject' ); $q->where( $q->expr->eq( 'varchar', $q->bindValue( 'Ukraine' ) ) ); $objects = $this->session->find( $q, 'PersistentTestObject' ); $this->assertEquals( 1, count( $objects ) ); // check that the data is correct $this->assertEquals( 'Ukraine', $objects[0]->varchar ); $this->assertEquals( 47732079, (int)$objects[0]->integer ); $this->assertEquals( 603.70, (float)$objects[0]->decimal ); $this->assertEquals( 'Ukraine has a long coastline to the black see.', $objects[0]->text ); } } ?>