getToolBox() public static method

To set the toolbox use R::setup() or R::configureFacadeWithToolbox(). To create a toolbox use Setup::kickstart(). Or create a manual toolbox using the ToolBox class.
public static getToolBox ( ) : ToolBox
return ToolBox
Esempio n. 1
0
 /**
  * Test SQLite table rebuilding.
  *
  * @return void
  */
 public function testRebuilder()
 {
     $toolbox = R::getToolBox();
     $adapter = $toolbox->getDatabaseAdapter();
     $writer = $toolbox->getWriter();
     $redbean = $toolbox->getRedBean();
     $pdo = $adapter->getDatabase();
     $book = R::dispense('book');
     $page = R::dispense('page');
     $book->xownPage[] = $page;
     $id = R::store($book);
     $book = R::load('book', $id);
     asrt(count($book->xownPage), 1);
     asrt((int) R::getCell('SELECT COUNT(*) FROM page'), 1);
     R::trash($book);
     asrt((int) R::getCell('SELECT COUNT(*) FROM page'), 0);
     $book = R::dispense('book');
     $page = R::dispense('page');
     $book->xownPage[] = $page;
     $id = R::store($book);
     $book = R::load('book', $id);
     asrt(count($book->xownPage), 1);
     asrt((int) R::getCell('SELECT COUNT(*) FROM page'), 1);
     $book->added = 2;
     R::store($book);
     $book->added = 'added';
     R::store($book);
     R::trash($book);
     asrt((int) R::getCell('SELECT COUNT(*) FROM page'), 0);
 }
Esempio n. 2
0
 /**
  * Does the toolbox contain the necessary tools ?
  *
  * @return void
  */
 public function testDoesToolboxContainTheTools()
 {
     $toolbox = R::getToolBox();
     asrt($toolbox->getDatabaseAdapter() instanceof Adapter, TRUE);
     asrt($toolbox->getRedBean() instanceof OODB, TRUE);
     asrt($toolbox->getWriter() instanceof QueryWriter, TRUE);
 }
Esempio n. 3
0
 /**
  * MySQL specific tests.
  *
  * @return void
  */
 public function testMySQL()
 {
     if ($this->currentlyActiveDriverID !== 'mysql') {
         return;
     }
     testpack('Throw exception in case of issue with assoc constraint');
     $bunny = R::dispense('bunny');
     $carrot = R::dispense('carrot');
     $faultyWriter = new \FaultyWriter(R::getToolBox()->getDatabaseAdapter());
     $faultyOODB = new OODB($faultyWriter);
     $faultyOODB->setBeanHelper(R::getRedBean()->getBeanHelper());
     $faultyToolbox = new ToolBox($faultyOODB, R::getToolBox()->getDatabaseAdapter(), $faultyWriter);
     $faultyAssociationManager = new AssociationManager($faultyToolbox);
     $faultyWriter->setSQLState('23000');
     $faultyAssociationManager->associate($bunny, $carrot);
     pass();
     $faultyWriter->setSQLState('42S22');
     R::nuke();
     try {
         $faultyAssociationManager->associate($bunny, $carrot);
         fail();
     } catch (SQL $exception) {
         pass();
     }
 }
Esempio n. 4
0
 /**
  * Test integration with pre-existing schemas.
  *
  * @return void
  */
 public function testPlaysNiceWithPreExitsingSchema()
 {
     $toolbox = R::getToolBox();
     $adapter = $toolbox->getDatabaseAdapter();
     $writer = $toolbox->getWriter();
     $redbean = $toolbox->getRedBean();
     $pdo = $adapter->getDatabase();
     $a = new AssociationManager($toolbox);
     $page = $redbean->dispense("page");
     $page->name = "John's page";
     $idpage = $redbean->store($page);
     $page2 = $redbean->dispense("page");
     $page2->name = "John's second page";
     $idpage2 = $redbean->store($page2);
     $a->associate($page, $page2);
     $adapter->exec("ALTER TABLE " . $writer->esc('page') . "\n\t\tCHANGE " . $writer->esc('name') . " " . $writer->esc('name') . "\n\t\tVARCHAR( 254 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL ");
     $page = $redbean->dispense("page");
     $page->name = "Just Another Page In a Table";
     $cols = $writer->getColumns("page");
     asrt($cols["name"], "varchar(254)");
     //$pdo->SethMode(1);
     $redbean->store($page);
     pass();
     // No crash?
     $cols = $writer->getColumns("page");
     asrt($cols["name"], "varchar(254)");
     //must still be same
 }
Esempio n. 5
0
 /**
  * Test the __toString method of the SQLHelper. 
  */
 public function testToString()
 {
     $toolbox = R::getToolBox();
     $adapter = $toolbox->getDatabaseAdapter();
     $sqlHelper = new SQLHelper($adapter);
     $sqlHelper->begin()->select('*')->from('table')->where('name = ?')->put('name');
     $str = (string) $sqlHelper;
     asrt(strpos($str, 'query') !== false, true);
     asrt(strpos($str, 'select * from table where name = ?') !== false, true);
     asrt(strpos($str, '=> Array') !== false, true);
     asrt(strpos($str, 'params') !== false, true);
 }
Esempio n. 6
0
 /**
  * Begin testing.
  * This method runs the actual test pack.
  *
  * @return void
  */
 public function testBatch()
 {
     R::freeze(FALSE);
     $toolbox = R::getToolBox();
     $adapter = $toolbox->getDatabaseAdapter();
     $writer = $toolbox->getWriter();
     $redbean = $toolbox->getRedBean();
     $pdo = $adapter->getDatabase();
     $page = $redbean->dispense("page");
     $page->name = "page no. 1";
     $page->rating = 1;
     $id1 = $redbean->store($page);
     $page = $redbean->dispense("page");
     $page->name = "page no. 2";
     $id2 = $redbean->store($page);
     $batch = $redbean->batch("page", array($id1, $id2));
     asrt(count($batch), 2);
     asrt($batch[$id1]->getMeta("type"), "page");
     asrt($batch[$id2]->getMeta("type"), "page");
     asrt((int) $batch[$id1]->id, $id1);
     asrt((int) $batch[$id2]->id, $id2);
     $book = $redbean->dispense("book");
     $book->name = "book 1";
     $redbean->store($book);
     $book = $redbean->dispense("book");
     $book->name = "book 2";
     $redbean->store($book);
     $book = $redbean->dispense("book");
     $book->name = "book 3";
     $redbean->store($book);
     $books = $redbean->batch("book", $adapter->getCol("SELECT id FROM book"));
     asrt(count($books), 3);
     $a = $redbean->batch('book', 9919);
     asrt(is_array($a), TRUE);
     asrt(count($a), 0);
     $a = $redbean->batch('triangle', 1);
     asrt(is_array($a), TRUE);
     asrt(count($a), 0);
     R::freeze(TRUE);
     $a = $redbean->batch('book', 9919);
     asrt(is_array($a), TRUE);
     asrt(count($a), 0);
     try {
         $a = $redbean->batch('triangle', 1);
         fail();
     } catch (SQL $e) {
         pass();
     }
     R::freeze(FALSE);
     asrt(R::wipe('spaghettimonster'), FALSE);
 }
