| 
									
										
										
										
											2011-11-17 15:13:11 +04:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* | 
					
						
							|  |  |  |  * @copyright NetMonsters <team@netmonsters.ru> | 
					
						
							|  |  |  |  * @link http://netmonsters.ru | 
					
						
							|  |  |  |  * @package Majestic | 
					
						
							|  |  |  |  * @subpackage UnitTests | 
					
						
							|  |  |  |  * @since 2011-11-7 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Unit tests for MongoModel class | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | require_once dirname(__FILE__) . '/../../Registry.php'; | 
					
						
							|  |  |  | require_once dirname(__FILE__) . '/../../Config.php'; | 
					
						
							|  |  |  | require_once dirname(__FILE__) . '/../../cache/Cacher.php'; | 
					
						
							| 
									
										
										
										
											2012-10-19 20:15:26 +04:00
										 |  |  | require_once dirname(__FILE__) . '/../../exception/GeneralException.php'; | 
					
						
							| 
									
										
										
										
											2011-11-17 15:13:11 +04:00
										 |  |  | require_once dirname(__FILE__) . '/../../model/DbExpr.php'; | 
					
						
							|  |  |  | require_once dirname(__FILE__) . '/../../model/Db.php'; | 
					
						
							|  |  |  | require_once dirname(__FILE__) . '/../../model/MongoDbCommand.php'; | 
					
						
							|  |  |  | require_once dirname(__FILE__) . '/../../model/DbStatement.php'; | 
					
						
							|  |  |  | require_once dirname(__FILE__) . '/../../model/MongoStatement.php'; | 
					
						
							|  |  |  | require_once dirname(__FILE__) . '/../../model/DbDriver.php'; | 
					
						
							|  |  |  | require_once dirname(__FILE__) . '/../../model/NoSqlDbDriver.php'; | 
					
						
							|  |  |  | require_once dirname(__FILE__) . '/../../model/MongoDriver.php'; | 
					
						
							|  |  |  | require_once dirname(__FILE__) . '/../../model/Model.php'; | 
					
						
							|  |  |  | require_once dirname(__FILE__) . '/../../model/MongoModel.php'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class MongoModelTest extends PHPUnit_Framework_TestCase | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-19 20:15:26 +04:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @var MongoModel | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2011-11-17 15:13:11 +04:00
										 |  |  |     private $model; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-19 20:15:26 +04:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @var ReflectionMethod | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private $method_count; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @var ReflectionMethod | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private $method_fetch; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-17 15:13:11 +04:00
										 |  |  |     public function run(PHPUnit_Framework_TestResult $result = NULL) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->setPreserveGlobalState(false); | 
					
						
							|  |  |  |         return parent::run($result); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function setUp() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $conf = array('default' => array('driver' => 'MongoDriver', 'hostname' => 'localhost', 'database' => 'test', 'username' => 'test', 'password' => '1234', 'port' => 27017)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->dbSetUp($conf); | 
					
						
							| 
									
										
										
										
											2011-12-13 13:43:15 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-17 15:13:11 +04:00
										 |  |  |         Config::set('Db', $conf); | 
					
						
							|  |  |  |         if (!class_exists('MockModel')) { | 
					
						
							|  |  |  |             $this->model = $this->getMockForAbstractClass('MongoModel', array(), 'MongoMockModel'); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             $this->model = new MongoMockModel(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2012-10-19 20:15:26 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $model = new ReflectionClass('MongoModel'); | 
					
						
							|  |  |  |         $this->method_count = $model->getMethod('count'); | 
					
						
							|  |  |  |         $this->method_count->setAccessible(true); | 
					
						
							|  |  |  |         $this->method_fetch = $model->getMethod('fetch'); | 
					
						
							|  |  |  |         $this->method_fetch->setAccessible(true); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-17 15:13:11 +04:00
										 |  |  |         set_new_overload(array($this, 'newCallback')); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-06 16:53:16 +04:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @group Mongo | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2011-11-17 15:13:11 +04:00
										 |  |  |     public function testModel() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->assertInstanceOf('MongoMockModel', $this->model); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @runInSeparateProcess | 
					
						
							| 
									
										
										
										
											2011-12-06 16:53:16 +04:00
										 |  |  |      * @group Mongo | 
					
						
							| 
									
										
										
										
											2011-11-17 15:13:11 +04:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2012-10-19 20:15:26 +04:00
										 |  |  |     public function testFetch() | 
					
						
							| 
									
										
										
										
											2011-11-17 15:13:11 +04:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2012-07-09 13:47:32 +04:00
										 |  |  |         Config::set('DEBUG', false); | 
					
						
							| 
									
										
										
										
											2012-10-19 20:15:26 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $mock = $this->getMock('CacheKey', array('set', 'get')); | 
					
						
							|  |  |  |         $mock->expects($this->once()) | 
					
						
							|  |  |  |                 ->method('set') | 
					
						
							|  |  |  |                 ->will($this->returnValue(true)); | 
					
						
							|  |  |  |         $mock->expects($this->once()) | 
					
						
							|  |  |  |                 ->method('get') | 
					
						
							|  |  |  |                 ->will($this->returnValue(false)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $result = $this->method_fetch->invoke($this->model, array(), array('order' => array('name' => -1), 'limit' => 2), $mock); | 
					
						
							|  |  |  |         $this->assertInstanceOf('ArrayObject', $result); | 
					
						
							|  |  |  |         $this->assertEquals('milk', $result->name); | 
					
						
							|  |  |  |         $result = $this->method_fetch->invoke($this->model, array(), array('fields' => array('name'))); | 
					
						
							| 
									
										
										
										
											2011-12-13 13:43:15 +04:00
										 |  |  |         $this->assertSame('bread', $result->name); | 
					
						
							| 
									
										
										
										
											2012-10-19 20:15:26 +04:00
										 |  |  |         $result = $this->method_fetch->invoke($this->model, array()); | 
					
						
							| 
									
										
										
										
											2011-12-13 13:43:15 +04:00
										 |  |  |         $this->setExpectedException('PHPUnit_Framework_Error'); | 
					
						
							| 
									
										
										
										
											2012-10-19 20:15:26 +04:00
										 |  |  |         $this->assertNull($result->pounds); | 
					
						
							| 
									
										
										
										
											2011-11-17 15:13:11 +04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @runInSeparateProcess | 
					
						
							| 
									
										
										
										
											2011-12-06 16:53:16 +04:00
										 |  |  |      * @group Mongo | 
					
						
							| 
									
										
										
										
											2011-11-17 15:13:11 +04:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2012-10-19 20:15:26 +04:00
										 |  |  |     public function testFetchField() | 
					
						
							| 
									
										
										
										
											2011-11-17 15:13:11 +04:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2012-07-09 13:47:32 +04:00
										 |  |  |         Config::set('DEBUG', false); | 
					
						
							| 
									
										
										
										
											2012-10-19 20:15:26 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $mock = $this->getMock('CacheKey', array('set', 'get')); | 
					
						
							|  |  |  |         $mock->expects($this->exactly(2)) | 
					
						
							|  |  |  |                 ->method('set') | 
					
						
							|  |  |  |                 ->will($this->returnValue(true)); | 
					
						
							|  |  |  |         $mock->expects($this->exactly(2)) | 
					
						
							|  |  |  |                 ->method('get') | 
					
						
							|  |  |  |                 ->will($this->returnValue(false)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $model = new ReflectionClass('MongoModel'); | 
					
						
							|  |  |  |         $method = $model->getMethod('fetchField'); | 
					
						
							|  |  |  |         $method->setAccessible(true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $result = $method->invoke($this->model, array('name' => 'milk'), array(), 'quantity', $mock); | 
					
						
							|  |  |  |         $this->assertEquals(1, $result); | 
					
						
							|  |  |  |         $result = $method->invoke($this->model, array(), array('skip' => 2), 'quantity', $mock); | 
					
						
							|  |  |  |         $this->assertEquals($result, $this->method_fetch->invoke($this->model, array(), array('skip' => 2))->quantity); | 
					
						
							| 
									
										
										
										
											2011-11-17 15:13:11 +04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @runInSeparateProcess | 
					
						
							| 
									
										
										
										
											2011-12-06 16:53:16 +04:00
										 |  |  |      * @group Mongo | 
					
						
							| 
									
										
										
										
											2011-11-17 15:13:11 +04:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2012-10-19 20:15:26 +04:00
										 |  |  |     public function testFetchAll() | 
					
						
							| 
									
										
										
										
											2011-11-17 15:13:11 +04:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2012-07-09 13:47:32 +04:00
										 |  |  |         Config::set('DEBUG', false); | 
					
						
							| 
									
										
										
										
											2012-10-19 20:15:26 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $mock = $this->getMock('CacheKey', array('set', 'get')); | 
					
						
							|  |  |  |         $mock->expects($this->once()) | 
					
						
							|  |  |  |                 ->method('set') | 
					
						
							|  |  |  |                 ->will($this->returnValue(true)); | 
					
						
							|  |  |  |         $mock->expects($this->once()) | 
					
						
							|  |  |  |                 ->method('get') | 
					
						
							|  |  |  |                 ->will($this->returnValue(false)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $model = new ReflectionClass('MongoModel'); | 
					
						
							|  |  |  |         $method = $model->getMethod('fetchAll'); | 
					
						
							|  |  |  |         $method->setAccessible(true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $result = $method->invoke($this->model, array('name' => 'eggs'), array(), $mock); | 
					
						
							|  |  |  |         $this->assertEquals(2, count($result)); | 
					
						
							|  |  |  |         $result = $method->invoke($this->model, array(), array('skip' => 2)); | 
					
						
							|  |  |  |         $this->assertEquals(3, count($result)); | 
					
						
							|  |  |  |         $this->assertEquals('fish', $result[0]->name); | 
					
						
							|  |  |  |         $this->assertEquals('milk', $result[1]->name); | 
					
						
							|  |  |  |         $this->assertEquals('eggs', $result[2]->name); | 
					
						
							| 
									
										
										
										
											2011-11-17 15:13:11 +04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @runInSeparateProcess | 
					
						
							| 
									
										
										
										
											2011-12-06 16:53:16 +04:00
										 |  |  |      * @group Mongo | 
					
						
							| 
									
										
										
										
											2011-11-17 15:13:11 +04:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2012-10-19 20:15:26 +04:00
										 |  |  |     public function testFetchOrderParam() | 
					
						
							| 
									
										
										
										
											2012-01-18 23:30:34 +04:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2012-07-09 13:47:32 +04:00
										 |  |  |         Config::set('DEBUG', false); | 
					
						
							| 
									
										
										
										
											2012-10-19 20:15:26 +04:00
										 |  |  |         $result = $this->method_fetch->invoke($this->model, array(), array('order' => 'name')); | 
					
						
							|  |  |  |         $this->assertSame('bread', $result->name); | 
					
						
							|  |  |  |         $result = $this->method_fetch->invoke($this->model, array(), array('order' => array('name' => 1, 'quantity' => -1), 'skip' => 1)); | 
					
						
							|  |  |  |         $this->assertSame(2.1, $result->price); | 
					
						
							|  |  |  |         $this->setExpectedException('GeneralException', 'Wrong order parameter given to query.'); | 
					
						
							|  |  |  |         $this->method_fetch->invoke($this->model, array(), array('order' => 1)); | 
					
						
							| 
									
										
										
										
											2012-01-18 23:30:34 +04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @runInSeparateProcess | 
					
						
							|  |  |  |      * @group Mongo | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2012-10-19 20:15:26 +04:00
										 |  |  |     public function testFetchSkipLimitParam() | 
					
						
							| 
									
										
										
										
											2011-12-06 14:05:18 +04:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2012-07-09 13:47:32 +04:00
										 |  |  |         Config::set('DEBUG', false); | 
					
						
							| 
									
										
										
										
											2012-10-19 20:15:26 +04:00
										 |  |  |         $result = $this->method_fetch->invoke($this->model, array(), array('order' => 'name')); | 
					
						
							|  |  |  |         $this->assertSame('bread', $result->name); | 
					
						
							|  |  |  |         $result = $this->method_fetch->invoke($this->model, array(), array('order' => array('name' => 1, 'quantity' => -1), 'skip' => 1, 'limit' => 1)); | 
					
						
							|  |  |  |         $this->assertSame(2.1, $result->price); | 
					
						
							| 
									
										
										
										
											2011-12-06 14:05:18 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-19 20:15:26 +04:00
										 |  |  |         $model = new ReflectionClass('MongoModel'); | 
					
						
							|  |  |  |         $method = $model->getMethod('fetchAll'); | 
					
						
							|  |  |  |         $method->setAccessible(true); | 
					
						
							|  |  |  |         $this->assertCount(3, $method->invoke($this->model, array(), array('limit' => 3))); | 
					
						
							|  |  |  |         $this->assertCount(2, $method->invoke($this->model, array(), array('skip' => 3))); | 
					
						
							|  |  |  |         $this->assertCount(5, $method->invoke($this->model, array(), array())); | 
					
						
							| 
									
										
										
										
											2011-12-06 14:05:18 +04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @runInSeparateProcess | 
					
						
							| 
									
										
										
										
											2011-12-06 16:53:16 +04:00
										 |  |  |      * @group Mongo | 
					
						
							| 
									
										
										
										
											2011-12-06 14:05:18 +04:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2012-10-19 20:15:26 +04:00
										 |  |  |     public function testFetchFieldsParam() | 
					
						
							| 
									
										
										
										
											2011-11-22 16:29:00 +04:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2012-07-09 13:47:32 +04:00
										 |  |  |         Config::set('DEBUG', false); | 
					
						
							| 
									
										
										
										
											2012-10-19 20:15:26 +04:00
										 |  |  |         $result = $this->method_fetch->invoke($this->model, array(), array('fields' => 'name')); | 
					
						
							|  |  |  |         $this->assertTrue(!isset($result->quantity)); | 
					
						
							|  |  |  |         $result = $this->method_fetch->invoke($this->model, array(), array('fields' => array('name', 'price'))); | 
					
						
							|  |  |  |         $this->assertTrue(!isset($result->quantity)); | 
					
						
							|  |  |  |         $result = $this->method_fetch->invoke($this->model, array(), array('fields' => array('name' => 1, 'price' => 1))); | 
					
						
							|  |  |  |         $this->assertTrue(!isset($result->quantity)); | 
					
						
							|  |  |  |         $this->setExpectedException('GeneralException', 'Wrong fields parameter given to query.'); | 
					
						
							|  |  |  |         $this->method_fetch->invoke($this->model, array(), array('fields' => 1)); | 
					
						
							| 
									
										
										
										
											2011-11-22 16:29:00 +04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @runInSeparateProcess | 
					
						
							| 
									
										
										
										
											2011-12-06 16:53:16 +04:00
										 |  |  |      * @group Mongo | 
					
						
							| 
									
										
										
										
											2011-11-22 16:29:00 +04:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2012-10-19 20:15:26 +04:00
										 |  |  |     public function testGet() | 
					
						
							| 
									
										
										
										
											2011-11-17 15:13:11 +04:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2012-07-09 13:47:32 +04:00
										 |  |  |         Config::set('DEBUG', false); | 
					
						
							| 
									
										
										
										
											2012-10-19 20:15:26 +04:00
										 |  |  |         $result = $this->method_fetch->invoke($this->model, array(), array('order' => array('name' => 1))); | 
					
						
							|  |  |  |         $this->assertEquals('bread', $result->name); | 
					
						
							|  |  |  |         $id = $result->_id; | 
					
						
							|  |  |  |         $this->assertEquals(10, $this->model->get($id)->quantity); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-11-17 15:13:11 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-19 20:15:26 +04:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @runInSeparateProcess | 
					
						
							|  |  |  |      * @group Mongo | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function testDelete() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         Config::set('DEBUG', false); | 
					
						
							|  |  |  |         $result = $this->method_fetch->invoke($this->model, array(), array('order' => array('name' => 1))); | 
					
						
							|  |  |  |         $id = $result->_id; | 
					
						
							|  |  |  |         $this->assertEquals(1, $this->model->delete($id)); | 
					
						
							|  |  |  |         $this->assertFalse($this->method_fetch->invoke($this->model, array('name' => 'bread'))); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-11-17 15:13:11 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-19 20:15:26 +04:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @runInSeparateProcess | 
					
						
							|  |  |  |      * @group Mongo | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function testUpdate() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         Config::set('DEBUG', false); | 
					
						
							|  |  |  |         $this->model->insert(array('name' => 'testbread', 'price' => 3.2, 'quantity' => 10)); | 
					
						
							|  |  |  |         $result = $this->method_fetch->invoke($this->model, array('name' => 'testbread')); | 
					
						
							|  |  |  |         $this->assertEquals(10, $result->quantity); | 
					
						
							|  |  |  |         $this->model->update(array('$set' => array('quantity' => 3)), $result->_id); | 
					
						
							|  |  |  |         $this->assertEquals(3, $this->model->get($result->_id)->quantity); | 
					
						
							|  |  |  |         $this->model->update(array('$set' => array('quantity' => 13)), (string)$result->_id); | 
					
						
							|  |  |  |         $this->assertEquals(13, $this->model->get($result->_id)->quantity); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-11-17 15:13:11 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-19 20:15:26 +04:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @runInSeparateProcess | 
					
						
							|  |  |  |      * @group Mongo | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function testBatchInsert() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         Config::set('DEBUG', false); | 
					
						
							|  |  |  |         $data = array( | 
					
						
							|  |  |  |             array('name' => 'first object'), | 
					
						
							|  |  |  |             array('name' => 'second object'), | 
					
						
							|  |  |  |             array('name' => 'equal object'), | 
					
						
							|  |  |  |             array('name' => 'equal object') | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |         $this->model->batchInsert($data); | 
					
						
							|  |  |  |         $this->assertEquals(1, $this->method_count->invoke($this->model, array('name' => 'first object'))); | 
					
						
							|  |  |  |         $this->assertEquals(2, $this->method_count->invoke($this->model, array('name' => 'equal object'))); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-11-17 15:13:11 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-19 20:15:26 +04:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @runInSeparateProcess | 
					
						
							|  |  |  |      * @group Mongo | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function testCount() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         Config::set('DEBUG', false); | 
					
						
							|  |  |  |         $this->assertEquals(5, $this->method_count->invoke($this->model)); | 
					
						
							|  |  |  |         $this->assertEquals(2, $this->method_count->invoke($this->model, array('name' => 'eggs'))); | 
					
						
							| 
									
										
										
										
											2011-11-17 15:13:11 +04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-13 13:43:15 +04:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @runInSeparateProcess | 
					
						
							|  |  |  |      * @group Mongo | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function testUseMongoId() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2012-07-09 13:47:32 +04:00
										 |  |  |         Config::set('DEBUG', false); | 
					
						
							| 
									
										
										
										
											2011-12-13 13:43:15 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $this->assertAttributeEquals(true, 'useMongoId', $this->model); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @runInSeparateProcess | 
					
						
							|  |  |  |      * @group Mongo | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function testId() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2012-07-09 13:47:32 +04:00
										 |  |  |         Config::set('DEBUG', false); | 
					
						
							| 
									
										
										
										
											2011-12-13 13:43:15 +04:00
										 |  |  |         $class = new ReflectionClass('MongoModel'); | 
					
						
							|  |  |  |         $prop = $class->getProperty('useMongoId'); | 
					
						
							|  |  |  |         $prop->setAccessible(true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->model->insert(array('_id' => 1, 'name' => 'testbread', 'price' => 3.2, 'quantity' => 10)); | 
					
						
							|  |  |  |         $this->model->insert(array('_id' => 2, 'name' => 'testbread', 'price' => 12, 'quantity' => 2)); | 
					
						
							| 
									
										
										
										
											2012-10-19 20:15:26 +04:00
										 |  |  |         $this->assertSame(2, $this->method_count->invoke($this->model, array('name' => 'testbread'))); | 
					
						
							| 
									
										
										
										
											2012-01-18 23:30:34 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-13 13:43:15 +04:00
										 |  |  |         $prop->setValue($this->model, false); | 
					
						
							|  |  |  |         $this->model->delete(1); | 
					
						
							| 
									
										
										
										
											2012-10-19 20:15:26 +04:00
										 |  |  |         $this->assertSame(1, $this->method_count->invoke($this->model, array('name' => 'testbread'))); | 
					
						
							| 
									
										
										
										
											2011-12-13 13:43:15 +04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @runInSeparateProcess | 
					
						
							|  |  |  |      * @group Mongo | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function testIdToMongoId() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2012-07-09 13:47:32 +04:00
										 |  |  |         Config::set('DEBUG', false); | 
					
						
							| 
									
										
										
										
											2011-12-13 13:43:15 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $this->model->insert(array('name' => 'testbread', 'price' => 3.2, 'quantity' => 10)); | 
					
						
							|  |  |  |         $this->model->insert(array('name' => 'testbread', 'price' => 12, 'quantity' => 2)); | 
					
						
							| 
									
										
										
										
											2012-10-19 20:15:26 +04:00
										 |  |  |         $this->assertSame(2, $this->method_count->invoke($this->model, array('name' => 'testbread'))); | 
					
						
							|  |  |  |         $id = $this->method_fetch->invoke($this->model, array('name' => 'testbread'))->_id->__toString(); | 
					
						
							| 
									
										
										
										
											2011-12-13 13:43:15 +04:00
										 |  |  |         $this->assertInternalType('string', $id); | 
					
						
							|  |  |  |         $item = $this->model->get($id); | 
					
						
							|  |  |  |         $this->assertSame('testbread', $item->name); | 
					
						
							|  |  |  |         $this->model->delete($id); | 
					
						
							| 
									
										
										
										
											2012-10-19 20:15:26 +04:00
										 |  |  |         $this->assertSame(1, $this->method_count->invoke($this->model, array('name' => 'testbread'))); | 
					
						
							| 
									
										
										
										
											2011-12-13 13:43:15 +04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-17 15:13:11 +04:00
										 |  |  |     public function tearDown() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $conf = array('driver' => 'MongoDriver', 'hostname' => 'localhost', 'database' => 'test', 'username' => 'test', 'password' => '1234', 'port' => 27017); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $connection = new Mongo('mongodb://' . $conf['hostname'] . ':' . $conf['port']); | 
					
						
							|  |  |  |         $db = $connection->selectDB($conf['database']); | 
					
						
							|  |  |  |         $db->authenticate($conf['username'], $conf['password']); | 
					
						
							|  |  |  |         $collection = 'mongomock'; | 
					
						
							| 
									
										
										
										
											2012-10-19 20:15:26 +04:00
										 |  |  |         $db->selectCollection($collection)->remove(array()); | 
					
						
							| 
									
										
										
										
											2011-11-17 15:13:11 +04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     protected function newCallback($className) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         switch ($className) { | 
					
						
							|  |  |  |             case 'CacheKey': | 
					
						
							|  |  |  |                 return 'MockCacheKey'; | 
					
						
							|  |  |  |             default: | 
					
						
							|  |  |  |                 return $className; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function dbSetUp($conf) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $data = array( | 
					
						
							|  |  |  |             array( | 
					
						
							|  |  |  |                 'name' => 'bread', | 
					
						
							|  |  |  |                 'price' => 3.2, | 
					
						
							|  |  |  |                 'quantity' => 10 | 
					
						
							|  |  |  |             ), | 
					
						
							|  |  |  |             array( | 
					
						
							|  |  |  |                 'name' => 'eggs', | 
					
						
							|  |  |  |                 'price' => 2.1, | 
					
						
							|  |  |  |                 'quantity' => 20 | 
					
						
							|  |  |  |             ), | 
					
						
							|  |  |  |             array( | 
					
						
							|  |  |  |                 'name' => 'fish', | 
					
						
							|  |  |  |                 'price' => 13.2, | 
					
						
							|  |  |  |                 'quantity' => 2 | 
					
						
							|  |  |  |             ), | 
					
						
							|  |  |  |             array( | 
					
						
							|  |  |  |                 'name' => 'milk', | 
					
						
							|  |  |  |                 'price' => 3.8, | 
					
						
							|  |  |  |                 'quantity' => 1 | 
					
						
							|  |  |  |             ), | 
					
						
							|  |  |  |             array( | 
					
						
							|  |  |  |                 'name' => 'eggs', | 
					
						
							|  |  |  |                 'price' => 2.3, | 
					
						
							|  |  |  |                 'quantity' => 5 | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |         $connection = new Mongo('mongodb://' . $conf['default']['hostname'] . ':' . $conf['default']['port']); | 
					
						
							|  |  |  |         $db = $connection->selectDB($conf['default']['database']); | 
					
						
							|  |  |  |         $db->authenticate($conf['default']['username'], $conf['default']['password']); | 
					
						
							|  |  |  |         $collection = 'mongomock'; | 
					
						
							|  |  |  |         $db->dropCollection($collection); | 
					
						
							|  |  |  |         $collection = $db->selectCollection($collection); | 
					
						
							| 
									
										
										
										
											2011-12-13 13:43:15 +04:00
										 |  |  |         foreach ($data as $document) { | 
					
						
							| 
									
										
										
										
											2011-11-17 15:13:11 +04:00
										 |  |  |             $collection->insert($document); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |