2
0

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