DatabaseConnectionFactoryTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace Illuminate\Tests\Database;
  3. use Illuminate\Container\Container;
  4. use Illuminate\Database\Capsule\Manager as DB;
  5. use Illuminate\Database\Connectors\ConnectionFactory;
  6. use InvalidArgumentException;
  7. use Mockery as m;
  8. use PDO;
  9. use PHPUnit\Framework\TestCase;
  10. use ReflectionProperty;
  11. class DatabaseConnectionFactoryTest extends TestCase
  12. {
  13. protected $db;
  14. protected function setUp(): void
  15. {
  16. $this->db = new DB;
  17. $this->db->addConnection([
  18. 'driver' => 'sqlite',
  19. 'database' => ':memory:',
  20. ]);
  21. $this->db->addConnection([
  22. 'url' => 'sqlite:///:memory:',
  23. ], 'url');
  24. $this->db->addConnection([
  25. 'driver' => 'sqlite',
  26. 'read' => [
  27. 'database' => ':memory:',
  28. ],
  29. 'write' => [
  30. 'database' => ':memory:',
  31. ],
  32. ], 'read_write');
  33. $this->db->setAsGlobal();
  34. }
  35. protected function tearDown(): void
  36. {
  37. m::close();
  38. }
  39. public function testConnectionCanBeCreated()
  40. {
  41. $this->assertInstanceOf(PDO::class, $this->db->getConnection()->getPdo());
  42. $this->assertInstanceOf(PDO::class, $this->db->getConnection()->getReadPdo());
  43. $this->assertInstanceOf(PDO::class, $this->db->getConnection('read_write')->getPdo());
  44. $this->assertInstanceOf(PDO::class, $this->db->getConnection('read_write')->getReadPdo());
  45. $this->assertInstanceOf(PDO::class, $this->db->getConnection('url')->getPdo());
  46. $this->assertInstanceOf(PDO::class, $this->db->getConnection('url')->getReadPdo());
  47. }
  48. public function testConnectionFromUrlHasProperConfig()
  49. {
  50. $this->db->addConnection([
  51. 'url' => 'mysql://root:pass@db/local?strict=true',
  52. 'unix_socket' => '',
  53. 'charset' => 'utf8mb4',
  54. 'collation' => 'utf8mb4_unicode_ci',
  55. 'prefix' => '',
  56. 'prefix_indexes' => true,
  57. 'strict' => false,
  58. 'engine' => null,
  59. ], 'url-config');
  60. $this->assertEquals([
  61. 'name' => 'url-config',
  62. 'driver' => 'mysql',
  63. 'database' => 'local',
  64. 'host' => 'db',
  65. 'username' => 'root',
  66. 'password' => 'pass',
  67. 'unix_socket' => '',
  68. 'charset' => 'utf8mb4',
  69. 'collation' => 'utf8mb4_unicode_ci',
  70. 'prefix' => '',
  71. 'prefix_indexes' => true,
  72. 'strict' => true,
  73. 'engine' => null,
  74. ], $this->db->getConnection('url-config')->getConfig());
  75. }
  76. public function testSingleConnectionNotCreatedUntilNeeded()
  77. {
  78. $connection = $this->db->getConnection();
  79. $pdo = new ReflectionProperty(get_class($connection), 'pdo');
  80. $pdo->setAccessible(true);
  81. $readPdo = new ReflectionProperty(get_class($connection), 'readPdo');
  82. $readPdo->setAccessible(true);
  83. $this->assertNotInstanceOf(PDO::class, $pdo->getValue($connection));
  84. $this->assertNotInstanceOf(PDO::class, $readPdo->getValue($connection));
  85. }
  86. public function testReadWriteConnectionsNotCreatedUntilNeeded()
  87. {
  88. $connection = $this->db->getConnection('read_write');
  89. $pdo = new ReflectionProperty(get_class($connection), 'pdo');
  90. $pdo->setAccessible(true);
  91. $readPdo = new ReflectionProperty(get_class($connection), 'readPdo');
  92. $readPdo->setAccessible(true);
  93. $this->assertNotInstanceOf(PDO::class, $pdo->getValue($connection));
  94. $this->assertNotInstanceOf(PDO::class, $readPdo->getValue($connection));
  95. }
  96. public function testIfDriverIsntSetExceptionIsThrown()
  97. {
  98. $this->expectException(InvalidArgumentException::class);
  99. $this->expectExceptionMessage('A driver must be specified.');
  100. $factory = new ConnectionFactory($container = m::mock(Container::class));
  101. $factory->createConnector(['foo']);
  102. }
  103. public function testExceptionIsThrownOnUnsupportedDriver()
  104. {
  105. $this->expectException(InvalidArgumentException::class);
  106. $this->expectExceptionMessage('Unsupported driver [foo]');
  107. $factory = new ConnectionFactory($container = m::mock(Container::class));
  108. $container->shouldReceive('bound')->once()->andReturn(false);
  109. $factory->createConnector(['driver' => 'foo']);
  110. }
  111. public function testCustomConnectorsCanBeResolvedViaContainer()
  112. {
  113. $factory = new ConnectionFactory($container = m::mock(Container::class));
  114. $container->shouldReceive('bound')->once()->with('db.connector.foo')->andReturn(true);
  115. $container->shouldReceive('make')->once()->with('db.connector.foo')->andReturn('connector');
  116. $this->assertSame('connector', $factory->createConnector(['driver' => 'foo']));
  117. }
  118. public function testSqliteForeignKeyConstraints()
  119. {
  120. $this->db->addConnection([
  121. 'url' => 'sqlite:///:memory:?foreign_key_constraints=true',
  122. ], 'constraints_set');
  123. $this->assertEquals(0, $this->db->getConnection()->select('PRAGMA foreign_keys')[0]->foreign_keys);
  124. $this->assertEquals(1, $this->db->getConnection('constraints_set')->select('PRAGMA foreign_keys')[0]->foreign_keys);
  125. }
  126. }