SqliteTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Database\Driver;
  16. use Cake\Database\Driver\Sqlite;
  17. use Cake\TestSuite\TestCase;
  18. use PDO;
  19. /**
  20. * Tests Sqlite driver
  21. */
  22. class SqliteTest extends TestCase
  23. {
  24. /**
  25. * Test connecting to Sqlite with default configuration
  26. *
  27. * @return void
  28. */
  29. public function testConnectionConfigDefault()
  30. {
  31. $driver = $this->getMockBuilder('Cake\Database\Driver\Sqlite')
  32. ->setMethods(['_connect'])
  33. ->getMock();
  34. $dsn = 'sqlite::memory:';
  35. $expected = [
  36. 'persistent' => false,
  37. 'database' => ':memory:',
  38. 'encoding' => 'utf8',
  39. 'username' => null,
  40. 'password' => null,
  41. 'flags' => [],
  42. 'init' => [],
  43. 'mask' => 420,
  44. ];
  45. $expected['flags'] += [
  46. PDO::ATTR_PERSISTENT => false,
  47. PDO::ATTR_EMULATE_PREPARES => false,
  48. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
  49. ];
  50. $driver->expects($this->once())->method('_connect')
  51. ->with($dsn, $expected);
  52. $driver->connect([]);
  53. }
  54. /**
  55. * Test connecting to Sqlite with custom configuration
  56. *
  57. * @return void
  58. */
  59. public function testConnectionConfigCustom()
  60. {
  61. $config = [
  62. 'persistent' => true,
  63. 'host' => 'foo',
  64. 'database' => 'bar.db',
  65. 'flags' => [1 => true, 2 => false],
  66. 'encoding' => 'a-language',
  67. 'init' => ['Execute this', 'this too'],
  68. 'mask' => 0666
  69. ];
  70. $driver = $this->getMockBuilder('Cake\Database\driver\Sqlite')
  71. ->setMethods(['_connect', 'connection'])
  72. ->setConstructorArgs([$config])
  73. ->getMock();
  74. $dsn = 'sqlite:bar.db';
  75. $expected = $config;
  76. $expected += ['username' => null, 'password' => null];
  77. $expected['flags'] += [
  78. PDO::ATTR_PERSISTENT => true,
  79. PDO::ATTR_EMULATE_PREPARES => false,
  80. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
  81. ];
  82. $connection = $this->getMockBuilder('StdClass')
  83. ->setMethods(['exec'])
  84. ->getMock();
  85. $connection->expects($this->at(0))->method('exec')->with('Execute this');
  86. $connection->expects($this->at(1))->method('exec')->with('this too');
  87. $connection->expects($this->exactly(2))->method('exec');
  88. $driver->expects($this->once())->method('_connect')
  89. ->with($dsn, $expected);
  90. $driver->expects($this->any())->method('connection')
  91. ->will($this->returnValue($connection));
  92. $driver->connect($config);
  93. }
  94. /**
  95. * Data provider for schemaValue()
  96. *
  97. * @return array
  98. */
  99. public static function schemaValueProvider()
  100. {
  101. return [
  102. [null, 'NULL'],
  103. [false, 'FALSE'],
  104. [true, 'TRUE'],
  105. [3.14159, '3.14159'],
  106. ['33', '33'],
  107. [66, 66],
  108. [0, 0],
  109. [10e5, '1000000'],
  110. ['farts', '"farts"'],
  111. ];
  112. }
  113. /**
  114. * Test the schemaValue method on Driver.
  115. *
  116. * @dataProvider schemaValueProvider
  117. * @return void
  118. */
  119. public function testSchemaValue($input, $expected)
  120. {
  121. $driver = new Sqlite();
  122. $mock = $this->getMockBuilder(PDO::class)
  123. ->setMethods(['quote', 'quoteIdentifier'])
  124. ->disableOriginalConstructor()
  125. ->getMock();
  126. $mock->expects($this->any())
  127. ->method('quote')
  128. ->will($this->returnCallback(function ($value) {
  129. return '"' . $value . '"';
  130. }));
  131. $driver->setConnection($mock);
  132. $this->assertEquals($expected, $driver->schemaValue($input));
  133. }
  134. }