Esempio n. 7
0
 /**
  * Test scanning and coding of values.
  *
  * @return void
  */
 public function testScanningAndCoding()
 {
     $toolbox = R::getToolBox();
     $adapter = $toolbox->getDatabaseAdapter();
     $writer = $toolbox->getWriter();
     $redbean = $toolbox->getRedBean();
     $pdo = $adapter->getDatabase();
     $writer->createTable("testtable");
     $writer->addColumn("testtable", "special", CUBRID::C_DATATYPE_SPECIAL_DATE);
     $cols = $writer->getColumns("testtable");
     asrt($writer->code($cols['special'], TRUE), CUBRID::C_DATATYPE_SPECIAL_DATE);
     asrt($writer->code($cols['special'], FALSE), CUBRID::C_DATATYPE_SPECIFIED);
     $writer->addColumn("testtable", "special2", CUBRID::C_DATATYPE_SPECIAL_DATETIME);
     $cols = $writer->getColumns("testtable");
     asrt($writer->code($cols['special2'], TRUE), CUBRID::C_DATATYPE_SPECIAL_DATETIME);
     asrt($writer->code($cols['special'], FALSE), CUBRID::C_DATATYPE_SPECIFIED);
 }
Esempio n. 8
0
 /**
  * Test storage of doubles.
  *
  * @return void
  */
 public function testDouble()
 {
     $toolbox = R::getToolBox();
     $adapter = $toolbox->getDatabaseAdapter();
     $writer = $toolbox->getWriter();
     $redbean = $toolbox->getRedBean();
     $pdo = $adapter->getDatabase();
     $largeDouble = 9.999998888899999E+23;
     //8.88889999922211e+17;
     $page = $redbean->dispense("page");
     $page->weight = $largeDouble;
     $id = $redbean->store($page);
     $cols = $writer->getColumns("page");
     asrt($cols["weight"], "double");
     $page = $redbean->load("page", $id);
     $page->name = "dont change the numbers!";
     $redbean->store($page);
     $page = $redbean->load("page", $id);
     $cols = $writer->getColumns("page");
     asrt($cols["weight"], "double");
 }
Esempio n. 9
0
 /**
  * Test parameter binding with SQLite.
  *
  * @return void
  */
 public function testParamBindWithSQLite()
 {
     $toolbox = R::getToolBox();
     $adapter = $toolbox->getDatabaseAdapter();
     $writer = $toolbox->getWriter();
     $redbean = $toolbox->getRedBean();
     $pdo = $adapter->getDatabase();
     asrt((int) $adapter->getCell("SELECT 123"), 123);
     asrt((int) $adapter->getCell("SELECT ?", array("987")), 987);
     asrt((int) $adapter->getCell("SELECT ?+?", array("987", "2")), 989);
     asrt((int) $adapter->getCell("SELECT :numberOne+:numberTwo", array(":numberOne" => 42, ":numberTwo" => 50)), 92);
     $pair = $adapter->getAssoc("SELECT 'thekey','thevalue' ");
     asrt(is_array($pair), TRUE);
     asrt(count($pair), 1);
     asrt(isset($pair["thekey"]), TRUE);
     asrt($pair["thekey"], "thevalue");
     testpack('Test whether we can properly bind and receive NULL values');
     asrt($adapter->getCell('SELECT :nil ', array(':nil' => 'NULL')), 'NULL');
     asrt($adapter->getCell('SELECT :nil ', array(':nil' => NULL)), NULL);
     asrt($adapter->getCell('SELECT ? ', array('NULL')), 'NULL');
     asrt($adapter->getCell('SELECT ? ', array(NULL)), NULL);
 }
Esempio n. 10
0
 /**
  * Test RedBeanPHP observers.
  *
  * @return void
  */
 public function testObserverMechanism()
 {
     $toolbox = R::getToolBox();
     $adapter = $toolbox->getDatabaseAdapter();
     $writer = $toolbox->getWriter();
     $redbean = $toolbox->getRedBean();
     asrt($adapter instanceof DBAdapter, TRUE);
     asrt($writer instanceof QueryWriter, TRUE);
     asrt($redbean instanceof OODB, TRUE);
     $observable = new \ObservableMock();
     $observer = new \ObserverMock();
     $observable->addEventListener("event1", $observer);
     $observable->addEventListener("event3", $observer);
     $observable->test("event1", "testsignal1");
     asrt($observer->event, "event1");
     asrt($observer->info, "testsignal1");
     $observable->test("event2", "testsignal2");
     asrt($observer->event, "event1");
     asrt($observer->info, "testsignal1");
     $observable->test("event3", "testsignal3");
     asrt($observer->event, "event3");
     asrt($observer->info, "testsignal3");
 }
