| 
									
										
										
										
											2011-11-01 19:23:25 +04:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* | 
					
						
							|  |  |  |  * @copyright NetMonsters <team@netmonsters.ru> | 
					
						
							|  |  |  |  * @link http://netmonsters.ru | 
					
						
							|  |  |  |  * @package Majestic | 
					
						
							|  |  |  |  * @subpackage UnitTests | 
					
						
							|  |  |  |  * @since 2011-11-1 | 
					
						
							|  |  |  |  *  | 
					
						
							|  |  |  |  * Unit tests for Db class | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | require_once dirname(__FILE__) . '/../../Registry.php'; | 
					
						
							|  |  |  | require_once dirname(__FILE__) . '/../../Config.php'; | 
					
						
							| 
									
										
										
										
											2011-11-03 16:52:33 +04:00
										 |  |  | require_once dirname(__FILE__) . '/../../model/DbDriver.php'; | 
					
						
							| 
									
										
										
										
											2011-11-01 19:23:25 +04:00
										 |  |  | require_once dirname(__FILE__) . '/../../model/Db.php'; | 
					
						
							| 
									
										
										
										
											2011-11-25 19:50:41 +04:00
										 |  |  | require_once dirname(__FILE__) . '/../../exception/GeneralException.php'; | 
					
						
							| 
									
										
										
										
											2011-11-30 13:26:36 +04:00
										 |  |  | require_once dirname(__FILE__) . '/../../exception/InitializationException.php'; | 
					
						
							| 
									
										
										
										
											2011-11-01 19:23:25 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | class DbTest extends PHPUnit_Framework_TestCase | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     public function testConnectNoParams() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2011-12-01 17:35:13 +04:00
										 |  |  |        $this->setExpectedException('InitializationException', 'Trying to get property of non-object'); | 
					
						
							| 
									
										
										
										
											2011-11-30 13:26:36 +04:00
										 |  |  |        Db::connect(); | 
					
						
							| 
									
										
										
										
											2011-11-01 19:23:25 +04:00
										 |  |  |     } | 
					
						
							|  |  |  |     public function testConnectConfigNotArray() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2011-12-01 17:35:13 +04:00
										 |  |  |         $this->setExpectedException('InitializationException', 'Connection parameters must be an array'); | 
					
						
							| 
									
										
										
										
											2011-11-01 19:23:25 +04:00
										 |  |  |         Db::connect('name', 'config'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-05 13:34:49 +04:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @group MySQL | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2011-11-01 19:23:25 +04:00
										 |  |  |     public function testConnectGlobalConfig() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2011-11-03 16:52:33 +04:00
										 |  |  |         $conf = array('hostname' => 'localhost', 'driver' => 'MySQLiDriverGlobalConfMock', 'database' => 'db', 'username' => 'test', 'password' => '1234'); | 
					
						
							|  |  |  |         $this->getMockForAbstractClass('DbDriver', array(), 'MySQLiDriverGlobalConfMock', false); | 
					
						
							|  |  |  |         Config::set('Db', array('global' =>$conf)); | 
					
						
							| 
									
										
										
										
											2011-11-30 13:26:36 +04:00
										 |  |  |         $driver = Db::connect('global'); | 
					
						
							|  |  |  |         $this->assertInstanceOf('DbDriver', $driver); | 
					
						
							| 
									
										
										
										
											2011-11-01 19:23:25 +04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-05 13:34:49 +04:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @group MySQL | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2011-11-01 19:23:25 +04:00
										 |  |  |     public function testConnectWithConfigParam() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2011-11-03 16:52:33 +04:00
										 |  |  |         $conf = array('hostname' => 'localhost', 'driver' => 'MySQLiDriverMock', 'database' => 'db', 'username' => 'test', 'password' => '1234'); | 
					
						
							|  |  |  |         $this->getMockForAbstractClass('DbDriver', array(), 'MySQLiDriverMock', false); | 
					
						
							|  |  |  |         $driver = Db::connect('mysql', $conf); | 
					
						
							| 
									
										
										
										
											2011-11-01 19:23:25 +04:00
										 |  |  |         $this->assertInstanceOf('DbDriver', $driver); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-12-01 17:35:13 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-01 19:23:25 +04:00
										 |  |  |     public function testConnectWithWrongDriver() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->getMock('NotDbDriver', array(), array(), 'NoDbDriverMock'); | 
					
						
							| 
									
										
										
										
											2011-12-02 12:25:47 +04:00
										 |  |  |         $this->setExpectedException('InitializationException', 'Database driver must extends DbDriver'); | 
					
						
							| 
									
										
										
										
											2011-11-01 19:23:25 +04:00
										 |  |  |         $driver = Db::connect('nodb', array('hostname' => 'localhost', 'driver' => 'NoDbDriverMock')); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |