TestCommon.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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('Zend_Db_Table_TableAccounts');
  44. $this->_table['bugs'] = $this->_getTable('Zend_Db_Table_TableBugs');
  45. $this->_table['bugs_products'] = $this->_getTable('Zend_Db_Table_TableBugsProducts');
  46. $this->_table['products'] = $this->_getTable('Zend_Db_Table_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. protected function _getSelectTable($table)
  61. {
  62. if (!array_key_exists($table, $this->_table)) {
  63. throw new Zend_Exception('Non-existent table name');
  64. }
  65. return $this->_table[$table];
  66. }
  67. /**
  68. * Test adding a FOR UPDATE clause to a Zend_Db_Select object.
  69. */
  70. protected function _selectForReadOnly($fields)
  71. {
  72. $table = $this->_getSelectTable('products');
  73. $select = $table->select()
  74. ->from($table, $fields);
  75. return $select;
  76. }
  77. /**
  78. * Test adding the FOR UPDATE query modifier to a Zend_Db_Select object.
  79. *
  80. */
  81. public function testSelectForReadOnly()
  82. {
  83. $select = $this->_selectForReadOnly(array('count' => 'COUNT(*)'));
  84. $this->assertTrue($select->isReadOnly());
  85. $select = $this->_selectForReadOnly(array());
  86. $this->assertFalse($select->isReadOnly());
  87. $select = $this->_selectForReadOnly(array('*'));
  88. $this->assertFalse($select->isReadOnly());
  89. }
  90. /**
  91. * Test adding a JOIN to a Zend_Db_Select object.
  92. */
  93. protected function _selectForJoinZendDbTable()
  94. {
  95. $table = $this->_getSelectTable('products');
  96. $select = $table->select()
  97. ->join(array('p' => 'zfbugs_products'), 'p.product_id = zfproduct.id', 'p.bug_id');
  98. return $select;
  99. }
  100. /**
  101. * Test adding a join to the select object without setting integrity check to false.
  102. *
  103. */
  104. public function testSelectForJoinZendDbTable()
  105. {
  106. $select = $this->_selectForJoinZendDbTable();
  107. try {
  108. $query = $select->assemble();
  109. $this->fail('Expected to catch Zend_Db_Table_Select_Exception');
  110. } catch (Zend_Exception $e) {
  111. $this->assertType('Zend_Db_Table_Select_Exception', $e);
  112. $this->assertEquals('Select query cannot join with another table', $e->getMessage());
  113. }
  114. }
  115. /**
  116. * Test adding a FOR UPDATE clause to a Zend_Db_Select object.
  117. */
  118. protected function _selectForToString1($tableName = null, $fields = array('*'), $useTable = true)
  119. {
  120. $table = $this->_getSelectTable($tableName);
  121. $select = $table->select();
  122. if ($useTable) {
  123. $select->from($table, $fields);
  124. }
  125. return $select;
  126. }
  127. /**
  128. * Test adding a FOR UPDATE clause to a Zend_Db_Select object.
  129. */
  130. protected function _selectForToString2($tableName, $fields = array('*'))
  131. {
  132. $select = $this->_db->select()
  133. ->from($tableName, $fields);
  134. return $select;
  135. }
  136. /**
  137. * Test string conversion to ensure Zend_Db_Table_Select is identical
  138. * to that of Zend_Db_Select.
  139. *
  140. */
  141. public function testSelectForToString()
  142. {
  143. // Test for all fields and no default table name on select
  144. $select1 = $this->_selectForToString1('products', null, false);
  145. $select2 = $this->_selectForToString2('zfproducts');
  146. $this->assertEquals($select1->assemble(), $select2->assemble());
  147. // Test for all fields by default
  148. $select1 = $this->_selectForToString1('products');
  149. $select2 = $this->_selectForToString2('zfproducts');
  150. $this->assertEquals($select1->assemble(), $select2->assemble());
  151. // Test for selected fields
  152. $select1 = $this->_selectForToString1('products', array('product_id', 'DISTINCT(product_name)'));
  153. $select2 = $this->_selectForToString2('zfproducts', array('product_id', 'DISTINCT(product_name)'));
  154. $this->assertEquals($select1->assemble(), $select2->assemble());
  155. }
  156. /**
  157. * Test to see if a Zend_Db_Table_Select object returns the table it's been
  158. * instantiated from.
  159. *
  160. */
  161. public function testDbSelectHasTableInstance()
  162. {
  163. $table = $this->_getSelectTable('products');
  164. $select = $table->select();
  165. $this->assertType('Zend_Db_Table_TableProducts', $select->getTable());
  166. }
  167. // ZF-3239
  168. // public function testFromPartIsAvailableRightAfterInstantiation()
  169. // {
  170. // $table = $this->_getSelectTable('products');
  171. // $select = $table->select();
  172. //
  173. // $keys = array_keys($select->getPart(Zend_Db_Select::FROM));
  174. //
  175. // $this->assertEquals('zfproducts', array_pop($keys));
  176. // }
  177. // ZF-3239 (from comments)
  178. // public function testColumnsMethodDoesntThrowExceptionRightAfterInstantiation()
  179. // {
  180. // $table = $this->_getSelectTable('products');
  181. //
  182. // try {
  183. // $select = $table->select()->columns('product_id');
  184. //
  185. // $this->assertType('Zend_Db_Table_Select', $select);
  186. // } catch (Zend_Db_Table_Select_Exception $e) {
  187. // $this->fail('Exception thrown: ' . $e->getMessage());
  188. // }
  189. // }
  190. // ZF-5424
  191. // public function testColumnsPartDoesntContainWildcardAfterSettingColumns()
  192. // {
  193. // $table = $this->_getSelectTable('products');
  194. //
  195. // $select = $table->select()->columns('product_id');
  196. //
  197. // $columns = $select->getPart(Zend_Db_Select::COLUMNS);
  198. //
  199. // $this->assertEquals(1, count($columns));
  200. // $this->assertEquals('product_id', $columns[0][1]);
  201. // }
  202. }