TestCommon.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Db
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Db_Select_TestCommon
  24. */
  25. require_once 'Zend/Db/Select/TestCommon.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Db
  29. * @subpackage UnitTests
  30. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. abstract class Zend_Db_Table_Select_TestCommon extends Zend_Db_Select_TestCommon
  34. {
  35. protected $_runtimeIncludePath = null;
  36. /**
  37. * @var array of Zend_Db_Table_Abstract
  38. */
  39. protected $_table = array();
  40. public function setUp()
  41. {
  42. parent::setUp();
  43. $this->_table['accounts'] = $this->_getTable('My_ZendDbTable_TableAccounts');
  44. $this->_table['bugs'] = $this->_getTable('My_ZendDbTable_TableBugs');
  45. $this->_table['bugs_products'] = $this->_getTable('My_ZendDbTable_TableBugsProducts');
  46. $this->_table['products'] = $this->_getTable('My_ZendDbTable_TableProducts');
  47. }
  48. public function tearDown()
  49. {
  50. if ($this->_runtimeIncludePath) {
  51. $this->_restoreIncludePath();
  52. }
  53. parent::tearDown();
  54. }
  55. protected function _getTable($tableClass, $options = array())
  56. {
  57. if (is_array($options) && !isset($options['db'])) {
  58. $options['db'] = $this->_db;
  59. }
  60. if (!class_exists($tableClass)) {
  61. $this->_useMyIncludePath();
  62. Zend_Loader::loadClass($tableClass);
  63. $this->_restoreIncludePath();
  64. }
  65. $table = new $tableClass($options);
  66. return $table;
  67. }
  68. protected function _useMyIncludePath()
  69. {
  70. $this->_runtimeIncludePath = get_include_path();
  71. set_include_path(dirname(__FILE__) . '/../_files/' . PATH_SEPARATOR . $this->_runtimeIncludePath);
  72. }
  73. protected function _restoreIncludePath()
  74. {
  75. set_include_path($this->_runtimeIncludePath);
  76. $this->_runtimeIncludePath = null;
  77. }
  78. /**
  79. * Get a Zend_Db_Table to provide the base select()
  80. *
  81. * @return Zend_Db_Table_Abstract
  82. */
  83. protected function _getSelectTable($table)
  84. {
  85. if (!array_key_exists($table, $this->_table)) {
  86. throw new Zend_Exception('Non-existent table name');
  87. }
  88. return $this->_table[$table];
  89. }
  90. /**
  91. * Test adding a FOR UPDATE clause to a Zend_Db_Select object.
  92. */
  93. protected function _selectForReadOnly($fields)
  94. {
  95. $table = $this->_getSelectTable('products');
  96. $select = $table->select()
  97. ->from($table, $fields);
  98. return $select;
  99. }
  100. /**
  101. * Test adding the FOR UPDATE query modifier to a Zend_Db_Select object.
  102. *
  103. */
  104. public function testSelectForReadOnly()
  105. {
  106. $select = $this->_selectForReadOnly(array('count' => 'COUNT(*)'));
  107. $this->assertTrue($select->isReadOnly());
  108. $select = $this->_selectForReadOnly(array());
  109. $this->assertFalse($select->isReadOnly());
  110. $select = $this->_selectForReadOnly(array('*'));
  111. $this->assertFalse($select->isReadOnly());
  112. }
  113. /**
  114. * Test adding a JOIN to a Zend_Db_Select object.
  115. */
  116. protected function _selectForJoinZendDbTable()
  117. {
  118. $table = $this->_getSelectTable('products');
  119. $select = $table->select()
  120. ->join(array('p' => 'zfbugs_products'), 'p.product_id = zfproduct.id', 'p.bug_id');
  121. return $select;
  122. }
  123. /**
  124. * Test adding a join to the select object without setting integrity check to false.
  125. *
  126. */
  127. public function testSelectForJoinZendDbTable()
  128. {
  129. $select = $this->_selectForJoinZendDbTable();
  130. try {
  131. $query = $select->assemble();
  132. $this->fail('Expected to catch Zend_Db_Table_Select_Exception');
  133. } catch (Zend_Exception $e) {
  134. $this->assertTrue($e instanceof Zend_Db_Table_Select_Exception);
  135. $this->assertEquals('Select query cannot join with another table', $e->getMessage());
  136. }
  137. }
  138. /**
  139. * Test adding a FOR UPDATE clause to a Zend_Db_Select object.
  140. */
  141. protected function _selectForToString1($tableName = null, $fields = array('*'), $useTable = true)
  142. {
  143. $table = $this->_getSelectTable($tableName);
  144. $select = $table->select();
  145. if ($useTable) {
  146. $select->from($table, $fields);
  147. }
  148. return $select;
  149. }
  150. /**
  151. * Test adding a FOR UPDATE clause to a Zend_Db_Select object.
  152. */
  153. protected function _selectForToString2($tableName, $fields = array('*'))
  154. {
  155. $select = $this->_db->select()
  156. ->from($tableName, $fields);
  157. return $select;
  158. }
  159. /**
  160. * Test string conversion to ensure Zend_Db_Table_Select is identical
  161. * to that of Zend_Db_Select.
  162. *
  163. */
  164. public function testSelectForToString()
  165. {
  166. // Test for all fields and no default table name on select
  167. $select1 = $this->_selectForToString1('products', null, false);
  168. $select2 = $this->_selectForToString2('zfproducts');
  169. $this->assertEquals($select1->assemble(), $select2->assemble());
  170. // Test for all fields by default
  171. $select1 = $this->_selectForToString1('products');
  172. $select2 = $this->_selectForToString2('zfproducts');
  173. $this->assertEquals($select1->assemble(), $select2->assemble());
  174. // Test for selected fields
  175. $select1 = $this->_selectForToString1('products', array('product_id', 'DISTINCT(product_name)'));
  176. $select2 = $this->_selectForToString2('zfproducts', array('product_id', 'DISTINCT(product_name)'));
  177. $this->assertEquals($select1->assemble(), $select2->assemble());
  178. }
  179. /**
  180. * Test to see if a Zend_Db_Table_Select object returns the table it's been
  181. * instantiated from.
  182. *
  183. */
  184. public function testDbSelectHasTableInstance()
  185. {
  186. $table = $this->_getSelectTable('products');
  187. $select = $table->select();
  188. $this->assertSame($table, $select->getTable());
  189. }
  190. /**
  191. * @group ZF-2798
  192. */
  193. public function testTableWillReturnSelectObjectWithFromPart()
  194. {
  195. $table = $this->_getSelectTable('accounts');
  196. $select1 = $table->select();
  197. $this->assertEquals(0, count($select1->getPart(Zend_Db_Table_Select::FROM)));
  198. $this->assertEquals(0, count($select1->getPart(Zend_Db_Table_Select::COLUMNS)));
  199. $select2 = $table->select(true);
  200. $this->assertEquals(1, count($select2->getPart(Zend_Db_Table_Select::FROM)));
  201. $this->assertEquals(1, count($select2->getPart(Zend_Db_Table_Select::COLUMNS)));
  202. $this->assertEquals($select1->__toString(), $select2->__toString());
  203. $select3 = $table->select();
  204. $select3->setIntegrityCheck(false);
  205. $select3->joinLeft('tableB', 'tableA.id=tableB.id');
  206. $select3Text = $select3->__toString();
  207. $this->assertNotContains('zfaccounts', $select3Text);
  208. $select4 = $table->select(Zend_Db_Table_Abstract::SELECT_WITH_FROM_PART);
  209. $select4->setIntegrityCheck(false);
  210. $select4->joinLeft('tableB', 'tableA.id=tableB.id');
  211. $select4Text = $select4->__toString();
  212. $this->assertContains('zfaccounts', $select4Text);
  213. $this->assertContains('tableA', $select4Text);
  214. $this->assertContains('tableB', $select4Text);
  215. }
  216. public function testAssembleDbTableUnionSelect()
  217. {
  218. $table = $this->_getSelectTable('accounts');
  219. $select1 = $table->select();
  220. $select2 = $table->select();
  221. $selectUnion = $table->select()->union(array($select1, $select2));
  222. $selectUnionSql = $selectUnion->assemble();
  223. }
  224. /**
  225. * Test the Adapter's fetchRow() method with a select with offset
  226. * @group ZF-8944
  227. */
  228. public function testAdapterFetchRowWithOffset()
  229. {
  230. $table = $this->_getSelectTable('products');
  231. $products = $this->_db->quoteIdentifier('zfproducts');
  232. $product_id = $this->_db->quoteIdentifier('product_id');
  233. // Grab the first two rows
  234. $data[0] = $this->_db->fetchRow("SELECT * from $products WHERE $product_id = 1");
  235. $data[1] = $this->_db->fetchRow("SELECT * from $products WHERE $product_id = 2");
  236. $select = $table->select();
  237. $select->order('product_id');
  238. $select->limit(1, 0);
  239. $row = $this->_db->fetchRow($select);
  240. $this->assertEquals($data[0], $row);
  241. $select = $table->select();
  242. $select->order('product_id');
  243. $select->limit(1, 1);
  244. $row = $this->_db->fetchRow($select);
  245. $this->assertEquals($data[1], $row);
  246. }
  247. // ZF-3239
  248. // public function testFromPartIsAvailableRightAfterInstantiation()
  249. // {
  250. // $table = $this->_getSelectTable('products');
  251. // $select = $table->select();
  252. //
  253. // $keys = array_keys($select->getPart(Zend_Db_Select::FROM));
  254. //
  255. // $this->assertEquals('zfproducts', array_pop($keys));
  256. // }
  257. // ZF-3239 (from comments)
  258. // public function testColumnsMethodDoesntThrowExceptionRightAfterInstantiation()
  259. // {
  260. // $table = $this->_getSelectTable('products');
  261. //
  262. // try {
  263. // $select = $table->select()->columns('product_id');
  264. //
  265. // $this->assertTrue($select instanceof Zend_Db_Table_Select);
  266. // } catch (Zend_Db_Table_Select_Exception $e) {
  267. // $this->fail('Exception thrown: ' . $e->getMessage());
  268. // }
  269. // }
  270. // ZF-5424
  271. // public function testColumnsPartDoesntContainWildcardAfterSettingColumns()
  272. // {
  273. // $table = $this->_getSelectTable('products');
  274. //
  275. // $select = $table->select()->columns('product_id');
  276. //
  277. // $columns = $select->getPart(Zend_Db_Select::COLUMNS);
  278. //
  279. // $this->assertEquals(1, count($columns));
  280. // $this->assertEquals('product_id', $columns[0][1]);
  281. // }
  282. }