Esempio n. 11
0
 /**
  * Test Full fluid UUID support.
  *
  */
 public function testFullSupport()
 {
     //Rewire objects to support UUIDs.
     $oldToolBox = R::getToolBox();
     $oldAdapter = $oldToolBox->getDatabaseAdapter();
     $uuidWriter = new \UUIDWriterPostgres($oldAdapter);
     $newRedBean = new OODB($uuidWriter);
     $newToolBox = new ToolBox($newRedBean, $oldAdapter, $uuidWriter);
     R::configureFacadeWithToolbox($newToolBox);
     list($mansion, $rooms, $ghosts, $key) = R::dispenseAll('mansion,room*3,ghost*4,key');
     $mansion->name = 'Haunted Mansion';
     $mansion->xownRoomList = $rooms;
     $rooms[0]->name = 'Green Room';
     $rooms[1]->name = 'Red Room';
     $rooms[2]->name = 'Blue Room';
     $ghosts[0]->name = 'zero';
     $ghosts[1]->name = 'one';
     $ghosts[2]->name = 'two';
     $ghosts[3]->name = 'three';
     $rooms[0]->noLoad()->sharedGhostList = array($ghosts[0], $ghosts[1]);
     $rooms[1]->noLoad()->sharedGhostList = array($ghosts[0], $ghosts[2]);
     $rooms[2]->noLoad()->sharedGhostList = array($ghosts[1], $ghosts[3], $ghosts[2]);
     $rooms[2]->xownKey = array($key);
     //Can we store a bean hierachy with UUIDs?
     R::debug(1);
     $id = R::store($mansion);
     //exit;
     asrt(is_string($id), TRUE);
     asrt(strlen($id), 36);
     $haunted = R::load('mansion', $id);
     asrt($haunted->name, 'Haunted Mansion');
     asrt(is_string($haunted->id), TRUE);
     asrt(strlen($haunted->id), 36);
     asrt(is_array($haunted->xownRoomList), TRUE);
     asrt(count($haunted->ownRoom), 3);
     $rooms = $haunted->xownRoomList;
     //Do some counting...
     $greenRoom = NULL;
     foreach ($rooms as $room) {
         if ($room->name === 'Green Room') {
             $greenRoom = $room;
             break;
         }
     }
     asrt(!is_null($greenRoom), TRUE);
     asrt(is_array($greenRoom->with(' ORDER BY id ')->sharedGhostList), TRUE);
     asrt(count($greenRoom->sharedGhostList), 2);
     $names = array();
     foreach ($greenRoom->sharedGhost as $ghost) {
         $names[] = $ghost->name;
     }
     sort($names);
     $names = implode(',', $names);
     asrt($names, 'one,zero');
     $rooms = $haunted->xownRoomList;
     $blueRoom = NULL;
     foreach ($rooms as $room) {
         if ($room->name === 'Blue Room') {
             $blueRoom = $room;
             break;
         }
     }
     asrt(!is_null($blueRoom), TRUE);
     asrt(is_array($blueRoom->sharedGhostList), TRUE);
     asrt(count($blueRoom->sharedGhostList), 3);
     $names = array();
     foreach ($blueRoom->sharedGhost as $ghost) {
         $names[] = $ghost->name;
     }
     sort($names);
     $names = implode(',', $names);
     asrt($names, 'one,three,two');
     $rooms = $haunted->xownRoomList;
     $redRoom = NULL;
     foreach ($rooms as $room) {
         if ($room->name === 'Red Room') {
             $redRoom = $room;
             break;
         }
     }
     $names = array();
     foreach ($redRoom->sharedGhost as $ghost) {
         $names[] = $ghost->name;
     }
     sort($names);
     $names = implode(',', $names);
     asrt($names, 'two,zero');
     asrt(!is_null($redRoom), TRUE);
     asrt(is_array($redRoom->sharedGhostList), TRUE);
     asrt(count($redRoom->sharedGhostList), 2);
     //Can we repaint a room?
     $redRoom->name = 'Yellow Room';
     $id = R::store($redRoom);
     $yellowRoom = R::load('room', $id);
     asrt($yellowRoom->name, 'Yellow Room');
     asrt(!is_null($yellowRoom), TRUE);
     asrt(is_array($yellowRoom->sharedGhostList), TRUE);
     asrt(count($yellowRoom->sharedGhostList), 2);
     //Can we throw one ghost out?
     array_pop($yellowRoom->sharedGhost);
     R::store($yellowRoom);
     $yellowRoom = $yellowRoom->fresh();
     asrt($yellowRoom->name, 'Yellow Room');
     asrt(!is_null($yellowRoom), TRUE);
     asrt(is_array($yellowRoom->sharedGhostList), TRUE);
     asrt(count($yellowRoom->sharedGhostList), 1);
     //can we remove one of the rooms?
     asrt(R::count('key'), 1);
     $list = $mansion->withCondition(' "name" = ? ', array('Blue Room'))->xownRoomList;
     $room = reset($list);
     unset($mansion->xownRoomList[$room->id]);
     R::store($mansion);
     asrt(R::count('room'), 2);
     //and what about its dependent beans?
     asrt(R::count('key'), 0);
     asrt(R::count('ghost_room'), 3);
     //and can we find ghosts?
     $ghosts = R::find('ghost');
     asrt(count($ghosts), 4);
     $ghosts = R::findAll('ghost', 'ORDER BY id');
     asrt(count($ghosts), 4);
     $ghosts = R::findAll('ghost', 'ORDER BY id LIMIT 2');
     asrt(count($ghosts), 2);
     $ghostZero = R::findOne('ghost', ' "name" = ? ', array('zero'));
     asrt($ghostZero instanceof OODBBean, TRUE);
     //can we create link properties on existing tables?
     $blackRoom = R::dispense('room');
     $blackRoom->name = 'Black Room';
     $ghostZero->link('ghost_room', array('mood' => 'grumpy'))->room = $blackRoom;
     R::store($ghostZero);
     $ghostZero = $ghostZero->fresh();
     $list = $ghostZero->sharedRoomList;
     asrt(count($list), 3);
     $ghostZero = $ghostZero->fresh();
     $list = $ghostZero->withCondition(' ghost_room.mood = ? ', array('grumpy'))->sharedRoomList;
     asrt(count($list), 1);
     //can we load a batch?
     $ids = R::getCol('SELECT id FROM ghost');
     $ghosts = R::batch('ghost', $ids);
     asrt(count($ghosts), 4);
     //can we do an aggregation?
     $ghosts = $greenRoom->aggr('ownGhostRoom', 'ghost', 'ghost');
     asrt(count($ghosts), 2);
     //can we duplicate the mansion?
     asrt(R::count('mansion'), 1);
     asrt(R::count('room'), 3);
     asrt(R::count('ghost'), 4);
     $copy = R::dup($mansion);
     R::store($copy);
     asrt(R::count('mansion'), 2);
     asrt(R::count('room'), 5);
     //black room does not belong to mansion 1
     asrt(R::count('ghost'), 4);
     //can we do some counting using the list?
     asrt($copy->countOwn('room'), 2);
     $rooms = $copy->withCondition(' "name" = ? ', array('Green Room'))->xownRoomList;
     $room = reset($rooms);
     asrt($room->countShared('ghost'), 2);
     //Finally restore old toolbox
     R::configureFacadeWithToolbox($oldToolBox);
 }
