SqliteTest.php 3.3 KB

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