2
0

TestCommon.php 9.0 KB

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