From 20f0207560a7a27790d0b437f151628d810da627 Mon Sep 17 00:00:00 2001 From: Gaetan Voyer-Perrault Date: Tue, 12 Jul 2011 15:40:44 -0700 Subject: [PATCH] Fix testing scripts. Extend auth & replica set test coverage. --- lib/MongoEntity.class.php | 3 +- test/all_tests.php | 16 ++++++-- test/test_mongo_entity_auth.php | 62 ++++++++++++++++++++++++++++-- test/test_mongo_entity_replica.php | 18 ++++++++- 4 files changed, 89 insertions(+), 10 deletions(-) diff --git a/lib/MongoEntity.class.php b/lib/MongoEntity.class.php index 3c291ef..a8c44b3 100644 --- a/lib/MongoEntity.class.php +++ b/lib/MongoEntity.class.php @@ -146,7 +146,6 @@ public static function loadCollection($collectionName = null,$databaseName = nul } catch (Exception $e) { /* Add logging */ - print $e->getMessage()."\n"; } return FALSE; } @@ -393,9 +392,11 @@ public function save($safe = false, $upsert = true){ } } catch(MongoCursorException $e) { + /* log exception */ return false; } catch(MongoCursorTimeoutException $e){ + /* log exception */ return false; } } diff --git a/test/all_tests.php b/test/all_tests.php index 7332d1d..de71713 100644 --- a/test/all_tests.php +++ b/test/all_tests.php @@ -70,7 +70,7 @@ private function stop_mongo_basic(){ $this->stop_mongo_node(new Mongo()); } catch (Exception $e) { - print $e->getMessage(); + // note exception means that shutdown succeeded } } @@ -103,13 +103,23 @@ private function stop_mongo_replica(){ try { print "Stopping server replica 1\n"; $this->stop_mongo_node(new Mongo("mongodb://localhost:6900", array('replicaset' => true))); + } + catch (Exception $e) { + // note exception means that shutdown succeeded + } + try { print "Stopping server replica 2\n"; $this->stop_mongo_node(new Mongo("mongodb://localhost:6901", array('replicaset' => true))); + } + catch (Exception $e) { + // note exception means that shutdown succeeded + } + try { print "Stopping server replica 3\n"; $this->stop_mongo_node(new Mongo("mongodb://localhost:6902", array('replicaset' => true))); } catch (Exception $e) { - print $e->getMessage(); + // note exception means that shutdown succeeded } } @@ -148,7 +158,7 @@ private function stop_mongo_auth(){ $this->stop_mongo_node(new Mongo('localhost:6904')); } catch (Exception $e) { - print $e->getMessage(); + // note exception means that shutdown succeeded } } diff --git a/test/test_mongo_entity_auth.php b/test/test_mongo_entity_auth.php index 40b15f9..1af22b7 100644 --- a/test/test_mongo_entity_auth.php +++ b/test/test_mongo_entity_auth.php @@ -22,15 +22,15 @@ class TestAuthEntityAuth extends UnitTestCase{ function setUp(){ -# $mongo = new Mongo(); -# $mongo->selectDB('test')->selectCollection('test')->drop(); + $coll = AuthEntity::loadCollection(); + $coll->drop(); } function teardown(){ -# $mongo = new Mongo(); -# $mongo->selectDB('test')->selectCollection('test')->drop(); + $coll = AuthEntity::loadCollection(); + $coll->drop(); } @@ -46,5 +46,59 @@ function testCreateAuth(){ $this->assertNotNull($data->id) ); } + function testLoadBasic(){ + $data = new AuthEntity(array('a' => 1, 'b' => 2)); + $this->assertTrue($data->save(true)); + + $data2 = new AuthEntity(); + $data2->load_single(); + + return ($this->assertEqual($data2->a, $data->a) && + $this->assertEqual($data->a, 1) && + $this->assertEqual($data->b, 2) && + $this->assertEqual($data2->b, $data->b)); + + } + + function testCreateAuthBad(){ + $data = new AuthEntityBad(); + + $data->a = 1; + $data->b = 2; + + return ($this->assertFalse($data->save())); + } + + function testLoadSpecific(){ + $data = new AuthEntity(array('a' => 1, 'b' => 2)); + $this->assertTrue($data->save(true)); + + $data2 = new AuthEntity(array('a' => 10, 'b' => 8)); + $data2->save(true); + $id = $data2->id; + + $data3 = new AuthEntity(); + $data3->load_single($id); + + return ($this->assertEqual($data2->a, 10) && + $this->assertEqual($data2->b, 8) && + $this->assertTrue($data2->a == $data3->a) && + $this->assertTrue($data2->b == $data3->b)); + } + + function testLoadSpecificFields(){ + $data = new AuthEntity(array('a' => 1, 'b' => 2, 'c' => 3)); + $this->assertTrue($data->save()); + $id = $data->id; + + $load = new AuthEntity(); + $load->load_single($id, array('a', 'b')); + + return ( $this->assertEqual($load->a, $data->a) && + $this->assertEqual($load->b, $data->b) && + $this->assertNull($load->c) && + $this->assertNotNull($data->c) ); + } + } ?> diff --git a/test/test_mongo_entity_replica.php b/test/test_mongo_entity_replica.php index 3507c44..79f6425 100644 --- a/test/test_mongo_entity_replica.php +++ b/test/test_mongo_entity_replica.php @@ -11,7 +11,21 @@ class MongoReplicaTest extends MongoEntity { } -class TestMongoReplica extends MongoTestCase{ +class TestMongoReplica extends UnitTestCase{ + + function setUp(){ + + $collection = MongoReplicaTest::loadCollection(); + $collection->drop(); + + } + + function teardown(){ + + $collection = MongoReplicaTest::loadCollection(); + $collection->drop(); + + } function testCreateBasic(){ $data = new MongoReplicaTest(); @@ -93,7 +107,7 @@ function testLoadSpecific(){ $this->assertTrue($data->save(true)); $data2 = new MongoReplicaTest(array('a' => 10, 'b' => 8)); - $data2->save(); + $data2->save(true); $id = $data2->id; $data3 = new MongoReplicaTest();