| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146 |
- <?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-2008 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @version $Id$
- */
- /**
- * @see Zend_Db_Table_TestSetup
- */
- require_once 'Zend/Db/Table/TestSetup.php';
- /**
- * @see Zend_Loader
- */
- require_once 'Zend/Loader.php';
- PHPUnit_Util_Filter::addFileToFilter(__FILE__);
- /**
- * @category Zend
- * @package Zend_Db
- * @subpackage UnitTests
- * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- */
- abstract class Zend_Db_Table_Relationships_TestCommon extends Zend_Db_Table_TestSetup
- {
- public function testTableRelationshipFindParentRow()
- {
- $bug_id = $this->_db->quoteIdentifier('bug_id', true);
- $account_name = $this->_db->foldCase('account_name');
- $table = $this->_table['bugs'];
- $childRows = $table->fetchAll("$bug_id = 1");
- $this->assertType('Zend_Db_Table_Rowset_Abstract', $childRows,
- 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($childRows));
- $childRow1 = $childRows->current();
- $this->assertType('Zend_Db_Table_Row_Abstract', $childRow1,
- 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($childRow1));
- $parentRow = $childRow1->findParentRow('My_ZendDbTable_TableAccounts');
- $this->assertType('Zend_Db_Table_Row_Abstract', $parentRow,
- 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($parentRow));
- $this->assertEquals('goofy', $parentRow->$account_name);
- }
- public function testTableRelationshipFindParentRowSelect()
- {
- $bug_id = $this->_db->quoteIdentifier('bug_id', true);
- $account_name = $this->_db->foldCase('account_name');
- $account_name_column = $this->_db->quoteIdentifier('account_name', true);
- $table = $this->_table['bugs'];
- $select = $table->select()->where($account_name_column . ' = ?', 'goofy');
- $childRows = $table->fetchAll("$bug_id = 1");
- $this->assertType('Zend_Db_Table_Rowset_Abstract', $childRows,
- 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($childRows));
- $childRow1 = $childRows->current();
- $this->assertType('Zend_Db_Table_Row_Abstract', $childRow1,
- 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($childRow1));
- $parentRow = $childRow1->findParentRow('My_ZendDbTable_TableAccounts', null, $select);
- $this->assertType('Zend_Db_Table_Row_Abstract', $parentRow,
- 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($parentRow));
- $this->assertEquals('goofy', $parentRow->$account_name);
- }
- public function testTableRelationshipMagicFindParentRow()
- {
- $bug_id = $this->_db->quoteIdentifier('bug_id', true);
- $account_name = $this->_db->foldCase('account_name');
- $table = $this->_table['bugs'];
- $childRows = $table->fetchAll("$bug_id = 1");
- $this->assertType('Zend_Db_Table_Rowset_Abstract', $childRows,
- 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($childRows));
- $childRow1 = $childRows->current();
- $this->assertType('Zend_Db_Table_Row_Abstract', $childRow1,
- 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($childRow1));
- $parentRow = $childRow1->findParentMy_ZendDbTable_TableAccounts();
- $this->assertType('Zend_Db_Table_Row_Abstract', $parentRow,
- 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($parentRow));
- $this->assertEquals('goofy', $parentRow->$account_name);
- }
- public function testTableRelationshipMagicFindParentRowSelect()
- {
- $bug_id = $this->_db->quoteIdentifier('bug_id', true);
- $account_name = $this->_db->foldCase('account_name');
- $account_name_column = $this->_db->quoteIdentifier('account_name', true);
- $table = $this->_table['bugs'];
- $select = $table->select()->where($account_name_column . ' = ?', 'goofy');
- $childRows = $table->fetchAll("$bug_id = 1");
- $this->assertType('Zend_Db_Table_Rowset_Abstract', $childRows,
- 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($childRows));
- $childRow1 = $childRows->current();
- $this->assertType('Zend_Db_Table_Row_Abstract', $childRow1,
- 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($childRow1));
- $parentRow = $childRow1->findParentMy_ZendDbTable_TableAccounts($select);
- $this->assertType('Zend_Db_Table_Row_Abstract', $parentRow,
- 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($parentRow));
- $this->assertEquals('goofy', $parentRow->$account_name);
- }
- public function testTableRelationshipMagicException()
- {
- $table = $this->_table['bugs'];
- $parentRows = $table->find(1);
- $parentRow1 = $parentRows->current();
- // Completely bogus method
- try {
- $result = $parentRow1->nonExistantMethod();
- $this->fail('Expected to catch Zend_Db_Table_Row_Exception');
- } catch (Zend_Exception $e) {
- $this->assertType('Zend_Db_Table_Row_Exception', $e,
- 'Expecting object of type Zend_Db_Table_Row_Exception got '.get_class($e));
- $this->assertEquals("Unrecognized method 'nonExistantMethod()'", $e->getMessage());
- }
- }
- public function testTableRelationshipFindParentRowException()
- {
- $bug_id = $this->_db->quoteIdentifier('bug_id', true);
- $table = $this->_table['bugs'];
- $childRows = $table->fetchAll("$bug_id = 1");
- $childRow1 = $childRows->current();
- try {
- $parentRow = $childRow1->findParentRow('nonexistant_class');
- $this->fail('Expected to catch Zend_Db_Table_Row_Exception for nonexistent table class');
- } catch (Zend_Exception $e) {
- $this->assertType('Zend_Db_Table_Row_Exception', $e,
- 'Expecting object of type Zend_Db_Table_Row_Exception got '.get_class($e));
- $this->assertEquals('File "nonexistant' . DIRECTORY_SEPARATOR . 'class.php" does not exist or class "nonexistant_class" was not found in the file', $e->getMessage());
- }
- try {
- $parentRow = $childRow1->findParentRow(new stdClass());
- $this->fail('Expected to catch Zend_Db_Table_Row_Exception for wrong table class');
- } catch (Zend_Exception $e) {
- $this->assertType('Zend_Db_Table_Exception', $e,
- 'Expecting object of type Zend_Db_Table_Exception got '.get_class($e));
- $this->assertEquals('Parent table must be a Zend_Db_Table_Abstract, but it is stdClass', $e->getMessage());
- }
- }
- public function testTableRelationshipFindManyToManyRowset()
- {
- $table = $this->_table['bugs'];
- $originRows = $table->find(1);
- $originRow1 = $originRows->current();
- $destRows = $originRow1->findManyToManyRowset('My_ZendDbTable_TableProducts', 'My_ZendDbTable_TableBugsProducts');
- $this->assertType('Zend_Db_Table_Rowset_Abstract', $destRows,
- 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($destRows));
- $this->assertEquals(3, $destRows->count());
- }
- public function testTableRelationshipFindManyToManyRowsetSelect()
- {
- $product_name = $this->_db->foldCase('product_name');
- $bug_id = $this->_db->foldCase('bug_id');
- $bug_id_column = $this->_db->quoteIdentifier('bug_id', true);
- $table = $this->_table['bugs'];
- $select = $table->select()->where($bug_id_column . ' = ?', 1)
- ->limit(2)
- ->order($product_name . ' ASC');
- $originRows = $table->find(1);
- $originRow1 = $originRows->current();
- $destRows = $originRow1->findManyToManyRowset('My_ZendDbTable_TableProducts', 'My_ZendDbTable_TableBugsProducts',
- null, null, $select);
- $this->assertType('Zend_Db_Table_Rowset_Abstract', $destRows,
- 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($destRows));
- $this->assertEquals(2, $destRows->count());
- $childRow = $destRows->current();
- $this->assertEquals('Linux', $childRow->$product_name);
- }
- public function testTableRelationshipMagicFindManyToManyRowset()
- {
- $table = $this->_table['bugs'];
- $originRows = $table->find(1);
- $originRow1 = $originRows->current();
- $destRows = $originRow1->findMy_ZendDbTable_TableProductsViaMy_ZendDbTable_TableBugsProducts();
- $this->assertType('Zend_Db_Table_Rowset_Abstract', $destRows,
- 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($destRows));
- $this->assertEquals(3, $destRows->count());
- }
- public function testTableRelationshipMagicFindManyToManyRowsetSelect()
- {
- $product_name = $this->_db->foldCase('product_name');
- $bug_id = $this->_db->foldCase('bug_id');
- $bug_id_column = $this->_db->quoteIdentifier('bug_id', true);
- $table = $this->_table['bugs'];
- $select = $table->select()->where($bug_id_column . ' = ?', 1)
- ->limit(2)
- ->order($product_name . ' ASC');
- $originRows = $table->find(1);
- $originRow1 = $originRows->current();
- $destRows = $originRow1->findMy_ZendDbTable_TableProductsViaMy_ZendDbTable_TableBugsProducts($select);
- $this->assertType('Zend_Db_Table_Rowset_Abstract', $destRows,
- 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($destRows));
- $this->assertEquals(2, $destRows->count());
- $childRow = $destRows->current();
- $this->assertEquals('Linux', $childRow->$product_name);
- }
- public function testTableRelationshipFindManyToManyRowsetException()
- {
- $table = $this->_table['bugs'];
- $originRows = $table->find(1);
- $originRow1 = $originRows->current();
- // Use nonexistant class for destination table
- try {
- $destRows = $originRow1->findManyToManyRowset('nonexistant_class', 'My_ZendDbTable_TableBugsProducts');
- $this->fail('Expected to catch Zend_Db_Table_Row_Exception for nonexistent table class');
- } catch (Zend_Exception $e) {
- $this->assertType('Zend_Db_Table_Exception', $e,
- 'Expecting object of type Zend_Db_Table_Exception got '.get_class($e));
- $this->assertEquals('File "nonexistant' . DIRECTORY_SEPARATOR . 'class.php" does not exist or class "nonexistant_class" was not found in the file', $e->getMessage());
- }
- // Use stdClass instead of table class for destination table
- try {
- $destRows = $originRow1->findManyToManyRowset(new stdClass(), 'My_ZendDbTable_TableBugsProducts');
- $this->fail('Expected to catch Zend_Db_Table_Row_Exception for nonexistent table class');
- } catch (Zend_Exception $e) {
- $this->assertType('Zend_Db_Table_Exception', $e,
- 'Expecting object of type Zend_Db_Table_Exception got '.get_class($e));
- $this->assertEquals('Match table must be a Zend_Db_Table_Abstract, but it is stdClass', $e->getMessage());
- }
- // Use nonexistant class for intersection table
- try {
- $destRows = $originRow1->findManyToManyRowset('My_ZendDbTable_TableProducts', 'nonexistant_class');
- $this->fail('Expected to catch Zend_Db_Table_Row_Exception for nonexistent table class');
- } catch (Zend_Exception $e) {
- $this->assertType('Zend_Db_Table_Exception', $e,
- 'Expecting object of type Zend_Db_Table_Exception got '.get_class($e));
- $this->assertEquals('File "nonexistant' . DIRECTORY_SEPARATOR . 'class.php" does not exist or class "nonexistant_class" was not found in the file', $e->getMessage());
- }
- // Use stdClass instead of table class for intersection table
- try {
- $destRows = $originRow1->findManyToManyRowset('My_ZendDbTable_TableProducts', new stdClass());
- $this->fail('Expected to catch Zend_Db_Table_Row_Exception for nonexistent table class');
- } catch (Zend_Exception $e) {
- $this->assertType('Zend_Db_Table_Exception', $e,
- 'Expecting object of type Zend_Db_Table_Exception got '.get_class($e));
- $this->assertEquals('Intersection table must be a Zend_Db_Table_Abstract, but it is stdClass', $e->getMessage());
- }
- }
- public function testTableRelationshipFindDependentRowset()
- {
- $table = $this->_table['bugs'];
- $bug_id = $this->_db->foldCase('bug_id');
- $product_id = $this->_db->foldCase('product_id');
- $parentRows = $table->find(1);
- $this->assertType('Zend_Db_Table_Rowset_Abstract', $parentRows,
- 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($parentRows));
- $parentRow1 = $parentRows->current();
- $childRows = $parentRow1->findDependentRowset('My_ZendDbTable_TableBugsProducts');
- $this->assertType('Zend_Db_Table_Rowset_Abstract', $childRows,
- 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($childRows));
- $this->assertEquals(3, $childRows->count());
- $childRow1 = $childRows->current();
- $this->assertType('Zend_Db_Table_Row_Abstract', $childRow1,
- 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($childRow1));
- $this->assertEquals(1, $childRow1->$bug_id);
- $this->assertEquals(1, $childRow1->$product_id);
- }
- public function testTableRelationshipFindDependentRowsetSelect()
- {
- $table = $this->_table['bugs'];
- $bug_id = $this->_db->foldCase('bug_id');
- $product_id = $this->_db->foldCase('product_id');
- $select = $table->select()->limit(2)
- ->order($product_id . ' DESC');
- $parentRows = $table->find(1);
- $this->assertType('Zend_Db_Table_Rowset_Abstract', $parentRows,
- 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($parentRows));
- $parentRow1 = $parentRows->current();
- $childRows = $parentRow1->findDependentRowset('My_ZendDbTable_TableBugsProducts', null, $select);
- $this->assertType('Zend_Db_Table_Rowset_Abstract', $childRows,
- 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($childRows));
- $childRow1 = $childRows->current();
- $this->assertType('Zend_Db_Table_Row_Abstract', $childRow1,
- 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($childRow1));
- $this->assertEquals(1, $childRow1->$bug_id);
- $this->assertEquals(3, $childRow1->$product_id);
- }
- public function testTableRelationshipMagicFindDependentRowset()
- {
- $table = $this->_table['bugs'];
- $bug_id = $this->_db->foldCase('bug_id');
- $product_id = $this->_db->foldCase('product_id');
- $parentRows = $table->find(1);
- $parentRow1 = $parentRows->current();
- $childRows = $parentRow1->findMy_ZendDbTable_TableBugsProducts();
- $this->assertType('Zend_Db_Table_Rowset_Abstract', $childRows,
- 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($childRows));
- $this->assertEquals(3, $childRows->count());
- $childRow1 = $childRows->current();
- $this->assertType('Zend_Db_Table_Row_Abstract', $childRow1,
- 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($childRow1));
- $this->assertEquals(1, $childRow1->$bug_id);
- $this->assertEquals(1, $childRow1->$product_id);
- }
- public function testTableRelationshipMagicFindDependentRowsetSelect()
- {
- $table = $this->_table['bugs'];
- $bug_id = $this->_db->foldCase('bug_id');
- $product_id = $this->_db->foldCase('product_id');
- $select = $table->select()->limit(2)
- ->order($product_id . ' DESC');
- $parentRows = $table->find(1);
- $parentRow1 = $parentRows->current();
- $childRows = $parentRow1->findMy_ZendDbTable_TableBugsProducts($select);
- $this->assertType('Zend_Db_Table_Rowset_Abstract', $childRows,
- 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($childRows));
- $this->assertEquals(2, $childRows->count());
- $childRow1 = $childRows->current();
- $this->assertType('Zend_Db_Table_Row_Abstract', $childRow1,
- 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($childRow1));
- $this->assertEquals(1, $childRow1->$bug_id);
- $this->assertEquals(3, $childRow1->$product_id);
- }
- public function testTableRelationshipFindDependentRowsetException()
- {
- $table = $this->_table['bugs'];
- $parentRows = $table->find(1);
- $parentRow1 = $parentRows->current();
- try {
- $childRows = $parentRow1->findDependentRowset('nonexistant_class');
- $this->fail('Expected to catch Zend_Db_Table_Row_Exception for nonexistent table class');
- } catch (Zend_Exception $e) {
- $this->assertType('Zend_Db_Table_Exception', $e,
- 'Expecting object of type Zend_Db_Table_Exception got '.get_class($e));
- $this->assertEquals('File "nonexistant' . DIRECTORY_SEPARATOR . 'class.php" does not exist or class "nonexistant_class" was not found in the file', $e->getMessage());
- }
- try {
- $childRows = $parentRow1->findDependentRowset(new stdClass());
- $this->fail('Expected to catch Zend_Db_Table_Row_Exception for wrong table class');
- } catch (Zend_Exception $e) {
- $this->assertType('Zend_Db_Table_Row_Exception', $e,
- 'Expecting object of type Zend_Db_Table_Row_Exception got '.get_class($e));
- $this->assertEquals('Dependent table must be a Zend_Db_Table_Abstract, but it is stdClass', $e->getMessage());
- }
- }
- /**
- * Ensures that basic cascading update functionality succeeds using strings for single columns
- *
- * @return void
- */
- public function testTableRelationshipCascadingUpdateUsageBasicString()
- {
- $bug = $this->_getTable('My_ZendDbTable_TableBugsCustom')
- ->find(1)
- ->current();
- $bug_id = $this->_db->foldCase('bug_id');
- $this->assertEquals(
- 3,
- count($bugProducts = $bug->findDependentRowset('My_ZendDbTable_TableBugsProductsCustom')),
- 'Expecting to find three dependent rows'
- );
- $bug->$bug_id = 333;
- $bug->save();
- $this->assertEquals(
- 3,
- count($bugProducts = $bug->findDependentRowset('My_ZendDbTable_TableBugsProductsCustom')),
- 'Expecting to find three dependent rows'
- );
- foreach ($bugProducts as $bugProduct) {
- $this->assertEquals(333, $bugProduct->$bug_id);
- }
- $bug->$bug_id = 1;
- $bug->save();
- $this->assertEquals(
- 3,
- count($bugProducts = $bug->findDependentRowset('My_ZendDbTable_TableBugsProductsCustom')),
- 'Expecting to find three dependent rows'
- );
- foreach ($bugProducts as $bugProduct) {
- $this->assertEquals(1, $bugProduct->$bug_id);
- }
- }
- /**
- * Ensures that basic cascading update functionality succeeds using arrays for single columns
- *
- * @return void
- */
- public function testTableRelationshipCascadingUpdateUsageBasicArray()
- {
- $account1 = $this->_getTable('My_ZendDbTable_TableAccountsCustom')
- ->find('mmouse')
- ->current();
- $account_name = $this->_db->foldCase('account_name');
- $reported_by = $this->_db->foldCase('reported_by');
- $this->assertEquals(
- 1,
- count($account1->findDependentRowset('My_ZendDbTable_TableBugsCustom')),
- 'Expecting to find one dependent row'
- );
- $account1->$account_name = 'daisy';
- $account1->save();
- $this->assertEquals(
- 1,
- count($account1Bugs = $account1->findDependentRowset('My_ZendDbTable_TableBugsCustom')),
- 'Expecting to find one dependent row'
- );
- foreach ($account1Bugs as $account1Bug) {
- $this->assertEquals('daisy', $account1Bug->$reported_by);
- }
- $account1->$account_name = 'mmouse';
- $account1->save();
- $this->assertEquals(
- 1,
- count($account1Bugs = $account1->findDependentRowset('My_ZendDbTable_TableBugsCustom')),
- 'Expecting to find one dependent row'
- );
- foreach ($account1Bugs as $account1Bug) {
- $this->assertEquals('mmouse', $account1Bug->$reported_by);
- }
- }
- /**
- * Ensures that cascading update functionality is not run when onUpdate != self::CASCADE
- *
- * @return void
- */
- public function testTableRelationshipCascadingUpdateUsageInvalidNoop()
- {
- $product1 = $this->_getTable('My_ZendDbTable_TableProductsCustom')
- ->find(1)
- ->current();
- $this->assertEquals(
- 1,
- count($product1->findDependentRowset('My_ZendDbTable_TableBugsProductsCustom')),
- 'Expecting to find one dependent row'
- );
- $product_id = $this->_db->foldCase('product_id');
- $product1->$product_id = 333;
- $product1->save();
- $this->assertEquals(
- 0,
- count($product1BugsProducts = $product1->findDependentRowset('My_ZendDbTable_TableBugsProductsCustom')),
- 'Expecting to find one dependent row'
- );
- $product1->$product_id = 1;
- $product1->save();
- $this->assertEquals(
- 1,
- count($product1BugsProducts = $product1->findDependentRowset('My_ZendDbTable_TableBugsProductsCustom')),
- 'Expecting to find one dependent row'
- );
- foreach ($product1BugsProducts as $product1BugsProduct) {
- $this->assertEquals(1, $product1BugsProduct->$product_id);
- }
- }
- /**
- * Ensures that basic cascading delete functionality succeeds using strings for single columns
- *
- * @return void
- */
- public function testTableRelationshipCascadingDeleteUsageBasicString()
- {
- $bug1 = $this->_getTable('My_ZendDbTable_TableBugsCustom')
- ->find(1)
- ->current();
- $this->assertEquals(
- 3,
- count($bug1->findDependentRowset('My_ZendDbTable_TableBugsProductsCustom')),
- 'Expecting to find three dependent rows'
- );
- $bug1->delete();
- $bug_id = $this->_db->quoteIdentifier('bug_id', true);
- $this->assertEquals(
- 0,
- count($this->_getTable('My_ZendDbTable_TableBugsProductsCustom')->fetchAll("$bug_id = 1")),
- 'Expecting cascading delete to have reduced dependent rows to zero'
- );
- }
- /**
- * Ensures that basic cascading delete functionality succeeds using arrays for single columns
- *
- * @return void
- */
- public function testTableRelationshipCascadingDeleteUsageBasicArray()
- {
- $reported_by = $this->_db->quoteIdentifier('reported_by', true);
- $account1 = $this->_getTable('My_ZendDbTable_TableAccountsCustom')
- ->find('mmouse')
- ->current();
- $this->assertEquals(
- 1,
- count($account1->findDependentRowset('My_ZendDbTable_TableBugsCustom')),
- 'Expecting to find one dependent row'
- );
- $account1->delete();
- $tableBugsCustom = $this->_getTable('My_ZendDbTable_TableBugsCustom');
- $this->assertEquals(
- 0,
- count(
- $tableBugsCustom->fetchAll(
- $tableBugsCustom->getAdapter()
- ->quoteInto("$reported_by = ?", 'mmouse')
- )
- ),
- 'Expecting cascading delete to have reduced dependent rows to zero'
- );
- }
- /**
- * Ensures that cascading delete functionality is not run when onDelete != self::CASCADE
- *
- * @return void
- */
- public function testTableRelationshipCascadingDeleteUsageInvalidNoop()
- {
- $product1 = $this->_getTable('My_ZendDbTable_TableProductsCustom')
- ->find(1)
- ->current();
- $this->assertEquals(
- 1,
- count($product1->findDependentRowset('My_ZendDbTable_TableBugsProductsCustom')),
- 'Expecting to find one dependent row'
- );
- $product1->delete();
- $product_id = $this->_db->quoteIdentifier('product_id', true);
- $this->assertEquals(
- 1,
- count($this->_getTable('My_ZendDbTable_TableBugsProductsCustom')->fetchAll("$product_id = 1")),
- 'Expecting to find one dependent row'
- );
- }
- public function testTableRelationshipGetReference()
- {
- $table = $this->_table['bugs'];
- $map = $table->getReference('My_ZendDbTable_TableAccounts', 'Reporter');
- $this->assertThat($map, $this->arrayHasKey('columns'));
- $this->assertThat($map, $this->arrayHasKey('refTableClass'));
- $this->assertThat($map, $this->arrayHasKey('refColumns'));
- }
- public function testTableRelationshipGetReferenceException()
- {
- $table = $this->_table['bugs'];
- try {
- $table->getReference('My_ZendDbTable_TableAccounts', 'Nonexistent');
- $this->fail('Expected to catch Zend_Db_Table_Exception for nonexistent reference rule');
- } catch (Zend_Exception $e) {
- $this->assertType('Zend_Db_Table_Exception', $e,
- 'Expecting object of type Zend_Db_Table_Exception got '.get_class($e));
- }
- try {
- $table->getReference('Nonexistent', 'Reporter');
- $this->fail('Expected to catch Zend_Db_Table_Exception for nonexistent rule tableClass');
- } catch (Zend_Exception $e) {
- $this->assertType('Zend_Db_Table_Exception', $e,
- 'Expecting object of type Zend_Db_Table_Exception got '.get_class($e));
- }
- try {
- $table->getReference('Nonexistent');
- $this->fail('Expected to catch Zend_Db_Table_Exception for nonexistent rule tableClass');
- } catch (Zend_Exception $e) {
- $this->assertType('Zend_Db_Table_Exception', $e,
- 'Expecting object of type Zend_Db_Table_Exception got '.get_class($e));
- }
- }
- /**
- * Ensures that findParentRow() returns an instance of a custom row class when passed an instance
- * of the table class having $_rowClass overridden.
- *
- * @return void
- */
- public function testTableRelationshipFindParentRowCustomInstance()
- {
- $this->_useMyIncludePath();
-
- $myRowClass = 'My_ZendDbTable_Row_TestMyRow';
- Zend_Loader::loadClass($myRowClass);
- $bug1Reporter = $this->_table['bugs']
- ->find(1)
- ->current()
- ->findParentRow($this->_table['accounts']->setRowClass($myRowClass));
- $this->assertType($myRowClass, $bug1Reporter,
- "Expecting object of type $myRowClass, got ".get_class($bug1Reporter));
- }
- /**
- * Ensures that findParentRow() returns an instance of a custom row class when passed a string class
- * name, where the class has $_rowClass overridden.
- *
- * @return void
- */
- public function testTableRelationshipFindParentRowCustomClass()
- {
- $this->_useMyIncludePath();
-
- $myRowClass = 'My_ZendDbTable_Row_TestMyRow';
- Zend_Loader::loadClass($myRowClass);
- Zend_Loader::loadClass('My_ZendDbTable_TableAccountsCustom');
- $bug1Reporter = $this->_getTable('My_ZendDbTable_TableBugsCustom')
- ->find(1)
- ->current()
- ->findParentRow(new My_ZendDbTable_TableAccountsCustom(array('db' => $this->_db)));
- $this->assertType($myRowClass, $bug1Reporter,
- "Expecting object of type $myRowClass, got ".get_class($bug1Reporter));
- }
- /**
- * Ensures that findDependentRowset() returns instances of custom row and rowset classes when
- * passed an instance of the table class.
- *
- * @return void
- */
- public function testTableRelationshipFindDependentRowsetCustomInstance()
- {
- $this->_useMyIncludePath();
-
- $myRowsetClass = 'My_ZendDbTable_Rowset_TestMyRowset';
- $myRowClass = 'My_ZendDbTable_Row_TestMyRow';
- Zend_Loader::loadClass($myRowsetClass);
- $account_name = $this->_db->quoteIdentifier('account_name', true);
- $bugs = $this->_table['accounts']
- ->fetchRow($this->_db->quoteInto("$account_name = ?", 'mmouse'))
- ->findDependentRowset(
- $this->_table['bugs']
- ->setRowsetClass($myRowsetClass)
- ->setRowClass($myRowClass),
- 'Engineer'
- );
- $this->assertType($myRowsetClass, $bugs,
- "Expecting object of type $myRowsetClass, got ".get_class($bugs));
- $this->assertEquals(3, count($bugs));
- foreach ($bugs as $bug) {
- $this->assertType($myRowClass, $bug,
- "Expecting object of type $myRowClass, got ".get_class($bug));
- }
- }
- /**
- * Ensures that findDependentRowset() returns instances of custom row and rowset classes when
- * passed the named class.
- *
- * @return void
- */
- public function testTableRelationshipFindDependentRowsetCustomClass()
- {
- $this->_useMyIncludePath();
-
- $myRowsetClass = 'My_ZendDbTable_Rowset_TestMyRowset';
- $myRowClass = 'My_ZendDbTable_Row_TestMyRow';
- Zend_Loader::loadClass($myRowsetClass);
- $account_name = $this->_db->quoteIdentifier('account_name', true);
- $bugs = $this->_getTable('My_ZendDbTable_TableAccountsCustom')
- ->fetchRow($this->_db->quoteInto("$account_name = ?", 'mmouse'))
- ->findDependentRowset('My_ZendDbTable_TableBugsCustom', 'Engineer');
- $this->assertType($myRowsetClass, $bugs,
- "Expecting object of type $myRowsetClass, got ".get_class($bugs));
- $this->assertEquals(3, count($bugs));
- foreach ($bugs as $bug) {
- $this->assertType($myRowClass, $bug,
- "Expecting object of type $myRowClass, got ".get_class($bug));
- }
- }
- /**
- * Ensures that findManyToManyRowset() returns instances of custom row and rowset class when
- * passed an instance of the table class.
- *
- * @return void
- */
- public function testTableRelationshipFindManyToManyRowsetCustomInstance()
- {
- $this->_useMyIncludePath();
-
- $myRowsetClass = 'My_ZendDbTable_Rowset_TestMyRowset';
- $myRowClass = 'My_ZendDbTable_Row_TestMyRow';
- Zend_Loader::loadClass($myRowsetClass);
- $bug1Products = $this->_table['bugs']
- ->find(1)
- ->current()
- ->findManyToManyRowset(
- $this->_table['products']
- ->setRowsetClass($myRowsetClass)
- ->setRowClass($myRowClass),
- 'My_ZendDbTable_TableBugsProducts'
- );
- $this->assertType($myRowsetClass, $bug1Products,
- "Expecting object of type $myRowsetClass, got ".get_class($bug1Products));
- $this->assertEquals(3, count($bug1Products));
- foreach ($bug1Products as $bug1Product) {
- $this->assertType($myRowClass, $bug1Product,
- "Expecting object of type $myRowClass, got ".get_class($bug1Product));
- }
- }
- /**
- * Ensures that findManyToManyRowset() returns instances of custom row and rowset classes when
- * passed the named class.
- *
- * @return void
- */
- public function testTableRelationshipFindManyToManyRowsetCustomClass()
- {
- $this->_useMyIncludePath();
-
- $myRowsetClass = 'My_ZendDbTable_Rowset_TestMyRowset';
- $myRowClass = 'My_ZendDbTable_Row_TestMyRow';
- Zend_Loader::loadClass($myRowsetClass);
- $bug1Products = $this->_getTable('My_ZendDbTable_TableBugsCustom')
- ->find(1)
- ->current()
- ->findManyToManyRowset(
- 'My_ZendDbTable_TableProductsCustom',
- 'My_ZendDbTable_TableBugsProductsCustom'
- );
- $this->assertType($myRowsetClass, $bug1Products,
- "Expecting object of type $myRowsetClass, got ".get_class($bug1Products));
- $this->assertEquals(3, count($bug1Products));
- foreach ($bug1Products as $bug1Product) {
- $this->assertType($myRowClass, $bug1Product,
- "Expecting object of type $myRowClass, got ".get_class($bug1Product));
- }
- }
- /**
- * Ensures that rows returned by findParentRow() are updatable.
- *
- * @return void
- */
- public function testTableRelationshipFindParentRowIsUpdateable()
- {
- $bug_id = $this->_db->quoteIdentifier('bug_id', true);
- $account_name = $this->_db->foldCase('account_name');
- $table = $this->_table['bugs'];
- $childRows = $table->fetchAll("$bug_id = 1");
- $this->assertType('Zend_Db_Table_Rowset_Abstract', $childRows,
- 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($childRows));
- $childRow1 = $childRows->current();
- $this->assertType('Zend_Db_Table_Row_Abstract', $childRow1,
- 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($childRow1));
- $parentRow = $childRow1->findParentRow('My_ZendDbTable_TableAccounts');
- $this->assertType('Zend_Db_Table_Row_Abstract', $parentRow,
- 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($parentRow));
- $this->assertEquals('goofy', $parentRow->$account_name);
- $parentRow->$account_name = 'clarabell';
- try {
- $parentRow->save();
- } catch (Zend_Exception $e) {
- $this->fail('Failed with unexpected '.get_class($e).': '.$e->getMessage());
- }
- $accounts = $this->_db->quoteIdentifier('zfaccounts', true);
- $account_name = $this->_db->quoteIdentifier('account_name', true);
- $accounts_list = $this->_db->fetchCol("SELECT $account_name from $accounts ORDER BY $account_name");
- // if the save() did an UPDATE instead of an INSERT, then goofy should
- // be missing, and clarabell should be present
- $this->assertEquals(array('clarabell', 'dduck', 'mmouse'), $accounts_list);
- }
- /**
- * Ensures that rows returned by findDependentRowset() are updatable.
- *
- * @return void
- */
- public function testTableRelationshipFindDependentRowsetIsUpdateable()
- {
- $table = $this->_table['accounts'];
- $bug_id_column = $this->_db->foldCase('bug_id');
- $bug_description = $this->_db->foldCase('bug_description');
- $parentRows = $table->find('mmouse');
- $this->assertType('Zend_Db_Table_Rowset_Abstract', $parentRows,
- 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($parentRows));
- $parentRow1 = $parentRows->current();
- $childRows = $parentRow1->findDependentRowset('My_ZendDbTable_TableBugs');
- $this->assertType('Zend_Db_Table_Rowset_Abstract', $childRows,
- 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($childRows));
- $this->assertEquals(1, $childRows->count());
- $childRow1 = $childRows->current();
- $this->assertType('Zend_Db_Table_Row_Abstract', $childRow1,
- 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($childRow1));
- $childRow1->$bug_description = 'Updated description';
- $bug_id = $childRow1->$bug_id_column;
- try {
- $childRow1->save();
- } catch (Zend_Exception $e) {
- $this->fail('Failed with unexpected '.get_class($e).': '.$e->getMessage());
- }
- // find the row we just updated and make sure it has the new value.
- $bugs_table = $this->_table['bugs'];
- $bugs_rows = $bugs_table->find($bug_id);
- $this->assertEquals(1, $bugs_rows->count());
- $bug1 = $bugs_rows->current();
- $this->assertEquals($bug_id, $bug1->$bug_id_column);
- $this->assertEquals('Updated description', $bug1->$bug_description);
- }
- /**
- * Ensures that rows returned by findManyToManyRowset() are updatable.
- *
- * @return void
- */
- public function testTableRelationshipFindManyToManyRowsetIsUpdateable()
- {
- $table = $this->_table['bugs'];
- $product_id_column = $this->_db->foldCase('product_id');
- $product_name = $this->_db->foldCase('product_name');
- $originRows = $table->find(1);
- $originRow1 = $originRows->current();
- $destRows = $originRow1->findManyToManyRowset('My_ZendDbTable_TableProducts', 'My_ZendDbTable_TableBugsProducts');
- $this->assertType('Zend_Db_Table_Rowset_Abstract', $destRows,
- 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($destRows));
- $this->assertEquals(3, $destRows->count());
- $row1 = $destRows->current();
- $product_id = $row1->$product_id_column;
- $row1->$product_name = 'AmigaOS';
- try {
- $row1->save();
- } catch (Zend_Exception $e) {
- $this->fail('Failed with unexpected '.get_class($e).': '.$e->getMessage());
- }
- // find the row we just updated and make sure it has the new value.
- $products_table = $this->_table['products'];
- $product_rows = $products_table->find($product_id);
- $this->assertEquals(1, $product_rows->count());
- $product_row = $product_rows->current();
- $this->assertEquals($product_id, $product_row->$product_id_column);
- $this->assertEquals('AmigaOS', $product_row->$product_name);
- }
- public function testTableRelationshipOmitRefColumns()
- {
- $refMap = array(
- 'Reporter' => array(
- 'columns' => array('reported_by'),
- 'refTableClass' => 'My_ZendDbTable_TableAccounts'
- )
- );
- $table = $this->_getTable('My_ZendDbTable_TableSpecial',
- array(
- 'name' => 'zfbugs',
- 'referenceMap' => $refMap
- )
- );
- $bug1 = $table->find(1)->current();
- $reporter = $bug1->findParentRow('My_ZendDbTable_TableAccounts');
- $this->assertEquals(array('account_name' => 'goofy'), $reporter->toArray());
- }
- /**
- * Test that findParentRow() works even if the column names are
- * not the same.
- */
- public function testTableRelationshipFindParentRowWithDissimilarColumns()
- {
- $bug_id = $this->_db->foldCase('bug_id');
- $product_id = $this->_db->foldCase('product_id');
- $intersectionTable = $this->_getBugsProductsWithDissimilarColumns();
- $intRow = $intersectionTable->find(2, 3)->current();
- $bugRow = $intRow->findParentRow('My_ZendDbTable_TableBugs');
- $this->assertEquals(2, $bugRow->$bug_id);
- $productRow = $intRow->findParentRow('My_ZendDbTable_TableProducts');
- $this->assertEquals(3, $productRow->$product_id);
- }
- /**
- * Test that findDependentRowset() works even if the column names are
- * not the same.
- */
- public function testTableRelationshipFindDependentRowsetWithDissimilarColumns()
- {
- $intersectionTable = $this->_getBugsProductsWithDissimilarColumns();
- $bugsTable = $this->_getTable('My_ZendDbTable_TableBugs');
- $bugRow = $bugsTable->find(2)->current();
- $intRows = $bugRow->findDependentRowset($intersectionTable);
- $this->assertEquals(array(2, 3), array_values($intRows->current()->toArray()));
- }
- /**
- * Test that findManyToManyRowset() works even if the column names are
- * not the same.
- */
- public function testTableRelationshipFindManyToManyRowsetWithDissimilarColumns()
- {
- $product_id = $this->_db->foldCase('product_id');
- $intersectionTable = $this->_getBugsProductsWithDissimilarColumns();
- $bugsTable = $this->_getTable('My_ZendDbTable_TableBugs');
- $bugRow = $bugsTable->find(2)->current();
- $productRows = $bugRow->findManyToManyRowset('My_ZendDbTable_TableProducts', $intersectionTable);
- $this->assertEquals(3, $productRows->current()->$product_id);
- }
- /**
- * Test that findManyToManyRowset() works even if the column types are
- * not the same.
- */
- public function testTableRelationshipFindManyToManyRowsetWithDissimilarTypes()
- {
- $table = $this->_table['products'];
- $originRows = $table->find(1);
- $originRow1 = $originRows->current();
- $destRows = $originRow1->findManyToManyRowset('My_ZendDbTable_TableBugs', 'My_ZendDbTable_TableBugsProducts');
- $this->assertType('Zend_Db_Table_Rowset_Abstract', $destRows,
- 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($destRows));
- $this->assertEquals(1, $destRows->count());
- }
-
-
-
- /**
- * Utility Methods Below
- *
- */
-
-
-
- /**
- * Create database table based on BUGS_PRODUCTS bug with alternative
- * spellings of column names. Then create a Table class for this
- * physical table and return it.
- */
- protected function _getBugsProductsWithDissimilarColumns()
- {
- $altCols = array(
- 'boog_id' => 'INTEGER NOT NULL',
- 'produck_id' => 'INTEGER NOT NULL',
- 'PRIMARY KEY' => 'boog_id,produck_id'
- );
- $this->_util->createTable('AltBugsProducts', $altCols);
- $altBugsProducts = $this->_db->quoteIdentifier($this->_db->foldCase('zfalt_bugs_products'), true);
- $bugsProducts = $this->_db->quoteIdentifier($this->_db->foldCase('zfbugs_products'), true);
- $this->_db->query("INSERT INTO $altBugsProducts SELECT * FROM $bugsProducts");
- $refMap = array(
- 'Boog' => array(
- 'columns' => array('boog_id'),
- 'refTableClass' => 'My_ZendDbTable_TableBugs',
- 'refColumns' => array('bug_id')
- ),
- 'Produck' => array(
- 'columns' => array('produck_id'),
- 'refTableClass' => 'My_ZendDbTable_TableProducts',
- 'refColumns' => array('product_id')
- )
- );
- $options = array('name' => 'zfalt_bugs_products', 'referenceMap' => $refMap);
- $table = $this->_getTable('My_ZendDbTable_TableSpecial', $options);
- return $table;
- }
- }
|