| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335 |
- <?php
- /**
- * Zend Framework
- *
- * LICENSE
- *
- * This source file is subject to the new BSD license that is bundled
- * with this package in the file LICENSE.txt.
- * It is also available through the world-wide-web at this URL:
- * http://framework.zend.com/license/new-bsd
- * If you did not receive a copy of the license and are unable to
- * obtain it through the world-wide-web, please send an email
- * to license@zend.com so we can send you a copy immediately.
- *
- * @category Zend
- * @package Zend_Db
- * @subpackage UnitTests
- * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @version $Id$
- */
- /**
- * @see Zend_Db_Select_TestCommon
- */
- require_once 'Zend/Db/Select/TestCommon.php';
- /**
- * @category Zend
- * @package Zend_Db
- * @subpackage UnitTests
- * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- */
- abstract class Zend_Db_Table_Select_TestCommon extends Zend_Db_Select_TestCommon
- {
- protected $_runtimeIncludePath = null;
- /**
- * @var array of Zend_Db_Table_Abstract
- */
- protected $_table = array();
- public function setUp()
- {
- parent::setUp();
- $this->_table['accounts'] = $this->_getTable('My_ZendDbTable_TableAccounts');
- $this->_table['bugs'] = $this->_getTable('My_ZendDbTable_TableBugs');
- $this->_table['bugs_products'] = $this->_getTable('My_ZendDbTable_TableBugsProducts');
- $this->_table['products'] = $this->_getTable('My_ZendDbTable_TableProducts');
- }
- public function tearDown()
- {
- if ($this->_runtimeIncludePath) {
- $this->_restoreIncludePath();
- }
- parent::tearDown();
- }
- protected function _getTable($tableClass, $options = array())
- {
- if (is_array($options) && !isset($options['db'])) {
- $options['db'] = $this->_db;
- }
- if (!class_exists($tableClass)) {
- $this->_useMyIncludePath();
- Zend_Loader::loadClass($tableClass);
- $this->_restoreIncludePath();
- }
- $table = new $tableClass($options);
- return $table;
- }
- protected function _useMyIncludePath()
- {
- $this->_runtimeIncludePath = get_include_path();
- set_include_path(dirname(__FILE__) . '/../_files/' . PATH_SEPARATOR . $this->_runtimeIncludePath);
- }
- protected function _restoreIncludePath()
- {
- set_include_path($this->_runtimeIncludePath);
- $this->_runtimeIncludePath = null;
- }
- /**
- * Get a Zend_Db_Table to provide the base select()
- *
- * @return Zend_Db_Table_Abstract
- */
- protected function _getSelectTable($table)
- {
- if (!array_key_exists($table, $this->_table)) {
- throw new Zend_Exception('Non-existent table name');
- }
- return $this->_table[$table];
- }
- /**
- * Test adding a FOR UPDATE clause to a Zend_Db_Select object.
- */
- protected function _selectForReadOnly($fields)
- {
- $table = $this->_getSelectTable('products');
- $select = $table->select()
- ->from($table, $fields);
- return $select;
- }
- /**
- * Test adding the FOR UPDATE query modifier to a Zend_Db_Select object.
- *
- */
- public function testSelectForReadOnly()
- {
- $select = $this->_selectForReadOnly(array('count' => 'COUNT(*)'));
- $this->assertTrue($select->isReadOnly());
- $select = $this->_selectForReadOnly(array());
- $this->assertFalse($select->isReadOnly());
- $select = $this->_selectForReadOnly(array('*'));
- $this->assertFalse($select->isReadOnly());
- }
- /**
- * Test adding a JOIN to a Zend_Db_Select object.
- */
- protected function _selectForJoinZendDbTable()
- {
- $table = $this->_getSelectTable('products');
- $select = $table->select()
- ->join(array('p' => 'zfbugs_products'), 'p.product_id = zfproduct.id', 'p.bug_id');
- return $select;
- }
- /**
- * Test adding a join to the select object without setting integrity check to false.
- *
- */
- public function testSelectForJoinZendDbTable()
- {
- $select = $this->_selectForJoinZendDbTable();
- try {
- $query = $select->assemble();
- $this->fail('Expected to catch Zend_Db_Table_Select_Exception');
- } catch (Zend_Exception $e) {
- $this->assertTrue($e instanceof Zend_Db_Table_Select_Exception);
- $this->assertEquals('Select query cannot join with another table', $e->getMessage());
- }
- }
- /**
- * Test adding a FOR UPDATE clause to a Zend_Db_Select object.
- */
- protected function _selectForToString1($tableName = null, $fields = array('*'), $useTable = true)
- {
- $table = $this->_getSelectTable($tableName);
- $select = $table->select();
- if ($useTable) {
- $select->from($table, $fields);
- }
- return $select;
- }
- /**
- * Test adding a FOR UPDATE clause to a Zend_Db_Select object.
- */
- protected function _selectForToString2($tableName, $fields = array('*'))
- {
- $select = $this->_db->select()
- ->from($tableName, $fields);
- return $select;
- }
- /**
- * Test string conversion to ensure Zend_Db_Table_Select is identical
- * to that of Zend_Db_Select.
- *
- */
- public function testSelectForToString()
- {
- // Test for all fields and no default table name on select
- $select1 = $this->_selectForToString1('products', null, false);
- $select2 = $this->_selectForToString2('zfproducts');
- $this->assertEquals($select1->assemble(), $select2->assemble());
- // Test for all fields by default
- $select1 = $this->_selectForToString1('products');
- $select2 = $this->_selectForToString2('zfproducts');
- $this->assertEquals($select1->assemble(), $select2->assemble());
- // Test for selected fields
- $select1 = $this->_selectForToString1('products', array('product_id', 'DISTINCT(product_name)'));
- $select2 = $this->_selectForToString2('zfproducts', array('product_id', 'DISTINCT(product_name)'));
- $this->assertEquals($select1->assemble(), $select2->assemble());
- }
- /**
- * Test to see if a Zend_Db_Table_Select object returns the table it's been
- * instantiated from.
- *
- */
- public function testDbSelectHasTableInstance()
- {
- $table = $this->_getSelectTable('products');
- $select = $table->select();
- $this->assertSame($table, $select->getTable());
- }
- /**
- * @group ZF-2798
- */
- public function testTableWillReturnSelectObjectWithFromPart()
- {
- $table = $this->_getSelectTable('accounts');
- $select1 = $table->select();
- $this->assertEquals(0, count($select1->getPart(Zend_Db_Table_Select::FROM)));
- $this->assertEquals(0, count($select1->getPart(Zend_Db_Table_Select::COLUMNS)));
- $select2 = $table->select(true);
- $this->assertEquals(1, count($select2->getPart(Zend_Db_Table_Select::FROM)));
- $this->assertEquals(1, count($select2->getPart(Zend_Db_Table_Select::COLUMNS)));
- $this->assertEquals($select1->__toString(), $select2->__toString());
- $select3 = $table->select();
- $select3->setIntegrityCheck(false);
- $select3->joinLeft('tableB', 'tableA.id=tableB.id');
- $select3Text = $select3->__toString();
- $this->assertNotContains('zfaccounts', $select3Text);
- $select4 = $table->select(Zend_Db_Table_Abstract::SELECT_WITH_FROM_PART);
- $select4->setIntegrityCheck(false);
- $select4->joinLeft('tableB', 'tableA.id=tableB.id');
- $select4Text = $select4->__toString();
- $this->assertContains('zfaccounts', $select4Text);
- $this->assertContains('tableA', $select4Text);
- $this->assertContains('tableB', $select4Text);
- }
- public function testAssembleDbTableUnionSelect()
- {
- $table = $this->_getSelectTable('accounts');
- $select1 = $table->select();
- $select2 = $table->select();
- $selectUnion = $table->select()->union(array($select1, $select2));
- $selectUnionSql = $selectUnion->assemble();
- }
- /**
- * Test the Adapter's fetchRow() method with a select with offset
- * @group ZF-8944
- */
- public function testAdapterFetchRowWithOffset()
- {
- $table = $this->_getSelectTable('products');
- $products = $this->_db->quoteIdentifier('zfproducts');
- $product_id = $this->_db->quoteIdentifier('product_id');
- // Grab the first two rows
- $data[0] = $this->_db->fetchRow("SELECT * from $products WHERE $product_id = 1");
- $data[1] = $this->_db->fetchRow("SELECT * from $products WHERE $product_id = 2");
- $select = $table->select();
- $select->order('product_id');
- $select->limit(1, 0);
- $row = $this->_db->fetchRow($select);
- $this->assertEquals($data[0], $row);
- $select = $table->select();
- $select->order('product_id');
- $select->limit(1, 1);
- $row = $this->_db->fetchRow($select);
- $this->assertEquals($data[1], $row);
- }
- // ZF-3239
- // public function testFromPartIsAvailableRightAfterInstantiation()
- // {
- // $table = $this->_getSelectTable('products');
- // $select = $table->select();
- //
- // $keys = array_keys($select->getPart(Zend_Db_Select::FROM));
- //
- // $this->assertEquals('zfproducts', array_pop($keys));
- // }
- // ZF-3239 (from comments)
- // public function testColumnsMethodDoesntThrowExceptionRightAfterInstantiation()
- // {
- // $table = $this->_getSelectTable('products');
- //
- // try {
- // $select = $table->select()->columns('product_id');
- //
- // $this->assertTrue($select instanceof Zend_Db_Table_Select);
- // } catch (Zend_Db_Table_Select_Exception $e) {
- // $this->fail('Exception thrown: ' . $e->getMessage());
- // }
- // }
- // ZF-5424
- // public function testColumnsPartDoesntContainWildcardAfterSettingColumns()
- // {
- // $table = $this->_getSelectTable('products');
- //
- // $select = $table->select()->columns('product_id');
- //
- // $columns = $select->getPart(Zend_Db_Select::COLUMNS);
- //
- // $this->assertEquals(1, count($columns));
- // $this->assertEquals('product_id', $columns[0][1]);
- // }
- }
|