/GraphDatabaseTiein/tests/dataset_pdo_test.php

https://github.com/F5/zetacomponents · PHP · 390 lines · 289 code · 67 blank · 34 comment · 1 complexity · a392a669c7c8cb1388b636582a08c3e9 MD5 · raw file

  1. <?php
  2. /**
  3. * ezcGraphDatabaseTest
  4. *
  5. * Licensed to the Apache Software Foundation (ASF) under one
  6. * or more contributor license agreements. See the NOTICE file
  7. * distributed with this work for additional information
  8. * regarding copyright ownership. The ASF licenses this file
  9. * to you under the Apache License, Version 2.0 (the
  10. * "License"); you may not use this file except in compliance
  11. * with the License. You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing,
  16. * software distributed under the License is distributed on an
  17. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  18. * KIND, either express or implied. See the License for the
  19. * specific language governing permissions and limitations
  20. * under the License.
  21. *
  22. * @package Graph
  23. * @version //autogen//
  24. * @subpackage Tests
  25. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  26. */
  27. /**
  28. * Tests for ezcGraph class.
  29. *
  30. * @package ImageAnalysis
  31. * @subpackage Tests
  32. */
  33. class ezcGraphDatabaseTest extends ezcTestCase
  34. {
  35. protected $basePath;
  36. protected $tempDir;
  37. public static function suite()
  38. {
  39. return new PHPUnit_Framework_TestSuite( "ezcGraphDatabaseTest" );
  40. }
  41. protected function setUp()
  42. {
  43. static $i = 0;
  44. $this->tempDir = $this->createTempDir( __CLASS__ . sprintf( '_%03d_', ++$i ) ) . '/';
  45. $this->basePath = dirname( __FILE__ ) . '/data/';
  46. // Try to build up database connection
  47. try
  48. {
  49. $db = ezcDbInstance::get();
  50. }
  51. catch ( Exception $e )
  52. {
  53. $this->markTestSkipped( 'Database connection required for PDO statement tests.' );
  54. }
  55. $this->q = new ezcQueryInsert( $db );
  56. try
  57. {
  58. $db->exec( 'DROP TABLE graph_pdo_test' );
  59. }
  60. catch ( Exception $e ) {} // eat
  61. // Create test table
  62. $db->exec( 'CREATE TABLE graph_pdo_test ( id INT, browser VARCHAR(255), hits INT )' );
  63. // Insert some data
  64. $db->exec( "INSERT INTO graph_pdo_test VALUES ( 0, 'Firefox', 2567 )" );
  65. $db->exec( "INSERT INTO graph_pdo_test VALUES ( 0, 'Opera', 543 )" );
  66. $db->exec( "INSERT INTO graph_pdo_test VALUES ( 0, 'Safari', 23 )" );
  67. $db->exec( "INSERT INTO graph_pdo_test VALUES ( 0, 'Konquror', 812 )" );
  68. $db->exec( "INSERT INTO graph_pdo_test VALUES ( 0, 'Lynx', 431 )" );
  69. $db->exec( "INSERT INTO graph_pdo_test VALUES ( 0, 'wget', 912 )" );
  70. }
  71. protected function tearDown()
  72. {
  73. if ( !$this->hasFailed() )
  74. {
  75. $this->removeTempDir();
  76. }
  77. $db = ezcDbInstance::get();
  78. $db->exec( 'DROP TABLE graph_pdo_test' );
  79. }
  80. public function testAutomaticDataSetUsage()
  81. {
  82. $db = ezcDbInstance::get();
  83. $statement = $db->prepare( 'SELECT browser, hits FROM graph_pdo_test' );
  84. $statement->execute();
  85. $dataset = new ezcGraphDatabaseDataSet( $statement );
  86. $dataSetArray = array(
  87. 'Firefox' => 2567,
  88. 'Opera' => 543,
  89. 'Safari' => 23,
  90. 'Konquror' => 812,
  91. 'Lynx' => 431,
  92. 'wget' => 912,
  93. );
  94. $count = 0;
  95. foreach ( $dataset as $key => $value )
  96. {
  97. list( $compareKey, $compareValue ) = each( $dataSetArray );
  98. $this->assertEquals(
  99. $compareKey,
  100. $key,
  101. 'Unexpected key for dataset value.'
  102. );
  103. $this->assertEquals(
  104. $compareValue,
  105. $value,
  106. 'Unexpected value for dataset.'
  107. );
  108. ++$count;
  109. }
  110. $this->assertEquals(
  111. $count,
  112. count( $dataSetArray ),
  113. 'Too few datasets found.'
  114. );
  115. }
  116. public function testAutomaticDataSetUsageSingleColumn()
  117. {
  118. $db = ezcDbInstance::get();
  119. $statement = $db->prepare( 'SELECT hits FROM graph_pdo_test' );
  120. $statement->execute();
  121. $dataset = new ezcGraphDatabaseDataSet( $statement );
  122. $dataSetArray = array(
  123. 'Firefox' => 2567,
  124. 'Opera' => 543,
  125. 'Safari' => 23,
  126. 'Konquror' => 812,
  127. 'Lynx' => 431,
  128. 'wget' => 912,
  129. );
  130. $count = 0;
  131. foreach ( $dataset as $key => $value )
  132. {
  133. list( $compareKey, $compareValue ) = each( $dataSetArray );
  134. $this->assertEquals(
  135. $count,
  136. $key,
  137. 'Unexpected key for dataset value.'
  138. );
  139. $this->assertEquals(
  140. $compareValue,
  141. $value,
  142. 'Unexpected value for dataset.'
  143. );
  144. ++$count;
  145. }
  146. $this->assertEquals(
  147. $count,
  148. count( $dataSetArray ),
  149. 'Too few datasets found.'
  150. );
  151. }
  152. public function testAutomaticDataSetUsageTooManyRows()
  153. {
  154. $db = ezcDbInstance::get();
  155. $statement = $db->prepare( 'SELECT * FROM graph_pdo_test' );
  156. $statement->execute();
  157. try
  158. {
  159. $dataset = new ezcGraphDatabaseDataSet( $statement );
  160. }
  161. catch ( ezcGraphDatabaseTooManyColumnsException $e )
  162. {
  163. return true;
  164. }
  165. $this->fail( 'Expected ezcGraphDatabaseTooManyColumnsException.' );
  166. }
  167. public function testSpecifiedDataSetUsage()
  168. {
  169. $db = ezcDbInstance::get();
  170. $statement = $db->prepare( 'SELECT * FROM graph_pdo_test' );
  171. $statement->execute();
  172. $dataset = new ezcGraphDatabaseDataSet(
  173. $statement,
  174. array(
  175. ezcGraph::KEY => 'browser',
  176. ezcGraph::VALUE => 'hits',
  177. )
  178. );
  179. $dataSetArray = array(
  180. 'Firefox' => 2567,
  181. 'Opera' => 543,
  182. 'Safari' => 23,
  183. 'Konquror' => 812,
  184. 'Lynx' => 431,
  185. 'wget' => 912,
  186. );
  187. $count = 0;
  188. foreach ( $dataset as $key => $value )
  189. {
  190. list( $compareKey, $compareValue ) = each( $dataSetArray );
  191. $this->assertEquals(
  192. $compareKey,
  193. $key,
  194. 'Unexpected key for dataset value.'
  195. );
  196. $this->assertEquals(
  197. $compareValue,
  198. $value,
  199. 'Unexpected value for dataset.'
  200. );
  201. ++$count;
  202. }
  203. $this->assertEquals(
  204. $count,
  205. count( $dataSetArray ),
  206. 'Too few datasets found.'
  207. );
  208. }
  209. public function testSpecifiedDataSetUsageSingleColumn()
  210. {
  211. $db = ezcDbInstance::get();
  212. $statement = $db->prepare( 'SELECT * FROM graph_pdo_test' );
  213. $statement->execute();
  214. $dataset = new ezcGraphDatabaseDataSet(
  215. $statement,
  216. array(
  217. ezcGraph::VALUE => 'hits',
  218. )
  219. );
  220. $dataSetArray = array(
  221. 'Firefox' => 2567,
  222. 'Opera' => 543,
  223. 'Safari' => 23,
  224. 'Konquror' => 812,
  225. 'Lynx' => 431,
  226. 'wget' => 912,
  227. );
  228. $count = 0;
  229. foreach ( $dataset as $key => $value )
  230. {
  231. list( $compareKey, $compareValue ) = each( $dataSetArray );
  232. $this->assertEquals(
  233. $count,
  234. $key,
  235. 'Unexpected key for dataset value.'
  236. );
  237. $this->assertEquals(
  238. $compareValue,
  239. $value,
  240. 'Unexpected value for dataset.'
  241. );
  242. ++$count;
  243. }
  244. $this->assertEquals(
  245. $count,
  246. count( $dataSetArray ),
  247. 'Too few datasets found.'
  248. );
  249. }
  250. public function testSpecifiedDataSetUsageBrokenKey()
  251. {
  252. $db = ezcDbInstance::get();
  253. $statement = $db->prepare( 'SELECT * FROM graph_pdo_test' );
  254. $statement->execute();
  255. try
  256. {
  257. $dataset = new ezcGraphDatabaseDataSet(
  258. $statement,
  259. array(
  260. ezcGraph::KEY => 'nonexistant',
  261. ezcGraph::VALUE => 'hits',
  262. )
  263. );
  264. }
  265. catch ( ezcGraphDatabaseMissingColumnException $e )
  266. {
  267. return true;
  268. }
  269. $this->fail( 'Expected ezcGraphDatabaseMissingColumnException.' );
  270. }
  271. public function testSpecifiedDataSetUsageBrokenValue()
  272. {
  273. $db = ezcDbInstance::get();
  274. $statement = $db->prepare( 'SELECT * FROM graph_pdo_test' );
  275. $statement->execute();
  276. try
  277. {
  278. $dataset = new ezcGraphDatabaseDataSet(
  279. $statement,
  280. array(
  281. ezcGraph::VALUE => 'nonexistant',
  282. )
  283. );
  284. }
  285. catch ( ezcGraphDatabaseMissingColumnException $e )
  286. {
  287. return true;
  288. }
  289. $this->fail( 'Expected ezcGraphDatabaseMissingColumnException.' );
  290. }
  291. public function testNonExceutedQuery()
  292. {
  293. $db = ezcDbInstance::get();
  294. $statement = $db->prepare( 'SELECT browser, hits FROM graph_pdo_test' );
  295. try
  296. {
  297. $dataset = new ezcGraphDatabaseDataSet( $statement );
  298. }
  299. catch ( ezcGraphDatabaseStatementNotExecutedException $e )
  300. {
  301. return true;
  302. }
  303. $this->fail( 'Expected ezcGraphDatabaseStatementNotExecutedException.' );
  304. }
  305. public function testDataSetCount()
  306. {
  307. $db = ezcDbInstance::get();
  308. $statement = $db->prepare( 'SELECT * FROM graph_pdo_test' );
  309. $statement->execute();
  310. $dataset = new ezcGraphDatabaseDataSet(
  311. $statement,
  312. array(
  313. ezcGraph::VALUE => 'hits',
  314. )
  315. );
  316. $this->assertEquals(
  317. count( $dataset ),
  318. 6,
  319. 'Wrong data set item count returned'
  320. );
  321. }
  322. }
  323. ?>