SqliteTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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_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_ERRMODE => PDO::ERRMODE_EXCEPTION
  78. ];
  79. $connection = $this->getMock('StdClass', ['exec']);
  80. $connection->expects($this->at(0))->method('exec')->with('Execute this');
  81. $connection->expects($this->at(1))->method('exec')->with('this too');
  82. $connection->expects($this->exactly(2))->method('exec');
  83. $driver->expects($this->once())->method('_connect')
  84. ->with($dsn, $expected);
  85. $driver->expects($this->any())->method('connection')
  86. ->will($this->returnValue($connection));
  87. $driver->connect($config);
  88. }
  89. /**
  90. * Data provider for schemaValue()
  91. *
  92. * @return array
  93. */
  94. public static function schemaValueProvider()
  95. {
  96. return [
  97. [null, 'NULL'],
  98. [false, 'FALSE'],
  99. [true, 'TRUE'],
  100. [3.14159, '3.14159'],
  101. ['33', '33'],
  102. [66, 66],
  103. [0, 0],
  104. [10e5, '1000000'],
  105. ['farts', '"farts"'],
  106. ];
  107. }
  108. /**
  109. * Test the schemaValue method on Driver.
  110. *
  111. * @dataProvider schemaValueProvider
  112. * @return void
  113. */
  114. public function testSchemaValue($input, $expected)
  115. {
  116. $driver = new Sqlite();
  117. $mock = $this->getMock('FakePdo', ['quote', 'quoteIdentifier']);
  118. $mock->expects($this->any())
  119. ->method('quote')
  120. ->will($this->returnCallback(function ($value) {
  121. return '"' . $value . '"';
  122. }));
  123. $driver->connection($mock);
  124. $this->assertEquals($expected, $driver->schemaValue($input));
  125. }
  126. }