SqliteTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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\Core\Configure;
  17. use Cake\Database\Driver\Sqlite;
  18. use Cake\Testsuite\TestCase;
  19. use \PDO;
  20. /**
  21. * Tests Sqlite driver
  22. */
  23. class SqliteTest extends TestCase
  24. {
  25. /**
  26. * Test connecting to Sqlite with default configuration
  27. *
  28. * @return void
  29. */
  30. public function testConnectionConfigDefault()
  31. {
  32. $driver = $this->getMock('Cake\Database\Driver\Sqlite', ['_connect']);
  33. $dsn = 'sqlite::memory:';
  34. $expected = [
  35. 'persistent' => false,
  36. 'database' => ':memory:',
  37. 'encoding' => 'utf8',
  38. 'username' => null,
  39. 'password' => null,
  40. 'flags' => [],
  41. 'init' => [],
  42. ];
  43. $expected['flags'] += [
  44. PDO::ATTR_PERSISTENT => false,
  45. PDO::ATTR_EMULATE_PREPARES => false,
  46. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
  47. ];
  48. $driver->expects($this->once())->method('_connect')
  49. ->with($dsn, $expected);
  50. $driver->connect([]);
  51. }
  52. /**
  53. * Test connecting to Sqlite with custom configuration
  54. *
  55. * @return void
  56. */
  57. public function testConnectionConfigCustom()
  58. {
  59. $config = [
  60. 'persistent' => true,
  61. 'host' => 'foo',
  62. 'database' => 'bar.db',
  63. 'flags' => [1 => true, 2 => false],
  64. 'encoding' => 'a-language',
  65. 'init' => ['Execute this', 'this too']
  66. ];
  67. $driver = $this->getMock(
  68. 'Cake\Database\driver\Sqlite',
  69. ['_connect', 'connection'],
  70. [$config]
  71. );
  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->getMock('StdClass', ['exec']);
  81. $connection->expects($this->at(0))->method('exec')->with('Execute this');
  82. $connection->expects($this->at(1))->method('exec')->with('this too');
  83. $connection->expects($this->exactly(2))->method('exec');
  84. $driver->expects($this->once())->method('_connect')
  85. ->with($dsn, $expected);
  86. $driver->expects($this->any())->method('connection')
  87. ->will($this->returnValue($connection));
  88. $driver->connect($config);
  89. }
  90. /**
  91. * Data provider for schemaValue()
  92. *
  93. * @return array
  94. */
  95. public static function schemaValueProvider()
  96. {
  97. return [
  98. [null, 'NULL'],
  99. [false, 'FALSE'],
  100. [true, 'TRUE'],
  101. [3.14159, '3.14159'],
  102. ['33', '33'],
  103. [66, 66],
  104. [0, 0],
  105. [10e5, '1000000'],
  106. ['farts', '"farts"'],
  107. ];
  108. }
  109. /**
  110. * Test the schemaValue method on Driver.
  111. *
  112. * @dataProvider schemaValueProvider
  113. * @return void
  114. */
  115. public function testSchemaValue($input, $expected)
  116. {
  117. $driver = new Sqlite();
  118. $mock = $this->getMock('FakePdo', ['quote', 'quoteIdentifier']);
  119. $mock->expects($this->any())
  120. ->method('quote')
  121. ->will($this->returnCallback(function ($value) {
  122. return '"' . $value . '"';
  123. }));
  124. $driver->connection($mock);
  125. $this->assertEquals($expected, $driver->schemaValue($input));
  126. }
  127. }