SqliteTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. /**
  27. * Test connecting to Sqlite with default configuration
  28. *
  29. * @return void
  30. */
  31. public function testConnectionConfigDefault()
  32. {
  33. $driver = $this->getMock('Cake\Database\Driver\Sqlite', ['_connect']);
  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->getMock(
  69. 'Cake\Database\driver\Sqlite',
  70. ['_connect', 'connection'],
  71. [$config]
  72. );
  73. $dsn = 'sqlite:bar.db';
  74. $expected = $config;
  75. $expected += ['username' => null, 'password' => null];
  76. $expected['flags'] += [
  77. PDO::ATTR_PERSISTENT => true,
  78. PDO::ATTR_EMULATE_PREPARES => false,
  79. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
  80. ];
  81. $connection = $this->getMock('StdClass', ['exec']);
  82. $connection->expects($this->at(0))->method('exec')->with('Execute this');
  83. $connection->expects($this->at(1))->method('exec')->with('this too');
  84. $connection->expects($this->exactly(2))->method('exec');
  85. $driver->expects($this->once())->method('_connect')
  86. ->with($dsn, $expected);
  87. $driver->expects($this->any())->method('connection')
  88. ->will($this->returnValue($connection));
  89. $driver->connect($config);
  90. }
  91. /**
  92. * Data provider for schemaValue()
  93. *
  94. * @return array
  95. */
  96. public static function schemaValueProvider()
  97. {
  98. return [
  99. [null, 'NULL'],
  100. [false, 'FALSE'],
  101. [true, 'TRUE'],
  102. [3.14159, '3.14159'],
  103. ['33', '33'],
  104. [66, 66],
  105. [0, 0],
  106. [10e5, '1000000'],
  107. ['farts', '"farts"'],
  108. ];
  109. }
  110. /**
  111. * Test the schemaValue method on Driver.
  112. *
  113. * @dataProvider schemaValueProvider
  114. * @return void
  115. */
  116. public function testSchemaValue($input, $expected)
  117. {
  118. $driver = new Sqlite();
  119. $mock = $this->getMock('FakePdo', ['quote', 'quoteIdentifier']);
  120. $mock->expects($this->any())
  121. ->method('quote')
  122. ->will($this->returnCallback(function ($value) {
  123. return '"' . $value . '"';
  124. }));
  125. $driver->connection($mock);
  126. $this->assertEquals($expected, $driver->schemaValue($input));
  127. }
  128. }