From 0f2ba3fadeaf07625b2d7337c084b2068db9a959 Mon Sep 17 00:00:00 2001 From: =?utf8?q?C=C3=A9sar=20D=2E=20Rodas?= Date: Sat, 29 May 2010 01:53:34 -0400 Subject: [PATCH] - Improved cloned objects support - Fixed bugs on unit testings --- lib/ActiveMongo.php | 18 ++++++++++++++++-- tests/CacheTest.php | 2 +- tests/HookTest.php | 8 ++++---- tests/QueryTest.php | 47 +++++++++++++++++++++++++++++++++++------------ tests/__SleepTest.php | 2 +- tests/tests.sh | 10 ++++++++-- 6 files changed, 65 insertions(+), 22 deletions(-) diff --git a/lib/ActiveMongo.php b/lib/ActiveMongo.php index b7db285..c59d898 100644 --- a/lib/ActiveMongo.php +++ b/lib/ActiveMongo.php @@ -520,7 +520,7 @@ abstract class ActiveMongo implements Iterator, Countable, ArrayAccess final static function addEvent($action, $callback) { if (!is_callable($callback)) { - throw new Exception("Invalid callback"); + throw new ActiveMongo_Exception("Invalid callback"); } $class = get_called_class(); @@ -1044,6 +1044,9 @@ abstract class ActiveMongo implements Iterator, Countable, ArrayAccess */ final function reset() { + if ($this->_cloned) { + throw new ActiveMongo_Exception("Cloned objects can't be reseted"); + } $this->_properties = NULL; $this->_cursor = NULL; $this->_cursor_ex = NULL; @@ -1148,6 +1151,9 @@ abstract class ActiveMongo implements Iterator, Countable, ArrayAccess */ final function rewind() { + if ($this->_cloned) { + throw new ActiveMongo_Exception("Cloned objects can't iterate"); + } if (!$this->_cursor_ex) { /* rely on MongoDB cursor */ if (!$this->_cursor InstanceOf MongoCursor) { @@ -1537,6 +1543,9 @@ abstract class ActiveMongo implements Iterator, Countable, ArrayAccess */ final function __clone() { + if (!$this->_current) { + throw new ActiveMongo_Exception("Empty objects can't be cloned"); + } unset($this->_cursor); $this->_cloned = TRUE; } @@ -1588,7 +1597,7 @@ abstract class ActiveMongo implements Iterator, Countable, ArrayAccess */ final private function _assertNotInQuery() { - if ($this->_cursor InstanceOf MongoCursor || $this->_cursor_ex != NULL) { + if ($this->_cloned || $this->_cursor InstanceOf MongoCursor || $this->_cursor_ex != NULL) { throw new ActiveMongo_Exception("You cannot modify the query, please reset the object"); } } @@ -1891,6 +1900,11 @@ abstract class ActiveMongo implements Iterator, Countable, ArrayAccess throw new ActiveMongo_Exception("Don't know how to parse {$sort_part_str}"); } + /* Columns name can't be empty */ + if (!trim($sort_part[0])) { + throw new ActiveMongo_Exception("Don't know how to parse {$sort_part_str}"); + } + switch (strtoupper($sort_part[1])) { case 'ASC': $sort_part[1] = 1; diff --git a/tests/CacheTest.php b/tests/CacheTest.php index ebc83e2..1e4a0aa 100644 --- a/tests/CacheTest.php +++ b/tests/CacheTest.php @@ -58,7 +58,7 @@ class CacheTest extends PHPUnit_Framework_TestCase { try { CacheableModel::drop(); - } Catch (Exception $e) { + } Catch (ActiveMongo_Exception $e) { } ActiveMongo_Cache::setDriver(new CacheDriverMem); } diff --git a/tests/HookTest.php b/tests/HookTest.php index ffc81f5..a660f3b 100644 --- a/tests/HookTest.php +++ b/tests/HookTest.php @@ -26,7 +26,7 @@ class HookTest extends PHPUnit_Framework_TestCase $c->M1 = 'foo'; $c->save(); $this->assertTrue(FALSE); - } catch(Exception $e) { + } catch(ActiveMongo_Exception $e) { $this->assertTrue(TRUE); $this->assertEquals($e->getMessage(), "Invalid M1 value"); } @@ -36,7 +36,7 @@ class HookTest extends PHPUnit_Framework_TestCase $c->no_throw = TRUE; $c->save(); $this->assertTrue(FALSE); - } catch(Exception $e) { + } catch(ActiveMongo_Exception $e) { $this->assertTrue(TRUE); $this->assertNotEquals($e->getMessage(), "Invalid M1 value"); } @@ -52,7 +52,7 @@ class HookTest extends PHPUnit_Framework_TestCase $this->assertTrue(TRUE); $d->delete(); $c->delete(); - } catch(Exception $e) { + } catch(ActiveMongo_Exception $e) { $this->assertTrue(FALSE); } } @@ -69,7 +69,7 @@ class HookTest extends PHPUnit_Framework_TestCase try { Model1::addEvent('after_update', 'invalid_callback'); $this->assertTrue(FALSE); - } catch (Exception $e) { + } catch (ActiveMongo_Exception $e) { $this->assertTrue(TRUE); } } diff --git a/tests/QueryTest.php b/tests/QueryTest.php index e66f41f..db08818 100644 --- a/tests/QueryTest.php +++ b/tests/QueryTest.php @@ -8,16 +8,16 @@ class QueryTest extends PHPUnit_Framework_TestCase ActiveMongo::connect(DB, "localhost"); try { Dummy::drop(); - } catch (Exception $e) {} + } catch (ActiveMongo_Exception $e) {} try { Model1::drop(); - } catch (Exception $e) {} + } catch (ActiveMongo_Exception $e) {} try { Model2::drop(); - } catch (Exception $e) {} + } catch (ActiveMongo_Exception $e) {} try { Model3::drop(); - } catch (Exception $e) {} + } catch (ActiveMongo_Exception $e) {} $this->assertTrue(TRUE); } @@ -25,7 +25,7 @@ class QueryTest extends PHPUnit_Framework_TestCase { try { Model3::drop(); - } catch (Exception $e) {} + } catch (ActiveMongo_Exception $e) {} $data = array(); /* Valid data */ @@ -368,6 +368,14 @@ class QueryTest extends PHPUnit_Framework_TestCase $c->reset(); + /* object with no results can't be cloned */ + try { + $foo = clone $c; + $this->AssertTrue(FALSE); + } catch (ActiveMongo_Exception $e) { + $this->AssertTrue(TRUE); + } + foreach ($c as $item) { $item_cloned = clone $item; $item_cloned->c = 1; @@ -377,10 +385,25 @@ class QueryTest extends PHPUnit_Framework_TestCase foreach ($item_cloned as $nitem) { $this->assertTrue(FALSE); } - } catch (Exception $e) { + } catch (ActiveMongo_Exception $e) { $this->assertTrue(TRUE); } } + + /* cloned object can't be reused */ + try { + $item_cloned->reset(); + $this->AssertTrue(FALSE); + } catch (ActiveMongo_Exception $e) { + $this->AssertTrue(TRUE); + } + try { + $item_cloned->where('a IN', array(1)); + $this->AssertTrue(FALSE); + } catch (ActiveMongo_Exception $e) { + $this->AssertTrue(TRUE); + } + } function testToSTring() @@ -427,7 +450,7 @@ class QueryTest extends PHPUnit_Framework_TestCase $this->assertTrue(Dummy::drop()); try { $this->assertFalse(Dummy::drop()); - } catch (Exception $e) { + } catch (ActiveMongo_Exception $e) { $this->assertTrue(TRUE); } } @@ -460,7 +483,7 @@ class QueryTest extends PHPUnit_Framework_TestCase /* Invalid call */ ActiveMongo::BatchInsert($documents); $this->assertTrue(False); - } catch (Exception $e) { + } catch (ActiveMongo_Exception $e) { $this->assertTrue(TRUE); } @@ -489,7 +512,7 @@ class QueryTest extends PHPUnit_Framework_TestCase try { $c->where("invalid field property", 3); $this->assertTrue(FALSE); - } catch (Exception $e) { + } catch (ActiveMongo_Exception $e) { $this->assertTrue(TRUE); } @@ -498,7 +521,7 @@ class QueryTest extends PHPUnit_Framework_TestCase "b" => 1, ), TRUE); $this->assertTrue(FALSE); - } catch (Exception $e) { + } catch (ActiveMongo_Exception $e) { $this->assertTrue(TRUE); } @@ -506,14 +529,14 @@ class QueryTest extends PHPUnit_Framework_TestCase try { $c->sort(" , , "); $this->assertTrue(FALSE); - } catch (Exception $e) { + } catch (ActiveMongo_Exception $e) { $this->assertTrue(TRUE); } try { $c->sort("c DESC, field BAR"); $this->assertTrue(FALSE); - } catch (Exception $e) { + } catch (ActiveMongo_Exception $e) { $this->assertTrue(TRUE); } diff --git a/tests/__SleepTest.php b/tests/__SleepTest.php index 403a0ad..d06cd0f 100644 --- a/tests/__SleepTest.php +++ b/tests/__SleepTest.php @@ -17,7 +17,7 @@ class SleepTest extends PHPUnit_Framework_TestCase { try { SleepModel::drop(); - } catch (Exception $e) {} + } catch (ActiveMongo_Exception $e) {} } function testSleep() diff --git a/tests/tests.sh b/tests/tests.sh index d3e2aae..ac2f735 100755 --- a/tests/tests.sh +++ b/tests/tests.sh @@ -1,5 +1,11 @@ #!/bin/bash -x +PHP52=~/bin/php-5.2/bin/php +OPTPHP52="-dextension=mongo.so" +PHP53=$(which php) +OPTPHP53="" +PHPUNIT=$(which phpunit) -phpunit ActiveMongoSuite.php -~/bin/php-5.2/bin/php -dextension=mongo.so $(which phpunit) ActiveMongoSuite.php +$PHP53 $PHPOPT53 $PHPUNIT ActiveMongoSuite.php +sleep 1 +$PHP52 $OPTPHP52 $PHPUNIT ActiveMongoSuite.php -- 2.11.4.GIT