You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
2.3 KiB

13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
  1. <?php
  2. /*
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage UnitTests
  7. * @since 2011-11-1
  8. *
  9. * Unit tests for Db class
  10. */
  11. require_once dirname(__FILE__) . '/../../Registry.php';
  12. require_once dirname(__FILE__) . '/../../Config.php';
  13. require_once dirname(__FILE__) . '/../../model/DbDriver.php';
  14. require_once dirname(__FILE__) . '/../../model/Db.php';
  15. require_once dirname(__FILE__) . '/../../exception/GeneralException.php';
  16. require_once dirname(__FILE__) . '/../../exception/InitializationException.php';
  17. class DbTest extends PHPUnit_Framework_TestCase
  18. {
  19. /**
  20. * @expectedExceptionMessage Trying to get property of non-object
  21. */
  22. public function testConnectNoParams()
  23. {
  24. $this->setExpectedException('InitializationException');
  25. Db::connect();
  26. }
  27. /**
  28. * @expectedExceptionMessage Connection parameters must be an array
  29. */
  30. public function testConnectConfigNotArray()
  31. {
  32. $this->setExpectedException('InitializationException');
  33. Db::connect('name', 'config');
  34. }
  35. public function testConnectGlobalConfig()
  36. {
  37. $conf = array('hostname' => 'localhost', 'driver' => 'MySQLiDriverGlobalConfMock', 'database' => 'db', 'username' => 'test', 'password' => '1234');
  38. $this->getMockForAbstractClass('DbDriver', array(), 'MySQLiDriverGlobalConfMock', false);
  39. Config::set('Db', array('global' =>$conf));
  40. $driver = Db::connect('global');
  41. $this->assertInstanceOf('DbDriver', $driver);
  42. }
  43. public function testConnectWithConfigParam()
  44. {
  45. $conf = array('hostname' => 'localhost', 'driver' => 'MySQLiDriverMock', 'database' => 'db', 'username' => 'test', 'password' => '1234');
  46. $this->getMockForAbstractClass('DbDriver', array(), 'MySQLiDriverMock', false);
  47. $driver = Db::connect('mysql', $conf);
  48. $this->assertInstanceOf('DbDriver', $driver);
  49. }
  50. /**
  51. * @expectedExceptionMessage Database driver must extends DbDriver
  52. */
  53. public function testConnectWithWrongDriver()
  54. {
  55. $this->getMock('NotDbDriver', array(), array(), 'NoDbDriverMock');
  56. $this->setExpectedException('InitializationException');
  57. $driver = Db::connect('nodb', array('hostname' => 'localhost', 'driver' => 'NoDbDriverMock'));
  58. }
  59. }