SqliteTest.php 3.4 KB

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