PageRenderTime 53ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/cake/tests/cases/libs/model/datasources/dbo/dbo_sqlite.test.php

https://github.com/hardsshah/bookmarks
PHP | 208 lines | 83 code | 11 blank | 114 comment | 1 complexity | 84f471ecd1e0556e7b72a3abe43b8b32 MD5 | raw file
  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * DboSqliteTest file
  5. *
  6. * PHP versions 4 and 5
  7. *
  8. * CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
  9. * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  10. *
  11. * Licensed under The MIT License
  12. * Redistributions of files must retain the above copyright notice.
  13. *
  14. * @filesource
  15. * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  16. * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
  17. * @package cake
  18. * @subpackage cake.cake.libs
  19. * @since CakePHP(tm) v 1.2.0
  20. * @version $Revision$
  21. * @modifiedby $LastChangedBy$
  22. * @lastmodified $Date$
  23. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  24. */
  25. App::import('Core', array('Model', 'DataSource', 'DboSource', 'DboSqlite'));
  26. /**
  27. * DboSqliteTestDb class
  28. *
  29. * @package cake
  30. * @subpackage cake.tests.cases.libs.model.datasources
  31. */
  32. class DboSqliteTestDb extends DboSqlite {
  33. /**
  34. * simulated property
  35. *
  36. * @var array
  37. * @access public
  38. */
  39. var $simulated = array();
  40. /**
  41. * execute method
  42. *
  43. * @param mixed $sql
  44. * @access protected
  45. * @return void
  46. */
  47. function _execute($sql) {
  48. $this->simulated[] = $sql;
  49. return null;
  50. }
  51. /**
  52. * getLastQuery method
  53. *
  54. * @access public
  55. * @return void
  56. */
  57. function getLastQuery() {
  58. return $this->simulated[count($this->simulated) - 1];
  59. }
  60. }
  61. /**
  62. * DboSqliteTest class
  63. *
  64. * @package cake
  65. * @subpackage cake.tests.cases.libs.model.datasources.dbo
  66. */
  67. class DboSqliteTest extends CakeTestCase {
  68. /**
  69. * Do not automatically load fixtures for each test, they will be loaded manually using CakeTestCase::loadFixtures
  70. *
  71. * @var boolean
  72. * @access public
  73. */
  74. var $autoFixtures = false;
  75. /**
  76. * Fixtures
  77. *
  78. * @var object
  79. * @access public
  80. */
  81. var $fixtures = array('core.user');
  82. /**
  83. * Actual DB connection used in testing
  84. *
  85. * @var DboSource
  86. * @access public
  87. */
  88. var $db = null;
  89. /**
  90. * Simulated DB connection used in testing
  91. *
  92. * @var DboSource
  93. * @access public
  94. */
  95. var $db2 = null;
  96. /**
  97. * Skip if cannot connect to SQLite
  98. *
  99. * @access public
  100. */
  101. function skip() {
  102. $this->_initDb();
  103. $this->skipif($this->db->config['driver'] != 'sqlite', 'SQLite connection not available');
  104. }
  105. /**
  106. * Set up test suite database connection
  107. *
  108. * @access public
  109. */
  110. function startTest() {
  111. $this->_initDb();
  112. }
  113. /**
  114. * Sets up a Dbo class instance for testing
  115. *
  116. * @access public
  117. */
  118. function setUp() {
  119. Configure::write('Cache.disable', true);
  120. $this->startTest();
  121. $this->db =& ConnectionManager::getDataSource('test_suite');
  122. $this->db2 = new DboSqliteTestDb($this->db->config, false);
  123. }
  124. /**
  125. * Sets up a Dbo class instance for testing
  126. *
  127. * @access public
  128. */
  129. function tearDown() {
  130. Configure::write('Cache.disable', false);
  131. unset($this->db2);
  132. }
  133. /**
  134. * Tests that SELECT queries from DboSqlite::listSources() are not cached
  135. *
  136. * @access public
  137. */
  138. function testTableListCacheDisabling() {
  139. $this->assertFalse(in_array('foo_test', $this->db->listSources()));
  140. $this->db->query('CREATE TABLE foo_test (test VARCHAR(255));');
  141. $this->assertTrue(in_array('foo_test', $this->db->listSources()));
  142. $this->db->query('DROP TABLE foo_test;');
  143. $this->assertFalse(in_array('foo_test', $this->db->listSources()));
  144. }
  145. /**
  146. * test Index introspection.
  147. *
  148. * @access public
  149. * @return void
  150. */
  151. function testIndex() {
  152. $name = $this->db->fullTableName('with_a_key');
  153. $this->db->query('CREATE TABLE ' . $name . ' ("id" int(11) PRIMARY KEY, "bool" int(1), "small_char" varchar(50), "description" varchar(40) );');
  154. $this->db->query('CREATE INDEX pointless_bool ON ' . $name . '("bool")');
  155. $this->db->query('CREATE UNIQUE INDEX char_index ON ' . $name . '("small_char")');
  156. $expected = array(
  157. 'PRIMARY' => array('column' => 'id', 'unique' => 1),
  158. 'pointless_bool' => array('column' => 'bool', 'unique' => 0),
  159. 'char_index' => array('column' => 'small_char', 'unique' => 1),
  160. );
  161. $result = $this->db->index($name);
  162. $this->assertEqual($expected, $result);
  163. $this->db->query('DROP TABLE ' . $name);
  164. $this->db->query('CREATE TABLE ' . $name . ' ("id" int(11) PRIMARY KEY, "bool" int(1), "small_char" varchar(50), "description" varchar(40) );');
  165. $this->db->query('CREATE UNIQUE INDEX multi_col ON ' . $name . '("small_char", "bool")');
  166. $expected = array(
  167. 'PRIMARY' => array('column' => 'id', 'unique' => 1),
  168. 'multi_col' => array('column' => array('small_char', 'bool'), 'unique' => 1),
  169. );
  170. $result = $this->db->index($name);
  171. $this->assertEqual($expected, $result);
  172. $this->db->query('DROP TABLE ' . $name);
  173. }
  174. /**
  175. * Tests that cached table descriptions are saved under the sanitized key name
  176. *
  177. * @access public
  178. */
  179. function testCacheKeyName() {
  180. Configure::write('Cache.disable', false);
  181. $dbName = 'db' . rand() . '$(*%&).db';
  182. $this->assertFalse(file_exists(TMP . $dbName));
  183. $config = $this->db->config;
  184. $db = new DboSqlite(array_merge($this->db->config, array('database' => TMP . $dbName)));
  185. $this->assertTrue(file_exists(TMP . $dbName));
  186. $db->execute("CREATE TABLE test_list (id VARCHAR(255));");
  187. $db->cacheSources = true;
  188. $this->assertEqual($db->listSources(), array('test_list'));
  189. $db->cacheSources = false;
  190. $fileName = '_' . preg_replace('/[^A-Za-z0-9_\-+]/', '_', TMP . $dbName) . '_list';
  191. $result = Cache::read($fileName, '_cake_model_');
  192. $this->assertEqual($result, array('test_list'));
  193. Cache::delete($fileName, '_cake_model_');
  194. Configure::write('Cache.disable', true);
  195. }
  196. }
  197. ?>