Esempio n. 12
0
 /**
  * Test parameter binding with\PDO.
  * 
  * @return void
  */
 public function testPDOParameterBinding()
 {
     $toolbox = R::getToolBox();
     $adapter = $toolbox->getDatabaseAdapter();
     $writer = $toolbox->getWriter();
     $redbean = $toolbox->getRedBean();
     $pdo = $adapter->getDatabase();
     R::getDatabaseAdapter()->getDatabase()->setUseStringOnlyBinding(TRUE);
     try {
         R::getAll("select * from job limit ? ", array(1));
         fail();
     } catch (\Exception $e) {
         pass();
     }
     try {
         R::getAll("select * from job limit :l ", array(":l" => 1));
         fail();
     } catch (\Exception $e) {
         pass();
     }
     try {
         R::exec("select * from job limit ? ", array(1));
         fail();
     } catch (\Exception $e) {
         pass();
     }
     try {
         R::exec("select * from job limit :l ", array(":l" => 1));
         fail();
     } catch (\Exception $e) {
         pass();
     }
     R::getDatabaseAdapter()->getDatabase()->setUseStringOnlyBinding(FALSE);
     try {
         R::getAll("select * from job limit ? ", array(1));
         pass();
     } catch (\Exception $e) {
         print_r($e);
         fail();
     }
     try {
         R::getAll("select * from job limit :l ", array(":l" => 1));
         pass();
     } catch (\Exception $e) {
         fail();
     }
     try {
         R::exec("select * from job limit ? ", array(1));
         pass();
     } catch (\Exception $e) {
         fail();
     }
     try {
         R::exec("select * from job limit :l ", array(":l" => 1));
         pass();
     } catch (\Exception $e) {
         fail();
     }
     testpack("Test findOrDispense");
     $person = R::findOrDispense("person", " job = ? ", array("developer"));
     asrt(count($person) > 0, TRUE);
     $person = R::findOrDispense("person", " job = ? ", array("musician"));
     asrt(count($person) > 0, TRUE);
     $musician = array_pop($person);
     asrt(intval($musician->id), 0);
     try {
         $adapter->exec("an invalid query");
         fail();
     } catch (SQL $e) {
         pass();
     }
     asrt((int) $adapter->getCell("SELECT 123"), 123);
     asrt((int) $adapter->getCell("SELECT ?", array("987")), 987);
     asrt((int) $adapter->getCell("SELECT ?+?", array("987", "2")), 989);
     asrt((int) $adapter->getCell("SELECT :numberOne+:numberTwo", array(":numberOne" => 42, ":numberTwo" => 50)), 92);
     $pair = $adapter->getAssoc("SELECT 'thekey','thevalue' ");
     asrt(is_array($pair), TRUE);
     asrt(count($pair), 1);
     asrt(isset($pair["thekey"]), TRUE);
     asrt($pair["thekey"], "thevalue");
     testpack('Test whether we can properly bind and receive NULL values');
     asrt($adapter->getCell('SELECT :nil ', array(':nil' => 'NULL')), 'NULL');
     asrt($adapter->getCell('SELECT :nil ', array(':nil' => NULL)), NULL);
     asrt($adapter->getCell('SELECT ? ', array('NULL')), 'NULL');
     asrt($adapter->getCell('SELECT ? ', array(NULL)), NULL);
 }
Esempio n. 13
0
 /**
  * Test FUSE and model formatting.
  * 
  * @todo move tagging tests to tag tester.
  * 
  * @return void
  */
 public function testFUSE()
 {
     $toolbox = R::getToolBox();
     $adapter = $toolbox->getDatabaseAdapter();
     $blog = R::dispense('blog');
     $blog->title = 'testing';
     $blog->blog = 'tesing';
     R::store($blog);
     $blogpost = R::load("blog", 1);
     $post = R::dispense("post");
     $post->message = "hello";
     $blog->sharedPost[] = $post;
     R::store($blog);
     $a = R::getAll("select * from blog ");
     R::tag($post, "lousy,smart");
     asrt(implode(',', R::tag($post)), "lousy,smart");
     R::tag($post, "clever,smart");
     $tagz = implode(',', R::tag($post));
     asrt($tagz == "smart,clever" || $tagz == "clever,smart", TRUE);
     R::tag($blog, array("smart", "interesting"));
     asrt(implode(',', R::tag($blog)), "smart,interesting");
     try {
         R::tag($blog, array("smart", "interesting", "lousy!"));
         pass();
     } catch (RedException $e) {
         fail();
     }
     asrt(implode(',', R::tag($blog)), "smart,interesting,lousy!");
     asrt(implode(",", R::tag($blog)), "smart,interesting,lousy!");
     R::untag($blog, array("smart", "interesting"));
     asrt(implode(",", R::tag($blog)), "lousy!");
     asrt(R::hasTag($blog, array("lousy!")), TRUE);
     asrt(R::hasTag($blog, array("lousy!", "smart")), TRUE);
     asrt(R::hasTag($blog, array("lousy!", "smart"), TRUE), FALSE);
     R::tag($blog, FALSE);
     asrt(count(R::tag($blog)), 0);
     R::tag($blog, array("funny", "comic"));
     asrt(count(R::tag($blog)), 2);
     R::addTags($blog, array("halloween"));
     asrt(count(R::tag($blog)), 3);
     asrt(R::hasTag($blog, array("funny", "commic", "halloween"), TRUE), FALSE);
     R::unTag($blog, array("funny"));
     R::addTags($blog, "horror");
     asrt(count(R::tag($blog)), 3);
     asrt(R::hasTag($blog, array("horror", "commic", "halloween"), TRUE), FALSE);
     // No double tags
     R::addTags($blog, "horror");
     asrt(R::hasTag($blog, array("horror", "commic", "halloween"), TRUE), FALSE);
     asrt(count(R::tag($blog)), 3);
 }
