/Database/tests/sqlabstraction/param_values.php

https://github.com/F5/zetacomponents · PHP · 345 lines · 272 code · 41 blank · 32 comment · 0 complexity · c16b0e89a7acc5b4b86c2b5bb6eca2cc MD5 · raw file

  1. <?php
  2. /**
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one
  5. * or more contributor license agreements. See the NOTICE file
  6. * distributed with this work for additional information
  7. * regarding copyright ownership. The ASF licenses this file
  8. * to you under the Apache License, Version 2.0 (the
  9. * "License"); you may not use this file except in compliance
  10. * with the License. You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing,
  15. * software distributed under the License is distributed on an
  16. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  17. * KIND, either express or implied. See the License for the
  18. * specific language governing permissions and limitations
  19. * under the License.
  20. *
  21. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  22. * @version //autogentag//
  23. * @filesource
  24. * @package Database
  25. * @subpackage Tests
  26. */
  27. /**
  28. * testing sql abstraction for common rdbms limits
  29. *
  30. * @package Database
  31. * @subpackage Tests
  32. */
  33. class ezcParamValuesTest extends ezcTestCase
  34. {
  35. protected $boolColumn;
  36. protected $intColumn;
  37. protected $nullColumn;
  38. protected $strColumn;
  39. protected $blobColumn;
  40. protected $clobColumn;
  41. protected $blob;
  42. protected $clob;
  43. protected $columnMapping = array(
  44. 'bool' => array(
  45. 'ezcDbHandlerMysql' => 'boolean',
  46. 'ezcDbHandlerOracle' => 'char',
  47. 'ezcDbHandlerPgsql' => 'boolean',
  48. 'ezcDbHandlerSqlite' => 'integer',
  49. 'ezcDbHandlerMssql' => 'integer',
  50. ),
  51. 'int' => array(
  52. 'ezcDbHandlerMysql' => 'bigint',
  53. 'ezcDbHandlerOracle' => 'number',
  54. 'ezcDbHandlerPgsql' => 'bigint',
  55. 'ezcDbHandlerSqlite' => 'integer',
  56. 'ezcDbHandlerMssql' => 'bigint',
  57. ),
  58. 'null' => array(
  59. 'ezcDbHandlerMysql' => 'bigint',
  60. 'ezcDbHandlerOracle' => 'number',
  61. 'ezcDbHandlerPgsql' => 'bigint',
  62. 'ezcDbHandlerSqlite' => 'integer',
  63. 'ezcDbHandlerMssql' => 'bigint',
  64. ),
  65. 'str' => array(
  66. 'ezcDbHandlerMysql' => 'varchar( 255 )',
  67. 'ezcDbHandlerOracle' => 'varchar2( 255 )',
  68. 'ezcDbHandlerPgsql' => 'varchar( 255 )',
  69. 'ezcDbHandlerSqlite' => 'text',
  70. 'ezcDbHandlerMssql' => 'varchar( 255 )',
  71. ),
  72. 'blob' => array(
  73. 'ezcDbHandlerMysql' => 'longblob',
  74. 'ezcDbHandlerOracle' => 'blob',
  75. 'ezcDbHandlerPgsql' => 'bytea',
  76. 'ezcDbHandlerSqlite' => 'blob',
  77. 'ezcDbHandlerMssql' => 'image',
  78. ),
  79. 'clob' => array(
  80. 'ezcDbHandlerMysql' => 'longtext',
  81. 'ezcDbHandlerOracle' => 'clob',
  82. 'ezcDbHandlerPgsql' => 'text',
  83. 'ezcDbHandlerSqlite' => 'clob',
  84. 'ezcDbHandlerMssql' => 'ntext',
  85. ),
  86. );
  87. public static function suite()
  88. {
  89. return new PHPUnit_Framework_TestSuite( 'ezcParamValuesTest' );
  90. }
  91. protected function setUp()
  92. {
  93. try {
  94. $db = ezcDbInstance::get();
  95. }
  96. catch ( Exception $e )
  97. {
  98. $this->markTestSkipped();
  99. }
  100. $this->assertNotNull( $db, 'Database instance is not initialized.' );
  101. $db->exec( 'CREATE TABLE ' .
  102. $db->quoteIdentifier( __CLASS__ ) .
  103. '( ' .
  104. ( $this->boolColumn = $db->quoteIdentifier( 'bool' ) ) . ' ' . $this->columnMapping['bool'][get_class( $db )] . ' NULL, ' .
  105. ( $this->intColumn = $db->quoteIdentifier( 'int' ) ) . ' ' . $this->columnMapping['int' ][get_class( $db )] . ' NULL, ' .
  106. ( $this->nullColumn = $db->quoteIdentifier( 'null' ) ) . ' ' . $this->columnMapping['null'][get_class( $db )] . ' NULL, ' .
  107. ( $this->strColumn = $db->quoteIdentifier( 'str' ) ) . ' ' . $this->columnMapping['str' ][get_class( $db )] . ' NULL, ' .
  108. ( $this->blobColumn = $db->quoteIdentifier( 'blob' ) ) . ' ' . $this->columnMapping['blob'][get_class( $db )] . ' NULL, ' .
  109. ( $this->clobColumn = $db->quoteIdentifier( 'clob' ) ) . ' ' . $this->columnMapping['clob'][get_class( $db )] . ' NULL ' .
  110. ')'
  111. );
  112. // Initialize content
  113. $this->blob = str_repeat( "\x00\x05\x10\x42", 1024 );
  114. $this->clob = str_repeat( "test", 1024 );
  115. }
  116. protected function tearDown()
  117. {
  118. $db = ezcDbInstance::get();
  119. $db->exec( 'DROP TABLE ' .
  120. $db->quoteIdentifier( __CLASS__ )
  121. );
  122. }
  123. public function testBooleanParam()
  124. {
  125. $db = ezcDbInstance::get();
  126. $query = $db->createInsertQuery();
  127. $query
  128. ->insertInto( $db->quoteIdentifier( __CLASS__ ) )
  129. ->set(
  130. $this->boolColumn,
  131. $query->bindValue( true )
  132. );
  133. $query->prepare()->execute();
  134. }
  135. public function testBooleanParamForce()
  136. {
  137. $db = ezcDbInstance::get();
  138. $query = $db->createInsertQuery();
  139. $query
  140. ->insertInto( $db->quoteIdentifier( __CLASS__ ) )
  141. ->set(
  142. $this->boolColumn,
  143. $query->bindValue( true, null, PDO::PARAM_BOOL )
  144. );
  145. $query->prepare()->execute();
  146. }
  147. public function testBooleanParamForceBroken()
  148. {
  149. $db = ezcDbInstance::get();
  150. $query = $db->createInsertQuery();
  151. $query
  152. ->insertInto( $db->quoteIdentifier( __CLASS__ ) )
  153. ->set(
  154. $this->boolColumn,
  155. $query->bindValue( 'some string', null, PDO::PARAM_BOOL )
  156. );
  157. $query->prepare()->execute();
  158. }
  159. public function testIntegerParam()
  160. {
  161. $db = ezcDbInstance::get();
  162. $query = $db->createInsertQuery();
  163. $query
  164. ->insertInto( $db->quoteIdentifier( __CLASS__ ) )
  165. ->set(
  166. $this->intColumn,
  167. $query->bindValue( 42 )
  168. );
  169. $query->prepare()->execute();
  170. }
  171. public function testIntegerParamForce()
  172. {
  173. $db = ezcDbInstance::get();
  174. $query = $db->createInsertQuery();
  175. $query
  176. ->insertInto( $db->quoteIdentifier( __CLASS__ ) )
  177. ->set(
  178. $this->intColumn,
  179. $query->bindValue( 42, null, PDO::PARAM_INT )
  180. );
  181. $query->prepare()->execute();
  182. }
  183. public function testIntegerParamForceBroken()
  184. {
  185. $db = ezcDbInstance::get();
  186. $query = $db->createInsertQuery();
  187. $query
  188. ->insertInto( $db->quoteIdentifier( __CLASS__ ) )
  189. ->set(
  190. $this->intColumn,
  191. $query->bindValue( '42 strings', null, PDO::PARAM_INT )
  192. );
  193. $query->prepare()->execute();
  194. }
  195. public function testStringParam()
  196. {
  197. $db = ezcDbInstance::get();
  198. $query = $db->createInsertQuery();
  199. $query
  200. ->insertInto( $db->quoteIdentifier( __CLASS__ ) )
  201. ->set(
  202. $this->strColumn,
  203. $query->bindValue( 'some string' )
  204. );
  205. $query->prepare()->execute();
  206. }
  207. public function testStringParamForce()
  208. {
  209. $db = ezcDbInstance::get();
  210. $query = $db->createInsertQuery();
  211. $query
  212. ->insertInto( $db->quoteIdentifier( __CLASS__ ) )
  213. ->set(
  214. $this->strColumn,
  215. $query->bindValue( 'some string', null, PDO::PARAM_INT )
  216. );
  217. $query->prepare()->execute();
  218. }
  219. public function testStringParamForceBroken()
  220. {
  221. $db = ezcDbInstance::get();
  222. $query = $db->createInsertQuery();
  223. $query
  224. ->insertInto( $db->quoteIdentifier( __CLASS__ ) )
  225. ->set(
  226. $this->strColumn,
  227. $query->bindValue( true, null, PDO::PARAM_INT )
  228. );
  229. $query->prepare()->execute();
  230. }
  231. public function testClobParam()
  232. {
  233. $db = ezcDbInstance::get();
  234. $query = $db->createInsertQuery();
  235. $query
  236. ->insertInto( $db->quoteIdentifier( __CLASS__ ) )
  237. ->set(
  238. $this->clobColumn,
  239. $query->bindValue( $this->clob )
  240. );
  241. $query->prepare()->execute();
  242. }
  243. public function testClobParamForce()
  244. {
  245. $db = ezcDbInstance::get();
  246. $query = $db->createInsertQuery();
  247. $query
  248. ->insertInto( $db->quoteIdentifier( __CLASS__ ) )
  249. ->set(
  250. $this->clobColumn,
  251. $query->bindValue( $this->clob, null, PDO::PARAM_LOB )
  252. );
  253. $query->prepare()->execute();
  254. }
  255. public function testClobParamForceBroken()
  256. {
  257. $db = ezcDbInstance::get();
  258. $query = $db->createInsertQuery();
  259. $query
  260. ->insertInto( $db->quoteIdentifier( __CLASS__ ) )
  261. ->set(
  262. $this->clobColumn,
  263. $query->bindValue( 42, null, PDO::PARAM_LOB )
  264. );
  265. $query->prepare()->execute();
  266. }
  267. public function testBlobParam()
  268. {
  269. $db = ezcDbInstance::get();
  270. $query = $db->createInsertQuery();
  271. $query
  272. ->insertInto( $db->quoteIdentifier( __CLASS__ ) )
  273. ->set(
  274. $this->blobColumn,
  275. $query->bindValue( $this->blob )
  276. );
  277. $query->prepare()->execute();
  278. }
  279. public function testBlobParamForce()
  280. {
  281. $db = ezcDbInstance::get();
  282. $query = $db->createInsertQuery();
  283. $query
  284. ->insertInto( $db->quoteIdentifier( __CLASS__ ) )
  285. ->set(
  286. $this->blobColumn,
  287. $query->bindValue( $this->blob, null, PDO::PARAM_LOB )
  288. );
  289. $query->prepare()->execute();
  290. }
  291. public function testBlobParamForceBroken()
  292. {
  293. $db = ezcDbInstance::get();
  294. $query = $db->createInsertQuery();
  295. $query
  296. ->insertInto( $db->quoteIdentifier( __CLASS__ ) )
  297. ->set(
  298. $this->blobColumn,
  299. $query->bindValue( 42, null, PDO::PARAM_LOB )
  300. );
  301. $query->prepare()->execute();
  302. }
  303. }
  304. ?>