SqliteTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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\DriverFeatureEnum;
  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(['createPdo'])
  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. 'log' => false,
  55. ];
  56. $expected['flags'] += [
  57. PDO::ATTR_PERSISTENT => false,
  58. PDO::ATTR_EMULATE_PREPARES => false,
  59. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  60. ];
  61. $driver->expects($this->once())->method('createPdo')
  62. ->with($dsn, $expected);
  63. $driver->connect([]);
  64. }
  65. /**
  66. * Test connecting to Sqlite with custom configuration
  67. */
  68. public function testConnectionConfigCustom(): void
  69. {
  70. $config = [
  71. 'persistent' => true,
  72. 'host' => 'foo',
  73. 'database' => 'bar.db',
  74. 'flags' => [1 => true, 2 => false],
  75. 'encoding' => 'a-language',
  76. 'init' => ['Execute this', 'this too'],
  77. 'mask' => 0666,
  78. ];
  79. $driver = $this->getMockBuilder('Cake\Database\driver\Sqlite')
  80. ->onlyMethods(['createPdo'])
  81. ->setConstructorArgs([$config])
  82. ->getMock();
  83. $dsn = 'sqlite:bar.db';
  84. $expected = $config;
  85. $expected += ['username' => null, 'password' => null, 'cache' => null, 'mode' => null, 'log' => false];
  86. $expected['flags'] += [
  87. PDO::ATTR_PERSISTENT => true,
  88. PDO::ATTR_EMULATE_PREPARES => false,
  89. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  90. ];
  91. $connection = $this->getMockBuilder('PDO')
  92. ->disableOriginalConstructor()
  93. ->onlyMethods(['exec'])
  94. ->getMock();
  95. $connection->expects($this->exactly(2))
  96. ->method('exec')
  97. ->with(
  98. ...self::withConsecutive(['Execute this'], ['this too'])
  99. );
  100. $driver->expects($this->once())->method('createPdo')
  101. ->with($dsn, $expected)
  102. ->willReturn($connection);
  103. $driver->connect($config);
  104. }
  105. /**
  106. * Tests creating multiple connections to same db.
  107. */
  108. public function testConnectionSharedCached()
  109. {
  110. $this->skipIf(!extension_loaded('pdo_sqlite'), 'Skipping as SQLite extension is missing');
  111. ConnectionManager::setConfig('test_shared_cache', [
  112. 'className' => Connection::class,
  113. 'driver' => Sqlite::class,
  114. 'database' => ':memory:',
  115. 'cache' => 'shared',
  116. ]);
  117. $connection = ConnectionManager::get('test_shared_cache');
  118. $this->assertSame([], $connection->getSchemaCollection()->listTables());
  119. $connection->execute('CREATE TABLE test (test int);');
  120. $this->assertSame(['test'], $connection->getSchemaCollection()->listTables());
  121. ConnectionManager::setConfig('test_shared_cache2', [
  122. 'className' => Connection::class,
  123. 'driver' => Sqlite::class,
  124. 'database' => ':memory:',
  125. 'cache' => 'shared',
  126. ]);
  127. $connection = ConnectionManager::get('test_shared_cache2');
  128. $this->assertSame(['test'], $connection->getSchemaCollection()->listTables());
  129. $this->assertFileDoesNotExist('file::memory:?cache=shared');
  130. }
  131. /**
  132. * Data provider for schemaValue()
  133. *
  134. * @return array
  135. */
  136. public static function schemaValueProvider(): array
  137. {
  138. return [
  139. [null, 'NULL'],
  140. [false, 'FALSE'],
  141. [true, 'TRUE'],
  142. [3.14159, '3.14159'],
  143. ['33', '33'],
  144. [66, 66],
  145. [0, 0],
  146. [10e5, '1000000'],
  147. ['farts', '"farts"'],
  148. ];
  149. }
  150. /**
  151. * Test the schemaValue method on Driver.
  152. *
  153. * @dataProvider schemaValueProvider
  154. * @param mixed $input
  155. * @param mixed $expected
  156. */
  157. public function testSchemaValue($input, $expected): void
  158. {
  159. $mock = $this->getMockBuilder(PDO::class)
  160. ->onlyMethods(['quote'])
  161. ->addMethods(['quoteIdentifier'])
  162. ->disableOriginalConstructor()
  163. ->getMock();
  164. $mock->expects($this->any())
  165. ->method('quote')
  166. ->willReturnCallback(function ($value) {
  167. return '"' . $value . '"';
  168. });
  169. $driver = $this->getMockBuilder(Sqlite::class)
  170. ->onlyMethods(['createPdo'])
  171. ->getMock();
  172. $driver->expects($this->any())
  173. ->method('createPdo')
  174. ->willReturn($mock);
  175. $this->assertEquals($expected, $driver->schemaValue($input));
  176. }
  177. /**
  178. * Tests driver-specific feature support check.
  179. */
  180. public function testSupports(): void
  181. {
  182. $driver = ConnectionManager::get('test')->getDriver();
  183. $this->skipIf(!$driver instanceof Sqlite);
  184. $featureVersions = [
  185. 'cte' => '3.8.3',
  186. 'window' => '3.28.0',
  187. ];
  188. foreach ($featureVersions as $feature => $version) {
  189. $this->assertSame(
  190. version_compare($driver->version(), $version, '>='),
  191. $driver->supports(DriverFeatureEnum::from($feature))
  192. );
  193. }
  194. $this->assertTrue($driver->supports(DriverFeatureEnum::DISABLE_CONSTRAINT_WITHOUT_TRANSACTION));
  195. $this->assertTrue($driver->supports(DriverFeatureEnum::SAVEPOINT));
  196. $this->assertTrue($driver->supports(DriverFeatureEnum::TRUNCATE_WITH_CONSTRAINTS));
  197. $this->assertFalse($driver->supports(DriverFeatureEnum::JSON));
  198. }
  199. /**
  200. * Tests identifier quoting
  201. */
  202. public function testQuoteIdentifier(): void
  203. {
  204. $driver = new Sqlite();
  205. $result = $driver->quoteIdentifier('name');
  206. $expected = '"name"';
  207. $this->assertEquals($expected, $result);
  208. $result = $driver->quoteIdentifier('Model.*');
  209. $expected = '"Model".*';
  210. $this->assertEquals($expected, $result);
  211. $result = $driver->quoteIdentifier('Items.No_ 2');
  212. $expected = '"Items"."No_ 2"';
  213. $this->assertEquals($expected, $result);
  214. $result = $driver->quoteIdentifier('Items.No_ 2 thing');
  215. $expected = '"Items"."No_ 2 thing"';
  216. $this->assertEquals($expected, $result);
  217. $result = $driver->quoteIdentifier('Items.No_ 2 thing AS thing');
  218. $expected = '"Items"."No_ 2 thing" AS "thing"';
  219. $this->assertEquals($expected, $result);
  220. $result = $driver->quoteIdentifier('Items.Item Category Code = :c1');
  221. $expected = '"Items"."Item Category Code" = :c1';
  222. $this->assertEquals($expected, $result);
  223. $result = $driver->quoteIdentifier('MTD()');
  224. $expected = 'MTD()';
  225. $this->assertEquals($expected, $result);
  226. $result = $driver->quoteIdentifier('(sm)');
  227. $expected = '(sm)';
  228. $this->assertEquals($expected, $result);
  229. $result = $driver->quoteIdentifier('name AS x');
  230. $expected = '"name" AS "x"';
  231. $this->assertEquals($expected, $result);
  232. $result = $driver->quoteIdentifier('Model.name AS x');
  233. $expected = '"Model"."name" AS "x"';
  234. $this->assertEquals($expected, $result);
  235. $result = $driver->quoteIdentifier('Function(Something.foo)');
  236. $expected = 'Function("Something"."foo")';
  237. $this->assertEquals($expected, $result);
  238. $result = $driver->quoteIdentifier('Function(SubFunction(Something.foo))');
  239. $expected = 'Function(SubFunction("Something"."foo"))';
  240. $this->assertEquals($expected, $result);
  241. $result = $driver->quoteIdentifier('Function(Something.foo) AS x');
  242. $expected = 'Function("Something"."foo") AS "x"';
  243. $this->assertEquals($expected, $result);
  244. $result = $driver->quoteIdentifier('name-with-minus');
  245. $expected = '"name-with-minus"';
  246. $this->assertEquals($expected, $result);
  247. $result = $driver->quoteIdentifier('my-name');
  248. $expected = '"my-name"';
  249. $this->assertEquals($expected, $result);
  250. $result = $driver->quoteIdentifier('Foo-Model.*');
  251. $expected = '"Foo-Model".*';
  252. $this->assertEquals($expected, $result);
  253. $result = $driver->quoteIdentifier('Team.P%');
  254. $expected = '"Team"."P%"';
  255. $this->assertEquals($expected, $result);
  256. $result = $driver->quoteIdentifier('Team.G/G');
  257. $expected = '"Team"."G/G"';
  258. $this->assertEquals($expected, $result);
  259. $result = $driver->quoteIdentifier('Model.name as y');
  260. $expected = '"Model"."name" AS "y"';
  261. $this->assertEquals($expected, $result);
  262. $result = $driver->quoteIdentifier('nämé');
  263. $expected = '"nämé"';
  264. $this->assertEquals($expected, $result);
  265. $result = $driver->quoteIdentifier('aßa.nämé');
  266. $expected = '"aßa"."nämé"';
  267. $this->assertEquals($expected, $result);
  268. $result = $driver->quoteIdentifier('aßa.*');
  269. $expected = '"aßa".*';
  270. $this->assertEquals($expected, $result);
  271. $result = $driver->quoteIdentifier('Modeß.nämé as y');
  272. $expected = '"Modeß"."nämé" AS "y"';
  273. $this->assertEquals($expected, $result);
  274. $result = $driver->quoteIdentifier('Model.näme Datum as y');
  275. $expected = '"Model"."näme Datum" AS "y"';
  276. $this->assertEquals($expected, $result);
  277. }
  278. }