Esempio n. 14
0
 /**
  * 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"));
 }
Esempio n. 15
0
 /**
  * Test duplication and caching.
  *
  * @return void
  */
 public function DupAndCache()
 {
     testpack('Dup() and Cache');
     $can = R::dispense('can')->setAttr('size', 3);
     $can->ownCoffee[] = R::dispense('coffee')->setAttr('color', 'black');
     $can->sharedTag[] = R::dispense('tag')->setAttr('name', 'cool');
     $can = R::load('can', R::store($can));
     $d = new DuplicationManager(R::getToolBox());
     $d->setCacheTables(TRUE);
     ob_start();
     R::debug(1);
     $x = $d->dup($can);
     $queries = ob_get_contents();
     R::debug(0);
     ob_end_clean();
     $len1 = strlen($queries);
     asrt($len1 > 40, TRUE);
     asrt(isset($x->ownCoffee), TRUE);
     asrt(count($x->ownCoffee), 1);
     asrt(isset($x->sharedTag), TRUE);
     asrt(count($x->sharedTag), 1);
     $cache = $d->getSchema();
     R::nuke();
     $can = R::dispense('can')->setAttr('size', 3);
     $can->ownCoffee[] = R::dispense('coffee')->setAttr('color', 'black');
     $can->sharedTag[] = R::dispense('tag')->setAttr('name', 'cool');
     $can = R::load('can', R::store($can));
     $d = new DuplicationManager(R::getToolBox());
     /**
      * $cache = '{"book": {
      *  "id": "INTEGER",
      *  "title": "TEXT"
      * }, "bean": {
      *  "id": "INTEGER",
      *  "prop": "INTEGER"
      * }, "pessoa": {
      *  "id": "INTEGER",
      *  "nome": "TEXT",
      *  "nome_meio": "TEXT",
      *  "sobrenome": "TEXT",
      *  "nascimento": "NUMERIC",
      *  "reg_owner": "TEXT"
      * }, "documento": {
      *  "id": "INTEGER",
      *  "nome_documento": "TEXT",
      *  "numero_documento": "TEXT",
      *  "reg_owner": "TEXT",
      *  "ownPessoa_id": "INTEGER"
      * }, "can": {
      *  "id": "INTEGER",
      *  "size": "INTEGER"
      * }, "coffee": {
      *  "id": "INTEGER",
      *  "color": "TEXT",
      *  "can_id": "INTEGER"
      * }, "tag": {
      *  "id": "INTEGER",
      *  "name": "TEXT"
      * }, "can_tag": {
      *  "id": "INTEGER",
      *  "tag_id": "INTEGER",
      *  "can_id": "INTEGER"
      * }}'
      */
     $d->setTables($cache);
     ob_start();
     R::debug(1);
     $x = $d->dup($can);
     $queries = ob_get_contents();
     ob_end_clean();
     R::debug(0);
     $len2 = strlen($queries);
     asrt(isset($x->ownCoffee), TRUE);
     asrt(count($x->ownCoffee), 1);
     asrt(isset($x->sharedTag), TRUE);
     asrt(count($x->sharedTag), 1);
     asrt(json_encode($cache), json_encode($d->getSchema()));
     asrt($len1 > $len2, TRUE);
 }
Esempio n. 16
0
 /**
  * Tests freezing the database.
  * After freezing the database, schema modifications are no longer
  * allowed and referring to missing columns will now cause exceptions.
  * 
  * @return void
  */
 public function testFreezer()
 {
     $toolbox = R::getToolBox();
     $adapter = $toolbox->getDatabaseAdapter();
     $writer = $toolbox->getWriter();
     $redbean = $toolbox->getRedBean();
     $pdo = $adapter->getDatabase();
     $a = new AssociationManager($toolbox);
     $post = $redbean->dispense('post');
     $post->title = 'title';
     $redbean->store($post);
     $page = $redbean->dispense('page');
     $page->name = 'title';
     $redbean->store($page);
     $page = $redbean->dispense("page");
     $page->name = "John's page";
     $idpage = $redbean->store($page);
     $page2 = $redbean->dispense("page");
     $page2->name = "John's second page";
     $idpage2 = $redbean->store($page2);
     $a->associate($page, $page2);
     $redbean->freeze(TRUE);
     $page = $redbean->dispense("page");
     $page->sections = 10;
     $page->name = "half a page";
     try {
         $id = $redbean->store($page);
         fail();
     } catch (SQL $e) {
         pass();
     }
     $post = $redbean->dispense("post");
     $post->title = "existing table";
     try {
         $id = $redbean->store($post);
         pass();
     } catch (SQL $e) {
         fail();
     }
     asrt(in_array("name", array_keys($writer->getColumns("page"))), TRUE);
     asrt(in_array("sections", array_keys($writer->getColumns("page"))), FALSE);
     $newtype = $redbean->dispense("newtype");
     $newtype->property = 1;
     try {
         $id = $redbean->store($newtype);
         fail();
     } catch (SQL $e) {
         pass();
     }
     $logger = R::debug(true, 1);
     // Now log and make sure no 'describe SQL' happens
     $page = $redbean->dispense("page");
     $page->name = "just another page that has been frozen...";
     $id = $redbean->store($page);
     $page = $redbean->load("page", $id);
     $page->name = "just a frozen page...";
     $redbean->store($page);
     $page2 = $redbean->dispense("page");
     $page2->name = "an associated frozen page";
     $a->associate($page, $page2);
     $a->related($page, "page");
     $a->unassociate($page, $page2);
     $a->clearRelations($page, "page");
     $items = $redbean->find("page", array(), array("1"));
     $redbean->trash($page);
     $redbean->freeze(FALSE);
     asrt(count($logger->grep("SELECT")) > 0, TRUE);
     asrt(count($logger->grep("describe")) < 1, TRUE);
     asrt(is_array($logger->getLogs()), TRUE);
     R::debug(false);
 }
