PageRenderTime 27ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/unsupported/tests/cubrid/ezSQL_cubridTest.php

http://github.com/jv2222/ezSQL
PHP | 281 lines | 226 code | 16 blank | 39 comment | 3 complexity | d051cfd5d3a40be22ba76b47df944745 MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. require_once('ez_sql_loader.php');
  3. require 'vendor/autoload.php';
  4. use PHPUnit\Framework\TestCase;
  5. /**
  6. * Generated by PHPUnit_SkeletonGenerator on 2018-03-08 at 03:16:41.
  7. */
  8. class ezSQL_cubridTest extends TestCase
  9. {
  10. /**
  11. * constant string user name
  12. */
  13. const TEST_DB_USER = 'ez_test';
  14. /**
  15. * constant string password
  16. */
  17. const TEST_DB_PASSWORD = 'ezTest';
  18. /**
  19. * constant database name
  20. */
  21. const TEST_DB_NAME = 'ez_test';
  22. /**
  23. * constant database host
  24. */
  25. const TEST_DB_HOST = 'localhost';
  26. /**
  27. * constant database port
  28. */
  29. const TEST_DB_PORT = 33000;
  30. /**
  31. * @var ezSQL_cubrid
  32. */
  33. protected $object;
  34. private $errors;
  35. function errorHandler($errno, $errstr, $errfile, $errline, $errcontext) {
  36. $this->errors[] = compact("errno", "errstr", "errfile",
  37. "errline", "errcontext");
  38. }
  39. function assertError($errstr, $errno) {
  40. foreach ($this->errors as $error) {
  41. if ($error["errstr"] === $errstr
  42. && $error["errno"] === $errno) {
  43. return;
  44. }
  45. }
  46. $this->fail("Error with level " . $errno .
  47. " and message '" . $errstr . "' not found in ",
  48. var_export($this->errors, TRUE));
  49. }
  50. /**
  51. * Sets up the fixture, for example, opens a network connection.
  52. * This method is called before a test is executed.
  53. */
  54. protected function setUp()
  55. {
  56. if (!extension_loaded('cubrid')) {
  57. $this->markTestSkipped(
  58. 'The cubrid Lib is not available.'
  59. );
  60. }
  61. $this->object = new ezSQL_cubrid(self::TEST_DB_USER, self::TEST_DB_PASSWORD, self::TEST_DB_NAME);
  62. }
  63. /**
  64. * Tears down the fixture, for example, closes a network connection.
  65. * This method is called after a test is executed.
  66. */
  67. protected function tearDown()
  68. {
  69. $this->object->query('DROP TABLE IF EXISTS unit_test');
  70. $this->object = null;
  71. }
  72. /**
  73. * @covers ezSQL_cubrid::quick_connect
  74. */
  75. public function testQuick_connect() {
  76. $this->assertTrue($this->object->quick_connect(self::TEST_DB_USER, self::TEST_DB_PASSWORD, self::TEST_DB_NAME));
  77. } // testQuick_connect
  78. /**
  79. * @covers ezSQL_cubrid::connect
  80. *
  81. */
  82. public function testConnect() {
  83. $this->errors = array();
  84. set_error_handler(array($this, 'errorHandler'));
  85. $this->assertFalse($this->object->connect('',''));
  86. $this->assertFalse($this->object->connect('self::TEST_DB_USER', 'self::TEST_DB_PASSWORD',' self::TEST_DB_NAME'));
  87. $this->assertTrue($this->object->connect(self::TEST_DB_USER, self::TEST_DB_PASSWORD, self::TEST_DB_NAME, self::TEST_DB_HOST, self::TEST_DB_PORT));
  88. } // testConnect
  89. /**
  90. * @covers ezSQL_cubrid::escape
  91. */
  92. public function testEscape() {
  93. $this->object->quick_connect(self::TEST_DB_USER, self::TEST_DB_PASSWORD, self::TEST_DB_NAME);
  94. $result = $this->object->escape("This is'nt escaped.");
  95. $this->assertEquals("This is''nt escaped.", $result);
  96. } // testEscape
  97. /**
  98. * @covers ezSQL_cubrid::sysdate
  99. */
  100. public function testSysdate() {
  101. $this->assertEquals('NOW()', $this->object->sysdate());
  102. } // testSysdate
  103. /**
  104. * @covers ezSQLcore::get_var
  105. */
  106. public function testGet_var() {
  107. $this->object->quick_connect(self::TEST_DB_USER, self::TEST_DB_PASSWORD, self::TEST_DB_NAME);
  108. // Demo of getting a single variable from the db
  109. // (and using abstracted function sysdate)
  110. $current_time = $this->object->get_var("SELECT " . $this->object->sysdate());
  111. $this->assertNotNull($current_time);
  112. } // testGet_var
  113. /**
  114. * @covers ezSQLcore::get_results
  115. */
  116. public function testGet_results() {
  117. $this->object->quick_connect(self::TEST_DB_USER, self::TEST_DB_PASSWORD, self::TEST_DB_NAME);
  118. // Get list of tables from current database..
  119. $my_tables = $this->object->get_results("SHOW TABLES",ARRAY_N);
  120. $this->assertNotNull($my_tables);
  121. // Loop through each row of results..
  122. foreach ( $my_tables as $table )
  123. {
  124. // Get results of DESC table..
  125. $this->assertNotNull($this->object->get_results("DESC $table[0]"));
  126. }
  127. } // testGet_results
  128. /**
  129. * @covers ezSQL_cubrid::query
  130. */
  131. public function testQuery() {
  132. $this->object->quick_connect(self::TEST_DB_USER, self::TEST_DB_PASSWORD, self::TEST_DB_NAME);
  133. $this->assertNotFalse($this->object->query('CREATE TABLE unit_test(id int, test_key varchar(50), PRIMARY KEY (ID))'));
  134. $this->assertEquals($this->object->query('INSERT INTO unit_test(id, test_key) VALUES(1, \'test 1\')'), 1);
  135. $this->object->dbh = null;
  136. $this->assertEquals($this->object->query('INSERT INTO unit_test(id, test_key) VALUES(2, \'test 2\')'),1);
  137. $this->object->disconnect();
  138. $this->assertFalse($this->object->query('INSERT INTO unit_test(id, test_key) VALUES(3, \'test 3\')'));
  139. } // testQuery
  140. /**
  141. * @covers ezSQLcore::insert
  142. */
  143. public function testInsert()
  144. {
  145. $this->object->quick_connect(self::TEST_DB_USER, self::TEST_DB_PASSWORD, self::TEST_DB_NAME);
  146. $this->object->query('DROP TABLE IF EXISTS unit_test');
  147. $this->object->query('CREATE TABLE unit_test(id int, test_key varchar(50), PRIMARY KEY (ID))');
  148. $result = $this->object->insert('unit_test', array('id'=>'1', 'test_key'=>'test 1' ));
  149. $this->assertEquals(0, $result);
  150. }
  151. /**
  152. * @covers ezSQLcore::update
  153. */
  154. public function testUpdate()
  155. {
  156. $this->object->quick_connect(self::TEST_DB_USER, self::TEST_DB_PASSWORD, self::TEST_DB_NAME);
  157. $this->object->query('CREATE TABLE unit_test(id int, test_key varchar(50), PRIMARY KEY (ID))');
  158. $this->object->insert('unit_test', array('id'=>'1', 'test_key'=>'test 1' ));
  159. $this->object->insert('unit_test', array('id'=>'2', 'test_key'=>'test 2' ));
  160. $this->object->insert('unit_test', array('id'=>'3', 'test_key'=>'test 3' ));
  161. $unit_test['test_key'] = 'testing';
  162. $where="id = 1";
  163. $this->assertEquals($this->object->update('unit_test', $unit_test, $where), 1);
  164. $this->assertEquals($this->object->update('unit_test', $unit_test, eq('test_key','test 3', _AND), eq('id','3')), 1);
  165. $this->assertEquals($this->object->update('unit_test', $unit_test, "id = 4"), 0);
  166. $this->assertEquals($this->object->update('unit_test', $unit_test, "test_key = test 2 and", "id = 2"), 1);
  167. }
  168. /**
  169. * @covers ezSQLcore::delete
  170. */
  171. public function testDelete()
  172. {
  173. $this->object->quick_connect(self::TEST_DB_USER, self::TEST_DB_PASSWORD, self::TEST_DB_NAME);
  174. $this->object->query('CREATE TABLE unit_test(id int, test_key varchar(50), PRIMARY KEY (ID))');
  175. $unit_test['id'] = '1';
  176. $unit_test['test_key'] = 'test 1';
  177. $this->object->insert('unit_test', $unit_test );
  178. $unit_test['id'] = '2';
  179. $unit_test['test_key'] = 'test 2';
  180. $this->object->insert('unit_test', $unit_test );
  181. $unit_test['id'] = '3';
  182. $unit_test['test_key'] = 'test 3';
  183. $this->object->insert('unit_test', $unit_test );
  184. $where='1';
  185. $this->assertEquals($this->object->delete('unit_test', array('id','=','1')), 1);
  186. $this->assertEquals($this->object->delete('unit_test',
  187. array('test_key','=',$unit_test['test_key'],'and'),
  188. array('id','=','3')), 1);
  189. $this->assertEquals($this->object->delete('unit_test', array('test_key','=',$where)), 0);
  190. $where="id = 2";
  191. $this->assertEquals($this->object->delete('unit_test', $where), 1);
  192. }
  193. /**
  194. * @covers ezSQLcore::selecting
  195. */
  196. public function testSelecting()
  197. {
  198. $this->object->quick_connect(self::TEST_DB_USER, self::TEST_DB_PASSWORD, self::TEST_DB_NAME);
  199. $this->object->query('CREATE TABLE unit_test(id int, test_key varchar(50), PRIMARY KEY (ID))');
  200. $this->object->insert('unit_test', array('id'=>'1', 'test_key'=>'testing 1' ));
  201. $this->object->insert('unit_test', array('id'=>'2', 'test_key'=>'testing 2' ));
  202. $this->object->insert('unit_test', array('id'=>'3', 'test_key'=>'testing 3' ));
  203. $result = $this->object->selecting('unit_test');
  204. $i = 1;
  205. foreach ($result as $row) {
  206. $this->assertEquals($i, $row->id);
  207. $this->assertEquals('testing ' . $i, $row->test_key);
  208. ++$i;
  209. }
  210. $where=eq('test_key','testing 2');
  211. $result = $this->object->selecting('unit_test', 'id', $where);
  212. foreach ($result as $row) {
  213. $this->assertEquals(2, $row->id);
  214. }
  215. $result = $this->object->selecting('unit_test', 'test_key', eq( 'id','3' ));
  216. foreach ($result as $row) {
  217. $this->assertEquals('testing 3', $row->test_key);
  218. }
  219. $result = $this->object->selecting('unit_test', array ('test_key'), "id = 1");
  220. foreach ($result as $row) {
  221. $this->assertEquals('testing 1', $row->test_key);
  222. }
  223. }
  224. /**
  225. * @covers ezSQL_cubrid::disconnect
  226. */
  227. public function testDisconnect() {
  228. $this->object->quick_connect(self::TEST_DB_USER, self::TEST_DB_PASSWORD, self::TEST_DB_NAME);
  229. $this->object->disconnect();
  230. $this->assertFalse($this->object->isConnected());
  231. } // testDisconnect
  232. /**
  233. * @covers ezSQL_cubrid::__construct
  234. */
  235. public function test__Construct() {
  236. $this->errors = array();
  237. set_error_handler(array($this, 'errorHandler'));
  238. $cubrid = $this->getMockBuilder(ezSQL_cubrid::class)
  239. ->setMethods(null)
  240. ->disableOriginalConstructor()
  241. ->getMock();
  242. $this->assertNull($cubrid->__construct());
  243. }
  244. }