/** * Test Chill mode. * * @return void */ public function testChill() { $bean = R::dispense('bean'); $bean->col1 = '1'; $bean->col2 = '2'; R::store($bean); asrt(count(R::getWriter()->getColumns('bean')), 3); $bean->col3 = '3'; R::store($bean); asrt(count(R::getWriter()->getColumns('bean')), 4); R::freeze(array('umbrella')); $bean->col4 = '4'; R::store($bean); asrt(count(R::getWriter()->getColumns('bean')), 5); R::freeze(array('bean')); $bean->col5 = '5'; try { R::store($bean); fail(); } catch (\Exception $e) { pass(); } asrt(count(R::getWriter()->getColumns('bean')), 5); R::freeze(array()); $bean->col5 = '5'; R::store($bean); asrt(count(R::getWriter()->getColumns('bean')), 6); }
/** * Nuclear test suite. * * @return void */ public function testNuke() { $bean = R::dispense('bean'); R::store($bean); asrt(count(R::getWriter()->getTables()), 1); R::nuke(); asrt(count(R::getWriter()->getTables()), 0); $bean = R::dispense('bean'); R::store($bean); asrt(count(R::getWriter()->getTables()), 1); R::freeze(); R::nuke(); // No effect asrt(count(R::getWriter()->getTables()), 1); R::freeze(FALSE); }
/** * Tests whether we can intelligently glue together SQL snippets. * * @return void */ public function testGlue() { $writer = R::getWriter(); //Can we add a condition without having to type 'WHERE' - usual suspects asrt($writer->glueSQLCondition(' name = ? '), ' WHERE name = ? '); asrt($writer->glueSQLCondition(' value1 > ? OR value < ? '), ' WHERE value1 > ? OR value < ? '); //Does it recognize NON-WHERE conditions? - usual suspects asrt($writer->glueSQLCondition(' ORDER BY name '), ' ORDER BY name '); asrt($writer->glueSQLCondition(' LIMIT 10 '), ' LIMIT 10 '); asrt($writer->glueSQLCondition(' OFFSET 20 '), ' OFFSET 20 '); //highly doubtful but who knows... - I think nobody will ever use this in a query snippet. asrt($writer->glueSQLCondition(' GROUP BY grp '), ' GROUP BY grp '); asrt($writer->glueSQLCondition(' HAVING x = ? '), ' HAVING x = ? '); //can we replace WHERE with AND ? asrt($writer->glueSQLCondition(' AND name = ? ', QueryWriter::C_GLUE_WHERE), ' WHERE name = ? '); //can we glue with AND instead of WHERE ? asrt($writer->glueSQLCondition(' value1 > ? OR value < ? ', QueryWriter::C_GLUE_AND), ' AND value1 > ? OR value < ? '); //non-cases asrt($writer->glueSQLCondition(' GROUP BY grp ', QueryWriter::C_GLUE_WHERE), ' GROUP BY grp '); asrt($writer->glueSQLCondition(' GROUP BY grp ', QueryWriter::C_GLUE_AND), ' GROUP BY grp '); }
/** * Test whether we can manually create indexes. * * @return void */ public function testAddingIndex() { R::nuke(); $sql = ' CREATE TABLE song ( id INT( 11 ) UNSIGNED NOT NULL AUTO_INCREMENT, album_id INT( 11 ) UNSIGNED NOT NULL, category VARCHAR( 255 ), PRIMARY KEY ( id ) ) ENGINE = InnoDB '; R::exec($sql); $sql = 'SHOW INDEX FROM song'; $indexes = R::getAll($sql); asrt(count($indexes), 1); asrt($indexes[0]['Table'], 'song'); asrt($indexes[0]['Key_name'], 'PRIMARY'); $writer = R::getWriter(); $writer->addIndex('song', 'index1', 'album_id'); $indexes = R::getAll('SHOW INDEX FROM song'); asrt(count($indexes), 2); asrt($indexes[0]['Table'], 'song'); asrt($indexes[0]['Key_name'], 'PRIMARY'); asrt($indexes[1]['Table'], 'song'); asrt($indexes[1]['Key_name'], 'index1'); //Cant add the same index twice $writer->addIndex('song', 'index2', 'category'); $indexes = R::getAll('SHOW INDEX FROM song'); asrt(count($indexes), 3); //Dont fail, just dont try { $writer->addIndex('song', 'index3', 'nonexistant'); pass(); } catch (\Exception $e) { fail(); } asrt(count($indexes), 3); try { $writer->addIndex('nonexistant', 'index4', 'nonexistant'); pass(); } catch (\Exception $e) { fail(); } asrt(count($indexes), 3); try { $writer->addIndex('nonexistant', '', 'nonexistant'); pass(); } catch (\Exception $e) { fail(); } asrt(count($indexes), 3); }
/** * Test trashAll(). */ public function testMultiDeleteUpdate() { testpack('test multi delete and multi update'); $beans = R::dispenseLabels('bean', array('a', 'b')); $ids = R::storeAll($beans); asrt((int) R::count('bean'), 2); R::trashAll(R::batch('bean', $ids)); asrt((int) R::count('bean'), 0); testpack('test assocManager check'); $rb = new OODB(R::getWriter()); try { $rb->getAssociationManager(); fail(); } catch (RedException $e) { pass(); } }
/** * Tests foreign keys but checks using ProxyWriter. * * @return void */ public function testFKInspect() { $faultyWriter = new \FaultyWriter(R::getDatabaseAdapter()); try { $null = \ProxyWriter::callMethod($faultyWriter, 'getForeignKeyForTypeProperty', 'test', 'test'); pass(); } catch (\Exception $e) { fail(); } asrt(is_null($null), TRUE); $writer = R::getWriter(); R::nuke(); $book = R::dispense('book'); $page = R::dispense('page'); $book->xownPage[] = $page; R::store($book); $keys = \ProxyWriter::callMethod($writer, 'getForeignKeyForTypeProperty', 'page', 'book_id'); asrt(is_array($keys), TRUE); asrt($keys['on_delete'], 'CASCADE'); $keys = \ProxyWriter::callMethod($writer, 'getForeignKeyForTypeProperty', 'page', 'id'); asrt(is_null($keys), TRUE); R::nuke(); $book = R::dispense('book'); $page = R::dispense('page'); $book->ownPage[] = $page; R::store($book); $keys = \ProxyWriter::callMethod($writer, 'getForeignKeyForTypeProperty', 'page', 'book_id'); asrt(is_array($keys), TRUE); asrt($keys['on_delete'], 'SET NULL'); $keys = \ProxyWriter::callMethod($writer, 'getForeignKeyForTypeProperty', 'page', 'id'); asrt(is_null($keys), TRUE); R::nuke(); $book = R::dispense('book'); $page = R::dispense('page'); $book->alias('magazine')->xownPage[] = $page; R::store($book); $keys = \ProxyWriter::callMethod($writer, 'getForeignKeyForTypeProperty', 'page', 'magazine_id'); asrt(is_array($keys), TRUE); asrt($keys['on_delete'], 'CASCADE'); $keys = \ProxyWriter::callMethod($writer, 'getForeignKeyForTypeProperty', 'page', 'book_id'); asrt(is_null($keys), TRUE); $keys = \ProxyWriter::callMethod($writer, 'getForeignKeyForTypeProperty', 'page', 'id'); asrt(is_null($keys), TRUE); R::nuke(); $book = R::dispense('book'); $page = R::dispense('page'); $book->cover = $page; R::store($book); $keys = \ProxyWriter::callMethod($writer, 'getForeignKeyForTypeProperty', 'book', 'cover_id'); asrt(is_array($keys), TRUE); asrt($keys['on_delete'], 'SET NULL'); $keys = \ProxyWriter::callMethod($writer, 'getForeignKeyForTypeProperty', 'book', 'page_id'); asrt(is_null($keys), TRUE); $keys = \ProxyWriter::callMethod($writer, 'getForeignKeyForTypeProperty', 'book', 'id'); asrt(is_null($keys), TRUE); R::nuke(); $book = R::dispense('book'); $category = R::dispense('category'); $book->sharedTag[] = $category; R::store($book); $keys = \ProxyWriter::callMethod($writer, 'getForeignKeyForTypeProperty', 'book_category', 'book_id'); asrt(is_array($keys), TRUE); $keys = \ProxyWriter::callMethod($writer, 'getForeignKeyForTypeProperty', 'book_category', 'category_id'); asrt(is_array($keys), TRUE); $keys = \ProxyWriter::callMethod($writer, 'getForeignKeyForTypeProperty', 'book_category', 'id'); asrt(is_null($keys), TRUE); }
/** * Test common Facade usage scenarios. * * @return void */ public function testCommonUsageFacade() { $toolbox = R::getToolBox(); $adapter = $toolbox->getDatabaseAdapter(); $writer = $toolbox->getWriter(); $redbean = $toolbox->getRedBean(); $pdo = $adapter->getDatabase(); $a = new AssociationManager($toolbox); asrt(R::getRedBean() instanceof OODB, TRUE); asrt(R::getToolBox() instanceof ToolBox, TRUE); asrt(R::getDatabaseAdapter() instanceof Adapter, TRUE); asrt(R::getWriter() instanceof QueryWriter, TRUE); $book = R::dispense("book"); asrt($book instanceof OODBBean, TRUE); $book->title = "a nice book"; $id = R::store($book); asrt($id > 0, TRUE); $book = R::load("book", (int) $id); asrt($book->title, "a nice book"); asrt(R::load('book', 999)->title, NULL); R::freeze(TRUE); try { R::load('bookies', 999); fail(); } catch (\Exception $e) { pass(); } R::freeze(FALSE); $author = R::dispense("author"); $author->name = "me"; R::store($author); $book9 = R::dispense("book"); $author9 = R::dispense("author"); $author9->name = "mr Nine"; $a9 = R::store($author9); $book9->author_id = $a9; $bk9 = R::store($book9); $book9 = R::load("book", $bk9); $author = R::load("author", $book9->author_id); asrt($author->name, "mr Nine"); R::trash($author); R::trash($book9); pass(); $book2 = R::dispense("book"); $book2->title = "second"; R::store($book2); $book3 = R::dispense("book"); $book3->title = "third"; R::store($book3); asrt(count(R::find("book")), 3); asrt(count(R::findAll("book")), 3); asrt(count(R::findAll("book", " LIMIT 2")), 2); asrt(count(R::find("book", " id=id ")), 3); asrt(count(R::find("book", " title LIKE ?", array("third"))), 1); asrt(count(R::find("book", " title LIKE ?", array("%d%"))), 2); // Find without where clause asrt(count(R::findAll('book', ' order by id')), 3); R::trash($book3); R::trash($book2); asrt(count(R::getAll("SELECT * FROM book ")), 1); asrt(count(R::getCol("SELECT title FROM book ")), 1); asrt((int) R::getCell("SELECT 123 "), 123); $book = R::dispense("book"); $book->title = "not so original title"; $author = R::dispense("author"); $author->name = "Bobby"; R::store($book); $aid = R::store($author); $author = R::findOne("author", " name = ? ", array("Bobby")); }
/** * All kinds of tests for basic CRUD. * * Does the data survive? * * @return void */ public function testUpdatingBeans() { testpack('Test basic support UUID/override ID default value'); $bean = R::dispense('bean'); R::store($bean); if ($this->currentlyActiveDriverID === 'mysql') { //otherwise UTF8 causes index overflow in mysql: SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes R::exec('alter table bean modify column id char(3);'); } else { R::getWriter()->widenColumn('bean', 'id', R::getWriter()->scanType('abc')); } $bean->id = 'abc'; R::store($bean); asrt($bean->id, 'abc'); testpack('Test Update'); try { R::store(array()); fail(); } catch (RedException $e) { pass(); } $toolbox = R::getToolBox(); $adapter = $toolbox->getDatabaseAdapter(); $writer = $toolbox->getWriter(); $redbean = $toolbox->getRedBean(); $pdo = $adapter->getDatabase(); $page = $redbean->dispense("page"); $page->name = "old name"; $id = $redbean->store($page); asrt($page->getMeta('tainted'), FALSE); $page->setAttr('name', "new name"); asrt($page->getMeta('tainted'), TRUE); $id = $redbean->store($page); $page = $redbean->load("page", $id); asrt($page->name, "new name"); // Null should == NULL after saving $page->rating = NULL; $newid = $redbean->store($page); $page = $redbean->load("page", $id); asrt($page->name, "new name"); asrt($page->rating === NULL, TRUE); asrt(!$page->rating, TRUE); $page->rating = FALSE; $newid = $redbean->store($page); asrt($newid, $id); $page = $redbean->load("page", $id); asrt($page->name, "new name"); asrt((bool) $page->rating, FALSE); asrt($page->rating == FALSE, TRUE); asrt(!$page->rating, TRUE); $page->rating = TRUE; $newid = $redbean->store($page); asrt($newid, $id); $page = $redbean->load("page", $id); asrt($page->name, "new name"); asrt((bool) $page->rating, TRUE); asrt($page->rating == TRUE, TRUE); asrt($page->rating == TRUE, TRUE); $page->rating = NULL; R::store($page); $page = R::load('page', $page->id); asrt($page->rating, NULL); $page->rating = "1"; $newid = $redbean->store($page); asrt($newid, $id); $page = $redbean->load("page", $id); asrt($page->name, "new name"); asrt($page->rating, "1"); $page->rating = "0"; $newid = $redbean->store($page); asrt($page->rating, "0"); $page->rating = 0; $newid = $redbean->store($page); asrt($page->rating, 0); $page->rating = "0"; $newid = $redbean->store($page); asrt($newid, $id); $page = $redbean->load("page", $id); asrt($page->name, "new name"); asrt(!$page->rating, TRUE); asrt($page->rating == 0, TRUE); asrt($page->rating == FALSE, TRUE); $page->rating = 5; $newid = $redbean->store($page); asrt($newid, $id); $page = $redbean->load("page", $id); asrt($page->name, "new name"); asrt(strval($page->rating), "5"); $page->rating = 300; $newid = $redbean->store($page); asrt($newid, $id); $page = $redbean->load("page", $id); asrt($page->name, "new name"); asrt(strval($page->rating), "300"); $page->rating = -2; $newid = $redbean->store($page); asrt($newid, $id); $page = $redbean->load("page", $id); asrt($page->name, "new name"); asrt(strval($page->rating), "-2"); $page->rating = 2.5; $newid = $redbean->store($page); asrt($newid, $id); $page = $redbean->load("page", $id); asrt($page->name, "new name"); asrt($page->rating == 2.5, TRUE); $page->rating = -3.3; $newid = $redbean->store($page); asrt($newid, $id); $page = $redbean->load("page", $id); asrt($page->name, "new name"); asrt($page->rating == -3.3, TRUE); $page->rating = "good"; $newid = $redbean->store($page); asrt($newid, $id); $page = $redbean->load("page", $id); asrt($page->name, "new name"); asrt($page->rating, "good"); $longtext = str_repeat('great! because..', 100); $page->rating = $longtext; $newid = $redbean->store($page); asrt($newid, $id); $page = $redbean->load("page", $id); asrt($page->name, "new name"); asrt($page->rating, $longtext); // Test leading zeros $numAsString = "0001"; $page->numasstring = $numAsString; $redbean->store($page); $page = $redbean->load("page", $id); asrt($page->numasstring, "0001"); $page->numnotstring = "0.123"; $redbean->store($page); $page = $redbean->load("page", $id); asrt($page->numnotstring == 0.123, TRUE); $page->numasstring2 = "00.123"; $redbean->store($page); $page = $redbean->load("page", $id); asrt($page->numasstring2, "00.123"); try { $redbean->trash(array()); fail(); } catch (RedException $e) { pass(); } $redbean->trash($page); asrt((int) $pdo->GetCell("SELECT count(*) FROM page"), 0); }
/** * Test single column bases unique constraints. * * @return void */ public function testSingleColUniqueConstraint() { testpack('Testing unique constraint on single column'); $book = R::dispense('book'); $book->title = 'bla'; $book->extra = 2; $id = R::store($book); R::getWriter()->addUniqueIndex('book', array('title')); $book = R::dispense('book'); $book->title = 'bla'; $expected = NULL; try { R::store($book); fail(); } catch (SQL $e) { $expected = $e; } asrt($expected instanceof SQL, TRUE); asrt(R::count('book'), 1); $book = R::load('book', $id); // Causes failure, table will be rebuild $book->extra = 'CHANGE'; $id2 = R::store($book); $book2 = R::load('book', $id2); $book = R::dispense('book'); $book->title = 'bla'; try { R::store($book); fail(); } catch (SQL $e) { $expected = $e; } asrt($expected instanceof SQL, TRUE); asrt(R::count('book'), 1); }
/** * Tests index fails. * * @return void */ public function testIndexCreationFail() { R::nuke(); $book = R::dispense('book'); $book->author_id = 'a'; R::store($book); $indexes = getIndexes('book'); //should just work fine asrt(in_array('index_foreignkey_book_author', $indexes), TRUE); //these should just pass, no indexes but no errors as well R::getWriter()->addIndex('book', 'bla', 'nonexist'); pass(); R::getWriter()->addIndex('book', '@#$', 'nonexist'); pass(); R::getWriter()->addIndex('nonexist', 'bla', 'nonexist'); pass(); $indexesAfter = getIndexes('book'); asrt(count($indexesAfter), count($indexes)); }
/** * Test whether we can manually create indexes. * * @return void */ public function testAddingIndex() { R::nuke(); $sql = 'CREATE TABLE song ( id INTEGER PRIMARY KEY AUTOINCREMENT, album_id INTEGER, category TEXT ) '; R::exec($sql); $writer = R::getWriter(); $indexes = R::getAll('PRAGMA index_list("song") '); asrt(count($indexes), 0); $writer->addIndex('song', 'index1', 'album_id'); $indexes = R::getAll('PRAGMA index_list("song") '); asrt(count($indexes), 1); $writer->addIndex('song', 'index1', 'album_id'); $indexes = R::getAll('PRAGMA index_list("song") '); asrt(count($indexes), 1); $writer->addIndex('song', 'index2', 'category'); $indexes = R::getAll('PRAGMA index_list("song") '); asrt(count($indexes), 2); try { $writer->addIndex('song', 'index1', 'nonexistant'); pass(); } catch (\Exception $ex) { fail(); } $indexes = R::getAll('PRAGMA index_list("song") '); asrt(count($indexes), 2); try { $writer->addIndex('nonexistant', 'index1', 'nonexistant'); pass(); } catch (\Exception $ex) { fail(); } $indexes = R::getAll('PRAGMA index_list("song") '); asrt(count($indexes), 2); }
/** * Test selecting. * * @return void */ public function testSelects() { $rooms = R::dispense('room', 2); $rooms[0]->kind = 'suite'; $rooms[1]->kind = 'classic'; $rooms[0]->number = 6; $rooms[1]->number = 7; R::store($rooms[0]); R::store($rooms[1]); $rooms = R::getAssoc('SELECT * FROM room WHERE id < -999'); asrt(is_array($rooms), TRUE); asrt(count($rooms), 0); $rooms = R::getAssoc('SELECT ' . R::getWriter()->esc('number') . ', kind FROM room ORDER BY kind ASC'); foreach ($rooms as $key => $room) { asrt($key === 6 || $key === 7, TRUE); asrt($room == 'classic' || $room == 'suite', TRUE); } $rooms = R::getDatabaseAdapter()->getAssoc('SELECT kind FROM room'); foreach ($rooms as $key => $room) { asrt($room == 'classic' || $room == 'suite', TRUE); asrt($room, $key); } $rooms = R::getAssoc('SELECT `number`, kind FROM rooms2 ORDER BY kind ASC'); asrt(count($rooms), 0); asrt(is_array($rooms), TRUE); // GetCell should return NULL in case of exception asrt(NULL, R::getCell('SELECT dream FROM fantasy')); }
/** * Test creation of link table. * * @return void */ public function testCreationOfLinkTable() { asrt(in_array('consult', R::getWriter()->getTables()), FALSE); $d = R::dispense('doctor')->setAttr('name', 'd1'); $p = R::dispense('patient')->setAttr('name', 'p1'); $d->sharedPatient[] = $p; R::store($d); asrt(in_array('consult', R::getWriter()->getTables()), TRUE); }
/** * Test exporting with filters. * * @return void */ public function ExportWithFilters() { testpack('Export with filters'); $book = R::dispense('book'); $pages = R::dispense('page', 2); $texts = R::dispense('text', 2); $images = R::dispense('image', 2); $author = R::dispense('author'); $pub = R::dispense('publisher'); $bookmarks = R::dispense('bookmark', 2); $pages[0]->ownText = array($texts[0]); $pages[0]->ownImage = array($images[0]); $pages[1]->ownText = array($texts[1]); $pages[1]->ownImage = array($images[1]); $pages[0]->sharedBookmark[] = $bookmarks[0]; $pages[1]->sharedBookmark[] = $bookmarks[1]; $bookmarks[0]->ownNote[] = R::dispense('note')->setAttr('text', 'a note'); $bookmarks[1]->ownNote[] = R::dispense('note')->setAttr('text', 'a note'); $book->ownPage = $pages; $book->author = $author; $author->publisher = $pub; $bookID = R::store($book); R::getDuplicationManager()->setTables(R::getWriter()->getTables()); $objects = R::exportAll(array($book), TRUE, array()); asrt(isset($objects[0]['ownPage']), TRUE); asrt(count($objects[0]['ownPage']), 2); asrt(isset($objects[0]['author']), TRUE); asrt(isset($objects[0]['ownPage'][0]['ownText']), TRUE); asrt(count($objects[0]['ownPage'][0]['ownText']), 1); asrt(isset($objects[0]['ownPage'][0]['ownImage']), TRUE); asrt(count($objects[0]['ownPage'][0]['ownImage']), 1); $objects = R::exportAll(array($book), TRUE, array('page', 'author', 'text', 'image')); asrt(isset($objects[0]['ownPage']), TRUE); asrt(count($objects[0]['ownPage']), 2); asrt(isset($objects[0]['author']), TRUE); asrt(isset($objects[0]['ownPage'][0]['ownText']), TRUE); asrt(count($objects[0]['ownPage'][0]['ownText']), 1); asrt(isset($objects[0]['ownPage'][0]['ownImage']), TRUE); asrt(count($objects[0]['ownPage'][0]['ownImage']), 1); $objects = R::exportAll(array($book), TRUE, 'author'); asrt(isset($objects[0]['ownPage']), FALSE); asrt(isset($objects[0]['ownPage'][0]['ownText']), FALSE); $objects = R::exportAll(array($book), TRUE, array('page')); asrt(isset($objects[0]['author']), FALSE); asrt(isset($objects[0]['ownPage'][0]['ownText']), FALSE); $objects = R::exportAll(array($book), TRUE, array('page', 'text')); asrt(isset($objects[0]['author']), FALSE); asrt(isset($objects[0]['ownPage']), TRUE); asrt(isset($objects[0]['ownPage'][0]['ownText']), TRUE); asrt(count($objects[0]['ownPage'][0]['ownText']), 1); asrt(isset($objects[0]['ownPage'][0]['ownImage']), FALSE); $objects = R::exportAll(array($book), TRUE, array('none')); asrt(isset($objects[0]['author']), FALSE); asrt(isset($objects[0]['ownPage']), FALSE); $texts = R::find('text'); R::getDuplicationManager()->setCacheTables(FALSE); testpack('Keyless export'); $book = R::load('book', $bookID); $book->ownPage; $export = $book->export(); asrt(isset($export['ownPage'][0]), TRUE); }
/** * Test whether we can manually create indexes. * * @return void */ public function testAddingIndex() { R::nuke(); $sql = 'CREATE TABLE song ( id SERIAL PRIMARY KEY, album_id INTEGER, category VARCHAR(255) )'; R::exec($sql); $indexes = R::getAll(" SELECT * FROM pg_indexes WHERE schemaname = 'public' AND tablename = 'song' "); asrt(count($indexes), 1); $writer = R::getWriter(); $writer->addIndex('song', 'index1', 'album_id'); $indexes = R::getAll(" SELECT * FROM pg_indexes WHERE schemaname = 'public' AND tablename = 'song' "); asrt(count($indexes), 2); //Cant add the same index twice $writer->addIndex('song', 'index1', 'album_id'); $indexes = R::getAll(" SELECT * FROM pg_indexes WHERE schemaname = 'public' AND tablename = 'song' "); asrt(count($indexes), 2); $writer->addIndex('song', 'index2', 'category'); $indexes = R::getAll(" SELECT * FROM pg_indexes WHERE schemaname = 'public' AND tablename = 'song' "); asrt(count($indexes), 3); //Dont fail, just dont try { $writer->addIndex('song', 'index3', 'nonexistant'); pass(); } catch (\Exception $e) { fail(); } $indexes = R::getAll(" SELECT * FROM pg_indexes WHERE schemaname = 'public' AND tablename = 'song' "); asrt(count($indexes), 3); try { $writer->addIndex('nonexistant', 'index4', 'nonexistant'); pass(); } catch (\Exception $e) { fail(); } $indexes = R::getAll(" SELECT * FROM pg_indexes WHERE schemaname = 'public' AND tablename = 'song' "); asrt(count($indexes), 3); }
/** * Test explicit flush. * * @return void */ public function testExplicitCacheFlush() { testpack('Test cache flush (explicit)'); R::debug(true, 1); $logger = R::getDatabaseAdapter()->getDatabase()->getLogger(); $bean = R::dispense('bean'); $bean->title = 'abc'; $id1 = R::store($bean); $logger->clear(); $bean = R::load('bean', $id1); asrt($bean->title, 'abc'); asrt(count($logger->grep('SELECT *')), 1); $bean = R::load('bean', $id1); asrt(count($logger->grep('SELECT *')), 1); R::getWriter()->flushCache(); $bean = R::load('bean', $id1); asrt(count($logger->grep('SELECT *')), 2); R::getWriter()->flushCache(); R::getWriter()->setUseCache(FALSE); }
/** * Test NULL. * * @return void */ public function testNull() { asrt(setget("NULL"), "NULL"); asrt(setget("NULL"), "NULL"); asrt(setget("0123", 1), "0123"); asrt(setget("0000123", 1), "0000123"); asrt(setget(NULL), NULL); asrt(setget(0) == 0, TRUE); asrt(setget(1) == 1, TRUE); asrt(setget(TRUE) == TRUE, TRUE); asrt(setget(FALSE) == FALSE, TRUE); // minor test sqltest $a = R::getWriter()->sqlStateIn('000', array()); // Unknown state must return FALSE. asrt($a, FALSE); try { R::getWriter()->esc('`aaa`'); fail(); } catch (\Exception $e) { pass(); } asrt($e instanceof RedException, TRUE); }
/** * Test Recursion */ public function testRecursion() { list($d1, $d2) = R::dispense('document', 2); $page = R::dispense('page'); list($p1, $p2) = R::dispense('paragraph', 2); list($e1, $e2) = R::dispense('excerpt', 2); $id2 = R::store($d2); $p1->name = 'a'; $p2->name = 'b'; $page->title = 'my page'; $page->ownParagraph = array($p1, $p2); $p1->ownExcerpt[] = $e1; $p2->ownExcerpt[] = $e2; $e1->ownDocument[] = $d2; $e2->ownDocument[] = $d1; $d1->ownPage[] = $page; $id1 = R::store($d1); $d1 = R::load('document', $id1); $d = R::dup($d1); $ids = array(); asrt($d instanceof OODBBean, TRUE); asrt(count($d->ownPage), 1); foreach (end($d->ownPage)->ownParagraph as $p) { foreach ($p->ownExcerpt as $e) { $ids[] = end($e->ownDocument)->id; } } sort($ids); asrt((int) $ids[0], 0); asrt((int) $ids[1], $id1); R::store($d); pass(); $phillies = R::dispense('diner'); list($lonelyman, $man, $woman) = R::dispense('guest', 3); $attendant = R::dispense('employee'); $lonelyman->name = 'Bennie Moten'; $man->name = 'Daddy Stovepipe'; $woman->name = 'Mississippi Sarah'; $attendant->name = 'Gus Cannon'; $phillies->sharedGuest = array($lonelyman, $man, $woman); $phillies->ownEmployee[] = $attendant; $props = R::dispense('prop', 2); $props[0]->kind = 'cigarette'; $props[1]->kind = 'coffee'; $thought = R::dispense('thought'); $thought->content = 'Blues'; $thought2 = R::dispense('thought'); $thought2->content = 'Jazz'; $woman->ownProp[] = $props[0]; $man->sharedProp[] = $props[1]; $attendant->ownThought = array($thought, $thought2); R::store($phillies); $diner = R::findOne('diner'); $diner2 = R::dup($diner); $id2 = R::store($diner2); $diner2 = R::load('diner', $id2); asrt(count($diner->ownEmployee), 1); asrt(count($diner2->ownEmployee), 1); asrt(count($diner->sharedGuest), 3); asrt(count($diner2->sharedGuest), 3); $employee = reset($diner->ownEmployee); asrt(count($employee->ownThought), 2); $employee = reset($diner2->ownEmployee); asrt(count($employee->ownThought), 2); // Can we change something in the duplicate without changing the original? $employee->name = 'Marvin'; $thought = R::dispense('thought'); $thought->content = 'depression'; $employee->ownThought[] = $thought; array_pop($diner2->sharedGuest); $guest = reset($diner2->sharedGuest); $guest->name = 'Arthur Dent'; $id2 = R::store($diner2); $diner2 = R::load('diner', $id2); asrt(count($diner->ownEmployee), 1); asrt(count($diner2->ownEmployee), 1); asrt(count($diner->sharedGuest), 3); asrt(count($diner2->sharedGuest), 2); $employeeOld = reset($diner->ownEmployee); asrt(count($employeeOld->ownThought), 2); $employee = reset($diner2->ownEmployee); asrt(count($employee->ownThought), 3); asrt($employee->name, 'Marvin'); asrt($employeeOld->name, 'Gus Cannon'); // However the shared beans must not be copied asrt(R::count('guest'), 3); asrt(R::count('guest_prop'), 1); $arthur = R::findOne('guest', ' ' . R::getWriter()->esc('name') . ' = ? ', array('Arthur Dent')); asrt($arthur->name, 'Arthur Dent'); }