SqliteTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 3.0.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Database\Driver;
  17. use Cake\Database\Connection;
  18. use Cake\Database\Driver\Sqlite;
  19. use Cake\Database\DriverInterface;
  20. use Cake\Datasource\ConnectionManager;
  21. use Cake\TestSuite\TestCase;
  22. use PDO;
  23. /**
  24. * Tests Sqlite driver
  25. */
  26. class SqliteTest extends TestCase
  27. {
  28. public function tearDown(): void
  29. {
  30. parent::tearDown();
  31. ConnectionManager::drop('test_shared_cache');
  32. ConnectionManager::drop('test_shared_cache2');
  33. }
  34. /**
  35. * Test connecting to Sqlite with default configuration
  36. */
  37. public function testConnectionConfigDefault(): void
  38. {
  39. $driver = $this->getMockBuilder('Cake\Database\Driver\Sqlite')
  40. ->onlyMethods(['_connect'])
  41. ->getMock();
  42. $dsn = 'sqlite::memory:';
  43. $expected = [
  44. 'persistent' => false,
  45. 'database' => ':memory:',
  46. 'encoding' => 'utf8',
  47. 'cache' => null,
  48. 'mode' => null,
  49. 'username' => null,
  50. 'password' => null,
  51. 'flags' => [],
  52. 'init' => [],
  53. 'mask' => 420,
  54. ];
  55. $expected['flags'] += [
  56. PDO::ATTR_PERSISTENT => false,
  57. PDO::ATTR_EMULATE_PREPARES => false,
  58. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  59. ];
  60. $driver->expects($this->once())->method('_connect')
  61. ->with($dsn, $expected);
  62. $driver->connect([]);
  63. }
  64. /**
  65. * Test connecting to Sqlite with custom configuration
  66. */
  67. public function testConnectionConfigCustom(): void
  68. {
  69. $config = [
  70. 'persistent' => true,
  71. 'host' => 'foo',
  72. 'database' => 'bar.db',
  73. 'flags' => [1 => true, 2 => false],
  74. 'encoding' => 'a-language',
  75. 'init' => ['Execute this', 'this too'],
  76. 'mask' => 0666,
  77. ];
  78. $driver = $this->getMockBuilder('Cake\Database\driver\Sqlite')
  79. ->onlyMethods(['_connect', 'getConnection'])
  80. ->setConstructorArgs([$config])
  81. ->getMock();
  82. $dsn = 'sqlite:bar.db';
  83. $expected = $config;
  84. $expected += ['username' => null, 'password' => null, 'cache' => null, 'mode' => null];
  85. $expected['flags'] += [
  86. PDO::ATTR_PERSISTENT => true,
  87. PDO::ATTR_EMULATE_PREPARES => false,
  88. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  89. ];
  90. $connection = $this->getMockBuilder('StdClass')
  91. ->addMethods(['exec'])
  92. ->getMock();
  93. $connection->expects($this->exactly(2))
  94. ->method('exec')
  95. ->withConsecutive(['Execute this'], ['this too']);
  96. $driver->expects($this->once())->method('_connect')
  97. ->with($dsn, $expected);
  98. $driver->expects($this->any())->method('getConnection')
  99. ->will($this->returnValue($connection));
  100. $driver->connect($config);
  101. }
  102. /**
  103. * Tests creating multiple connections to same db.
  104. */
  105. public function testConnectionSharedCached()
  106. {
  107. $this->skipIf(PHP_VERSION_ID < 80100 || !extension_loaded('pdo_sqlite'), 'Skipping as SQLite extension is missing');
  108. ConnectionManager::setConfig('test_shared_cache', [
  109. 'className' => Connection::class,
  110. 'driver' => Sqlite::class,
  111. 'database' => ':memory:',
  112. 'cache' => 'shared',
  113. ]);
  114. $connection = ConnectionManager::get('test_shared_cache');
  115. $this->assertSame([], $connection->getSchemaCollection()->listTables());
  116. $connection->execute('CREATE TABLE test (test int);');
  117. $this->assertSame(['test'], $connection->getSchemaCollection()->listTables());
  118. ConnectionManager::setConfig('test_shared_cache2', [
  119. 'className' => Connection::class,
  120. 'driver' => Sqlite::class,
  121. 'database' => ':memory:',
  122. 'cache' => 'shared',
  123. ]);
  124. $connection = ConnectionManager::get('test_shared_cache2');
  125. $this->assertSame(['test'], $connection->getSchemaCollection()->listTables());
  126. $this->assertFileDoesNotExist('file::memory:?cache=shared');
  127. }
  128. /**
  129. * Data provider for schemaValue()
  130. *
  131. * @return array
  132. */
  133. public static function schemaValueProvider(): array
  134. {
  135. return [
  136. [null, 'NULL'],
  137. [false, 'FALSE'],
  138. [true, 'TRUE'],
  139. [3.14159, '3.14159'],
  140. ['33', '33'],
  141. [66, 66],
  142. [0, 0],
  143. [10e5, '1000000'],
  144. ['farts', '"farts"'],
  145. ];
  146. }
  147. /**
  148. * Test the schemaValue method on Driver.
  149. *
  150. * @dataProvider schemaValueProvider
  151. * @param mixed $input
  152. * @param mixed $expected
  153. */
  154. public function testSchemaValue($input, $expected): void
  155. {
  156. $driver = new Sqlite();
  157. $mock = $this->getMockBuilder(PDO::class)
  158. ->onlyMethods(['quote'])
  159. ->addMethods(['quoteIdentifier'])
  160. ->disableOriginalConstructor()
  161. ->getMock();
  162. $mock->expects($this->any())
  163. ->method('quote')
  164. ->will($this->returnCallback(function ($value) {
  165. return '"' . $value . '"';
  166. }));
  167. $driver->setConnection($mock);
  168. $this->assertEquals($expected, $driver->schemaValue($input));
  169. }
  170. /**
  171. * Tests driver-specific feature support check.
  172. */
  173. public function testSupports(): void
  174. {
  175. $driver = ConnectionManager::get('test')->getDriver();
  176. $this->skipIf(!$driver instanceof Sqlite);
  177. $featureVersions = [
  178. 'cte' => '3.8.3',
  179. 'window' => '3.28.0',
  180. ];
  181. $this->assertSame(
  182. version_compare($driver->version(), $featureVersions['cte'], '>='),
  183. $driver->supports(DriverInterface::FEATURE_CTE)
  184. );
  185. $this->assertSame(
  186. version_compare($driver->version(), $featureVersions['window'], '>='),
  187. $driver->supports(DriverInterface::FEATURE_WINDOW)
  188. );
  189. $this->assertFalse($driver->supports(DriverInterface::FEATURE_JSON));
  190. $this->assertTrue($driver->supports(DriverInterface::FEATURE_SAVEPOINT));
  191. $this->assertTrue($driver->supports(DriverInterface::FEATURE_QUOTE));
  192. $this->assertFalse($driver->supports('this-is-fake'));
  193. }
  194. /**
  195. * Tests driver-specific feature support check.
  196. */
  197. public function testDeprecatedSupports(): void
  198. {
  199. $driver = ConnectionManager::get('test')->getDriver();
  200. $this->skipIf(!$driver instanceof Sqlite);
  201. $this->deprecated(function () use ($driver) {
  202. $this->assertSame($driver->supportsCTEs(), $driver->supports(DriverInterface::FEATURE_CTE));
  203. $this->assertSame($driver->supportsWindowFunctions(), $driver->supports(DriverInterface::FEATURE_WINDOW));
  204. });
  205. }
  206. }