/** * Test closing database connection. * * @return void */ public function testClose() { asrt(R::$adapter->getDatabase()->isConnected(), TRUE); R::close(); asrt(R::$adapter->getDatabase()->isConnected(), FALSE); // Can we create a database using empty setup? R::setup(); $id = R::store(R::dispense('bean')); asrt($id > 0, TRUE); // Test freeze via kickstart in setup $toolbox = RedBean_Setup::kickstart('sqlite:/tmp/bla.txt', NULL, NULL, TRUE); asrt($toolbox->getRedBean()->isFrozen(), TRUE); // Test Oracle setup $toolbox = RedBean_Setup::kickstart('oracle:test-value', 'test', 'test', FALSE); asrt($toolbox instanceof RedBean_ToolBox, TRUE); }
/** * Adds a database to the facade, afterwards you can select the database using * selectDatabase($key). * * @param string $key ID for the database * @param string $dsn DSN for the database * @param string $user User for connection * @param null|string $pass Password for connection * @param bool $frozen Whether this database is frozen or not * * @return void */ public static function addDatabase($key, $dsn, $user = null, $pass = null, $frozen = false) { self::$toolboxes[$key] = RedBean_Setup::kickstart($dsn, $user, $pass, $frozen); }
/** * Adds a database to the facade, afterwards you can select the database using * selectDatabase($key), where $key is the name you assigned to this database. * * Usage: * * R::addDatabase( 'database-1', 'sqlite:/tmp/db1.txt' ); * R::selectDatabase( 'database-1' ); //to select database again * * This method allows you to dynamically add (and select) new databases * to the facade. Adding a database with the same key as an older database * will cause this entry to be overwritten. * * @param string $key ID for the database * @param string $dsn DSN for the database * @param string $user User for connection * @param NULL|string $pass Password for connection * @param bool $frozen Whether this database is frozen or not * * @return void */ public static function addDatabase($key, $dsn, $user = NULL, $pass = NULL, $frozen = FALSE, $autoSetEncoding = TRUE) { self::$toolboxes[$key] = RedBean_Setup::kickstart($dsn, $user, $pass, $frozen, $autoSetEncoding); }
print "[" . $tests . "]"; } function fail() { printtext("FAILED TEST"); debug_print_backtrace(); exit; } function testpack($name) { printtext("testing: " . $name); } //INCLUDE YOUR REDBEAN FILE HERE! require "rb.php"; //require("RedBean/redbean.inc.php"); $toolbox = RedBean_Setup::kickstart("pgsql:host={$ini['pgsql']['host']} dbname={$ini['pgsql']['schema']}", $ini['pgsql']['user'], $ini['pgsql']['pass']); function droptables() { global $toolbox; foreach ($toolbox->getWriter()->getTables() as $t) { try { $toolbox->getDatabaseAdapter()->Exec("drop table \"{$t}\" cascade"); } catch (Exception $e) { } } } //Observable Mock Object class ObservableMock extends RedBean_Observable { public function test($eventname, $info) {
/** * Kickstarts redbean for you. * @param string $dsn * @param string $username * @param string $password */ public static function setup($dsn = "sqlite:/tmp/red.db", $username = NULL, $password = NULL) { RedBean_Setup::kickstart($dsn, $username, $password); self::$toolbox = RedBean_Setup::getToolBox(); self::$writer = self::$toolbox->getWriter(); self::$adapter = self::$toolbox->getDatabaseAdapter(); self::$redbean = self::$toolbox->getRedBean(); self::$associationManager = new RedBean_AssociationManager(self::$toolbox); self::$treeManager = new RedBean_TreeManager(self::$toolbox); self::$linkManager = new RedBean_LinkManager(self::$toolbox); self::$extAssocManager = new RedBean_ExtAssociationManager(self::$toolbox); $helper = new RedBean_ModelHelper(); self::$redbean->addEventListener("update", $helper); self::$redbean->addEventListener("open", $helper); self::$redbean->addEventListener("delete", $helper); }
/** * Adds a database to the facade, afterwards you can select the database using * selectDatabase($key), where $key is the name you assigned to this database. * * Usage: * * R::addDatabase( 'database-1', 'sqlite:/tmp/db1.txt' ); * R::selectDatabase( 'database-1' ); //to select database again * * This method allows you to dynamically add (and select) new databases * to the facade. Adding a database with the same key will cause an exception. * * @param string $key ID for the database * @param string $dsn DSN for the database * @param string $user User for connection * @param NULL|string $pass Password for connection * @param bool $frozen Whether this database is frozen or not * * @return void */ public static function addDatabase($key, $dsn, $user = NULL, $pass = NULL, $frozen = FALSE, $autoSetEncoding = TRUE) { if (isset(self::$toolboxes[$key])) { throw new RedBean_Exception_Security('A database has already be specified for this key.'); } self::$toolboxes[$key] = RedBean_Setup::kickstart($dsn, $user, $pass, $frozen, $autoSetEncoding); }
/** * Kickstarts redbean for you. * @param string $dsn * @param string $username * @param string $password */ public static function setup( $dsn="sqlite:/tmp/red.db", $username=NULL, $password=NULL ) { RedBean_Setup::kickstart( $dsn, $username, $password ); $toolbox = RedBean_Setup::getToolBox(); self::configureFacadeWithToolbox($toolbox); }
function testpack($name) { printtext("testing: " . $name); } //Test the Setup Class testpack("Test Setup"); //Can we load all modules properly? require "RedBean/redbean.inc.php"; if (interface_exists("RedBean_ObjectDatabase")) { pass(); } else { fail(); } //Test whether a non mysql DSN throws an exception try { RedBean_Setup::kickstart("blackhole:host=localhost;dbname=oodb", "root", ""); fail(); } catch (RedBean_Exception_NotImplemented $e) { pass(); } $toolbox = RedBean_Setup::kickstartDev("mysql:host=localhost;dbname=oodb", "root", ""); /** * Observable Mock * This is just for testing */ class ObservableMock extends RedBean_Observable { public function test($eventname, $info) { $this->signal($eventname, $info); }
public static function failedTest() { die("<b style='color:red'>FAIL</b>"); } public static function test($value, $expected) { if ($value != $expected) { die("<b style='color:red'>FAIL</b>"); } else { self::instance()->progress(); } } } //Use this database for tests require "oodb.php"; RedBean_Setup::kickstart("mysql:host=localhost;dbname=oodb", "root", "", false, "innodb", false); SmartTest::instance()->testPack = "Basic test suite"; $tests = 0; SmartTest::instance()->progress(); //Test description: Does the redbean core class exist? if (class_exists("RedBean_OODB")) { SmartTest::instance()->progress(); } else { SmartTest::failedTest(); } //Test description: Does the redbean decorator class exist? if (class_exists("RedBean_Decorator")) { SmartTest::instance()->progress(); } else { SmartTest::failedTest(); }
printtext("FAILED TEST"); debug_print_backtrace(); exit; } function testpack($name) { printtext("testing: " . $name); } //Test the Setup Class testpack("Test Setup"); //Can we load all modules properly? //INCLUDE YOUR REDBEAN FILE HERE! require "rb.php"; //require("RedBean/redbean.inc.php"); //Test whether we can setup a connection $toolbox = RedBean_Setup::kickstart("sqlite:{$ini['sqlite']['file']}"); //prepare... empty the database foreach ($toolbox->getWriter()->getTables() as $table) { $sql = "DROP TABLE `" . $table . "`"; $toolbox->getDatabaseAdapter()->exec($sql); } //check whether we emptied the database correctly... asrt(count($toolbox->getWriter()->getTables()), 0); /** * Observable Mock * This is just for testing */ class ObservableMock extends RedBean_Observable { public function test($eventname, $info) {
/** * Test Facade transactions. * * @return void * * @throws Exception */ public function testTransactionInFacade() { testpack('Test transaction in facade'); $bean = R::dispense('bean'); $bean->name = 'a'; R::store($bean); R::trash($bean); R::freeze(TRUE); $bean = R::dispense('bean'); $bean->name = 'a'; R::store($bean); asrt(R::count('bean'), 1); R::trash($bean); asrt(R::count('bean'), 0); $bean = R::dispense('bean'); $bean->name = 'a'; $id = R::transaction(function () use(&$bean) { return R::transaction(function () use(&$bean) { return R::store($bean); }); }); asrt((int) $id, (int) $bean->id); R::trash($bean); $bean = R::dispense('bean'); $bean->name = 'a'; $id = R::transaction(function () use(&$bean) { return R::store($bean); }); asrt((int) $id, (int) $bean->id); R::trash($bean); $bean = R::dispense('bean'); $bean->name = 'a'; try { R::transaction(function () use($bean) { R::store($bean); R::transaction(function () { throw new Exception(); }); }); } catch (Exception $e) { pass(); } asrt(R::count('bean'), 0); $bean = R::dispense('bean'); $bean->name = 'a'; try { R::transaction(function () use($bean) { R::transaction(function () use($bean) { R::store($bean); throw new Exception(); }); }); } catch (Exception $e) { pass(); } asrt(R::count('bean'), 0); $bean = R::dispense('bean'); $bean->name = 'a'; try { R::transaction(function () use($bean) { R::transaction(function () use($bean) { R::store($bean); }); }); } catch (Exception $e) { pass(); } asrt(R::count('bean'), 1); R::freeze(FALSE); try { R::transaction('nope'); fail(); } catch (Exception $e) { pass(); } testpack('Test Camelcase 2 underscore'); $names = array('oneACLRoute' => 'one_acl_route', 'ALLUPPERCASE' => 'alluppercase', 'clientServerArchitecture' => 'client_server_architecture', 'camelCase' => 'camel_case', 'peer2peer' => 'peer2peer', 'fromUs4You' => 'from_us4_you', 'lowercase' => 'lowercase', 'a1A2b' => 'a1a2b'); $bean = R::dispense('bean'); foreach ($names as $name => $becomes) { $bean->{$name} = 1; asrt(isset($bean->{$becomes}), TRUE); } testpack('Test debugger check.'); $old = R::$adapter; R::$adapter = NULL; try { R::debug(TRUE); fail(); } catch (RedBean_Exception_Security $e) { pass(); } R::$adapter = $old; R::debug(FALSE); testpack('Misc Tests'); try { $candy = R::dispense('CandyBar'); fail(); } catch (RedBean_Exception_Security $e) { pass(); } $candy = R::dispense('candybar'); $s = strval($candy); asrt($s, 'candy!'); $obj = new stdClass(); $bean = R::dispense('bean'); $bean->property1 = 'property1'; $bean->exportToObj($obj); asrt($obj->property1, 'property1'); R::debug(1); flush(); ob_start(); R::exec('SELECT 123'); $out = ob_get_contents(); ob_end_clean(); flush(); pass(); asrt(strpos($out, 'SELECT 123') !== FALSE, TRUE); R::debug(0); flush(); ob_start(); R::exec('SELECT 123'); $out = ob_get_contents(); ob_end_clean(); flush(); pass(); asrt($out, ''); R::debug(0); pass(); testpack('test to string override'); $band = R::dispense('band'); $str = strval($band); asrt($str, 'bigband'); testpack('test whether we can use isset/set in model'); $band->setProperty('property1', 123); asrt($band->property1, 123); asrt($band->checkProperty('property1'), TRUE); asrt($band->checkProperty('property2'), FALSE); $band = new Model_Band(); $bean = R::dispense('band'); $bean->property3 = 123; $band->loadBean($bean); $bean->property4 = 345; $band->setProperty('property1', 123); asrt($band->property1, 123); asrt($band->checkProperty('property1'), TRUE); asrt($band->checkProperty('property2'), FALSE); asrt($band->property3, 123); asrt($band->property4, 345); testpack('Test blackhole DSN and setup()'); R::setup('blackhole:database'); pass(); asrt(isset(R::$toolboxes['default']), TRUE); try { R::$toolboxes['default']->getDatabaseAdapter()->getDatabase()->connect(); fail(); } catch (PDOException $e) { pass(); /** * Make sure the message is non-descriptive - avoid revealing * security details if user hasn't configured error reporting improperly. */ asrt($e->getMessage(), 'Could not connect to database (?).'); } R::setup('blackhole:dbname=mydatabase;password=dontshowthisone'); pass(); asrt(isset(R::$toolboxes['default']), TRUE); try { R::$toolboxes['default']->getDatabaseAdapter()->getDatabase()->connect(); fail(); } catch (PDOException $e) { pass(); /** * Make sure the message is non-descriptive - avoid revealing * security details if user hasn't configured error reporting improperly. */ asrt($e->getMessage(), 'Could not connect to database (mydatabase).'); } testpack('Can we pass a PDO object to Setup?'); $pdo = new PDO('sqlite:test.db'); $toolbox = RedBean_Setup::kickstart($pdo); asrt($toolbox instanceof RedBean_ToolBox, TRUE); asrt($toolbox->getDatabaseAdapter() instanceof RedBean_Adapter, TRUE); asrt($toolbox->getDatabaseAdapter()->getDatabase()->getPDO() instanceof PDO, TRUE); testpack('Test array interface of beans'); $bean = R::dispense('bean'); $bean->hello = 'hi'; $bean->world = 'planet'; asrt($bean['hello'], 'hi'); asrt(isset($bean['hello']), TRUE); asrt(isset($bean['bye']), FALSE); $bean['world'] = 'sphere'; asrt($bean->world, 'sphere'); foreach ($bean as $key => $el) { if ($el == 'sphere' || $el == 'hi' || $el == 0) { pass(); } else { fail(); } if ($key == 'hello' || $key == 'world' || $key == 'id') { pass(); } else { fail(); } } asrt(count($bean), 3); unset($bean['hello']); asrt(count($bean), 2); asrt(count(R::dispense('countable')), 1); // Otherwise untestable... $bean->setBeanHelper(new RedBean_BeanHelper_Facade()); R::$redbean->setBeanHelper(new RedBean_BeanHelper_Facade()); pass(); // Test whether properties like owner and shareditem are still possible testpack('Test Bean Interface for Lists'); $bean = R::dispense('bean'); // Must not be list, because first char after own is lowercase asrt(is_array($bean->owner), FALSE); // Must not be list, because first char after shared is lowercase asrt(is_array($bean->shareditem), FALSE); asrt(is_array($bean->own), FALSE); asrt(is_array($bean->shared), FALSE); asrt(is_array($bean->own_item), FALSE); asrt(is_array($bean->shared_item), FALSE); asrt(is_array($bean->{'own item'}), FALSE); asrt(is_array($bean->{'shared Item'}), FALSE); }
public function testDateTimeHinting() { $toolbox = RedBean_Setup::kickstart(Yii::app()->db->connectionString, Yii::app()->db->username, Yii::app()->db->password); R::exec("drop table if exists bean"); // Not Coding Standard $bean = R::dispense("bean"); // Not Coding Standard $bean->setMeta("hint", array("prop" => "datetime")); // Not Coding Standard $bean->prop = "2010-01-01 10:00:00"; // Not Coding Standard R::store($bean); // Not Coding Standard $rows = R::getAll('desc bean'); $this->assertEquals('prop', $rows[1]['Field']); $this->assertEquals('datetime', $rows[1]['Type']); }