PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/tests/cases/data/source/database/adapter/MySqlTest.php

https://bitbucket.org/d1rk/lithium
PHP | 258 lines | 187 code | 47 blank | 24 comment | 2 complexity | 091dbd14fe39becd8adb4add39b3c1cb MD5 | raw file
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2012, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\tests\cases\data\source\database\adapter;
  9. use lithium\data\Connections;
  10. use lithium\data\model\Query;
  11. use lithium\data\source\database\adapter\MySql;
  12. use lithium\tests\mocks\data\source\database\adapter\MockMySql;
  13. class MySqlTest extends \lithium\test\Unit {
  14. protected $_dbConfig = array();
  15. public $db = null;
  16. /**
  17. * Skip the test if a MySQL adapter configuration is unavailable.
  18. * @todo Tie into the Environment class to ensure that the test database is being used.
  19. */
  20. public function skip() {
  21. $this->skipIf(!MySql::enabled(), 'MySQL Extension is not loaded');
  22. $this->_dbConfig = Connections::get('test', array('config' => true));
  23. $hasDb = (isset($this->_dbConfig['adapter']) && $this->_dbConfig['adapter'] == 'MySql');
  24. $message = 'Test database is either unavailable, or not using a MySQL adapter';
  25. $this->skipIf(!$hasDb, $message);
  26. $this->db = new MySql($this->_dbConfig);
  27. $lithium = LITHIUM_LIBRARY_PATH . '/lithium';
  28. $sqlFile = $lithium . '/tests/mocks/data/source/database/adapter/mysql_companies.sql';
  29. $sql = file_get_contents($sqlFile);
  30. $this->db->read($sql, array('return' => 'resource'));
  31. }
  32. /**
  33. * Tests that the object is initialized with the correct default values.
  34. */
  35. public function testConstructorDefaults() {
  36. $db = new MockMySql(array('autoConnect' => false));
  37. $result = $db->get('_config');
  38. $expected = array(
  39. 'autoConnect' => false, 'encoding' => null,'persistent' => true,
  40. 'host' => 'localhost:3306', 'login' => 'root', 'password' => '',
  41. 'database' => null, 'init' => true
  42. );
  43. $this->assertEqual($expected, $result);
  44. }
  45. /**
  46. * Tests that this adapter can connect to the database, and that the status is properly
  47. * persisted.
  48. */
  49. public function testDatabaseConnection() {
  50. $db = new MySql(array('autoConnect' => false) + $this->_dbConfig);
  51. $this->assertTrue($db->connect());
  52. $this->assertTrue($db->isConnected());
  53. $this->assertTrue($db->disconnect());
  54. $this->assertFalse($db->isConnected());
  55. $db = new MySQL(array(
  56. 'autoConnect' => false, 'encoding' => null,'persistent' => false,
  57. 'host' => 'localhost:3306', 'login' => 'garbage', 'password' => '',
  58. 'database' => 'garbage', 'init' => true
  59. ) + $this->_dbConfig);
  60. $this->expectException();
  61. $this->assertFalse($db->connect());
  62. $this->assertFalse($db->isConnected());
  63. $this->assertTrue($db->disconnect());
  64. $this->assertFalse($db->isConnected());
  65. }
  66. public function testDatabaseEncoding() {
  67. $this->assertTrue($this->db->isConnected());
  68. $this->assertTrue($this->db->encoding('utf8'));
  69. $this->assertEqual('UTF-8', $this->db->encoding());
  70. $this->assertTrue($this->db->encoding('UTF-8'));
  71. $this->assertEqual('UTF-8', $this->db->encoding());
  72. }
  73. public function testValueByIntrospect() {
  74. $expected = "'string'";
  75. $result = $this->db->value("string");
  76. $this->assertTrue(is_string($result));
  77. $this->assertEqual($expected, $result);
  78. $expected = "'\'this string is escaped\''";
  79. $result = $this->db->value("'this string is escaped'");
  80. $this->assertTrue(is_string($result));
  81. $this->assertEqual($expected, $result);
  82. $this->assertIdentical(1, $this->db->value(true));
  83. $this->assertIdentical(1, $this->db->value('1'));
  84. $this->assertIdentical(1.1, $this->db->value('1.1'));
  85. }
  86. public function testColumnAbstraction() {
  87. $result = $this->db->invokeMethod('_column', array('varchar'));
  88. $this->assertIdentical(array('type' => 'string'), $result);
  89. $result = $this->db->invokeMethod('_column', array('tinyint(1)'));
  90. $this->assertIdentical(array('type' => 'boolean'), $result);
  91. $result = $this->db->invokeMethod('_column', array('varchar(255)'));
  92. $this->assertIdentical(array('type' => 'string', 'length' => 255), $result);
  93. $result = $this->db->invokeMethod('_column', array('text'));
  94. $this->assertIdentical(array('type' => 'text'), $result);
  95. $result = $this->db->invokeMethod('_column', array('text'));
  96. $this->assertIdentical(array('type' => 'text'), $result);
  97. $result = $this->db->invokeMethod('_column', array('decimal(12,2)'));
  98. $this->assertIdentical(array('type' => 'float', 'length' => 12, 'precision' => 2), $result);
  99. $result = $this->db->invokeMethod('_column', array('int(11)'));
  100. $this->assertIdentical(array('type' => 'integer', 'length' => 11), $result);
  101. }
  102. public function testRawSqlQuerying() {
  103. $this->assertTrue($this->db->create(
  104. 'INSERT INTO companies (name, active) VALUES (?, ?)',
  105. array('Test', 1)
  106. ));
  107. $result = $this->db->read('SELECT * From companies AS Company WHERE name = {:name}', array(
  108. 'name' => 'Test',
  109. 'return' => 'array'
  110. ));
  111. $this->assertEqual(1, count($result));
  112. $expected = array('id', 'name', 'active', 'created', 'modified');
  113. $this->assertEqual($expected, array_keys($result[0]));
  114. $this->assertTrue(is_numeric($result[0]['id']));
  115. unset($result[0]['id']);
  116. $expected = array('name' => 'Test', 'active' => '1', 'created' => null, 'modified' => null);
  117. $this->assertIdentical($expected, $result[0]);
  118. $this->assertTrue($this->db->delete('DELETE From companies WHERE name = {:name}', array(
  119. 'name' => 'Test'
  120. )));
  121. $result = $this->db->read('SELECT * From companies AS Company WHERE name = {:name}', array(
  122. 'name' => 'Test',
  123. 'return' => 'array'
  124. ));
  125. $this->assertFalse($result);
  126. }
  127. public function testAbstractColumnResolution() {
  128. }
  129. public function testExecuteException() {
  130. $this->expectException();
  131. $this->db->read('SELECT deliberate syntax error');
  132. }
  133. public function testEnabledFeatures() {
  134. $this->assertTrue(MySql::enabled());
  135. $this->assertTrue(MySql::enabled('relationships'));
  136. $this->assertFalse(MySql::enabled('arrays'));
  137. }
  138. public function testEntityQuerying() {
  139. $sources = $this->db->sources();
  140. $this->assertTrue(is_array($sources));
  141. $this->assertFalse(empty($sources));
  142. }
  143. public function testQueryOrdering() {
  144. $insert = new Query(array(
  145. 'type' => 'create',
  146. 'source' => 'companies',
  147. 'data' => array(
  148. 'name' => 'Foo',
  149. 'active' => true,
  150. 'created' => date('Y-m-d H:i:s')
  151. )
  152. ));
  153. $this->assertIdentical(true, $this->db->create($insert));
  154. $insert->data(array(
  155. 'name' => 'Bar',
  156. 'created' => date('Y-m-d H:i:s', strtotime('-5 minutes'))
  157. ));
  158. $this->assertIdentical(true, $this->db->create($insert));
  159. $insert->data(array(
  160. 'name' => 'Baz',
  161. 'created' => date('Y-m-d H:i:s', strtotime('-10 minutes'))
  162. ));
  163. $this->assertIdentical(true, $this->db->create($insert));
  164. $read = new Query(array(
  165. 'type' => 'read',
  166. 'source' => 'companies',
  167. 'fields' => array('name'),
  168. 'order' => array('created' => 'asc')
  169. ));
  170. $result = $this->db->read($read, array('return' => 'array'));
  171. $expected = array(
  172. array('name' => 'Baz'),
  173. array('name' => 'Bar'),
  174. array('name' => 'Foo')
  175. );
  176. $this->assertEqual($expected, $result);
  177. $read->order(array('created' => 'desc'));
  178. $result = $this->db->read($read, array('return' => 'array'));
  179. $expected = array(
  180. array('name' => 'Foo'),
  181. array('name' => 'Bar'),
  182. array('name' => 'Baz')
  183. );
  184. $this->assertEqual($expected, $result);
  185. $delete = new Query(array('type' => 'delete', 'source' => 'companies'));
  186. $this->assertTrue($this->db->delete($delete));
  187. }
  188. /**
  189. * Ensures that DELETE queries are not generated with table aliases, as MySQL does not support
  190. * this.
  191. */
  192. public function testDeletesWithoutAliases() {
  193. $delete = new Query(array('type' => 'delete', 'source' => 'companies'));
  194. $this->assertTrue($this->db->delete($delete));
  195. }
  196. /**
  197. * Tests that describing a table's schema returns the correct column meta-information.
  198. */
  199. public function testDescribe() {
  200. $result = $this->db->describe('companies');
  201. $expected = array(
  202. 'id' => array('type' => 'integer', 'length' => 11, 'null' => false, 'default' => null),
  203. 'name' => array('type' => 'string', 'length' => 255, 'null' => true, 'default' => null),
  204. 'active' => array('type' => 'boolean', 'null' => true, 'default' => null),
  205. 'created' => array('type' => 'datetime', 'null' => true, 'default' => null),
  206. 'modified' => array('type' => 'datetime', 'null' => true, 'default' => null)
  207. );
  208. $this->assertEqual($expected, $result);
  209. }
  210. }
  211. ?>