SqliteTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.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. ];
  44. $expected['flags'] += [
  45. PDO::ATTR_PERSISTENT => false,
  46. PDO::ATTR_EMULATE_PREPARES => false,
  47. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
  48. ];
  49. $driver->expects($this->once())->method('_connect')
  50. ->with($dsn, $expected);
  51. $driver->connect([]);
  52. }
  53. /**
  54. * Test connecting to Sqlite with custom configuration
  55. *
  56. * @return void
  57. */
  58. public function testConnectionConfigCustom()
  59. {
  60. $config = [
  61. 'persistent' => true,
  62. 'host' => 'foo',
  63. 'database' => 'bar.db',
  64. 'flags' => [1 => true, 2 => false],
  65. 'encoding' => 'a-language',
  66. 'init' => ['Execute this', 'this too']
  67. ];
  68. $driver = $this->getMockBuilder('Cake\Database\driver\Sqlite')
  69. ->setMethods(['_connect', 'connection'])
  70. ->setConstructorArgs([$config])
  71. ->getMock();
  72. $dsn = 'sqlite:bar.db';
  73. $expected = $config;
  74. $expected += ['username' => null, 'password' => null];
  75. $expected['flags'] += [
  76. PDO::ATTR_PERSISTENT => true,
  77. PDO::ATTR_EMULATE_PREPARES => false,
  78. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
  79. ];
  80. $connection = $this->getMockBuilder('StdClass')
  81. ->setMethods(['exec'])
  82. ->getMock();
  83. $connection->expects($this->at(0))->method('exec')->with('Execute this');
  84. $connection->expects($this->at(1))->method('exec')->with('this too');
  85. $connection->expects($this->exactly(2))->method('exec');
  86. $driver->expects($this->once())->method('_connect')
  87. ->with($dsn, $expected);
  88. $driver->expects($this->any())->method('connection')
  89. ->will($this->returnValue($connection));
  90. $driver->connect($config);
  91. }
  92. /**
  93. * Data provider for schemaValue()
  94. *
  95. * @return array
  96. */
  97. public static function schemaValueProvider()
  98. {
  99. return [
  100. [null, 'NULL'],
  101. [false, 'FALSE'],
  102. [true, 'TRUE'],
  103. [3.14159, '3.14159'],
  104. ['33', '33'],
  105. [66, 66],
  106. [0, 0],
  107. [10e5, '1000000'],
  108. ['farts', '"farts"'],
  109. ];
  110. }
  111. /**
  112. * Test the schemaValue method on Driver.
  113. *
  114. * @dataProvider schemaValueProvider
  115. * @return void
  116. */
  117. public function testSchemaValue($input, $expected)
  118. {
  119. $driver = new Sqlite();
  120. $pdo = PDO::class;
  121. if (version_compare(PHP_VERSION, '5.6', '<')) {
  122. $pdo = 'FakePdo';
  123. }
  124. $mock = $this->getMockBuilder($pdo)
  125. ->setMethods(['quote', 'quoteIdentifier'])
  126. ->disableOriginalConstructor()
  127. ->getMock();
  128. $mock->expects($this->any())
  129. ->method('quote')
  130. ->will($this->returnCallback(function ($value) {
  131. return '"' . $value . '"';
  132. }));
  133. $driver->connection($mock);
  134. $this->assertEquals($expected, $driver->schemaValue($input));
  135. }
  136. }