DriverManagerTest.php 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace Doctrine\Tests\DBAL;
  3. require_once __DIR__ . '/../TestInit.php';
  4. class DriverManagerTest extends \Doctrine\Tests\DbalTestCase
  5. {
  6. /**
  7. * @expectedException \Doctrine\DBAL\DBALException
  8. */
  9. public function testInvalidPdoInstance()
  10. {
  11. $options = array(
  12. 'pdo' => 'test'
  13. );
  14. $test = \Doctrine\DBAL\DriverManager::getConnection($options);
  15. }
  16. public function testValidPdoInstance()
  17. {
  18. $options = array(
  19. 'pdo' => new \PDO('sqlite::memory:')
  20. );
  21. $conn = \Doctrine\DBAL\DriverManager::getConnection($options);
  22. $this->assertEquals('sqlite', $conn->getDatabasePlatform()->getName());
  23. }
  24. /**
  25. * @group DBAL-32
  26. */
  27. public function testPdoInstanceSetErrorMode()
  28. {
  29. $pdo = new \PDO('sqlite::memory:');
  30. $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_SILENT);
  31. $options = array(
  32. 'pdo' => $pdo
  33. );
  34. $conn = \Doctrine\DBAL\DriverManager::getConnection($options);
  35. $this->assertEquals(\PDO::ERRMODE_EXCEPTION, $pdo->getAttribute(\PDO::ATTR_ERRMODE));
  36. }
  37. /**
  38. * @expectedException \Doctrine\DBAL\DBALException
  39. */
  40. public function testCheckParams()
  41. {
  42. $conn = \Doctrine\DBAL\DriverManager::getConnection(array());
  43. }
  44. /**
  45. * @expectedException \Doctrine\DBAL\DBALException
  46. */
  47. public function testInvalidDriver()
  48. {
  49. $conn = \Doctrine\DBAL\DriverManager::getConnection(array('driver' => 'invalid_driver'));
  50. }
  51. public function testCustomPlatform()
  52. {
  53. $mockPlatform = new \Doctrine\Tests\DBAL\Mocks\MockPlatform();
  54. $options = array(
  55. 'pdo' => new \PDO('sqlite::memory:'),
  56. 'platform' => $mockPlatform
  57. );
  58. $conn = \Doctrine\DBAL\DriverManager::getConnection($options);
  59. $this->assertSame($mockPlatform, $conn->getDatabasePlatform());
  60. }
  61. public function testCustomWrapper()
  62. {
  63. $wrapperClass = 'Doctrine\Tests\Mocks\ConnectionMock';
  64. $options = array(
  65. 'pdo' => new \PDO('sqlite::memory:'),
  66. 'wrapperClass' => $wrapperClass,
  67. );
  68. $conn = \Doctrine\DBAL\DriverManager::getConnection($options);
  69. $this->assertInstanceOf($wrapperClass, $conn);
  70. }
  71. public function testInvalidWrapperClass()
  72. {
  73. $this->setExpectedException('\Doctrine\DBAL\DBALException');
  74. $options = array(
  75. 'pdo' => new \PDO('sqlite::memory:'),
  76. 'wrapperClass' => 'stdClass',
  77. );
  78. $conn = \Doctrine\DBAL\DriverManager::getConnection($options);
  79. }
  80. public function testInvalidDriverClass()
  81. {
  82. $this->setExpectedException('\Doctrine\DBAL\DBALException');
  83. $options = array(
  84. 'driverClass' => 'stdClass'
  85. );
  86. $conn = \Doctrine\DBAL\DriverManager::getConnection($options);
  87. }
  88. public function testValidDriverClass()
  89. {
  90. $options = array(
  91. 'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
  92. );
  93. $conn = \Doctrine\DBAL\DriverManager::getConnection($options);
  94. $this->assertInstanceOf('Doctrine\DBAL\Driver\PDOMySql\Driver', $conn->getDriver());
  95. }
  96. }