Esempio n. 17
0
 /**
  * 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);
 }
Esempio n. 18
0
 /**
  * (FALSE should be stored as 0 not as '')
  *
  * @return voids
  */
 public function testZeroIssue()
 {
     testpack("Zero issue");
     $toolbox = R::getToolBox();
     $redbean = $toolbox->getRedBean();
     $adapter = $toolbox->getDatabaseAdapter();
     $writer = $toolbox->getWriter();
     $pdo = $adapter->getDatabase();
     $pdo->Execute("DROP TABLE IF EXISTS `zero`");
     $bean = $redbean->dispense("zero");
     $bean->zero = FALSE;
     $bean->title = "bla";
     $redbean->store($bean);
     asrt(count($redbean->find("zero", array(), " zero = 0 ")), 1);
     R::store(R::dispense('hack'));
     testpack("Test RedBean Security - bean interface ");
     asrt(in_array("hack", $adapter->getCol("show tables")), TRUE);
     $bean = $redbean->load("page", "13; drop table hack");
     asrt(in_array("hack", $adapter->getCol("show tables")), TRUE);
     try {
         $bean = $redbean->load("page where 1; drop table hack", 1);
     } catch (\Exception $e) {
     }
     asrt(in_array("hack", $adapter->getCol("show tables")), TRUE);
     $bean = $redbean->dispense("page");
     $evil = "; drop table hack";
     $bean->id = $evil;
     try {
         $redbean->store($bean);
     } catch (\Exception $e) {
     }
     asrt(in_array("hack", $adapter->getCol("show tables")), TRUE);
     unset($bean->id);
     $bean->name = "\"" . $evil;
     try {
         $redbean->store($bean);
     } catch (\Exception $e) {
     }
     asrt(in_array("hack", $adapter->getCol("show tables")), TRUE);
     $bean->name = "'" . $evil;
     try {
         $redbean->store($bean);
     } catch (\Exception $e) {
     }
     asrt(in_array("hack", $adapter->getCol("show tables")), TRUE);
     $bean->{$evil} = 1;
     try {
         $redbean->store($bean);
     } catch (\Exception $e) {
     }
     asrt(in_array("hack", $adapter->getCol("show tables")), TRUE);
     unset($bean->{$evil});
     $bean->id = 1;
     $bean->name = "\"" . $evil;
     try {
         $redbean->store($bean);
     } catch (\Exception $e) {
     }
     asrt(in_array("hack", $adapter->getCol("show tables")), TRUE);
     $bean->name = "'" . $evil;
     try {
         $redbean->store($bean);
     } catch (\Exception $e) {
     }
     asrt(in_array("hack", $adapter->getCol("show tables")), TRUE);
     $bean->{$evil} = 1;
     try {
         $redbean->store($bean);
     } catch (\Exception $e) {
     }
     asrt(in_array("hack", $adapter->getCol("show tables")), TRUE);
     try {
         $redbean->trash($bean);
     } catch (\Exception $e) {
     }
     asrt(in_array("hack", $adapter->getCol("show tables")), TRUE);
     try {
         $redbean->find("::", array(), "");
     } catch (\Exception $e) {
         pass();
     }
     $adapter->exec("drop table if exists sometable");
     testpack("Test RedBean Security - query writer");
     try {
         $writer->createTable("sometable` ( `id` INT( 11 ) UNSIGNED NOT NULL AUTO_INCREMENT , PRIMARY KEY ( `id` ) ) ENGINE = InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ; drop table hack; --");
     } catch (\Exception $e) {
     }
     asrt(in_array("hack", $adapter->getCol("show tables")), TRUE);
     testpack("Test ANSI92 issue in clearrelations");
     $pdo->Execute("DROP TABLE IF EXISTS book_group");
     $pdo->Execute("DROP TABLE IF EXISTS author_book");
     $pdo->Execute("DROP TABLE IF EXISTS book");
     $pdo->Execute("DROP TABLE IF EXISTS author");
     $redbean = $toolbox->getRedBean();
     $a = new AssociationManager($toolbox);
     $book = $redbean->dispense("book");
     $author1 = $redbean->dispense("author");
     $author2 = $redbean->dispense("author");
     $book->title = "My First Post";
     $author1->name = "Derek";
     $author2->name = "Whoever";
     set1toNAssoc($a, $book, $author1);
     set1toNAssoc($a, $book, $author2);
     pass();
     $pdo->Execute("DROP TABLE IF EXISTS book_group");
     $pdo->Execute("DROP TABLE IF EXISTS book_author");
     $pdo->Execute("DROP TABLE IF EXISTS author_book");
     $pdo->Execute("DROP TABLE IF EXISTS book");
     $pdo->Execute("DROP TABLE IF EXISTS author");
     $redbean = $toolbox->getRedBean();
     $a = new AssociationManager($toolbox);
     $book = $redbean->dispense("book");
     $author1 = $redbean->dispense("author");
     $author2 = $redbean->dispense("author");
     $book->title = "My First Post";
     $author1->name = "Derek";
     $author2->name = "Whoever";
     $a->associate($book, $author1);
     $a->associate($book, $author2);
     pass();
     testpack("Test Association Issue Group keyword (Issues 9 and 10)");
     $pdo->Execute("DROP TABLE IF EXISTS `book_group`");
     $pdo->Execute("DROP TABLE IF EXISTS `group`");
     $group = $redbean->dispense("group");
     $group->name = "mygroup";
     $redbean->store($group);
     try {
         $a->associate($group, $book);
         pass();
     } catch (SQL $e) {
         fail();
     }
     // Test issue SQL error 23000
     try {
         $a->associate($group, $book);
         pass();
     } catch (SQL $e) {
         fail();
     }
     asrt((int) $adapter->getCell("select count(*) from book_group"), 1);
     //just 1 rec!
     $pdo->Execute("DROP TABLE IF EXISTS book_group");
     $pdo->Execute("DROP TABLE IF EXISTS author_book");
     $pdo->Execute("DROP TABLE IF EXISTS book");
     $pdo->Execute("DROP TABLE IF EXISTS author");
     $redbean = $toolbox->getRedBean();
     $a = new AssociationManager($toolbox);
     $book = $redbean->dispense("book");
     $author1 = $redbean->dispense("author");
     $author2 = $redbean->dispense("author");
     $book->title = "My First Post";
     $author1->name = "Derek";
     $author2->name = "Whoever";
     $a->unassociate($book, $author1);
     $a->unassociate($book, $author2);
     pass();
     $redbean->trash($redbean->dispense("bla"));
     pass();
     $bean = $redbean->dispense("bla");
     $bean->name = 1;
     $bean->id = 2;
     $redbean->trash($bean);
     pass();
 }
