2
0

TestCommon.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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-2008 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. PHPUnit_Util_Filter::addFileToFilter(__FILE__);
  27. /**
  28. * @category Zend
  29. * @package Zend_Db
  30. * @subpackage UnitTests
  31. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. */
  34. abstract class Zend_Db_Table_Select_TestCommon extends Zend_Db_Select_TestCommon
  35. {
  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. protected function _getTable($tableClass, $options = array())
  49. {
  50. if (is_array($options) && !isset($options['db'])) {
  51. $options['db'] = $this->_db;
  52. }
  53. @Zend_Loader::loadClass($tableClass);
  54. $table = new $tableClass($options);
  55. return $table;
  56. }
  57. /**
  58. * Get a Zend_Db_Table to provide the base select()
  59. *
  60. * @return Zend_Db_Table_Abstract
  61. */
  62. protected function _getSelectTable($table)
  63. {
  64. if (!array_key_exists($table, $this->_table)) {
  65. throw new Zend_Exception('Non-existent table name');
  66. }
  67. return $this->_table[$table];
  68. }
  69. /**
  70. * Test adding a FOR UPDATE clause to a Zend_Db_Select object.
  71. */
  72. protected function _selectForReadOnly($fields)
  73. {
  74. $table = $this->_getSelectTable('products');
  75. $select = $table->select()
  76. ->from($table, $fields);
  77. return $select;
  78. }
  79. /**
  80. * Test adding the FOR UPDATE query modifier to a Zend_Db_Select object.
  81. *
  82. */
  83. public function testSelectForReadOnly()
  84. {
  85. $select = $this->_selectForReadOnly(array('count' => 'COUNT(*)'));
  86. $this->assertTrue($select->isReadOnly());
  87. $select = $this->_selectForReadOnly(array());
  88. $this->assertFalse($select->isReadOnly());
  89. $select = $this->_selectForReadOnly(array('*'));
  90. $this->assertFalse($select->isReadOnly());
  91. }
  92. /**
  93. * Test adding a JOIN to a Zend_Db_Select object.
  94. */
  95. protected function _selectForJoinZendDbTable()
  96. {
  97. $table = $this->_getSelectTable('products');
  98. $select = $table->select()
  99. ->join(array('p' => 'zfbugs_products'), 'p.product_id = zfproduct.id', 'p.bug_id');
  100. return $select;
  101. }
  102. /**
  103. * Test adding a join to the select object without setting integrity check to false.
  104. *
  105. */
  106. public function testSelectForJoinZendDbTable()
  107. {
  108. $select = $this->_selectForJoinZendDbTable();
  109. try {
  110. $query = $select->assemble();
  111. $this->fail('Expected to catch Zend_Db_Table_Select_Exception');
  112. } catch (Zend_Exception $e) {
  113. $this->assertType('Zend_Db_Table_Select_Exception', $e);
  114. $this->assertEquals('Select query cannot join with another table', $e->getMessage());
  115. }
  116. }
  117. /**
  118. * Test adding a FOR UPDATE clause to a Zend_Db_Select object.
  119. */
  120. protected function _selectForToString1($tableName = null, $fields = array('*'), $useTable = true)
  121. {
  122. $table = $this->_getSelectTable($tableName);
  123. $select = $table->select();
  124. if ($useTable) {
  125. $select->from($table, $fields);
  126. }
  127. return $select;
  128. }
  129. /**
  130. * Test adding a FOR UPDATE clause to a Zend_Db_Select object.
  131. */
  132. protected function _selectForToString2($tableName, $fields = array('*'))
  133. {
  134. $select = $this->_db->select()
  135. ->from($tableName, $fields);
  136. return $select;
  137. }
  138. /**
  139. * Test string conversion to ensure Zend_Db_Table_Select is identical
  140. * to that of Zend_Db_Select.
  141. *
  142. */
  143. public function testSelectForToString()
  144. {
  145. // Test for all fields and no default table name on select
  146. $select1 = $this->_selectForToString1('products', null, false);
  147. $select2 = $this->_selectForToString2('zfproducts');
  148. $this->assertEquals($select1->assemble(), $select2->assemble());
  149. // Test for all fields by default
  150. $select1 = $this->_selectForToString1('products');
  151. $select2 = $this->_selectForToString2('zfproducts');
  152. $this->assertEquals($select1->assemble(), $select2->assemble());
  153. // Test for selected fields
  154. $select1 = $this->_selectForToString1('products', array('product_id', 'DISTINCT(product_name)'));
  155. $select2 = $this->_selectForToString2('zfproducts', array('product_id', 'DISTINCT(product_name)'));
  156. $this->assertEquals($select1->assemble(), $select2->assemble());
  157. }
  158. /**
  159. * Test to see if a Zend_Db_Table_Select object returns the table it's been
  160. * instantiated from.
  161. *
  162. */
  163. public function testDbSelectHasTableInstance()
  164. {
  165. $table = $this->_getSelectTable('products');
  166. $select = $table->select();
  167. $this->assertType('My_ZendDbTable_Products', $select->getTable());
  168. }
  169. /**
  170. * @group ZF-2798
  171. */
  172. public function testTableWillReturnSelectObjectWithFromPart()
  173. {
  174. $table = $this->_getSelectTable('accounts');
  175. $select1 = $table->select();
  176. $this->assertEquals(0, count($select1->getPart(Zend_Db_Table_Select::FROM)));
  177. $this->assertEquals(0, count($select1->getPart(Zend_Db_Table_Select::COLUMNS)));
  178. $select2 = $table->select(true);
  179. $this->assertEquals(1, count($select2->getPart(Zend_Db_Table_Select::FROM)));
  180. $this->assertEquals(1, count($select2->getPart(Zend_Db_Table_Select::COLUMNS)));
  181. $this->assertEquals($select1->__toString(), $select2->__toString());
  182. $select3 = $table->select();
  183. $select3->setIntegrityCheck(false);
  184. $select3->joinLeft('tableB', 'tableA.id=tableB.id');
  185. $select3Text = $select3->__toString();
  186. $this->assertNotContains('zfaccounts', $select3Text);
  187. $select4 = $table->select(Zend_Db_Table_Abstract::SELECT_WITH_FROM_PART);
  188. $select4->setIntegrityCheck(false);
  189. $select4->joinLeft('tableB', 'tableA.id=tableB.id');
  190. $select4Text = $select4->__toString();
  191. $this->assertContains('zfaccounts', $select4Text);
  192. $this->assertContains('tableA', $select4Text);
  193. $this->assertContains('tableB', $select4Text);
  194. }
  195. // ZF-3239
  196. // public function testFromPartIsAvailableRightAfterInstantiation()
  197. // {
  198. // $table = $this->_getSelectTable('products');
  199. // $select = $table->select();
  200. //
  201. // $keys = array_keys($select->getPart(Zend_Db_Select::FROM));
  202. //
  203. // $this->assertEquals('zfproducts', array_pop($keys));
  204. // }
  205. // ZF-3239 (from comments)
  206. // public function testColumnsMethodDoesntThrowExceptionRightAfterInstantiation()
  207. // {
  208. // $table = $this->_getSelectTable('products');
  209. //
  210. // try {
  211. // $select = $table->select()->columns('product_id');
  212. //
  213. // $this->assertType('Zend_Db_Table_Select', $select);
  214. // } catch (Zend_Db_Table_Select_Exception $e) {
  215. // $this->fail('Exception thrown: ' . $e->getMessage());
  216. // }
  217. // }
  218. // ZF-5424
  219. // public function testColumnsPartDoesntContainWildcardAfterSettingColumns()
  220. // {
  221. // $table = $this->_getSelectTable('products');
  222. //
  223. // $select = $table->select()->columns('product_id');
  224. //
  225. // $columns = $select->getPart(Zend_Db_Select::COLUMNS);
  226. //
  227. // $this->assertEquals(1, count($columns));
  228. // $this->assertEquals('product_id', $columns[0][1]);
  229. // }
  230. }