/** * Handles a JSON RPC 2 request to perform a custom operation on a bean. * * @param string $id request ID, identification for request * @param string $beanType type of the bean you want to store * @param string $action action you want to invoke on bean model * @param array $data data array * * @return string */ private function custom($id, $beanType, $action, $data) { $modelName = $this->modelHelper->getModelName($beanType); if (!class_exists($modelName)) { return $this->resp(NULL, $id, self::C_JSONRPC2_METHOD_NOT_FOUND, 'No such bean in the can!'); } $beanModel = new $modelName(); if (!method_exists($beanModel, $action)) { return $this->resp(NULL, $id, self::C_JSONRPC2_METHOD_NOT_FOUND, "Method not found in Bean: {$beanType} "); } return $this->resp(call_user_func_array(array($beanModel, $action), $data), $id); }
/** * Test FUSE and model formatting. * * @todo move tagging tests to tag tester. * * @return void */ public function testFUSE() { $toolbox = R::$toolbox; $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"; R::associate($blog, $post); $a = R::getAll("select * from blog "); RedBean_ModelHelper::setModelFormatter(new mymodelformatter()); $w = R::dispense("weirdo"); asrt($w->blah(), "yes!"); 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 (RedBean_Exception $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); }
/** * Test dependency injection with RedBeanPHP. * * @return void */ public function testDependencyInjection() { // base scenario $geek = R::dispense('geek'); list($coffee, $cocoa) = $geek->getObjects(); asrt($coffee instanceof Dependency_Coffee, FALSE); asrt($cocoa instanceof Dependency_Cocoa, FALSE); // Base scenario with empty container, don't fail $di = new RedBean_DependencyInjector(); $geek = R::dispense('geek'); list($coffee, $cocoa) = $geek->getObjects(); asrt($coffee instanceof Dependency_Coffee, FALSE); asrt($cocoa instanceof Dependency_Cocoa, FALSE); // Succesfull scenario, one missing $di = new RedBean_DependencyInjector(); $di->addDependency('Coffee', new Dependency_Coffee()); RedBean_ModelHelper::setDependencyInjector($di); $geek = R::dispense('geek'); list($coffee, $cocoa) = $geek->getObjects(); asrt($coffee instanceof Dependency_Coffee, TRUE); asrt($cocoa instanceof Dependency_Cocoa, FALSE); // Success scenario $di = new RedBean_DependencyInjector(); $di->addDependency('Coffee', new Dependency_Coffee()); $di->addDependency('Cocoa', new Dependency_Cocoa()); RedBean_ModelHelper::setDependencyInjector($di); $geek = R::dispense('geek'); list($coffee, $cocoa) = $geek->getObjects(); asrt($coffee instanceof Dependency_Coffee, TRUE); asrt($cocoa instanceof Dependency_Cocoa, TRUE); // Don't fail if not exists $di->addDependency('NonExistantObject', new Dependency_Coffee()); $geek = R::dispense('geek'); $geek = R::dispense('geek'); list($coffee, $cocoa) = $geek->getObjects(); asrt($coffee instanceof Dependency_Coffee, TRUE); asrt($cocoa instanceof Dependency_Cocoa, TRUE); // Can we go back to base scenario? RedBean_ModelHelper::clearDependencyInjector(); $geek = R::dispense('geek'); list($coffee, $cocoa) = $geek->getObjects(); asrt($coffee instanceof Dependency_Coffee, FALSE); asrt($cocoa instanceof Dependency_Cocoa, FALSE); }
/** * Processes a JSON object request. * * @param array $jsonObject JSON request object * * @return mixed $result result */ public function handleJSONRequest($jsonString) { //Decode JSON string $jsonArray = json_decode($jsonString, true); if (!$jsonArray) { return $this->resp(null, null, -32700, 'Cannot Parse JSON'); } if (!isset($jsonArray['jsonrpc'])) { return $this->resp(null, null, -32600, 'No RPC version'); } if ($jsonArray['jsonrpc'] != '2.0') { return $this->resp(null, null, -32600, 'Incompatible RPC Version'); } //DO we have an ID to identify this request? if (!isset($jsonArray['id'])) { return $this->resp(null, null, -32600, 'No ID'); } //Fetch the request Identification String. $id = $jsonArray['id']; //Do we have a method? if (!isset($jsonArray['method'])) { return $this->resp(null, $id, -32600, 'No method'); } //Do we have params? if (!isset($jsonArray['params'])) { $data = array(); } else { $data = $jsonArray['params']; } //Check method signature $method = explode(':', trim($jsonArray['method'])); if (count($method) != 2) { return $this->resp(null, $id, -32600, 'Invalid method signature. Use: BEAN:ACTION'); } //Collect Bean and Action $beanType = $method[0]; $action = $method[1]; //May not contain anything other than ALPHA NUMERIC chars and _ if (preg_match('/\\W/', $beanType)) { return $this->resp(null, $id, -32600, 'Invalid Bean Type String'); } if (preg_match('/\\W/', $action)) { return $this->resp(null, $id, -32600, 'Invalid Action String'); } try { switch ($action) { case 'store': if (!isset($data[0])) { return $this->resp(null, $id, -32602, 'First param needs to be Bean Object'); } $data = $data[0]; if (!isset($data['id'])) { $bean = RedBean_Facade::dispense($beanType); } else { $bean = RedBean_Facade::load($beanType, $data['id']); } $bean->import($data); $rid = RedBean_Facade::store($bean); return $this->resp($rid, $id); case 'load': if (!isset($data[0])) { return $this->resp(null, $id, -32602, 'First param needs to be Bean ID'); } $bean = RedBean_Facade::load($beanType, $data[0]); return $this->resp($bean->export(), $id); case 'trash': if (!isset($data[0])) { return $this->resp(null, $id, -32602, 'First param needs to be Bean ID'); } $bean = RedBean_Facade::load($beanType, $data[0]); RedBean_Facade::trash($bean); return $this->resp('OK', $id); default: $modelName = $this->modelHelper->getModelName($beanType); if (!class_exists($modelName)) { return $this->resp(null, $id, -32601, 'No such bean in the can!'); } $beanModel = new $modelName(); if (!method_exists($beanModel, $action)) { return $this->resp(null, $id, -32601, "Method not found in Bean: {$beanType} "); } return $this->resp(call_user_func_array(array($beanModel, $action), $data), $id); } } catch (Exception $exception) { return $this->resp(null, $id, -32099, $exception->getCode() . '-' . $exception->getMessage()); } }
/** * Instantiates a model formatter with the right namespace and registers it to RedBean * * @return void */ protected function setModelNamespace($namespace) { \RedBean_ModelHelper::setModelFormatter(new ModelFormatter($namespace)); }
public static function setModelFormatter(RedBean_IModelFormatter $modelFormatter) { self::$modelFormatter = $modelFormatter; }
public static function setModelFormatter($modelFormatter) { self::$modelFormatter = $modelFormatter; }
/** * Configures the facade, want to have a new Writer? A new Object Database or a new * Adapter and you want it on-the-fly? Use this method to hot-swap your facade with a new * toolbox. * * @param RedBean_ToolBox $tb toolbox * * @return RedBean_ToolBox */ public function configureFacadeWithToolbox(RedBean_ToolBox $tb) { $oldTools = $this->toolbox; $this->toolbox = $tb; $this->writer = $this->toolbox->getWriter(); $this->adapter = $this->toolbox->getDatabaseAdapter(); $this->redbean = $this->toolbox->getRedBean(); $this->finder = new RedBean_Finder($this->toolbox); $this->associationManager = new RedBean_AssociationManager($this->toolbox); $this->redbean->setAssociationManager($this->associationManager); $this->labelMaker = new RedBean_LabelMaker($this->toolbox); $this->extAssocManager = new RedBean_AssociationManager_ExtAssociationManager($this->toolbox); $helper = new RedBean_ModelHelper(); $helper->attachEventListeners($this->redbean); $this->associationManager->addEventListener('delete', $helper); $this->duplicationManager = new RedBean_DuplicationManager($this->toolbox); $this->tagManager = new RedBean_TagManager($this->toolbox); $this->f = new RedBean_SQLHelper($this->adapter); return $oldTools; }
/** * Reroutes a call to Model if exists. (new fuse) * @param string $method * @param array $args * @return mixed $mixed */ public function __call($method, $args) { if (!isset($this->__info["model"])) { //@todo eliminate this dependency! $modelName = RedBean_ModelHelper::getModelName($this->getMeta("type")); if (!class_exists($modelName)) { return null; } $obj = new $modelName(); $obj->loadBean($this); $this->__info["model"] = $obj; } if (!method_exists($this->__info["model"], $method)) { return null; } return call_user_func_array(array($this->__info["model"], $method), $args); }
/** * Configures the facade, want to have a new Writer? A new Object Database or a new * Adapter and you want it on-the-fly? Use this method to hot-swap your facade with a new * toolbox. * * @param RedBean_ToolBox $tb toolbox * * @return RedBean_ToolBox */ public static function configureFacadeWithToolbox(RedBean_ToolBox $tb) { $oldTools = self::$toolbox; self::$toolbox = $tb; self::$writer = self::$toolbox->getWriter(); self::$adapter = self::$toolbox->getDatabaseAdapter(); self::$redbean = self::$toolbox->getRedBean(); self::$finder = new RedBean_Finder(self::$toolbox); self::$associationManager = new RedBean_AssociationManager(self::$toolbox); self::$redbean->setAssociationManager(self::$associationManager); self::$labelMaker = new RedBean_LabelMaker(self::$toolbox); self::$extAssocManager = new RedBean_AssociationManager_ExtAssociationManager(self::$toolbox); $helper = new RedBean_ModelHelper(); $helper->attachEventListeners(self::$redbean); self::$associationManager->addEventListener('delete', $helper); self::$duplicationManager = new RedBean_DuplicationManager(self::$toolbox); self::$tagManager = new RedBean_TagManager(self::$toolbox); self::$f = new RedBean_SQLHelper(self::$adapter); return $oldTools; }
/** * Processes a JSON object request. * * @param array $jsonObject * @return mixed $result */ public function handleJSONRequest($jsonString) { $jsonArray = json_decode($jsonString, true); if (!$jsonArray) { return $this->resp(null, null, -32700, "Cannot Parse JSON"); } if (!isset($jsonArray["jsonrpc"])) { return $this->resp(null, null, -32600, "No RPC version"); } if ($jsonArray["jsonrpc"] != "2.0") { return $this->resp(null, null, -32600, "Incompatible RPC Version"); } if (!isset($jsonArray["id"])) { return $this->resp(null, null, -32600, "No ID"); } $id = $jsonArray["id"]; if (!isset($jsonArray["method"])) { return $this->resp(null, $id, -32600, "No method"); } if (!isset($jsonArray["params"])) { $data = array(); } else { $data = $jsonArray["params"]; } $method = explode(":", trim($jsonArray["method"])); if (count($method) != 2) { return $this->resp(null, $id, -32600, "Invalid method signature. Use: BEAN:ACTION"); } $beanType = $method[0]; $action = $method[1]; if (preg_match("/\\W/", $beanType)) { return $this->resp(null, $id, -32600, "Invalid Bean Type String"); } if (preg_match("/\\W/", $action)) { return $this->resp(null, $id, -32600, "Invalid Action String"); } try { switch ($action) { case "store": if (!isset($data[0])) { return $this->resp(null, $id, -32602, "First param needs to be Bean Object"); } $data = $data[0]; if (!isset($data["id"])) { $bean = R::dispense($beanType); } else { $bean = R::load($beanType, $data["id"]); } $bean->import($data); $rid = R::store($bean); return $this->resp($rid, $id); break; case "load": if (!isset($data[0])) { return $this->resp(null, $id, -32602, "First param needs to be Bean ID"); } $bean = R::load($beanType, $data[0]); return $this->resp($bean->export(), $id); break; case "trash": if (!isset($data[0])) { return $this->resp(null, $id, -32602, "First param needs to be Bean ID"); } $bean = R::load($beanType, $data[0]); R::trash($bean); return $this->resp("OK", $id); break; default: $modelName = $this->modelHelper->getModelName($beanType); if (!class_exists($modelName)) { return $this->resp(null, $id, -32601, "No such bean in the can!"); } $beanModel = new $modelName(); if (!method_exists($beanModel, $action)) { return $this->resp(null, $id, -32601, "Method not found in Bean: {$beanType} "); } return $this->resp(call_user_func_array(array($beanModel, $action), $data), $id); } } catch (Exception $exception) { return $this->resp(null, $id, -32099, $exception->getCode() . "-" . $exception->getMessage()); } }
/** * Sets the model formatter to be used to discover a model * for Fuse. * * @param string $modelFormatter * * @return void */ public function setModelFormatter($modelFormatter) { $this->helper->setModelFormatter($modelFormatter); }
/** * Test graph() method. * * @return void */ public function testGraph() { RedBean_Plugin_Cooker::enableBeanLoading(TRUE); R::dependencies(array()); $currentDriver = $this->currentlyActiveDriverID; global $lifeCycle; $toolbox = R::$toolbox; $adapter = $toolbox->getDatabaseAdapter(); $writer = $toolbox->getWriter(); $redbean = $toolbox->getRedBean(); try { R::graph(array(array(array('a' => 'b')))); fail(); } catch (RedBean_Exception_Security $e) { pass(); } try { R::graph('ABC'); fail(); } catch (RedBean_Exception_Security $e) { pass(); } try { R::graph(123); fail(); } catch (RedBean_Exception_Security $e) { pass(); } try { R::graph(array(new stdClass())); fail(); } catch (RedBean_Exception_Security $e) { pass(); } list($v1, $v2, $v3) = R::dispense('village', 3); list($b1, $b2, $b3, $b4, $b5, $b6) = R::dispense('building', 6); list($f1, $f2, $f3, $f4, $f5, $f6) = R::dispense('farmer', 6); list($u1, $u2, $u3, $u4, $u5, $u6) = R::dispense('furniture', 6); list($a1, $a2) = R::dispense('army', 2); $a1->strength = 100; $a2->strength = 200; $v1->name = 'v1'; $v2->name = 'v2'; $v3->name = 'v3'; $v1->ownBuilding = array($b4, $b6); $v2->ownBuilding = array($b1); $v3->ownBuilding = array($b5); $b1->ownFarmer = array($f1, $f2); $b6->ownFarmer = array($f3); $b5->ownFarmer = array($f4); $b5->ownFurniture = array($u6, $u5, $u4); $v2->sharedArmy[] = $a2; $v3->sharedArmy = array($a2, $a1); $i2 = R::store($v2); $i1 = R::store($v1); $i3 = R::store($v3); $v1 = R::load('village', $i1); $v2 = R::load('village', $i2); $v3 = R::load('village', $i3); asrt(count($v3->ownBuilding), 1); asrt(count(reset($v3->ownBuilding)->ownFarmer), 1); asrt(count(reset($v3->ownBuilding)->ownFurniture), 3); asrt(count($v3->sharedArmy), 2); asrt(count($v1->sharedArmy), 0); asrt(count($v2->sharedArmy), 1); asrt(count($v2->ownBuilding), 1); asrt(count($v1->ownBuilding), 2); asrt(count(reset($v1->ownBuilding)->ownFarmer), 0); asrt(count(end($v1->ownBuilding)->ownFarmer), 1); asrt(count($v3->ownTapestry), 0); // Change the names and add the same building should not change the graph $v1->name = 'village I'; $v2->name = 'village II'; $v3->name = 'village III'; $v1->ownBuilding[] = $b4; $i2 = R::store($v2); $i1 = R::store($v1); $i3 = R::store($v3); $v1 = R::load('village', $i1); $v2 = R::load('village', $i2); $v3 = R::load('village', $i3); asrt(count($v3->ownBuilding), 1); asrt(count(reset($v3->ownBuilding)->ownFarmer), 1); asrt(count(reset($v3->ownBuilding)->ownFurniture), 3); asrt(count($v3->sharedArmy), 2); asrt(count($v1->sharedArmy), 0); asrt(count($v2->sharedArmy), 1); asrt(count($v2->ownBuilding), 1); asrt(count($v1->ownBuilding), 2); asrt(count(reset($v1->ownBuilding)->ownFarmer), 0); asrt(count(end($v1->ownBuilding)->ownFarmer), 1); asrt(count($v3->ownTapestry), 0); $b = reset($v1->ownBuilding); R::trash($b); $n = R::count('village'); asrt($n, 3); $n = R::count('army'); R::trash($v1); asrt(R::count('army'), $n); R::trash($v2); asrt(R::count('army'), $n); R::trash($v3); asrt(R::count('army'), $n); $json = '{"mysongs": { "type": "playlist", "name": "JazzList", "ownTrack": [ { "type": "track", "name": "harlem nocturne", "order": "1", "sharedSong": [ { "type": "song", "url": "music.com.harlem" } ], "cover": { "type": "cover", "url": "albumart.com\\/duke1" } }, { "type": "track", "name": "brazil", "order": "2", "sharedSong": [ { "type": "song", "url": "music.com\\/djan" } ], "cover": { "type": "cover", "url": "picasa\\/django" } } ] }}'; $playList = json_decode($json, TRUE); $playList = R::graph($playList); $id = R::store(reset($playList)); $play = R::load("playlist", $id); asrt(count($play->ownTrack), 2); foreach ($play->ownTrack as $track) { asrt(count($track->sharedSong), 1); asrt($track->cover instanceof RedBean_OODBBean, TRUE); } $json = '{"mysongs": { "type": "playlist", "id": "1", "ownTrack": [ { "type": "track", "name": "harlem nocturne", "order": "1", "sharedSong": [ { "type": "song", "id": "1" } ], "cover": { "type": "cover", "id": "2" } }, { "type": "track", "name": "brazil", "order": "2", "sharedSong": [ { "type": "song", "url": "music.com\\/djan" } ], "cover": { "type": "cover", "url": "picasa\\/django" } } ] }}'; $playList = json_decode($json, TRUE); $cooker = new RedBean_Plugin_Cooker(); $cooker->setToolbox(R::$toolbox); $playList = $cooker->graph($playList); $id = R::store(reset($playList)); $play = R::load("playlist", $id); asrt(count($play->ownTrack), 2); foreach ($play->ownTrack as $track) { asrt(count($track->sharedSong), 1); asrt($track->cover instanceof RedBean_OODBBean, TRUE); } $track = reset($play->ownTrack); $song = reset($track->sharedSong); asrt(intval($song->id), 1); asrt($song->url, "music.com.harlem"); $json = '{"mysongs": { "type": "playlist", "id": "1", "ownTrack": [ { "type": "track", "name": "harlem nocturne", "order": "1", "sharedSong": [ { "type": "song", "id": "1", "url": "changedurl" } ], "cover": { "type": "cover", "id": "2" } }, { "type": "track", "name": "brazil", "order": "2", "sharedSong": [ { "type": "song", "url": "music.com\\/djan" } ], "cover": { "type": "cover", "url": "picasa\\/django" } } ] }}'; $playList = json_decode($json, TRUE); $cooker = new RedBean_Plugin_Cooker(); $cooker->setToolbox(R::$toolbox); $playList = $cooker->graph($playList); $id = R::store(reset($playList)); $play = R::load("playlist", $id); asrt(count($play->ownTrack), 2); foreach ($play->ownTrack as $track) { asrt(count($track->sharedSong), 1); asrt($track->cover instanceof RedBean_OODBBean, TRUE); } $track = reset($play->ownTrack); $song = reset($track->sharedSong); asrt(intval($song->id), 1); asrt($song->url, "changedurl"); // Tree $page = R::dispense('page'); $page->name = 'root of all evil'; list($subPage, $subSubPage, $subNeighbour, $subOfSubNeighbour, $subSister) = R::dispense('page', 5); $subPage->name = 'subPage'; $subSubPage->name = 'subSubPage'; $subOfSubNeighbour->name = 'subOfSubNeighbour'; $subNeighbour->name = 'subNeighbour'; $subSister->name = 'subSister'; $page->ownPage = array($subPage, $subNeighbour, $subSister); R::store($page); asrt(count($page->ownPage), 3); foreach ($page->ownPage as $p) { if ($p->name == 'subPage') { $p->ownPage[] = $subSubPage; } if ($p->name == 'subNeighbour') { $p->ownPage[] = $subOfSubNeighbour; } } R::store($page); asrt(count($page->ownPage), 3); list($first, $second) = array_keys($page->ownPage); foreach ($page->ownPage as $p) { if ($p->name == 'subPage' || $p->name == 'subNeighbour') { asrt(count($p->ownPage), 1); } else { asrt(count($p->ownPage), 0); } } R::nuke(); $canes = candy_canes(); $id = R::store($canes[0]); $cane = R::load('cane', $id); asrt($cane->label, 'Cane No. 0'); asrt($cane->cane->label, 'Cane No. 1'); asrt($cane->cane->cane->label, 'Cane No. 4'); asrt($cane->cane->cane->cane->label, 'Cane No. 7'); asrt($cane->cane->cane->cane->cane, NULL); // Test backward compatibility asrt($page->owner, NULL); RedBean_ModelHelper::setModelFormatter(NULL); $band = R::dispense('band'); $musicians = R::dispense('bandmember', 5); $band->ownBandmember = $musicians; try { R::store($band); fail(); } catch (Exception $e) { pass(); } $band = R::dispense('band'); $musicians = R::dispense('bandmember', 4); $band->ownBandmember = $musicians; try { $id = R::store($band); pass(); } catch (Exception $e) { fail(); } $band = R::load('band', $id); $band->ownBandmember[] = R::dispense('bandmember'); try { R::store($band); fail(); } catch (Exception $e) { pass(); } // Test fuse $lifeCycle = ""; $bandmember = R::dispense('bandmember'); $bandmember->name = 'Fatz Waller'; $id = R::store($bandmember); $bandmember = R::load('bandmember', $id); R::trash($bandmember); $expected = 'calleddispenseid0calledupdateid0nameFatzWallercalledafter_updateid5nameFatzWallercalleddispenseid0calledopen5calleddeleteid5band_idnullnameFatzWallercalledafter_deleteid0band_idnullnameFatzWaller'; $lifeCycle = preg_replace("/\\W/", "", $lifeCycle); asrt($lifeCycle, $expected); // Test whether a nested bean will be saved if tainted R::nuke(); $page = R::dispense('page'); $page->title = 'a blank page'; $book = R::dispense('book'); $book->title = 'shiny white pages'; $book->ownPage[] = $page; $id = R::store($book); $book = R::load('book', $id); $page = reset($book->ownPage); asrt($page->title, 'a blank page'); $page->title = 'slightly different white'; R::store($book); $book = R::load('book', $id); $page = reset($book->ownPage); asrt($page->title, 'slightly different white'); $page = R::dispense('page'); $page->title = 'x'; $book = R::load('book', $id); $book->title = 'snow white pages'; $page->book = $book; $pid = R::store($page); $page = R::load('page', $pid); asrt($page->book->title, 'snow white pages'); // Test you cannot unset a relation list asrt(count($book->ownPage), 2); unset($book->ownPage); $book = R::load('book', R::store($book)); asrt(count($book->ownPage), 2); $book->sharedTree = R::dispense('tree'); R::store($book); $c = R::count('page'); asrt(R::count('tree'), 1); R::trash($book); asrt(R::count('page'), $c); asrt(R::count('tree'), 1); R::nuke(); $v = R::dispense('village'); list($b1, $b2) = R::dispense('building', 2); $b1->name = 'a'; $b2->name = 'b'; $b2->village = $v; $v->ownBuilding[] = $b1; $b1->ownFurniture[] = R::dispense('furniture'); $id = R::store($b2); $b2 = R::load('building', $id); asrt(count($b2->village->ownBuilding), 2); $buildings = $b2->village->ownBuilding; foreach ($buildings as $b) { if ($b->id != $id) { asrt(count($b->ownFurniture), 1); } } // Save a form using graph and ignore empty beans R::nuke(); $product = R::dispense('product'); $product->name = 'shampoo'; $productID = R::store($product); $coupon = R::dispense('coupon'); $coupon->name = '567'; $couponID = R::store($coupon); $form = array('type' => 'order', 'ownProduct' => array(array('id' => $productID, 'type' => 'product')), 'ownCustomer' => array(array('type' => 'customer', 'name' => 'Bill'), array('type' => 'customer', 'name' => '')), 'sharedCoupon' => array(array('type' => 'coupon', 'name' => '123'), array('type' => 'coupon', 'id' => $couponID))); $order = R::graph($form, TRUE); asrt($order->getMeta('type'), 'order'); asrt(count($order->ownProduct), 1); asrt(count($order->ownCustomer), 1); asrt(count($order->sharedCoupon), 2); asrt(end($order->ownProduct)->id, $productID); asrt(end($order->ownProduct)->name, 'shampoo'); asrt(end($order->ownCustomer)->name, 'Bill'); asrt($order->sharedCoupon[$couponID]->name, '567'); R::nuke(); $form = array('type' => 'person', 'name' => 'Fred', 'phone' => ''); $bean = R::graph($form); asrt($bean->name, 'Fred'); asrt($bean->phone, ''); $cooker = new RedBean_Plugin_Cooker(); $cooker->setUseNullFlag(TRUE); $form = array('type' => 'person', 'name' => 'Fred', 'phone' => ''); $bean = R::graph($form); asrt($bean->name, 'Fred'); asrt($bean->phone, NULL); RedBean_Plugin_Cooker::setUseNullFlagSt(FALSE); // Save a form using graph and ignore empty beans, wrong nesting R::nuke(); $product = R::dispense('product'); $product->name = 'shampoo'; $productID = R::store($product); $coupon = R::dispense('coupon'); $coupon->name = '567'; $couponID = R::store($coupon); $form = array('type' => 'order', 'ownProduct' => array(array(array('id' => $productID, 'type' => 'product')))); try { $order = R::graph($form, TRUE); fail(); } catch (RedBean_Exception_Security $e) { pass(); } // Without ignore empty beans R::nuke(); $product = R::dispense('product'); $product->name = 'shampoo'; $productID = R::store($product); $coupon = R::dispense('coupon'); $coupon->name = '567'; $couponID = R::store($coupon); $form = array('type' => 'order', 'ownProduct' => array(array('id' => $productID, 'type' => 'product')), 'ownCustomer' => array(array('type' => 'customer', 'name' => 'Bill'), array('type' => 'customer', 'name' => '')), 'sharedCoupon' => array(array('type' => 'coupon', 'name' => '123'), array('type' => 'coupon', 'id' => $couponID))); RedBean_Plugin_Cooker::enableBeanLoading(FALSE); $exc = FALSE; try { $order = R::graph($form); fail(); } catch (Exception $e) { $exc = $e; } asrt($exc instanceof RedBean_Exception_Security, TRUE); RedBean_Plugin_Cooker::enableBeanLoading(TRUE); $order = R::graph($form); asrt($order->getMeta('type'), 'order'); asrt(count($order->ownProduct), 1); asrt(count($order->ownCustomer), 2); asrt(count($order->sharedCoupon), 2); asrt(end($order->ownProduct)->id, $productID); // Make sure zeros are preserved $form = array('type' => 'laptop', 'price' => 0); $product = R::graph($form); asrt(isset($product->price), TRUE); asrt($product->price, 0); }
testpack("test model formatting"); class mymodelformatter implements RedBean_IModelFormatter { public function formatModel($model) { return "my_weird_" . $model . "_model"; } } class my_weird_weirdo_model extends RedBean_SimpleModel { public function blah() { return "yes!"; } } RedBean_ModelHelper::setModelFormatter(new mymodelformatter()); $w = R::dispense("weirdo"); asrt($w->blah(), "yes!"); //R::debug(1); testpack("Test Tagging"); R::tag($post, "lousy,smart"); R::$flagUseLegacyTaggingAPI = true; asrt(R::tag($post), "lousy,smart"); R::tag($post, "clever,smart"); asrt(R::tag($post), "smart,clever"); R::tag($blog, array("smart", "interesting")); asrt(R::tag($blog), "smart,interesting"); try { R::tag($blog, array("smart", "interesting", "lousy!")); pass(); } catch (RedBean_Exception $e) {
/** * Prepare test pack (mostly: nuke the entire database) */ public function prepare() { R::freeze(FALSE); RedBean_ModelHelper::setModelFormatter(new DefaultModelFormatter()); R::nuke(); }