Esempio n. 19
0
    return AMFUtil::addtypeown($data, $arrTypes);
});
// public static function getLog()
R::ext('getLog', function () {
    $amf = new AMFUtil();
    $amf->setToolbox(R::getToolBox());
    return $amf::getLog();
});
// public static function arrayCastRecursive($array)
R::ext('arrayCastRecursive', function ($array) {
    return AMFUtil::arrayCastRecursive($array);
});
// public static function deleteRBrelation($beans){
R::ext('deleteRBrelation', function ($beans) {
    $amf = new AMFUtil();
    $amf->setToolbox(R::getToolBox());
    return $amf->deleteRBrelation($beans);
});
// public static function Create_Multiple_Dynamic_Classes()
R::ext('CreateMultipleDynamicClasses', function () {
    $amf = new AMFUtil();
    $amf->setConnection(R::getDatabaseAdapter()->getDatabase()->getPDO());
    return $amf->CreateMultipleDynamicClasses();
});
//check PDO from RB
R::ext('getConnection', function () {
    $amf = new AMFUtil();
    $amf->setConnection(R::getDatabaseAdapter()->getDatabase()->getPDO());
    return $amf->getConnection();
});
R::ext('checkDecaredClass', function ($class) {
Esempio n. 20
0
 /**
  * Test beautification of column names.
  *
  * @return void
  */
 public function testBeautifulColumnNames()
 {
     testpack('Beautiful column names');
     $town = R::dispense('town');
     $town->isCapital = FALSE;
     $town->hasTrainStation = TRUE;
     $town->name = 'BeautyVille';
     $houses = R::dispense('house', 2);
     $houses[0]->isForSale = TRUE;
     $town->ownHouse = $houses;
     R::store($town);
     $town = R::load('town', $town->id);
     asrt($town->isCapital == FALSE, TRUE);
     asrt($town->hasTrainStation == TRUE, TRUE);
     asrt($town->name == 'BeautyVille', TRUE);
     testpack('Accept datetime objects.');
     $cal = R::dispense('calendar');
     $cal->when = new \DateTime('2000-01-01', new \DateTimeZone('Pacific/Nauru'));
     asrt($cal->when, '2000-01-01 00:00:00');
     testpack('Affected rows test');
     $currentDriver = $this->currentlyActiveDriverID;
     $toolbox = R::getToolBox();
     $adapter = $toolbox->getDatabaseAdapter();
     $writer = $toolbox->getWriter();
     $redbean = $toolbox->getRedBean();
     $pdo = $adapter->getDatabase();
     $bean = $redbean->dispense('bean');
     $bean->prop = 3;
     //make test run with strict mode as well
     $redbean->store($bean);
     $adapter->exec('UPDATE bean SET prop = 2');
     asrt($adapter->getAffectedRows(), 1);
     testpack('Testing Logger');
     R::getDatabaseAdapter()->getDatabase()->setLogger(new RDefault());
     asrt(R::getDatabaseAdapter()->getDatabase()->getLogger() instanceof Logger, TRUE);
     asrt(R::getDatabaseAdapter()->getDatabase()->getLogger() instanceof RDefault, TRUE);
     $bean = R::dispense('bean');
     $bean->property = 1;
     $bean->unsetAll(array('property'));
     asrt($bean->property, NULL);
     asrt($bean->setAttr('property', 2) instanceof OODBBean, TRUE);
     asrt($bean->property, 2);
     asrt(preg_match('/\\d\\d\\d\\d\\-\\d\\d\\-\\d\\d/', R::isoDate()), 1);
     asrt(preg_match('/\\d\\d\\d\\d\\-\\d\\d\\-\\d\\d\\s\\d\\d:\\d\\d:\\d\\d/', R::isoDateTime()), 1);
     $redbean = R::getRedBean();
     $adapter = R::getDatabaseAdapter();
     $writer = R::getWriter();
     asrt($redbean instanceof OODB, TRUE);
     asrt($adapter instanceof Adapter, TRUE);
     asrt($writer instanceof QueryWriter, TRUE);
     R::setRedBean($redbean);
     pass();
     //cant really test this
     R::setDatabaseAdapter($adapter);
     pass();
     //cant really test this
     R::setWriter($writer);
     pass();
     //cant really test this
     $u1 = R::dispense('user');
     $u1->name = 'Gabor';
     $u1->login = '******';
     $u2 = R::dispense('user');
     $u2->name = 'Eric';
     $u2->login = '******';
     R::store($u1);
     R::store($u2);
     $list = R::getAssoc('select login,' . R::getWriter()->esc('name') . ' from ' . R::getWriter()->esc('user') . ' ');
     asrt($list['e'], 'Eric');
     asrt($list['g'], 'Gabor');
     $painting = R::dispense('painting');
     $painting->name = 'Nighthawks';
     $id = R::store($painting);
     testpack('Testing SQL Error Types');
     foreach ($writer->typeno_sqltype as $code => $text) {
         asrt(is_integer($code), TRUE);
         asrt(is_string($text), TRUE);
     }
     foreach ($writer->sqltype_typeno as $text => $code) {
         asrt(is_integer($code), TRUE);
         asrt(is_string($text), TRUE);
     }
     testpack('Testing Nowhere Pt. 1 (unfrozen)');
     foreach (array('exec', 'getAll', 'getCell', 'getAssoc', 'getRow', 'getCol') as $method) {
         R::$method('select * from nowhere');
         pass();
     }
     testpack('Testing Nowhere Pt. 2 (frozen)');
     R::freeze(TRUE);
     foreach (array('exec', 'getAll', 'getCell', 'getAssoc', 'getRow', 'getCol') as $method) {
         try {
             R::$method('select * from nowhere');
             fail();
         } catch (SQL $e) {
             pass();
         }
     }
     R::freeze(FALSE);
 }
Esempio n. 21
0
 /**
  * Test the database driver and low level functions.
  * 
  * @return void
  */
 public function testDriver()
 {
     $currentDriver = $this->currentlyActiveDriverID;
     R::store(R::dispense('justabean'));
     $adapter = new TroubleDapter(R::getToolBox()->getDatabaseAdapter()->getDatabase());
     $adapter->setSQLState('HY000');
     $writer = new SQLiteT($adapter);
     $redbean = new OODB($writer);
     $toolbox = new ToolBox($redbean, $adapter, $writer);
     // We can only test this for a known driver...
     if ($currentDriver === 'sqlite') {
         try {
             $redbean->find('bean');
             pass();
         } catch (\Exception $e) {
             var_dump($e->getSQLState());
             fail();
         }
     }
     $adapter->setSQLState(-999);
     try {
         $redbean->find('bean');
         fail();
     } catch (\Exception $e) {
         pass();
     }
     try {
         $redbean->wipe('justabean');
         fail();
     } catch (\Exception $e) {
         pass();
     }
     $toolbox = R::getToolBox();
     $adapter = $toolbox->getDatabaseAdapter();
     $writer = $toolbox->getWriter();
     $redbean = $toolbox->getRedBean();
     $pdo = $adapter->getDatabase();
     $page = $redbean->dispense("page");
     try {
         $adapter->exec("an invalid query");
         fail();
     } catch (SQL $e) {
         pass();
     }
     // Special data type description should result in magic number 99 (specified)
     if ($currentDriver == 'mysql') {
         asrt($writer->code(MySQL::C_DATATYPE_SPECIAL_DATE), 99);
     }
     if ($currentDriver == 'pgsql') {
         asrt($writer->code(PostgreSQL::C_DATATYPE_SPECIAL_DATE), 99);
     }
     if ($currentDriver == 'CUBRID') {
         asrt($writer->code(CUBRID::C_DATATYPE_SPECIAL_DATE), 99);
     }
     asrt((int) $adapter->getCell("SELECT 123"), 123);
     $page->aname = "my page";
     $id = (int) $redbean->store($page);
     asrt((int) $page->id, 1);
     asrt((int) $pdo->GetCell("SELECT count(*) FROM page"), 1);
     asrt($pdo->GetCell("SELECT aname FROM page LIMIT 1"), "my page");
     asrt((int) $id, 1);
     $page = $redbean->load("page", 1);
     asrt($page->aname, "my page");
     asrt((bool) $page->getMeta("type"), TRUE);
     asrt(isset($page->id), TRUE);
     asrt($page->getMeta("type"), "page");
     asrt((int) $page->id, $id);
 }
Esempio n. 22
0
 /**
  * Test various.
  * Test various somewhat uncommon trash/unassociate scenarios.
  * (i.e. unassociate unrelated beans, trash non-persistant beans etc).
  * Should be handled gracefully - no output checking.
  *
  * @return void
  */
 public function testVaria2()
 {
     $toolbox = R::getToolBox();
     $redbean = $toolbox->getRedBean();
     $a = new AssociationManager($toolbox);
     $book = $redbean->dispense("book");
     $author1 = $redbean->dispense("author");
     $author2 = $redbean->dispense("author");
     $book->title = "My First Post";
     $author1->name = "Derek";
     $author2->name = "Whoever";
     $a->unassociate($book, $author1);
     $a->unassociate($book, $author2);
     pass();
     $redbean->trash($redbean->dispense("bla"));
     pass();
     $bean = $redbean->dispense("bla");
     $bean->name = 1;
     $bean->id = 2;
     $redbean->trash($bean);
     pass();
 }
Esempio n. 23
0
 /**
  * Begin testing.
  * This method runs the actual test pack.
  *
  * @return void
  */
 public function testFinding()
 {
     $toolbox = R::getToolBox();
     $adapter = $toolbox->getDatabaseAdapter();
     $writer = $toolbox->getWriter();
     $redbean = $toolbox->getRedBean();
     $pdo = $adapter->getDatabase();
     $a = new AssociationManager($toolbox);
     $page = $redbean->dispense("page");
     $page->name = "John's page";
     $idpage = $redbean->store($page);
     $page2 = $redbean->dispense("page");
     $page2->name = "John's second page";
     $idpage2 = $redbean->store($page2);
     $a->associate($page, $page2);
     $pageOne = $redbean->dispense("page");
     $pageOne->name = "one";
     $pageMore = $redbean->dispense("page");
     $pageMore->name = "more";
     $pageEvenMore = $redbean->dispense("page");
     $pageEvenMore->name = "evenmore";
     $pageOther = $redbean->dispense("page");
     $pageOther->name = "othermore";
     set1toNAssoc($a, $pageOther, $pageMore);
     set1toNAssoc($a, $pageOne, $pageMore);
     set1toNAssoc($a, $pageOne, $pageEvenMore);
     asrt(count($redbean->find("page", array(), " name LIKE '%more%' ", array())), 3);
     asrt(count($redbean->find("page", array(), " name LIKE :str ", array(":str" => '%more%'))), 3);
     asrt(count($redbean->find("page", array(), array(" name LIKE :str ", array(":str" => '%more%')))), 3);
     asrt(count($redbean->find("page", array(), " name LIKE :str ", array(":str" => '%mxore%'))), 0);
     asrt(count($redbean->find("page", array("id" => array(2, 3)))), 2);
     $bean = $redbean->dispense("wine");
     $bean->name = "bla";
     for ($i = 0; $i < 10; $i++) {
         $redbean->store($bean);
     }
     $redbean->find("wine", array("id" => 5));
     //  Finder:where call OODB::convertToBeans
     $bean2 = $redbean->load("anotherbean", 5);
     asrt($bean2->id, 0);
     $keys = $adapter->getCol("SELECT id FROM page WHERE " . $writer->esc('name') . " LIKE '%John%'");
     asrt(count($keys), 2);
     $pages = $redbean->batch("page", $keys);
     asrt(count($pages), 2);
     $p = R::findLast('page');
     pass();
     $row = R::getRow('select * from page ');
     asrt(is_array($row), TRUE);
     asrt(isset($row['name']), TRUE);
     // Test findAll -- should not throw an exception
     asrt(count(R::findAll('page')) > 0, TRUE);
     asrt(count(R::findAll('page', ' ORDER BY id ')) > 0, TRUE);
     $beans = R::findOrDispense("page");
     asrt(count($beans), 6);
     asrt(is_null(R::findLast('nothing')), TRUE);
     try {
         R::find('bean', ' id > 0 ', 'invalid bindings argument');
         fail();
     } catch (RedException $exception) {
         pass();
     }
 }
Esempio n. 24
0
 public function getToolbox()
 {
     return Facade::getToolBox();
 }
Esempio n. 25
0
 /**
  * @param string $dsn
  * @param $user
  * @param string $password
  * @param bool $isDevMode
  */
 public static function setupRedBean($dsn, $user, $password = '', $isDevMode = false)
 {
     $toolbox = R::getToolBox();
     if (empty($toolbox)) {
         R::setup($dsn, $user, $password, !$isDevMode);
     }
 }