| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863 |
- <?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-2014 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_Registry
- */
- require_once 'Zend/Registry.php';
- /**
- * @see Zend_Db_Table
- */
- require_once 'Zend/Db/Table.php';
- /**
- * @see Zend_Cache_Backend_BlackHole
- */
- require_once 'Zend/Cache/Backend/BlackHole.php';
- /**
- * @category Zend
- * @package Zend_Db
- * @subpackage UnitTests
- * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- */
- abstract class Zend_Db_Table_TestCommon extends Zend_Db_Table_TestSetup
- {
- public function testTableConstructor()
- {
- $bugs = $this->_table['bugs'];
- $info = $bugs->info();
- $config = array('db' => $this->_db,
- 'schema' => $info['schema'],
- 'name' => $info['name'],
- 'primary' => $info['primary'],
- 'cols' => $info['cols'],
- 'metadata' => $info['metadata'],
- 'metadataCache' => null,
- 'rowClass' => $info['rowClass'],
- 'rowsetClass' => $info['rowsetClass'],
- 'referenceMap' => $info['referenceMap'],
- 'dependentTables' => $info['dependentTables'],
- 'sequence' => $info['sequence'],
- 'unknownKey' => 'testValue');
- $table = new My_ZendDbTable_TableBugs($config);
- }
- // ZF-2379
- public function testAddReference()
- {
- $expectedReferences = array(
- 'columns' => array('reported_by'),
- 'refTableClass' => 'My_ZendDbTable_TableAccounts',
- 'refColumns' => array('account_name')
- );
- $products = $this->_table['products'];
- $products->addReference('Reporter', 'reported_by',
- 'My_ZendDbTable_TableAccounts', 'account_name');
- $references = $products->getReference('My_ZendDbTable_TableAccounts');
- $this->assertEquals($expectedReferences, $references);
- }
- // ZF-2666
- public function testIsIdentity()
- {
- $bugs = $this->_table['bugs'];
- $this->assertTrue($bugs->isIdentity('bug_id'));
- }
- /**
- * @group ZF-2510
- */
- public function testMetadataCacheInClassFlagShouldBeEnabledByDefault()
- {
- $bugs = $this->_table['bugs'];
- $this->assertTrue($bugs->metadataCacheInClass());
- }
- /**
- * @group ZF-2510
- */
- public function testMetadataCacheInClassFlagShouldBeMutable()
- {
- $bugs = $this->_table['bugs'];
- $this->assertTrue($bugs->metadataCacheInClass());
- $bugs->setMetadataCacheInClass(false);
- $this->assertFalse($bugs->metadataCacheInClass());
- }
- public function testTableInfo()
- {
- $bugs = $this->_table['bugs'];
- $this->assertTrue($bugs instanceof Zend_Db_Table_Abstract);
- $info = $bugs->info();
- $keys = array(
- Zend_Db_Table_Abstract::SCHEMA,
- Zend_Db_Table_Abstract::NAME,
- Zend_Db_Table_Abstract::COLS,
- Zend_Db_Table_Abstract::PRIMARY,
- Zend_Db_Table_Abstract::METADATA,
- Zend_Db_Table_Abstract::ROW_CLASS,
- Zend_Db_Table_Abstract::ROWSET_CLASS,
- Zend_Db_Table_Abstract::REFERENCE_MAP,
- Zend_Db_Table_Abstract::DEPENDENT_TABLES,
- Zend_Db_Table_Abstract::SEQUENCE,
- );
- $this->assertEquals($keys, array_keys($info));
- $this->assertEquals('zfbugs', $info['name']);
- $this->assertEquals(8, count($info['cols']));
- $cols = array(
- 'bug_id',
- 'bug_description',
- 'bug_status',
- 'created_on',
- 'updated_on',
- 'reported_by',
- 'assigned_to',
- 'verified_by'
- );
- $this->assertEquals($cols, $info['cols']);
- $this->assertEquals(1, count($info['primary']));
- $pk = array('bug_id');
- $this->assertEquals($pk, array_values($info['primary']));
- $name = $bugs->info(Zend_Db_Table_Abstract::NAME);
- $this->assertEquals('zfbugs', $name);
- try {
- $value = $bugs->info('_non_existent_');
- $this->fail('Expected to catch Zend_Db_Table_Exception');
- } catch (Zend_Exception $e) {
- $this->assertTrue($e instanceof Zend_Db_Table_Exception);
- $this->assertEquals('There is no table information for the key "_non_existent_"', $e->getMessage());
- }
- }
- /**
- * Ensures expected behavior when a table is assigned a Row class of stdClass
- *
- * @return void
- */
- public function testTableSetRowClassStdclass()
- {
- $productRowset = $this->_table['products']->setRowClass('stdClass')->fetchAll();
- $this->assertEquals(
- 3,
- $productRowsetCount = count($productRowset),
- "Expected rowset with 3 elements; got $productRowsetCount"
- );
- foreach ($productRowset as $productRow) {
- $this->assertThat(
- $productRow,
- $this->isInstanceOf('stdClass'),
- 'Expected row to be instance of stdClass; got ' . get_class($productRow)
- );
- }
- }
- /**
- * Ensures expected behavior when a table is assigned a Rowset class of stdClass
- *
- * @return void
- */
- public function testTableSetRowsetClassStdclass()
- {
- $productRowset = $this->_table['products']->setRowsetClass('stdClass')->fetchAll();
- $this->assertThat(
- $productRowset,
- $this->isInstanceOf('stdClass'),
- 'Expected rowset to be instance of stdClass; got ' . get_class($productRowset)
- );
- }
- public function testTableImplicitName()
- {
- include_once '_files/My/ZendDbTable/TableSpecial.php';
- // TableSpecial.php contains class bugs_products too.
- $table = new zfbugs_products(array('db' => $this->_db));
- $info = $table->info();
- $this->assertContains('name', array_keys($info));
- $this->assertEquals('zfbugs_products', $info['name']);
- }
- public function testTableOptionName()
- {
- $tableName = 'zfbugs';
- $table = $this->_getTable('My_ZendDbTable_TableSpecial',
- array('name' => $tableName)
- );
- $info = $table->info();
- $this->assertContains('name', array_keys($info));
- $this->assertEquals($tableName, $info['name']);
- }
- public function testTableOptionSchema()
- {
- $schemaName = $this->_util->getSchema();
- $tableName = 'zfbugs';
- $table = $this->_getTable('My_ZendDbTable_TableSpecial',
- array('name' => $tableName, 'schema' => $schemaName)
- );
- $info = $table->info();
- $this->assertContains('schema', array_keys($info));
- $this->assertEquals($schemaName, $info['schema']);
- }
- public function testTableArgumentAdapter()
- {
- $table = $this->_getTable('My_ZendDbTable_TableBugs',
- $this->_db);
- $db = $table->getAdapter();
- $this->assertSame($this->_db, $db);
- }
- public function testTableOptionAdapter()
- {
- $table = $this->_getTable('My_ZendDbTable_TableBugs',
- array('db' => $this->_db));
- $db = $table->getAdapter();
- $this->assertSame($this->_db, $db);
- }
- public function testTableOptionRowClass()
- {
- $table = $this->_getTable('My_ZendDbTable_TableBugs',
- array('rowClass' => 'stdClass'));
- $rowClass = $table->getRowClass();
- $this->assertEquals($rowClass, 'stdClass');
- $table = $this->_getTable('My_ZendDbTable_TableBugs',
- array('rowsetClass' => 'stdClass'));
- $rowsetClass = $table->getRowsetClass();
- $this->assertEquals($rowsetClass, 'stdClass');
- }
- public function testTableGetRowClass()
- {
- $table = $this->_table['products'];
- $this->assertTrue($table instanceof Zend_Db_Table_Abstract);
- $rowClass = $table->getRowClass();
- $this->assertEquals($rowClass, 'Zend_Db_Table_Row');
- $rowsetClass = $table->getRowsetClass();
- $this->assertEquals($rowsetClass, 'Zend_Db_Table_Rowset');
- }
- public function testTableOptionReferenceMap()
- {
- $refReporter = array(
- 'columns' => array('reported_by'),
- 'refTableClass' => 'My_ZendDbTable_TableAccounts',
- 'refColumns' => array('account_id')
- );
- $refEngineer = array(
- 'columns' => array('assigned_to'),
- 'refTableClass' => 'My_ZendDbTable_TableAccounts',
- 'refColumns' => array('account_id')
- );
- $refMap = array(
- 'Reporter' => $refReporter,
- 'Engineer' => $refEngineer
- );
- $table = $this->_getTable('My_ZendDbTable_TableBugs',
- array('referenceMap' => $refMap));
- $this->assertEquals($refReporter, $table->getReference('My_ZendDbTable_TableAccounts'));
- $this->assertEquals($refReporter, $table->getReference('My_ZendDbTable_TableAccounts', 'Reporter'));
- $this->assertEquals($refEngineer, $table->getReference('My_ZendDbTable_TableAccounts', 'Engineer'));
- }
- public function testTableExceptionOptionReferenceMap()
- {
- $refReporter = array(
- 'columns' => array('reported_by'),
- 'refTableClass' => 'My_ZendDbTable_TableAccounts',
- 'refColumns' => array('account_id')
- );
- $refEngineer = array(
- 'columns' => array('assigned_to'),
- 'refTableClass' => 'My_ZendDbTable_TableAccounts',
- 'refColumns' => array('account_id')
- );
- $refMap = array(
- 'Reporter' => $refReporter,
- 'Engineer' => $refEngineer
- );
- $table = $this->_getTable('My_ZendDbTable_TableBugs',
- array('referenceMap' => $refMap));
- try {
- $ref = $table->getReference('My_ZendDbTable_TableAccounts', 'Verifier');
- $this->fail('Expected to catch Zend_Db_Table_Exception');
- } catch (Zend_Exception $e) {
- $this->assertTrue($e instanceof Zend_Db_Table_Exception);
- $this->assertEquals('No reference rule "Verifier" from table My_ZendDbTable_TableBugs to table My_ZendDbTable_TableAccounts', $e->getMessage());
- }
- try {
- $ref = $table->getReference('My_ZendDbTable_TableProducts');
- $this->fail('Expected to catch Zend_Db_Table_Exception');
- } catch (Zend_Exception $e) {
- $this->assertTrue($e instanceof Zend_Db_Table_Exception);
- $this->assertEquals('No reference from table My_ZendDbTable_TableBugs to table My_ZendDbTable_TableProducts', $e->getMessage());
- }
- try {
- $ref = $table->getReference('My_ZendDbTable_TableProducts', 'Product');
- $this->fail('Expected to catch Zend_Db_Table_Exception');
- } catch (Zend_Exception $e) {
- $this->assertTrue($e instanceof Zend_Db_Table_Exception);
- $this->assertEquals('No reference rule "Product" from table My_ZendDbTable_TableBugs to table My_ZendDbTable_TableProducts', $e->getMessage());
- }
- try {
- $ref = $table->getReference('My_ZendDbTable_TableProducts', 'Reporter');
- $this->fail('Expected to catch Zend_Db_Table_Exception');
- } catch (Zend_Exception $e) {
- $this->assertTrue($e instanceof Zend_Db_Table_Exception);
- $this->assertEquals('Reference rule "Reporter" does not reference table My_ZendDbTable_TableProducts', $e->getMessage());
- }
- }
- public function testTableOptionDependentTables()
- {
- $depTables = array('Zend_Db_Table_Foo');
- $table = $this->_getTable('My_ZendDbTable_TableBugs',
- array('dependentTables' => $depTables));
- $this->assertEquals($depTables, $table->getDependentTables());
- }
- public function testTableSetRowClass()
- {
- $table = $this->_table['products'];
- $this->assertTrue($table instanceof Zend_Db_Table_Abstract);
- $table->setRowClass('stdClass');
- $rowClass = $table->getRowClass();
- $this->assertEquals($rowClass, 'stdClass');
- $table->setRowsetClass('stdClass');
- $rowsetClass = $table->getRowsetClass();
- $this->assertEquals($rowsetClass, 'stdClass');
- }
- public function testTableSetDefaultAdapter()
- {
- /**
- * Don't use _getTable() method because it defaults the adapter
- */
- Zend_Loader::loadClass('My_ZendDbTable_TableBugs');
- Zend_Db_Table_Abstract::setDefaultAdapter($this->_db);
- $db = Zend_Db_Table_Abstract::getDefaultAdapter();
- $this->assertSame($this->_db, $db);
- $table = new My_ZendDbTable_TableBugs();
- $db = $table->getAdapter();
- $this->assertSame($this->_db, $db);
- }
- public function testTableWithNoAdapterAndNoDefaultAdapter()
- {
- Zend_Db_Table_Abstract::setDefaultAdapter(null);
- $this->assertNull(Zend_Db_Table_Abstract::getDefaultAdapter());
- try {
- $table = new My_ZendDbTable_TableBugs();
- $this->fail('Zend_Db_Table_Exception should be thrown');
- }catch(Zend_Exception $e) {
- $this->assertTrue($e instanceof Zend_Db_Table_Exception,
- 'Expecting object of type Zend_Db_Table_Exception, got '.get_class($e));
- }
- }
- public function testTableSetDefaultAdapterNull()
- {
- Zend_Db_Table_Abstract::setDefaultAdapter($this->_db);
- $db = Zend_Db_Table_Abstract::getDefaultAdapter();
- $this->assertSame($this->_db, $db);
- Zend_Db_Table_Abstract::setDefaultAdapter();
- $this->assertNull(Zend_Db_Table_Abstract::getDefaultAdapter());
- }
- public function testTableSetDefaultAdapterRegistry()
- {
- /**
- * Don't use _getTable() method because it defaults the adapter
- */
- Zend_Loader::loadClass('My_ZendDbTable_TableBugs');
- Zend_Registry::set('registered_db', $this->_db);
- Zend_Db_Table_Abstract::setDefaultAdapter('registered_db');
- $db = Zend_Db_Table_Abstract::getDefaultAdapter();
- $this->assertSame($this->_db, $db);
- $table = new My_ZendDbTable_TableBugs();
- $db = $table->getAdapter();
- $this->assertSame($this->_db, $db);
- }
- public function testTableSetDefaultAdapterException()
- {
- try {
- Zend_Db_Table_Abstract::setDefaultAdapter(new stdClass());
- $this->fail('Expected to catch Zend_Db_Table_Exception');
- } catch (Zend_Exception $e) {
- $this->assertTrue($e instanceof Zend_Db_Table_Exception,
- 'Expecting object of type Zend_Db_Table_Exception, got '.get_class($e));
- $this->assertEquals("Argument must be of type Zend_Db_Adapter_Abstract, or a Registry key where a Zend_Db_Adapter_Abstract object is stored", $e->getMessage());
- }
- try {
- Zend_Db_Table_Abstract::setDefaultAdapter(327);
- $this->fail('Expected to catch Zend_Db_Table_Exception');
- } catch (Exception $e) {
- $this->assertTrue($e instanceof Zend_Db_Table_Exception,
- 'Expecting object of type Zend_Db_Table_Exception, got '.get_class($e));
- $this->assertEquals("Argument must be of type Zend_Db_Adapter_Abstract, or a Registry key where a Zend_Db_Adapter_Abstract object is stored", $e->getMessage());
- }
- }
- public function testTableExceptionPrimaryKeyNotSpecified()
- {
- try {
- $table = $this->_getTable('My_ZendDbTable_TableBugs', array('primary' => ''));
- $primary = $table->info(Zend_Db_Table_Abstract::PRIMARY);
- $this->fail('Expected to catch Zend_Db_Table_Exception');
- } catch (Zend_Exception $e) {
- $this->assertTrue($e instanceof Zend_Db_Table_Exception,
- 'Expecting object of type Zend_Db_Table_Exception, got '.get_class($e));
- $this->assertContains("Primary key column(s)", $e->getMessage());
- $this->assertContains("are not columns in this table", $e->getMessage());
- }
- }
- public function testTableExceptionInvalidPrimaryKey()
- {
- try {
- $table = new My_ZendDbTable_TableBugs(array('primary' => 'foo'));
- $primary = $table->info(Zend_Db_Table_Abstract::PRIMARY);
- $this->fail('Expected to catch Zend_Db_Table_Exception');
- } catch (Zend_Exception $e) {
- $this->assertTrue($e instanceof Zend_Db_Table_Exception,
- 'Expecting object of type Zend_Db_Table_Exception, got '.get_class($e));
- $this->assertContains("Primary key column(s)", $e->getMessage());
- $this->assertContains("are not columns in this table", $e->getMessage());
- }
- }
- public function testTableExceptionNoPrimaryKey()
- {
- // create a table that has no primary key
- $this->_util->createTable('noprimarykey', array('id' => 'INTEGER'));
- $tableName = $this->_util->getTableName('noprimarykey');
- try {
- $table = $this->_getTable('My_ZendDbTable_TableSpecial',
- array('name' => $tableName));
- $primary = $table->info(Zend_Db_Table_Abstract::PRIMARY);
- $this->fail('Expected to catch Zend_Db_Table_Exception');
- } catch (Zend_Exception $e) {
- $this->assertTrue($e instanceof Zend_Db_Table_Exception,
- 'Expecting object of type Zend_Db_Table_Exception, got '.get_class($e));
- $this->assertEquals("A table must have a primary key, but none was found for table '{$tableName}'", $e->getMessage());
- }
- $this->_util->dropTable($tableName);
- }
- public function testTableWithNoPrimaryKeyButOptionSpecifiesOne()
- {
- // create a table that has no primary key constraint
- $this->_util->createTable('noprimarykey', array('id' => 'INTEGER'));
- $tableName = $this->_util->getTableName('noprimarykey');
- try {
- $table = $this->_getTable('My_ZendDbTable_TableSpecial',
- array('name' => $tableName, 'primary' => 'id'));
- } catch (Zend_Exception $e) {
- $this->fail('Expected to succeed without a Zend_Db_Table_Exception');
- }
- $info = $table->info();
- $this->assertEquals(array(1=>'id'), $info['primary']);
- $this->_util->dropTable($tableName);
- }
- public function testTableAdapterException()
- {
- Zend_Loader::loadClass('My_ZendDbTable_TableBugs');
- /**
- * options array points 'db' to integer scalar
- */
- try {
- $table = new My_ZendDbTable_TableBugs(array('db' => 327));
- $this->fail('Expected to catch Zend_Db_Table_Exception');
- } catch (Zend_Exception $e) {
- $this->assertTrue($e instanceof Zend_Db_Table_Exception,
- 'Expecting object of type Zend_Db_Table_Exception, got '.get_class($e));
- $this->assertEquals("Argument must be of type Zend_Db_Adapter_Abstract, or a Registry key where a Zend_Db_Adapter_Abstract object is stored", $e->getMessage());
- }
- /**
- * options array points 'db' to Registry key containing integer scalar
- */
- Zend_Registry::set('registered_db', 327);
- try {
- $table = new My_ZendDbTable_TableBugs(array('db' => 'registered_db'));
- $this->fail('Expected to catch Zend_Db_Table_Exception');
- } catch (Zend_Exception $e) {
- $this->assertTrue($e instanceof Zend_Db_Table_Exception,
- 'Expecting object of type Zend_Db_Table_Exception, got '.get_class($e));
- $this->assertEquals("Argument must be of type Zend_Db_Adapter_Abstract, or a Registry key where a Zend_Db_Adapter_Abstract object is stored", $e->getMessage());
- }
- }
- public function testTableFindSingleRow()
- {
- $table = $this->_table['bugs'];
- $rowset = $table->find(1);
- $this->assertTrue($rowset instanceof Zend_Db_Table_Rowset_Abstract,
- 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
- $this->assertEquals(1, count($rowset));
- }
- public function testTableFindMultipleRows()
- {
- $table = $this->_table['bugs'];
- $rowset = $table->find(array(1, 2));
- $this->assertTrue($rowset instanceof Zend_Db_Table_Rowset_Abstract,
- 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
- $this->assertEquals(2, count($rowset));
- }
- public function testTableFindExceptionTooFewKeys()
- {
- $table = $this->_table['bugs_products'];
- try {
- $table->find(1);
- $this->fail('Expected to catch Zend_Db_Table_Exception for missing key');
- } catch (Zend_Exception $e) {
- $this->assertTrue($e instanceof Zend_Db_Table_Exception,
- 'Expecting object of type Zend_Db_Table_Exception, got '.get_class($e));
- $this->assertEquals('Too few columns for the primary key', $e->getMessage());
- }
- }
- public function testTableFindExceptionTooManyKeys()
- {
- $table = $this->_table['bugs'];
- try {
- $table->find(1, 2);
- $this->fail('Expected to catch Zend_Db_Table_Exception for incorrect key count');
- } catch (Zend_Exception $e) {
- $this->assertTrue($e instanceof Zend_Db_Table_Exception,
- 'Expecting object of type Zend_Db_Table_Exception, got '.get_class($e));
- $this->assertEquals('Too many columns for the primary key', $e->getMessage());
- }
- }
- public function testTableFindCompoundSingleRow()
- {
- $table = $this->_table['bugs_products'];
- $rowset = $table->find(1, 2);
- $this->assertTrue($rowset instanceof Zend_Db_Table_Rowset_Abstract,
- 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
- $this->assertEquals(1, count($rowset));
- }
- public function testTableFindCompoundMultipleRows()
- {
- $table = $this->_table['bugs_products'];
- $rowset = $table->find(array(1, 1), array(2, 3));
- $this->assertTrue($rowset instanceof Zend_Db_Table_Rowset_Abstract,
- 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
- $this->assertEquals(2, count($rowset));
- }
- public function testTableFindCompoundMultipleExceptionIncorrectValueCount()
- {
- $table = $this->_table['bugs_products'];
- try {
- $rowset = $table->find(array(1, 1), 2);
- $this->fail('Expected to catch Zend_Db_Table_Exception for incorrect key count');
- } catch (Zend_Exception $e) {
- $this->assertTrue($e instanceof Zend_Db_Table_Exception,
- 'Expecting object of type Zend_Db_Table_Exception, got '.get_class($e));
- $this->assertEquals('Missing value(s) for the primary key', $e->getMessage());
- }
- }
- /**
- * @group ZF-3349
- */
- public function testTableFindMultipleRowsWithKeys()
- {
- $table = $this->_table['products'];
- $rowset = $table->find(array(0 => 1, 1 => 2, 99 => 3));
- $this->assertTrue($rowset instanceof Zend_Db_Table_Rowset_Abstract,
- 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
- $this->assertEquals(3, count($rowset));
- }
- /**
- *
- * @group ZF-5775
- */
- public function testTableFindWithEmptyArray()
- {
- $table = $this->_table['products'];
- $rowset = $table->find(array());
- $this->assertTrue($rowset instanceof Zend_Db_Table_Rowset_Abstract,
- 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
- $this->assertEquals(0, count($rowset));
- }
- public function testTableInsert()
- {
- $table = $this->_table['bugs'];
- $row = array (
- 'bug_id' => null,
- 'bug_description' => 'New bug',
- 'bug_status' => 'NEW',
- 'created_on' => '2007-04-02',
- 'updated_on' => '2007-04-02',
- 'reported_by' => 'micky',
- 'assigned_to' => 'goofy',
- 'verified_by' => 'dduck'
- );
- $insertResult = $table->insert($row);
- $lastInsertId = $this->_db->lastInsertId();
- $this->assertEquals($insertResult, $lastInsertId);
- $this->assertEquals(5, $lastInsertId);
- }
- /**
- * @group ZF-3837
- */
- public function testTableInsertWhenAutoIncrementFieldIsAnEmptyStringShouldProduceNewAutoIncrementValue()
- {
- $table = $this->_table['bugs'];
- $row = array (
- 'bug_id' => '',
- 'bug_description' => 'New bug',
- 'bug_status' => 'NEW',
- 'created_on' => '2007-04-02',
- 'updated_on' => '2007-04-02',
- 'reported_by' => 'micky',
- 'assigned_to' => 'goofy',
- 'verified_by' => 'dduck'
- );
- $insertResult = $table->insert($row);
- $lastInsertId = $this->_db->lastInsertId('zfbugs', 'bug_id');
- $this->assertEquals($insertResult, $lastInsertId);
- $this->assertEquals(5, $lastInsertId);
- }
- public function testTableInsertWithSchema()
- {
- $schemaName = $this->_util->getSchema();
- $tableName = 'zfbugs';
- $identifier = join('.', array_filter(array($schemaName, $tableName)));
- $table = $this->_getTable('My_ZendDbTable_TableSpecial',
- array('name' => $tableName, 'schema' => $schemaName)
- );
- $row = array (
- 'bug_description' => 'New bug',
- 'bug_status' => 'NEW',
- 'created_on' => '2007-04-02',
- 'updated_on' => '2007-04-02',
- 'reported_by' => 'micky',
- 'assigned_to' => 'goofy',
- 'verified_by' => 'dduck'
- );
- $profilerEnabled = $this->_db->getProfiler()->getEnabled();
- $this->_db->getProfiler()->setEnabled(true)->setFilterQueryType(Zend_Db_Profiler::INSERT);
- $insertResult = $table->insert($row);
- $this->_db->getProfiler()->setEnabled($profilerEnabled);
- $qp = $this->_db->getProfiler()->getLastQueryProfile();
- $tableSpec = $this->_db->quoteIdentifier($identifier, true);
- $this->assertContains("INSERT INTO $tableSpec ", $qp->getQuery());
- }
- public function testTableInsertSequence()
- {
- $table = $this->_getTable('My_ZendDbTable_TableProducts',
- array(Zend_Db_Table_Abstract::SEQUENCE => 'zfproducts_seq'));
- $row = array (
- 'product_name' => 'Solaris'
- );
- $insertResult = $table->insert($row);
- $lastInsertId = $this->_db->lastInsertId('zfproducts');
- $lastSequenceId = $this->_db->lastSequenceId('zfproducts_seq');
- $this->assertEquals($insertResult, $lastInsertId);
- $this->assertEquals($insertResult, $lastSequenceId);
- $this->assertEquals(4, $insertResult);
- }
- public function testTableInsertNaturalCompound()
- {
- $table = $this->_table['bugs_products'];
- $row = array(
- 'bug_id' => 2,
- 'product_id' => 1
- );
- $primary = $table->insert($row);
- $this->assertTrue(is_array($primary));
- $this->assertEquals(2, count($primary));
- $this->assertEquals(array(2, 1), array_values($primary));
- }
- /**
- * @todo
- *
- public function testTableInsertNaturalExceptionKeyViolation()
- {
- $table = $this->_table['bugs'];
- $row = array (
- 'bug_id' => 1,
- 'bug_description' => 'New bug',
- 'bug_status' => 'NEW',
- 'created_on' => '2007-04-02',
- 'updated_on' => '2007-04-02',
- 'reported_by' => 'micky',
- 'assigned_to' => 'goofy'
- );
- try {
- $insertResult = $table->insert($row);
- $this->fail('Expected to catch Zend_Db_Table_Exception for key violation');
- } catch (Zend_Exception $e) {
- echo "*** caught ".get_class($e)."\n";
- echo "*** ".$e->getMessage()."\n";
- $this->assertEquals('xxx', $e->getMessage());
- }
- }
- */
- /**
- * @todo
- *
- public function testTableInsertNaturalCompoundExceptionKeyViolation()
- {
- $table = $this->_table['bugs_products'];
- $row = array(
- 'bug_id' => 1,
- 'product_id' => 1
- );
- try {
- $table->insert($row);
- $this->fail('Expected to catch Zend_Db_Table_Exception for key violation');
- } catch (Zend_Exception $e) {
- echo "*** caught ".get_class($e)."\n";
- echo "*** ".$e->getMessage()."\n";
- $this->assertEquals('xxx', $e->getMessage());
- }
- }
- */
- /**
- * See ZF-1739 in our issue tracker.
- * @group ZF-1739
- */
- public function testTableInsertWithHighMemoryUsage()
- {
- $this->markTestSkipped('Very slow test inserts thousands of rows');
- $table = $this->_table['products'];
- // insert one row to prime the pump
- $table->insert(array('product_name' => "product0"));
- // measure current memory usage
- $mem1 = memory_get_usage();
- // insert a lot of rows
- $n = 100000;
- for ($i = 1; $i <= $n; $i++)
- {
- $table->insert(array('product_name' => "product$i"));
- if ($i % 1000 == 0) {
- echo '.';
- }
- }
- // measure new memory usage
- $mem2 = memory_get_usage();
- // compare new memory usage to original
- $mem_delta = $mem2-$mem1;
- $this->assertThat($mem_delta, $this->lessThan(513));
- }
- /**
- * @group ZF-2953
- */
- public function testTableInsertWithEmptyValueAsPrimaryKey()
- {
- $table = $this->_table['bugs'];
- $row = array (
- 'bug_description' => 'New bug',
- 'bug_status' => 'NEW',
- 'created_on' => '2007-04-02',
- 'updated_on' => '2007-04-02',
- 'reported_by' => 'micky',
- 'assigned_to' => 'goofy',
- 'verified_by' => 'dduck'
- );
- // empty string
- $row['bug_id'] = '';
- $insertResult = $table->insert($row);
- $this->assertTrue(is_numeric($insertResult), 'Empty string did not return assigned primary key');
- // false (bool)
- $row['bug_id'] = false;
- $insertResult = $table->insert($row);
- $this->assertTrue(is_numeric($insertResult), 'Bool false did not return assigned primary key');
- // empty array
- $row['bug_id'] = array();
- $insertResult = $table->insert($row);
- $this->assertTrue(is_numeric($insertResult), 'Empty array did not return assigned primary key');
- // zero '0'
- $row['bug_id'] = '0';
- $table->delete('bug_id > 0'); // clear table
- $insertResult = $table->insert($row);
- $this->assertEquals('0', $insertResult, 'Zero string did not return assigned primary key');
- // zero 0
- $row['bug_id'] = 0;
- $table->delete('bug_id = 0'); // clear table
- $insertResult = $table->insert($row);
- $this->assertEquals('0', $insertResult, 'Zero int did not return assigned primary key');
- }
- public function testTableUpdate()
- {
- $bug_id = $this->_db->quoteIdentifier('bug_id', true);
- $bug_description = $this->_db->foldCase('bug_description');
- $bug_status = $this->_db->foldCase('bug_status');
- $data = array(
- $bug_description => 'Implement Do What I Mean function',
- $bug_status => 'INCOMPLETE'
- );
- $table = $this->_table['bugs'];
- $result = $table->update($data, "$bug_id = 2");
- $this->assertEquals(1, $result);
- // Query the row to see if we have the new values.
- $rowset = $table->find(2);
- $this->assertTrue($rowset instanceof Zend_Db_Table_Rowset_Abstract,
- 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
- $this->assertEquals(1, count($rowset), "Expecting rowset count to be 1");
- $row = $rowset->current();
- $this->assertTrue($row instanceof Zend_Db_Table_Row_Abstract,
- 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row));
- $bug_id = $this->_db->foldCase('bug_id');
- $this->assertEquals(2, $row->$bug_id, "Expecting row->bug_id to be 2");
- $this->assertEquals($data[$bug_description], $row->$bug_description);
- $this->assertEquals($data[$bug_status], $row->$bug_status);
- }
- public function testTableUpdateWithSchema()
- {
- $bug_id = $this->_db->quoteIdentifier('bug_id', true);
- $bug_description = $this->_db->foldCase('bug_description');
- $bug_status = $this->_db->foldCase('bug_status');
- $schemaName = $this->_util->getSchema();
- $tableName = 'zfbugs';
- $identifier = join('.', array_filter(array($schemaName, $tableName)));
- $table = $this->_getTable('My_ZendDbTable_TableSpecial',
- array('name' => $tableName, 'schema' => $schemaName)
- );
- $data = array(
- $bug_description => 'Implement Do What I Mean function',
- $bug_status => 'INCOMPLETE'
- );
- $profilerEnabled = $this->_db->getProfiler()->getEnabled();
- $this->_db->getProfiler()->setEnabled(true);
- $result = $table->update($data, "$bug_id = 2");
- $this->_db->getProfiler()->setEnabled($profilerEnabled);
- $this->assertEquals(1, $result);
- $qp = $this->_db->getProfiler()->getLastQueryProfile();
- $tableSpec = $this->_db->quoteIdentifier($identifier, true);
- $this->assertContains("UPDATE $tableSpec ", $qp->getQuery());
- }
- public function testTableUpdateWhereArray()
- {
- $bug_id = $this->_db->quoteIdentifier('bug_id', true);
- $bug_status = $this->_db->quoteIdentifier('bug_status', true);
- $bug_description = $this->_db->foldCase('bug_description');
- $data = array(
- $bug_description => 'Synesthesia',
- );
- $where = array(
- "$bug_id IN (1, 3)",
- "$bug_status != 'UNKNOWN'"
- );
- $this->assertEquals(2, $this->_table['bugs']->update($data, $where));
- $count = 0;
- foreach ($this->_table['bugs']->find(array(1, 3)) as $row) {
- $this->assertEquals($data[$bug_description], $row->$bug_description);
- ++$count;
- }
- $this->assertEquals(2, $count);
- }
- public function testTableDelete()
- {
- $bug_id = $this->_db->quoteIdentifier('bug_id', true);
- $table = $this->_table['bugs'];
- $rowset = $table->find(array(1, 2));
- $this->assertEquals(2, count($rowset));
- $table->delete("$bug_id = 2");
- $rowset = $table->find(array(1, 2));
- $this->assertEquals(1, count($rowset));
- }
- public function testTableDeleteWithSchema()
- {
- $bug_id = $this->_db->quoteIdentifier('bug_id', true);
- $schemaName = $this->_util->getSchema();
- $tableName = 'zfbugs';
- $identifier = join('.', array_filter(array($schemaName, $tableName)));
- $table = $this->_getTable('My_ZendDbTable_TableSpecial',
- array('name' => $tableName, 'schema' => $schemaName)
- );
- $profilerEnabled = $this->_db->getProfiler()->getEnabled();
- $this->_db->getProfiler()->setEnabled(true);
- $result = $table->delete("$bug_id = 2");
- $this->_db->getProfiler()->setEnabled($profilerEnabled);
- $qp = $this->_db->getProfiler()->getLastQueryProfile();
- $tableSpec = $this->_db->quoteIdentifier($identifier, true);
- $this->assertContains("DELETE FROM $tableSpec ", $qp->getQuery());
- }
- public function testTableDeleteWhereArray()
- {
- $bug_id = $this->_db->quoteIdentifier('bug_id', true);
- $bug_status = $this->_db->quoteIdentifier('bug_status', true);
- $where = array(
- "$bug_id IN (1, 3)",
- "$bug_status != 'UNKNOWN'"
- );
- $this->assertEquals(2, $this->_table['bugs']->delete($where));
- $this->assertEquals(0, count($this->_table['bugs']->find(array(1, 3))));
- }
- public function testTableCreateRow()
- {
- $table = $this->_table['bugs'];
- $row = $table->createRow();
- $this->assertTrue($row instanceof Zend_Db_Table_Row_Abstract,
- 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row));
- $this->assertTrue(isset($row->bug_description));
- $this->assertEquals($row, $table->fetchNew());
- }
- public function testTableCreateRowWithData()
- {
- $table = $this->_table['bugs'];
- $data = array (
- 'bug_description' => 'New bug',
- 'bug_status' => 'NEW',
- 'created_on' => '2007-04-02',
- 'updated_on' => '2007-04-02',
- 'reported_by' => 'micky',
- 'assigned_to' => 'goofy'
- );
- $row = $table->createRow($data);
- $this->assertTrue($row instanceof Zend_Db_Table_Row_Abstract,
- 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row));
- $this->assertTrue(isset($row->bug_description));
- $this->assertEquals('New bug', $row->bug_description);
- }
- /**
- * @group ZF-8944
- * @group ZF-10598
- * @group ZF-11253
- */
- public function testTableFetchRowOffset()
- {
- $reported_by = $this->_db->quoteIdentifier('reported_by', true);
- $table = $this->_table['bugs'];
- $row = $table->fetchRow(array("$reported_by = ?" => 'goofy'), null, 1);
- $this->assertTrue($row instanceof Zend_Db_Table_Row_Abstract,
- 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row));
- $bug_id = $this->_db->foldCase('bug_id');
- $this->assertEquals(2, $row->$bug_id);
- }
- /**
- * @group ZF-8944
- * @group ZF-10598
- * @group ZF-11253
- */
- public function testTableFetchRowOffsetSelect()
- {
- $reported_by = $this->_db->quoteIdentifier('reported_by', true);
- $table = $this->_table['bugs'];
- $select = $table->select()
- ->where("$reported_by = ?", 'goofy')
- ->limit(1, 1);
- $row = $table->fetchRow($select);
- $this->assertTrue($row instanceof Zend_Db_Table_Row_Abstract,
- 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row));
- $bug_id = $this->_db->foldCase('bug_id');
- $this->assertEquals(2, $row->$bug_id);
- }
- public function testTableFetchRow()
- {
- $table = $this->_table['bugs'];
- $bug_description = $this->_db->foldCase('bug_description');
- $row = $table->fetchRow();
- $this->assertTrue($row instanceof Zend_Db_Table_Row_Abstract,
- 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row));
- $this->assertTrue(isset($row->$bug_description));
- }
- public function testTableFetchRowWhere()
- {
- $bug_id = $this->_db->quoteIdentifier('bug_id', true);
- $table = $this->_table['bugs'];
- $row = $table->fetchRow("$bug_id = 2");
- $this->assertTrue($row instanceof Zend_Db_Table_Row_Abstract,
- 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row));
- $bug_id = $this->_db->foldCase('bug_id');
- $this->assertEquals(2, $row->$bug_id);
- }
- public function testTableFetchRowWhereArray()
- {
- $bug_id = $this->_db->quoteIdentifier('bug_id', true);
- $table = $this->_table['bugs'];
- $row = $table->fetchRow(array("$bug_id = ?" => 2));
- $this->assertTrue($row instanceof Zend_Db_Table_Row_Abstract,
- 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row));
- $bug_id = $this->_db->foldCase('bug_id');
- $this->assertEquals(2, $row->$bug_id);
- }
- public function testTableFetchRowWhereSelect()
- {
- $bug_id = $this->_db->quoteIdentifier('bug_id', true);
- $table = $this->_table['bugs'];
- $select = $table->select()
- ->where("$bug_id = ?", 2);
- $row = $table->fetchRow($select);
- $this->assertTrue($row instanceof Zend_Db_Table_Row_Abstract,
- 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row));
- $bug_id = $this->_db->foldCase('bug_id');
- $this->assertEquals(2, $row->$bug_id);
- }
- public function testTableFetchRowOrderAsc()
- {
- $bug_id = $this->_db->quoteIdentifier('bug_id', true);
- $table = $this->_table['bugs'];
- $row = $table->fetchRow("$bug_id > 1", "bug_id ASC");
- $this->assertTrue($row instanceof Zend_Db_Table_Row_Abstract,
- 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row));
- $bug_id = $this->_db->foldCase('bug_id');
- $this->assertEquals(2, $row->$bug_id);
- }
- public function testTableFetchRowOrderSelectAsc()
- {
- $bug_id = $this->_db->quoteIdentifier('bug_id', true);
- $table = $this->_table['bugs'];
- $select = $table->select()
- ->where("$bug_id > ?", 1)
- ->order("bug_id ASC");
- $row = $table->fetchRow($select);
- $this->assertTrue($row instanceof Zend_Db_Table_Row_Abstract,
- 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row));
- $bug_id = $this->_db->foldCase('bug_id');
- $this->assertEquals(2, $row->$bug_id);
- }
- public function testTableFetchRowOrderDesc()
- {
- $bug_id = $this->_db->quoteIdentifier('bug_id', true);
- $table = $this->_table['bugs'];
- $row = $table->fetchRow(null, "bug_id DESC");
- $this->assertTrue($row instanceof Zend_Db_Table_Row_Abstract,
- 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row));
- $bug_id = $this->_db->foldCase('bug_id');
- $this->assertEquals(4, $row->$bug_id);
- }
- public function testTableFetchRowOrderSelectDesc()
- {
- $bug_id = $this->_db->quoteIdentifier('bug_id', true);
- $table = $this->_table['bugs'];
- $select = $table->select()
- ->where("$bug_id > ?", 1)
- ->order("bug_id DESC");
- $row = $table->fetchRow($select);
- $this->assertTrue($row instanceof Zend_Db_Table_Row_Abstract,
- 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row));
- $bug_id = $this->_db->foldCase('bug_id');
- $this->assertEquals(4, $row->$bug_id);
- }
- public function testTableFetchRowEmpty()
- {
- $bug_id = $this->_db->quoteIdentifier('bug_id', true);
- $table = $this->_table['bugs'];
- $row = $table->fetchRow("$bug_id = -1");
- $this->assertEquals(null, $row,
- 'Expecting null result for non-existent row');
- }
- public function testTableFetchAll()
- {
- $table = $this->_table['bugs'];
- $rowset = $table->fetchAll();
- $this->assertTrue($rowset instanceof Zend_Db_Table_Rowset_Abstract,
- 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
- $this->assertEquals(4, count($rowset));
- $row1 = $rowset->current();
- $this->assertTrue($row1 instanceof Zend_Db_Table_Row_Abstract,
- 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1));
- }
- public function testTableFetchAllWhere()
- {
- $bug_id = $this->_db->quoteIdentifier('bug_id', true);
- $table = $this->_table['bugs'];
- $rowset = $table->fetchAll("$bug_id = 2");
- $this->assertTrue($rowset instanceof Zend_Db_Table_Rowset_Abstract,
- 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
- $this->assertEquals(1, count($rowset));
- $row1 = $rowset->current();
- $this->assertTrue($row1 instanceof Zend_Db_Table_Row_Abstract,
- 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1));
- $bug_id = $this->_db->foldCase('bug_id');
- $this->assertEquals(2, $row1->$bug_id);
- }
- public function testTableFetchAllWhereSelect()
- {
- $bug_id = $this->_db->quoteIdentifier('bug_id', true);
- $table = $this->_table['bugs'];
- $select = $table->select()
- ->where("$bug_id = ?", 2);
- $rowset = $table->fetchAll($select);
- $this->assertTrue($rowset instanceof Zend_Db_Table_Rowset_Abstract,
- 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
- $this->assertEquals(1, count($rowset));
- $row1 = $rowset->current();
- $this->assertTrue($row1 instanceof Zend_Db_Table_Row_Abstract,
- 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1));
- $bug_id = $this->_db->foldCase('bug_id');
- $this->assertEquals(2, $row1->$bug_id);
- }
- public function testTableFetchAllOrder()
- {
- $table = $this->_table['bugs'];
- $rowset = $table->fetchAll(null, 'bug_id DESC');
- $this->assertTrue($rowset instanceof Zend_Db_Table_Rowset_Abstract,
- 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
- $this->assertEquals(4, count($rowset));
- $row1 = $rowset->current();
- $this->assertTrue($row1 instanceof Zend_Db_Table_Row_Abstract,
- 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1));
- $bug_id = $this->_db->foldCase('bug_id');
- $this->assertEquals(4, $row1->$bug_id);
- }
- public function testTableFetchAllOrderSelect()
- {
- $table = $this->_table['bugs'];
- $select = $table->select()
- ->order('bug_id DESC');
- $rowset = $table->fetchAll($select);
- $this->assertTrue($rowset instanceof Zend_Db_Table_Rowset_Abstract,
- 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($rowset));
- $this->assertEquals(4, count($rowset));
- $row1 = $rowset->current();
- $this->assertTrue($row1 instanceof Zend_Db_Table_Row_Abstract,
- 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($row1));
- $bug_id = $this->_db->foldCase('bug_id');
- $this->assertEquals(4, $row1->$bug_id);
- }
- public function testTableFetchAllOrderExpr()
- {
- $bug_id = $this->_db->quoteIdentifier('bug_id', true);
- $table = $this->_table['bugs'];
- $rowset = $table->fetchAll(null, new Zend_Db_Expr("$bug_id + 1 DESC"));
- $this->assertTrue($rowset instanceof Zend_Db_Table_Rowset,
- 'Expecting object of type Zend_Db_Table_Rowset, got '.get_class($rowset));
- $this->assertEquals(4, count($rowset));
- $row1 = $rowset->current();
- $this->assertTrue($row1 instanceof Zend_Db_Table_Row,
- 'Expecting object of type Zend_Db_Table_Row, got '.get_class($row1));
- $bug_id = $this->_db->foldCase('bug_id');
- $this->assertEquals(4, $row1->$bug_id);
- }
- public function testTableFetchAllLimit()
- {
- $table = $this->_table['bugs'];
- $rowset = $table->fetchAll(null, 'bug_id ASC', 2, 1);
- $this->assertTrue($rowset instanceof Zend_Db_Table_Rowset,
- 'Expecting object of type Zend_Db_Table_Rowset, got '.get_class($rowset));
- $this->assertEquals(2, count($rowset));
- $row1 = $rowset->current();
- $this->assertTrue($row1 instanceof Zend_Db_Table_Row,
- 'Expecting object of type Zend_Db_Table_Row, got '.get_class($row1));
- $bug_id = $this->_db->foldCase('bug_id');
- $this->assertEquals(2, $row1->$bug_id);
- }
- public function testTableFetchAllLimitSelect()
- {
- $table = $this->_table['bugs'];
- $select = $table->select()
- ->order('bug_id ASC')
- ->limit(2, 1);
- $rowset = $table->fetchAll($select);
- $this->assertTrue($rowset instanceof Zend_Db_Table_Rowset,
- 'Expecting object of type Zend_Db_Table_Rowset, got '.get_class($rowset));
- $this->assertEquals(2, count($rowset));
- $row1 = $rowset->current();
- $this->assertTrue($row1 instanceof Zend_Db_Table_Row,
- 'Expecting object of type Zend_Db_Table_Row, got '.get_class($row1));
- $bug_id = $this->_db->foldCase('bug_id');
- $this->assertEquals(2, $row1->$bug_id);
- }
- public function testTableFetchAllEmpty()
- {
- $bug_id = $this->_db->quoteIdentifier('bug_id', true);
- $table = $this->_table['bugs'];
- $rowset = $table->fetchAll("$bug_id = -1");
- $this->assertEquals(0, count($rowset));
- }
- public function testTableLoadsCustomRowClass()
- {
- $this->_useMyIncludePath();
- if (class_exists('My_ZendDbTable_Row_TestMyRow')) {
- $this->markTestSkipped("Cannot test loading the custom Row class because it is already loaded");
- return;
- }
- $this->assertFalse(class_exists('My_ZendDbTable_Row_TestMyRow', false),
- 'Expected TestMyRow class not to be loaded (#1)');
- $this->assertFalse(class_exists('My_ZendDbTable_Rowset_TestMyRowset', false),
- 'Expected TestMyRowset class not to be loaded (#1)');
- // instantiating the table does not creat a rowset
- // so the custom classes are not loaded yet
- $bugsTable = $this->_getTable('My_ZendDbTable_TableBugsCustom');
- $this->assertFalse(class_exists('My_ZendDbTable_Row_TestMyRow', false),
- 'Expected TestMyRow class not to be loaded (#2)');
- $this->assertFalse(class_exists('My_ZendDbTable_Rowset_TestMyRowset', false),
- 'Expected TestMyRowset class not to be loaded (#2)');
- // creating a rowset makes the table load the rowset class
- // and the rowset constructor loads the row class.
- $bugs = $bugsTable->fetchAll();
- $this->assertTrue(class_exists('My_ZendDbTable_Row_TestMyRow', false),
- 'Expected TestMyRow class to be loaded (#3)');
- $this->assertTrue(class_exists('My_ZendDbTable_Rowset_TestMyRowset', false),
- 'Expected TestMyRowset class to be loaded (#3)');
- }
- /**
- * Ensures that Zend_Db_Table_Abstract::setDefaultMetadataCache() performs as expected
- *
- * @return void
- */
- public function testTableSetDefaultMetadataCache()
- {
- $cache = $this->_getCache();
- Zend_Db_Table_Abstract::setDefaultMetadataCache($cache);
- $this->assertSame($cache, Zend_Db_Table_Abstract::getDefaultMetadataCache());
- Zend_Db_Table_Abstract::setDefaultMetadataCache();
- $this->assertNull(Zend_Db_Table_Abstract::getDefaultMetadataCache());
- }
- public function testTableSetDefaultMetadataCacheRegistry()
- {
- $cache = $this->_getCache();
- Zend_Registry::set('registered_metadata_cache', $cache);
- Zend_Db_Table_Abstract::setDefaultMetadataCache('registered_metadata_cache');
- $this->assertSame($cache, Zend_Db_Table_Abstract::getDefaultMetadataCache());
- }
- public function testTableMetadataCacheRegistry()
- {
- $cache = $this->_getCache();
- Zend_Registry::set('registered_metadata_cache', $cache);
- $tableBugsCustom1 = $this->_getTable(
- 'My_ZendDbTable_TableBugsCustom',
- array('metadataCache' => 'registered_metadata_cache')
- );
- $this->assertSame($cache, $tableBugsCustom1->getMetadataCache());
- }
- public function testTableSetDefaultMetadataCacheWriteAccess()
- {
- $cache = $this->_getCacheNowrite();
- Zend_Db_Table_Abstract::setDefaultMetadataCache($cache);
- try {
- $bugsTable = $this->_getTable('My_ZendDbTable_TableBugs');
- $primary = $bugsTable->info(Zend_Db_Table_Abstract::PRIMARY);
- $this->fail('Expected to catch PHPUnit_Framework_Error');
- } catch (PHPUnit_Framework_Error $e) {
- $this->assertEquals(E_USER_NOTICE, $e->getCode(), 'Error type not E_USER_NOTICE');
- $this->assertEquals('Failed saving metadata to metadataCache', $e->getMessage());
- }
- Zend_Db_Table_Abstract::setDefaultMetadataCache(null);
- }
- /**
- * Ensures that table metadata caching works as expected when the cache object
- * is set in the configuration for a new table object.
- *
- * @return void
- */
- public function testTableMetadataCacheNew()
- {
- $cache = $this->_getCache();
- $tableBugsCustom1 = $this->_getTable(
- 'My_ZendDbTable_TableBugsCustom',
- array('metadataCache' => $cache)
- );
- $this->assertTrue(
- $tableBugsCustom1->getMetadataCache() instanceof Zend_Cache_Core
- );
- $this->assertFalse($tableBugsCustom1->isMetadataFromCache, 'Failed asserting metadata is not from cache');
- $tableBugsCustom1->setup();
- $this->assertTrue($tableBugsCustom1->isMetadataFromCache, 'Failed asserting metadata is from cache');
- $cache->clean(Zend_Cache::CLEANING_MODE_ALL);
- $tableBugsCustom1->setup();
- $this->assertFalse($tableBugsCustom1->isMetadataFromCache, 'Failed asserting metadata is not from cache after cleaning');
- }
- /**
- * Ensures that table metadata caching can be persistent in the object even
- * after a flushed cache, if the setMetadataCacheInClass property is true.
- *
- * @group ZF-2510
- * @return void
- */
- public function testTableMetadataCacheInClass()
- {
- $cache = $this->_getCache();
- $tableBugsCustom1 = $this->_getTable(
- 'My_ZendDbTable_TableBugsCustom',
- array(
- 'metadataCache' => $cache,
- 'metadataCacheInClass' => true,
- )
- );
- $this->assertTrue(
- $tableBugsCustom1->getMetadataCache() instanceof Zend_Cache_Core
- );
- $this->assertFalse($tableBugsCustom1->isMetadataFromCache, 'Failed asserting metadata is not from cache');
- $tableBugsCustom1->setup();
- $this->assertTrue($tableBugsCustom1->isMetadataFromCache, 'Failed asserting metadata is from cache');
- $cache->clean(Zend_Cache::CLEANING_MODE_ALL);
- $tableBugsCustom1->setup();
- $this->assertTrue($tableBugsCustom1->isMetadataFromCache, 'Failed asserting metadata is from cache after cleaning');
- }
- /**
- * Ensures that table metadata caching works as expected when the default cache object
- * is set for the abstract table class.
- *
- * @return void
- */
- public function testTableMetadataCacheClass()
- {
- $cache = $this->_getCache();
- Zend_Db_Table_Abstract::setDefaultMetadataCache($cache);
- $tableBugsCustom1 = $this->_getTable('My_ZendDbTable_TableBugsCustom');
- $this->assertFalse($tableBugsCustom1->isMetadataFromCache);
- $this->assertTrue(
- $tableBugsCustom1->getMetadataCache() instanceof Zend_Cache_Core
- );
- $tableBugsCustom1->setup();
- $this->assertTrue($tableBugsCustom1->isMetadataFromCache);
- $cache->clean(Zend_Cache::CLEANING_MODE_ALL);
- $tableBugsCustom1->setup();
- $this->assertFalse($tableBugsCustom1->isMetadataFromCache);
- }
- public function testTableSetDefaultMetadataCacheException()
- {
- try {
- Zend_Db_Table_Abstract::setDefaultMetadataCache(new stdClass());
- $this->fail('Expected to catch Zend_Db_Table_Exception');
- } catch (Zend_Exception $e) {
- $this->assertTrue($e instanceof Zend_Db_Table_Exception,
- 'Expecting object of type Zend_Db_Table_Exception, got '.get_class($e));
- $this->assertEquals("Argument must be of type Zend_Cache_Core, or a Registry key where a Zend_Cache_Core object is stored", $e->getMessage());
- }
- try {
- Zend_Db_Table_Abstract::setDefaultMetadataCache(327);
- $this->fail('Expected to catch Zend_Db_Table_Exception');
- } catch (Exception $e) {
- $this->assertTrue($e instanceof Zend_Db_Table_Exception,
- 'Expecting object of type Zend_Db_Table_Exception, got '.get_class($e));
- $this->assertEquals("Argument must be of type Zend_Cache_Core, or a Registry key where a Zend_Cache_Core object is stored", $e->getMessage());
- }
- }
- public function testTableMetadataCacheException()
- {
- Zend_Loader::loadClass('My_ZendDbTable_TableBugs');
- /**
- * options array points 'metadataCache' to integer scalar
- */
- try {
- $table = new My_ZendDbTable_TableBugs(array('metadataCache' => 327));
- $this->fail('Expected to catch Zend_Db_Table_Exception');
- } catch (Zend_Exception $e) {
- $this->assertTrue($e instanceof Zend_Db_Table_Exception,
- 'Expecting object of type Zend_Db_Table_Exception, got '.get_class($e));
- $this->assertEquals("Argument must be of type Zend_Cache_Core, or a Registry key where a Zend_Cache_Core object is stored", $e->getMessage());
- }
- /**
- * options array points 'metadataCache' to Registry key containing integer scalar
- */
- Zend_Registry::set('registered_metadata_cache', 327);
- try {
- $table = new My_ZendDbTable_TableBugs(array('metadataCache' => 'registered_metadata_cache'));
- $this->fail('Expected to catch Zend_Db_Table_Exception');
- } catch (Zend_Exception $e) {
- $this->assertTrue($e instanceof Zend_Db_Table_Exception,
- 'Expecting object of type Zend_Db_Table_Exception, got '.get_class($e));
- $this->assertEquals("Argument must be of type Zend_Cache_Core, or a Registry key where a Zend_Cache_Core object is stored", $e->getMessage());
- }
- }
- public function testTableCascadeUpdate()
- {
- $table = $this->_table['products'];
- $row1 = $table->find(1)->current();
- $rows1 = $row1->findManyToManyRowset('My_ZendDbTable_TableBugs', 'My_ZendDbTable_TableBugsProducts');
- $product_id = $this->_db->foldCase('product_id');
- $row1->$product_id = 999999;
- $row1->save();
- $rows2 = $row1->findManyToManyRowset('My_ZendDbTable_TableBugs', 'My_ZendDbTable_TableBugsProducts');
- $this->assertEquals(999999, $row1->$product_id);
- $this->assertEquals(count($rows1), count($rows2));
- // Test for 'false' value in cascade config
- $bug_id = $this->_db->foldCase('bug_id');
- $row2 = $rows2->current();
- $row2->$bug_id = 999999;
- $row2->save();
- }
- public function testTableCascadeDelete()
- {
- $table = $this->_table['products'];
- $row1 = $table->find(2)->current();
- $row1->delete();
- // Test for 'false' value in cascade config
- $table = $this->_table['bugs'];
- $row2 = $table->find(1)->current();
- $row2->delete();
- $table = $this->_table['bugs_products'];
- $product_id = $this->_db->quoteIdentifier('product_id', true);
- $select = $table->select()
- ->where($product_id . ' = ?', 2);
- $rows = $table->fetchAll($select);
- $this->assertEquals(0, count($rows));
- }
- /**
- * @group ZF-1103
- */
- public function testTableCascadeRecurseDelete()
- {
- $tblRecursive = $this->_getTable('My_ZendDbTable_TableCascadeRecursive');
- // Enforce initial table structure
- $parentRow = $tblRecursive->find(1)->current();
- $this->assertTrue($parentRow instanceof Zend_Db_Table_Row);
- $childRows = $parentRow->findDependentRowset('My_ZendDbTable_TableCascadeRecursive', 'Children');
- $this->assertTrue($childRows instanceof Zend_Db_Table_Rowset);
- $this->assertEquals(2, count($childRows));
- foreach ( $childRows as $childRow ) {
- $this->assertTrue($childRow instanceof Zend_Db_Table_Row);
- $subChildRows = $childRow->findDependentRowset('My_ZendDbTable_TableCascadeRecursive', 'Children');
- $this->assertTrue($subChildRows instanceof Zend_Db_Table_Rowset);
- $this->assertEquals( $childRow['item_id'] == 3 ? 2 : 0 , count($subChildRows));
- }
- // Perform the delete
- $parentRow->delete();
- // Assert that all children of #1 (2,3,4,5) are removed recursively
- $this->assertNull($tblRecursive->find(1)->current());
- $this->assertNull($tblRecursive->find(2)->current());
- $this->assertNull($tblRecursive->find(3)->current());
- $this->assertNull($tblRecursive->find(4)->current());
- $this->assertNull($tblRecursive->find(5)->current());
- //... but #6 remains
- $this->assertTrue($tblRecursive->find(6)->current() instanceof Zend_Db_Table_Row);
- }
- /**
- * @group ZF-11810
- */
- public function testTableCascadeRecurseDeleteUsingTableDeleteMethod()
- {
- $tblRecursive = $this->_getTable('My_ZendDbTable_TableCascadeRecursive');
- // Enforce initial table structure
- $parentRow = $tblRecursive->find(1)->current();
- $this->assertTrue($parentRow instanceof Zend_Db_Table_Row);
- $childRows = $parentRow->findDependentRowset('My_ZendDbTable_TableCascadeRecursive', 'Children');
- $this->assertTrue($childRows instanceof Zend_Db_Table_Rowset);
- $this->assertEquals(2, count($childRows));
- foreach ( $childRows as $childRow ) {
- $this->assertTrue($childRow instanceof Zend_Db_Table_Row);
- $subChildRows = $childRow->findDependentRowset('My_ZendDbTable_TableCascadeRecursive', 'Children');
- $this->assertTrue($subChildRows instanceof Zend_Db_Table_Rowset);
- $this->assertEquals( $childRow['item_id'] == 3 ? 2 : 0 , count($subChildRows));
- }
- // Perform the delete
- $tblRecursive->delete($tblRecursive->getAdapter()->quoteInto('item_id = ?', 1));
- // Assert that all children of #1 (2,3,4,5) are removed recursively
- $this->assertNull($tblRecursive->find(1)->current());
- $this->assertNull($tblRecursive->find(2)->current());
- $this->assertNull($tblRecursive->find(3)->current());
- $this->assertNull($tblRecursive->find(4)->current());
- $this->assertNull($tblRecursive->find(5)->current());
- //... but #6 remains
- $this->assertTrue($tblRecursive->find(6)->current() instanceof Zend_Db_Table_Row);
- }
- public function testSerialiseTable()
- {
- $table = $this->_table['products'];
- $this->assertTrue(is_string(serialize($table)));
- }
- /**
- * @group ZF-1343
- */
- public function testTableFetchallCanHandleWhereWithParameritizationCharacters()
- {
- $product_name = $this->_db->quoteIdentifier('product_name');
- $table = $this->_table['products'];
- $where = $table->getAdapter()->quoteInto("$product_name = ?", "some?product's");
- $rows = $table->fetchAll($where);
- $this->assertEquals(0, count($rows));
- }
- /**
- * @group ZF-3486
- */
- public function testTableConcreteInstantiation()
- {
- Zend_Db_Table::setDefaultAdapter($this->_db);
- $table = new Zend_Db_Table('zfbugs');
- $rowset = $table->find(1);
- $this->assertEquals(1, count($rowset));
- Zend_Db_Table::setDefaultAdapter();
- $table = new Zend_Db_Table(array(
- 'name' => 'zfbugs',
- 'db' => $this->_db
- ));
- $rowset = $table->find(1);
- $this->assertEquals(1, count($rowset));
- }
- /**
- * Returns a clean Zend_Cache_Core with File backend
- *
- * @return Zend_Cache_Core
- */
- protected function _getCache()
- {
- /**
- * @see Zend_Cache
- */
- require_once 'Zend/Cache.php';
- $folder = dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'cachefiles';
- $frontendOptions = array(
- 'automatic_serialization' => true
- );
- $backendOptions = array(
- 'cache_dir' => $folder,
- 'file_name_prefix' => 'Zend_Db_Table_TestCommon'
- );
- $cacheFrontend = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);
- $cacheFrontend->clean(Zend_Cache::CLEANING_MODE_ALL);
- return $cacheFrontend;
- }
- /**
- * Returns a clean Zend_Cache_Core with File backend
- *
- * @return Zend_Cache_Core
- */
- protected function _getCacheNowrite()
- {
- /**
- * @see Zend_Cache
- */
- require_once 'Zend/Cache.php';
- $folder = dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'nofiles';
- if (!file_exists($folder)) {
- mkdir($folder, 0777);
- }
- $frontendOptions = array(
- 'automatic_serialization' => true
- );
- $backendOptions = array(
- 'cache_dir' => $folder,
- 'file_name_prefix' => 'Zend_Db_Table_TestCommon'
- );
- $cacheFrontend = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);
- $cacheFrontend->clean(Zend_Cache::CLEANING_MODE_ALL);
- rmdir($folder);
- return $cacheFrontend;
- }
- /**
- * @group ZF-5674
- */
- public function testTableAndIdentityWithVeryLongName()
- {
- Zend_Db_Table::setDefaultAdapter($this->_db);
- // create test table using no identifier quoting
- $this->_util->createTable('thisisaveryverylongtablename', array(
- 'thisisalongtablenameidentity' => 'IDENTITY',
- 'stuff' => 'VARCHAR(32)'
- ));
- $tableName = $this->_util->getTableName('thisisaveryverylongtablename');
- $table = new Zend_Db_Table('thisisaveryverylongtablename');
- $row = $table->createRow($this->_getRowForTableAndIdentityWithVeryLongName());
- $row->save();
- $rowset = $table->find(1);
- $this->assertEquals(1, count($rowset));
- $this->_util->dropTable('thisisaveryverylongtablename');
- }
- protected function _getRowForTableAndIdentityWithVeryLongName()
- {
- return array('stuff' => 'information');
- }
- /**
- * @group ZF-7042
- * @group ZF-10778
- */
- public function testCacheIdGeneratedToMetadata()
- {
- /**
- * @see Zend_Cache
- */
- require_once 'Zend/Cache.php';
- /**
- * @see Zend_Cache_Backend_BlackHole
- */
- require_once 'Zend/Cache/Backend/BlackHole.php';
- Zend_Db_Table::setDefaultAdapter($this->_db);
- $dbConfig = $this->_db->getConfig();
- $cacheId = md5(
- (isset($dbConfig['port']) ? ':'.$dbConfig['port'] : null)
- . (isset($dbConfig['host']) ? ':'.$dbConfig['host'] : null)
- . '/'.$dbConfig['dbname'].':.cache_metadata'
- );
- $metadata = array('id' => array('PRIMARY' => true));
- $cacheBackend = $this->getMock('Zend_Cache_Backend_BlackHole');
- $cacheBackend->expects($this->any())
- ->method('load')
- ->with($this->equalTo($cacheId))
- ->will($this->returnValue($metadata));
- $cache = Zend_Cache::factory('Core', $cacheBackend, array('automatic_serialization' => false));
- Zend_Db_Table_Abstract::setDefaultMetadataCache($cache);
- $this->_util->createTable('cache_metadata', array(
- 'id' => 'IDENTITY',
- 'name' => 'VARCHAR(32)'
- ));
- $configTable = array(
- 'name' => 'cache_metadata',
- 'primary' => 'id'
- );
- $table = new Zend_Db_Table($configTable);
- $table->info(Zend_Db_Table::METADATA);
- $this->_util->dropTable('cache_metadata');
- Zend_Db_Table_Abstract::setDefaultMetadataCache(null);
- }
- }
|