You can also pass an array containing a selection of frozen types.
Let's call this chilly mode, it's just like fluid mode except that
certain types (i.e. tables) aren't touched.
public static freeze ( $tf = TRUE ) |
/** * Cant have underscored beans * @expectedException \RedBeanPHP\RedException */ public function testTwoWordsUnderscoredBean() { R::freeze(false); $testName = R::dispense('test_name'); $id = R::store($testName); R::load('test_name', $id); R::freeze($this->frozen); }
/** * Tests whether we can NULLify a parent bean * page->book if the parent (book) is already * NULL. (isset vs array_key_exists bug). * * @return void */ public function testUnsetParent() { R::nuke(); $book = R::dispense('book'); $book->title = 'My Book'; $page = R::dispense('page'); $page->text = 'Lorem Ipsum'; $book->ownPage[] = $page; R::store($book); $page = $page->fresh(); R::freeze(TRUE); asrt((int) $page->book->id, (int) $book->id); unset($page->book); R::store($page); $page = $page->fresh(); asrt((int) $page->book->id, (int) $book->id); $page->book = NULL; R::store($page); $page = $page->fresh(); asrt($page->book, NULL); asrt($page->book_id, NULL); asrt($page->bookID, NULL); asrt($page->bookId, NULL); $page = R::dispense('page'); $page->text = 'Another Page'; $page->book = NULL; try { R::store($page); fail(); } catch (\Exception $exception) { pass(); } unset($page->book); R::store($page); $page = $page->fresh(); $page->book = NULL; //this must set field id to NULL not ADD column! try { R::store($page); pass(); } catch (\Exception $exception) { fail(); } $page = $page->fresh(); $page->book = NULL; R::store($page); $page = $page->fresh(); asrt(is_null($page->book_id), TRUE); $page->book = $book; R::store($page); $page = $page->fresh(); asrt((int) $page->book->id, (int) $book->id); $page->book = NULL; R::store($page); asrt(is_null($page->book_id), TRUE); asrt(is_null($page->book), TRUE); R::freeze(FALSE); }
/** * 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); }
/** * 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); }
protected function setUp() { $config = new Config(); $config->setProtected('basePath', BASE_PATH); setCommonConfig($config); setUniqueConfig($config); Application::setupRedBean('sqlite:test.db', 'user', 'password', $this->frozen, 'sqlite'); R::freeze(false); R::nuke(); R::freeze($this->frozen); $this->app = __setupApp(); /** $http Mock Http object. */ $http = $this->getMock('Skully\\Core\\Http'); $http->expects($this->any())->method('redirect')->will($this->returnCallback('stubRedirect')); $this->app->setHttp($http); }
/** * Test count and wipe. * * @return void */ public function testCountAndWipe() { testpack("Test count and wipe"); $page = R::dispense("page"); $page->name = "ABC"; R::store($page); $n1 = R::count("page"); $page = R::dispense("page"); $page->name = "DEF"; R::store($page); $n2 = R::count("page"); asrt($n1 + 1, $n2); R::wipe("page"); asrt(R::count("page"), 0); asrt(R::getRedBean()->count("page"), 0); asrt(R::getRedBean()->count("kazoo"), 0); // non existing table R::freeze(TRUE); asrt(R::getRedBean()->count("kazoo"), 0); // non existing table R::freeze(FALSE); $page = R::dispense('page'); $page->name = 'foo'; R::store($page); $page = R::dispense('page'); $page->name = 'bar'; R::store($page); asrt(R::count('page', ' name = ? ', array('foo')), 1); // Now count something that does not exist, this should return 0. (just be polite) asrt(R::count('teapot', ' name = ? ', array('flying')), 0); asrt(R::count('teapot'), 0); $currentDriver = $this->currentlyActiveDriverID; // Some drivers don't support that many error codes. if ($currentDriver === 'mysql' || $currentDriver === 'postgres') { try { R::count('teaport', ' for tea '); fail(); } catch (SQL $e) { pass(); } } }
protected function setUp() { $config = new Config(); $config->setProtected('basePath', BASE_PATH); setCommonConfig($config); setUniqueConfig($config); $dbConfig = $config->getProtected('dbConfig'); if ($dbConfig['type'] == 'mysql') { Application::setupRedBean("mysql:host={$dbConfig['host']};dbname={$dbConfig['dbname']};port={$dbConfig['port']}", $dbConfig['user'], $dbConfig['password'], $config->getProtected('isDevMode')); } elseif ($dbConfig['type'] == 'sqlite') { Application::setupRedBean("sqlite:{$dbConfig['dbname']}", $dbConfig['user'], $dbConfig['password'], $config->getProtected('isDevMode')); } R::freeze(false); R::nuke(); R::freeze($this->frozen); $this->app = __setupApp(); /** $http Mock Http object. */ $http = $this->getMock('Skully\\Core\\Http'); $http->expects($this->any())->method('redirect')->will($this->returnCallback('stubRedirect')); $this->app->setHttp($http); }
/** * Test nullifying aliased parent. * * @return void */ public function testUnsetAliasedParent() { R::nuke(); $book = R::dispense('book'); $author = R::dispense('author'); $book->coauthor = $author; R::store($book); $book = $book->fresh(); asrt(is_null($book->fetchAs('author')->coauthor), FALSE); unset($book->coauthor); R::store($book); $book = $book->fresh(); asrt(is_null($book->fetchAs('author')->coauthor), FALSE); $book->coauthor = NULL; R::store($book); $book = $book->fresh(); asrt(is_null($book->fetchAs('author')->coauthor), TRUE); R::trash($book); R::trash($author); R::freeze(TRUE); $book = R::dispense('book'); $author = R::dispense('author'); $book->coauthor = $author; R::store($book); $book = $book->fresh(); asrt(is_null($book->fetchAs('author')->coauthor), FALSE); unset($book->coauthor); R::store($book); $book = $book->fresh(); asrt(is_null($book->fetchAs('author')->coauthor), FALSE); $book->coauthor = NULL; R::store($book); $book = $book->fresh(); asrt(is_null($book->fetchAs('author')->coauthor), TRUE); R::trash($book); R::trash($author); R::freeze(FALSE); }
/** * Tests whether loading non-existant beans * returns the same results in frozen mode. * * @return */ public function testLoadNonExistant() { R::nuke(); R::store(R::dispense('bean')); R::freeze(TRUE); $bean = R::load('bean', 123); R::freeze(FALSE); asrt($bean instanceof OODBBean, TRUE); asrt($bean->id, 0); }
<?php // Error reporting //error_reporting(E_ALL ^ E_NOTICE); // SlimPHP portable route fix $_SERVER['SCRIPT_NAME'] = preg_replace('/public\\/index\\.php$/', 'index.php', $_SERVER['SCRIPT_NAME'], 1); // RedBeanPHP alias fix use RedBeanPHP\Facade as R; // Load Config require 'app/config.php'; // Autoload require 'vendor/autoload.php'; // RedBeanPHP setup R::setup('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USERNAME, DB_PASSWORD); R::freeze(DB_FREEZE); // Slim app instance $app = new \Slim\Slim(); // Slim Config $app->config(['templates.path' => 'app/views', 'debug' => APP_DEBUG]); // Set webroot for portable $app->hook('slim.before', function () use($app) { $app->wroot = $app->request->getUrl() . $app->request->getRootUri(); $app->view()->appendData(array('wroot' => $app->wroot)); }); // HybridAuth instance $app->container->singleton('hybridInstance', function ($app) { $config = ["base_url" => $app->wroot . "/cb", "providers" => ["Facebook" => ["enabled" => true, "keys" => ["id" => FB_ID, "secret" => FB_SECRET], "scope" => "email, user_about_me, user_birthday, user_location", "trustForwarded" => false], "Twitter" => ["enabled" => true, "keys" => ["key" => TW_KEY, "secret" => TW_SECRET]]], "debug_mode" => HYBRIDAUTH_DEBUG_MODE, "debug_file" => HYBRIDAUTH_DEBUG_FILE]; $instance = new Hybrid_Auth($config); return $instance; }); // Auth Check
/** * Prepare test pack (mostly: nuke the entire database) */ public function prepare() { R::freeze(FALSE); R::debug(FALSE); R::nuke(); }
public static function ConfigSetup($arrconfig = null) { if (is_array($arrconfig) && !empty($arrconfig)) { if ($arrconfig['server']) { self::$server = $arrconfig['server']; } if ($arrconfig['databasename']) { self::$databasename = $arrconfig['databasename']; } if ($arrconfig['username']) { self::$username = $arrconfig['username']; } if ($arrconfig['password']) { self::$password = $arrconfig['password']; } if ($arrconfig['port']) { self::$port = $arrconfig['port']; } } self::$connection = new PDO('mysql:host=' . self::$server . ';port=' . self::$port . ';dbname=' . self::$databasename . ';', self::$username, self::$password); self::$connection->query('SET NAMES utf8'); R::setup(self::$connection); R::freeze(true); self::$logsrv = new \RedBeanPHP\Plugin\SystemlogsService(); R::debug(true, 1); }
/** * Test Facade bind function method. * Test for MySQL WKT spatial format. */ public function testFunctionFilters() { R::nuke(); R::bindFunc('read', 'location.point', 'asText'); R::bindFunc('write', 'location.point', 'GeomFromText'); R::store(R::dispense('location')); R::freeze(true); try { R::find('location'); fail(); } catch (SQL $exception) { pass(); } R::freeze(false); try { R::find('location'); pass(); } catch (SQL $exception) { fail(); } $location = R::dispense('location'); $location->point = 'POINT(14 6)'; R::store($location); $columns = R::inspect('location'); asrt($columns['point'], 'point'); $location = $location->fresh(); asrt($location->point, 'POINT(14 6)'); R::nuke(); $location = R::dispense('location'); $location->point = 'LINESTRING(0 0,1 1,2 2)'; R::store($location); $columns = R::inspect('location'); asrt($columns['point'], 'linestring'); $location->bustcache = 2; R::store($location); $location = $location->fresh(); asrt($location->point, 'LINESTRING(0 0,1 1,2 2)'); R::nuke(); $location = R::dispense('location'); $location->point = 'POLYGON((0 0,10 0,10 10,0 10,0 0),(5 5,7 5,7 7,5 7,5 5))'; R::store($location); $columns = R::inspect('location'); asrt($columns['point'], 'polygon'); $location->bustcache = 4; R::store($location); $location = $location->fresh(); asrt($location->point, 'POLYGON((0 0,10 0,10 10,0 10,0 0),(5 5,7 5,7 7,5 7,5 5))'); R::bindFunc('read', 'location.point', NULL); $location->bustcache = 1; R::store($location); $location = $location->fresh(); asrt($location->point === 'POLYGON((0 0,10 0,10 10,0 10,0 0),(5 5,7 5,7 7,5 7,5 5))', FALSE); $filters = AQueryWriter::getSQLFilters(); asrt(is_array($filters), TRUE); asrt(count($filters), 2); asrt(isset($filters[QueryWriter::C_SQLFILTER_READ]), TRUE); asrt(isset($filters[QueryWriter::C_SQLFILTER_WRITE]), TRUE); R::bindFunc('read', 'place.point', 'asText'); R::bindFunc('write', 'place.point', 'GeomFromText'); R::bindFunc('read', 'place.line', 'asText'); R::bindFunc('write', 'place.line', 'GeomFromText'); R::nuke(); $place = R::dispense('place'); $place->point = 'POINT(13.2 666.6)'; $place->line = 'LINESTRING(9.2 0,3 1.33)'; R::store($place); $columns = R::inspect('place'); asrt($columns['point'], 'point'); asrt($columns['line'], 'linestring'); $place = R::findOne('place'); asrt($place->point, 'POINT(13.2 666.6)'); asrt($place->line, 'LINESTRING(9.2 0,3 1.33)'); R::bindFunc('read', 'place.point', NULL); R::bindFunc('write', 'place.point', NULL); R::bindFunc('read', 'place.line', NULL); R::bindFunc('write', 'place.line', NULL); }
/** * Test Transactions. * * @return void */ public function testTransactions() { testpack('transactions'); R::begin(); $bean = R::dispense('bean'); R::store($bean); R::commit(); asrt(R::count('bean'), 1); R::wipe('bean'); R::freeze(1); R::begin(); $bean = R::dispense('bean'); R::store($bean); R::rollback(); asrt(R::count('bean'), 0); R::freeze(FALSE); testpack('genSlots'); asrt(R::genSlots(array('a', 'b')), '?,?'); asrt(R::genSlots(array('a')), '?'); asrt(R::genSlots(array()), ''); }
/** * Test whether we can set and reset the chill list and check the contents * of the chill list. * * @return void */ public function testChillTest() { R::freeze(array('beer')); $oodb = R::getRedBean(); asrt($oodb->isChilled('beer'), TRUE); asrt($oodb->isChilled('wine'), FALSE); R::freeze(FALSE); $oodb = R::getRedBean(); asrt($oodb->isChilled('beer'), TRUE); asrt($oodb->isChilled('wine'), FALSE); R::freeze(TRUE); $oodb = R::getRedBean(); asrt($oodb->isChilled('beer'), TRUE); asrt($oodb->isChilled('wine'), FALSE); R::freeze(array()); $oodb = R::getRedBean(); asrt($oodb->isChilled('beer'), FALSE); asrt($oodb->isChilled('wine'), FALSE); }
/** * Tests whether freeze() switches the repository object * as it is supposed to do. * * @return void */ public function testRepoSwitching() { asrt(class_exists('RedBeanPHP\\Repository'), TRUE); asrt(class_exists('RedBeanPHP\\Repository\\Fluid'), TRUE); asrt(class_exists('RedBeanPHP\\Repository\\Frozen'), TRUE); R::freeze(FALSE); $redbean = R::getRedBean(); $repo = $redbean->getCurrentRepository(); asrt(is_object($repo), TRUE); asrt($repo instanceof Repository, TRUE); asrt($repo instanceof FluidRepo, TRUE); R::freeze(TRUE); $fluid = $repo; $repo = $redbean->getCurrentRepository(); asrt(is_object($repo), TRUE); asrt($repo instanceof Repository, TRUE); asrt($repo instanceof FrozenRepo, TRUE); $frozen = $repo; R::freeze(FALSE); $redbean = R::getRedBean(); $repo = $redbean->getCurrentRepository(); asrt(is_object($repo), TRUE); asrt($repo instanceof Repository, TRUE); asrt($repo instanceof FluidRepo, TRUE); asrt($repo, $fluid); R::freeze(TRUE); $fluid = $repo; $repo = $redbean->getCurrentRepository(); asrt(is_object($repo), TRUE); asrt($repo instanceof Repository, TRUE); asrt($repo instanceof FrozenRepo, TRUE); asrt($repo, $frozen); R::freeze(FALSE); }
/** * Test duplication and tainting. * * @return void */ public function testDupAndExportNonTainting() { testpack('Dup() and Export() should not taint beans'); $p = R::dispense('page'); $b = R::dispense('book'); $b->ownPage[] = $p; $b->title = 'a'; $id = R::store($b); $b = R::load('book', $id); asrt(!$b->getMeta('tainted'), TRUE); R::exportAll($b); asrt(!$b->getMeta('tainted'), TRUE); R::dup($b); asrt(!$b->getMeta('tainted'), TRUE); testpack('Test issue with ownItems and stealing Ids.'); R::nuke(); $bill = R::dispense('bill'); $item = R::dispense('item'); $element = R::dispense('element'); $bill->ownItem[] = $item; $bill->sharedElement[] = $element; R::store($bill); $bill = R::load('bill', 1); $bill->ownItem; $bill->sharedElement; $copy = R::dup($bill); R::store($copy); $rows = R::getAll('select * from bill_element'); asrt(count($rows), 2); $rows = R::getAll('select * from item'); foreach ($rows as $row) { asrt($row['bill_id'] > 0, TRUE); } R::nuke(); $this->runOnce(); R::freeze(TRUE); $this->runOnce(FALSE); R::freeze(FALSE); }
/** * Same as above but now with intermediate save, counts must be same */ public function testVersioningIntermediateSaves() { $document = R::dispense('document'); $page = R::dispense('page'); $document->title = 'test'; $page->content = 'lorem ipsum'; $user = R::dispense('user'); $user->name = 'Leo'; $document->sharedUser[] = $user; $document->ownPage[] = $page; $document->starship_id = 3; $document->planet = R::dispense('planet'); R::store($document); $duplicate = R::dup($document); R::store($document); R::store($duplicate); R::store($document); $duplicate = R::dup($document); R::store($document); R::store($duplicate); asrt(R::count('planet'), 1); asrt(R::count('user'), 1); asrt(R::count('document'), 3); asrt(R::count('page'), 3); asrt(R::count('spaceship'), 0); // same, but now with intermediate save, counts must be same R::freeze(TRUE); $document = R::dispense('document'); $page = R::dispense('page'); $document->title = 'test'; $page->content = 'lorem ipsum'; $user = R::dispense('user'); $user->name = 'Leo'; $document->sharedUser[] = $user; $document->ownPage[] = $page; $document->starship_id = 3; $document->planet = R::dispense('planet'); R::store($document); $duplicate = R::dup($document); R::store($document); R::store($duplicate); R::store($document); $duplicate = R::dup($document); R::store($document); R::store($duplicate); asrt(R::count('planet'), 2); asrt(R::count('user'), 2); asrt(R::count('document'), 6); asrt(R::count('page'), 6); asrt(R::count('spaceship'), 0); R::freeze(FALSE); }
public function testMeta() { R::freeze(false); $testmodel = $this->app->createModel('testmodel', array('name' => 'test')); R::store($testmodel); $bean = R::findOne('testmodel'); $bean->box()->setMeta('test', array('test')); $exported = $bean->box()->export(true); $this->assertEquals(array('test'), $exported['test']); R::freeze($this->frozen); }
/** * Tests the more complicated scenarios for * with-joins. * * @return void */ public function testComplexInFrozenMode() { R::freeze(FALSE); $this->testComplexCombinationsJoins(); R::freeze(TRUE); $this->testComplexCombinationsJoins(); R::freeze(FALSE); }
/** * 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('Misc Tests'); 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('Can we pass a\\PDO object to Setup?'); $pdo = new \PDO('sqlite:test.db'); R::addDatabase('pdo', $pdo); R::selectDatabase('pdo'); R::getCell('SELECT 123;'); 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 SimpleFacadeBeanHelper()); R::getRedBean()->setBeanHelper(new SimpleFacadeBeanHelper()); 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); }
/** * Tests whether we can update or unset a parent bean * with an alias without having to use fetchAs and * without loading the aliased bean causing table-not-found * errors. */ public function testUpdatingParentBeansWithAliases() { testpack('Test updating parent beans with aliases'); R::nuke(); $trans = R::dispense('transaction'); $seller = R::dispense('user'); $trans->seller = $seller; $id = R::store($trans); R::freeze(true); $trans = R::load('transaction', $id); //should not try to load seller, should not require fetchAs(). try { $trans->seller = R::dispense('user'); pass(); } catch (Exception $e) { fail(); } $trans = R::load('transaction', $id); //same for unset... try { unset($trans->seller); pass(); } catch (Exception $e) { fail(); } R::freeze(false); $account = R::dispense('user'); asrt(count($account->alias('seller')->ownTransaction), 0); $account->alias('seller')->ownTransaction = R::dispense('transaction', 10); $account->alias('boo'); //try to trick me... $id = R::store($account); R::freeze(true); $account = R::load('user', $id); asrt(count($account->alias('seller')->ownTransaction), 10); //you cannot unset a list unset($account->alias('seller')->ownTransaction); $id = R::store($account); $account = R::load('user', $id); asrt(count($account->alias('seller')->ownTransaction), 10); $account->alias('seller')->ownTransaction = array(); $id = R::store($account); $account = R::load('user', $id); asrt(count($account->alias('seller')->ownTransaction), 0); asrt(count($account->ownTransaction), 0); R::freeze(false); //but also make sure we don't cause extra column issue #335 R::nuke(); $building = R::dispense('building'); $village = R::dispense('village'); $building->village = $village; R::store($building); $building = $building->fresh(); $building->village = NULL; R::store($building); $building = $building->fresh(); $columns = R::inspect('building'); asrt(isset($columns['village']), false); asrt(isset($building->village), false); R::nuke(); $building = R::dispense('building'); $village = R::dispense('village'); $building->village = $village; R::store($building); $building = $building->fresh(); unset($building->village); R::store($building); $building = $building->fresh(); $columns = R::inspect('building'); asrt(isset($columns['village']), false); asrt(isset($building->village), false); $building = R::dispense('building'); $village = R::dispense('village'); $building->village = $village; R::store($building); $building = $building->fresh(); $building->village = false; R::store($building); $building = $building->fresh(); $columns = R::inspect('building'); asrt(isset($columns['village']), false); asrt(isset($building->village), false); }
/** * Tests whether we can store an empty bean. * An empty bean has no properties, only ID. Normally we would * skip the ID field in an INSERT, this test forces the driver * to specify a value for the ID field. Different writers have to * use different values: Mysql uses NULL to insert a new auto-generated ID, * while Postgres has to use DEFAULT. */ public function testEmptyBean() { testpack('Test Empty Bean Storage.'); R::nuke(); $bean = R::dispense('emptybean'); $id = R::store($bean); asrt($id > 0, TRUE); asrt(R::count('emptybean'), 1); $bean = R::dispense('emptybean'); $id = R::store($bean); asrt($id > 0, TRUE); asrt(R::count('emptybean'), 2); //also test in frozen mode R::freeze(TRUE); $bean = R::dispense('emptybean'); $id = R::store($bean); asrt($id > 0, TRUE); asrt(R::count('emptybean'), 3); R::freeze(FALSE); }
/** * 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")); }
/** * Test error handling of SQL states. * * @return void */ public function testFindError() { R::freeze(FALSE); $page = R::dispense('page'); $page->title = 'abc'; R::store($page); //Column does not exist, in fluid mode no error! try { R::find('page', ' xtitle = ? ', array('x')); pass(); } catch (SQL $e) { fail(); } //Table does not exist, in fluid mode no error! try { R::find('pagex', ' title = ? ', array('x')); pass(); } catch (SQL $e) { fail(); } //Syntax error, error in fluid mode if possible to infer from SQLSTATE (MySQL/Postgres) try { R::find('page', ' invalid SQL '); //In SQLite only get HY000 - not very descriptive so suppress more errors in fluid mode then. if ($this->currentlyActiveDriverID === 'sqlite' || $this->currentlyActiveDriverID === 'CUBRID') { pass(); } else { fail(); } } catch (SQL $e) { pass(); } //Frozen, always error... R::freeze(TRUE); //Column does not exist, in frozen mode error! try { R::find('page', ' xtitle = ? ', array('x')); fail(); } catch (SQL $e) { pass(); } //Table does not exist, in frozen mode error! try { R::find('pagex', ' title = ? ', array('x')); fail(); } catch (SQL $e) { pass(); } //Syntax error, in frozen mode error! try { R::find('page', ' invalid SQL '); fail(); } catch (SQL $e) { pass(); } R::freeze(FALSE); }
/** * Test basic operations in frozen mode. */ public function testBasicOperationsFrozen() { R::nuke(); $author = R::xdispense(AUTHOR); $author->name = 'Mr. Quill'; $book = R::xdispense(BOOK); $book->title = 'Good Stories'; $book2 = R::xdispense(BOOK); $book2->title = 'Good Stories 2'; $friend = R::xdispense(FRIEND); $friend->name = 'Muse'; $publisher = R::xdispense(PUBLISHER); $publisher->name = 'Good Books'; $author->{BOOKLIST} = array($book, $book2); $author->{FRIENDLIST}[] = $friend; $author->{PUBLISHER} = $publisher; $coAuthor = R::xdispense(AUTHOR); $coAuthor->name = 'Xavier'; $book2->{COAUTHOR} = $coAuthor; R::store($author); R::freeze(TRUE); asrt($author->name, 'Mr. Quill'); asrt(count($author->{BOOKLIST}), 2); $firstBook = reset($author->{BOOKLIST}); asrt($firstBook->title, 'Good Stories'); asrt(count($author->{FRIENDLIST}), 1); $firstFriend = reset($author->{FRIENDLIST}); $parent = $author->{PUBLISHER}; asrt($parent instanceof OODBBean, TRUE); $tables = R::inspect(); //have all tables been prefixed? foreach ($tables as $table) { asrt(strpos($table, 'tbl_'), 0); } //Can we make an export? $export = R::exportAll(R::findOne(AUTHOR), TRUE); $export = reset($export); asrt(isset($export[PUBLISHER]), TRUE); asrt(isset($export[BOOKLIST]), TRUE); asrt(isset($export[FRIENDLIST]), TRUE); asrt(isset($export['ownBook']), FALSE); asrt(isset($export['sharedFriend']), FALSE); asrt(isset($export['publisher']), FALSE); R::freeze(FALSE); }
/** * Only fire update query if the bean really contains different * values. But make sure beans several 'parents' away still get * saved. * * @return void */ public function testBeanTainting() { $logger = R::getDatabaseAdapter()->getDatabase()->getLogger(); list($i, $k, $c, $s) = R::dispenseAll('invoice,customer,city,state'); $i->customer = $k; $i->status = 0; $k->city = $c; $c->state = $s; $s->name = 'x'; R::store($i); $i = $i->fresh(); asrt($i->customer->city->state->name, 'x'); $i->status = 1; R::freeze(true); $logger = R::debug(1, 1); //do we properly skip unmodified but tainted parent beans? R::store($i); $numberOfUpdateQueries = $logger->grep('UPDATE'); asrt(count($numberOfUpdateQueries), 1); //does cascade update still work? $i = $i->fresh(); $i->customer->city->state->name = 'y'; R::store($i); $i = $i->fresh(); asrt($i->customer->city->state->name, 'y'); $i = $i->fresh(); $differentCity = R::dispense('city'); R::store($differentCity); $i->customer->city = $differentCity; R::store($i); $i = $i->fresh(); asrt($i->customer->city->id != $c->id, TRUE); asrt(is_null($i->customer->city->state), TRUE); $i->customer->city = NULL; R::store($i); $i = $i->fresh(); asrt(is_null($i->customer->city), TRUE); $i->customer = $k; $i->status = 0; $k->city = $c; $c->state = $s; $s->name = 'x'; R::store($i); R::freeze(FALSE); $i = $i->fresh(); //can we still change remote parent? $i->customer->city->name = 'q'; $logger->clear(); R::store($i); $numberOfUpdateQueries = $logger->grep('UPDATE'); //print_r($logger->getLogs()); asrt(count($numberOfUpdateQueries), 1); $i = $i->fresh(); asrt($i->customer->city->name, 'q'); //do we properly skip unmodified but tainted parent beans? $i->status = 3; $logger->clear(); R::store($i); $numberOfUpdateQueries = $logger->grep('UPDATE'); asrt(count($numberOfUpdateQueries), 1); }
/** * CRUD performance Array Access. * * @return void */ public function crudaa() { R::freeze(TRUE); $book = R::dispense('book'); $book['title'] = 'Book'; $page = R::dispense('page'); $page['content'] = 'Content'; $page['title'] = 'data'; $page['sequence'] = 'data'; $page['order'] = 'data'; $page['columns'] = 'data'; $page['paragraphs'] = 'data'; $page['paragraphs1'] = 'data'; $page['paragraphs2'] = 'data'; $page['paragraphs3'] = 'data'; $page['paragraphs4'] = 'data'; $tag = R::dispense('tag'); $tag['label'] = 'Tag '; $book->ownPage[] = $page; $book->noLoad()->sharedTag[] = $tag; R::store($book); $book = $book->fresh(); $book->ownPage; $book->sharedTag; R::trash($book); }
/** * @param ConfigInterface $config * @throws InvalidConfigException */ public function __construct(ConfigInterface $config) { $this->setConfig($config); if ($this->configIsEmpty('timezone')) { $this->getConfigObject()->setProtected('timezone', 'Asia/Jakarta'); } if ($this->configIsEmpty('publicDir')) { $this->getConfigObject()->setPublic('publicDir', 'public/'); } $this->setupTheme(); $this->langAdjustment(); date_default_timezone_set($this->config('timezone')); $this->addLangfile('common', $this->config('language')); $this->addLangfile('common', $this->getLanguage()); // Setting up RedBean $dbConfig = $config->getProtected('dbConfig'); if (!empty($dbConfig)) { $namespace = 'App'; if (!$this->configIsEmpty('namespace')) { $namespace = $this->config('namespace'); } if (!defined('REDBEAN_MODEL_PREFIX')) { define('REDBEAN_MODEL_PREFIX', '\\' . $namespace . '\\Models\\'); } if ($dbConfig['type'] == 'mysql') { self::setupRedBean("mysql:host={$dbConfig['host']};dbname={$dbConfig['dbname']};port={$dbConfig['port']}", $dbConfig['user'], $dbConfig['password'], $config->getProtected('isDevMode')); } elseif ($dbConfig['type'] == 'sqlite') { self::setupRedBean("sqlite:{$dbConfig['dbname']}", $dbConfig['user'], $dbConfig['password'], $config->getProtected('isDevMode')); } if (!$this->configIsEmpty('freeze') && $this->config('freeze') == false) { R::freeze(false); } // Below is needed so that RedBeanPHP\SimpleModel may use $this->app: SimpleFacadeBeanHelper::setFactoryFunction(function ($beanTypeName) { /** @var \Skully\App\Models\BaseModel $model */ $model = new $beanTypeName(); $model->setApp($this); return $model; }); } }