/** * 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); }
/** * Tests the handling of trashed beans in frozen mode. * Are the lists unset etc? * * @return void */ public function testTrash() { R::nuke(); $book = R::dispense('book'); $book->xownPageList[] = R::dispense('page'); $book->sharedTagList[] = R::dispense('tag'); R::store($book); $book = $book->fresh(); R::freeze(TRUE); $book->xownPageList = array(); R::store($book); $book = $book->fresh(); asrt(R::count('page'), 0); $book->xownPageList[] = R::dispense('page'); R::store($book); $book = $book->fresh(); asrt(R::count('page'), 1); $book->xownPageList; $book->sharedTagList; R::trash($book); asrt(R::count('book'), 0); asrt(R::count('page'), 0); asrt(R::count('tag'), 1); asrt(R::count('book_tag'), 0); R::freeze(FALSE); }
/** * Test whether we can use foreign keys with keywords. * * @return void */ public function testKWConflicts() { R::nuke(); $metrics = R::dispense('metrics'); $constraint = R::dispense('constraint'); $constraint->xownMetrics[] = $metrics; R::store($constraint); asrt(1, R::count('metrics')); R::trash($constraint); asrt(0, R::count('metrics')); }
/** * Test for issue90. * Checking 'own' relationship, makes it impossible to trash a bean. * * @return void */ public function testIssue90() { $s = R::dispense('box'); $s->name = 'a'; $f = R::dispense('bottle'); $s->ownBottle[] = $f; R::store($s); $s2 = R::dispense('box'); $s2->name = 'a'; R::store($s2); R::trash($s2); pass(); }
public function create(Request $request, $model) { $model_name = $model; $record = R::dispense($model_name); $id = R::store($record); $record = R::load($model, $id); $columns = $record->export(); R::trash($record); $uri = $request->path(); echo "<pre>\n"; echo "Create Uri: {$uri}\n" . "Model: {$model}\n"; var_dump($columns); echo "</pre>"; }
/** * * @param int $itemID * @return int */ public function deletesystemlogs($itemID, $userid = NULL) { try { $rs = R::load($this->tbname, $itemID); if ($rs->id) { $row = R::trash($rs); return $itemID; } else { throw new Exception('No Data for Delete'); } } catch (Exception $e) { throw new Exception($e->getMessage()); } }
/** * In the past it was not possible to export beans * like 'feed' (Model_Feed). * * @return void */ public function testExportIssue() { R::nuke(); $feed = R::dispense('feed'); $feed->post = array('first', 'second'); R::store($feed); $rows = R::getAll('SELECT * FROM feed'); asrt($rows[0]['post'], '["first","second"]'); $feed = $feed->fresh(); asrt(is_array($feed->post), TRUE); asrt($feed->post[0], 'first'); asrt($feed->post[1], 'second'); R::store($feed); $rows = R::getAll('SELECT * FROM feed'); asrt($rows[0]['post'], '["first","second"]'); $feed = R::load('feed', $feed->id); $feed->post[] = 'third'; R::store($feed); $rows = R::getAll('SELECT * FROM feed'); asrt($rows[0]['post'], '["first","second","third"]'); $feed = $feed->fresh(); asrt(is_array($feed->post), TRUE); asrt($feed->post[0], 'first'); asrt($feed->post[1], 'second'); asrt($feed->post[2], 'third'); //now the catch: can we use export? //PHP Fatal error: Call to a member function export() on a non-object $feeds = R::exportAll(R::find('feed')); asrt(is_array($feeds), TRUE); $feed = reset($feeds); asrt($feed['post'][0], 'first'); asrt($feed['post'][1], 'second'); asrt($feed['post'][2], 'third'); //can we also dup()? $feedOne = R::findOne('feed'); R::store(R::dup($feedOne)); asrt(R::count('feed'), 2); //can we delete? R::trash($feedOne); asrt(R::count('feed'), 1); $feedTwo = R::findOne('feed'); $feed = $feedTwo->export(); asrt($feed['post'][0], 'first'); asrt($feed['post'][1], 'second'); asrt($feed['post'][2], 'third'); }
/** * Test if RedBeanPHP can properly handle keywords. * * @return void */ public function testKeywords() { $keywords = array('anokeyword', 'znokeyword', 'group', 'drop', 'inner', 'join', 'select', 'table', 'int', 'cascade', 'float', 'call', 'in', 'status', 'order', 'limit', 'having', 'else', 'if', 'while', 'distinct', 'like'); foreach ($keywords as $k) { R::nuke(); $bean = R::dispense($k); $bean->{$k} = $k; $id = R::store($bean); $bean = R::load($k, $id); $bean2 = R::dispense('other'); $bean2->name = $k; $bean->bean = $bean2; $bean->ownBean[] = $bean2; $bean->sharedBean[] = $bean2; $id = R::store($bean); R::trash($bean); pass(); } }
/** * Test boxing beans. * * @return void */ public function testBoxing() { R::nuke(); $bean = R::dispense('boxedbean')->box(); R::trash($bean); pass(); $bean = R::dispense('boxedbean'); $bean->sharedBoxbean = R::dispense('boxedbean')->box(); R::store($bean); pass(); $bean = R::dispense('boxedbean'); $bean->ownBoxedbean = R::dispense('boxedbean')->box(); R::store($bean); pass(); $bean = R::dispense('boxedbean'); $bean->other = R::dispense('boxedbean')->box(); R::store($bean); pass(); $bean = R::dispense('boxedbean'); $bean->title = 'MyBean'; $box = $bean->box(); asrt($box instanceof \Model_Boxedbean, TRUE); R::store($box); }
/** * Test basic CRUD operations. */ public function testBasicOperations() { //Can we dispense a naughty bean? (with underscore) $author = R::xdispense(AUTHOR); asrt($author instanceof OODBBean, TRUE); asrt($author->getMeta('type'), AUTHOR); $author->name = 'Mr. Quill'; $book = R::xdispense(BOOK); asrt($book instanceof OODBBean, TRUE); asrt($book->getMeta('type'), BOOK); $book->title = 'Good Stories'; $friend = R::xdispense(FRIEND); $friend->name = 'Muse'; asrt($friend instanceof OODBBean, TRUE); asrt($friend->getMeta('type'), FRIEND); $publisher = R::xdispense(PUBLISHER); $publisher->name = 'Good Books'; asrt($publisher instanceof OODBBean, TRUE); asrt($publisher->getMeta('type'), PUBLISHER); asrt(is_array($author->{BOOKLIST}), TRUE); //add books to the book list using the constant $author->{BOOKLIST}[] = $book; asrt(count($author->{BOOKLIST}), 1); //can we also add friends? (N-M) $author->{FRIENDLIST}[] = $friend; $author->{PUBLISHER} = $publisher; $id = R::store($author); asrt($id > 0, TRUE); $author = $author->fresh(); //Can we add another friend after reload? $author->{FRIENDLIST}[] = R::xdispense(FRIEND)->setAttr('name', 'buddy'); R::store($author); $author = $author->fresh(); //Now check the contents of the bean, its lists (books,friends) and parent (publisher) asrt($author->name, 'Mr. Quill'); asrt(count($author->{BOOKLIST}), 1); $firstBook = reset($author->{BOOKLIST}); asrt($firstBook->title, 'Good Stories'); asrt(count($author->{FRIENDLIST}), 2); $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); //Can we duplicate? $copy = R::dup($author); $copy->name = 'Mr. Clone'; R::store($copy); $copy = $copy->fresh(); asrt($copy->name, 'Mr. Clone'); asrt(count($copy->{BOOKLIST}), 1); $firstBook = reset($copy->{BOOKLIST}); asrt($firstBook->title, 'Good Stories'); asrt(count($copy->{FRIENDLIST}), 2); $firstFriend = reset($copy->{FRIENDLIST}); $parent = $copy->{PUBLISHER}; asrt($parent instanceof OODBBean, TRUE); //Can we count? asrt(R::count(AUTHOR), 2); $copy = $copy->fresh(); asrt($copy->countOwn(BOOK), 1); asrt($copy->countShared(FRIEND), 2); //Can we delete? R::trash($author); asrt(R::count(AUTHOR), 1); //Can we nuke? R::nuke(); asrt(R::count(AUTHOR), 0); asrt(count(R::inspect()), 0); }
/** * Destroy */ public function destroy(Request $request, $id) { $item = R::load('item', $id); R::trash($item); }
/** * Test ENUM functionality offered by Label Maker. * * @return void */ public function testENUM() { testpack('test ENUM'); $coffee = R::dispense('coffee'); $coffee->taste = R::enum('flavour:mocca'); //did we create an enum? asrt(implode('', R::gatherLabels(R::enum('flavour'))), 'MOCCA'); R::store($coffee); $coffee = $coffee->fresh(); //test enum identity check - with alias asrt($coffee->fetchAs('flavour')->taste->equals(R::enum('flavour:mocca')), TRUE); asrt($coffee->fetchAs('flavour')->taste->equals(R::enum('flavour:banana')), FALSE); //now we have two flavours asrt(R::count('flavour'), 2); asrt(implode(',', R::gatherLabels(R::enum('flavour'))), 'BANANA,MOCCA'); $coffee->flavour = R::enum('flavour:mocca'); R::store($coffee); //same results, can we have multiple flavours? asrt($coffee->fetchAs('flavour')->taste->equals(R::enum('flavour:mocca')), TRUE); asrt($coffee->fetchAs('flavour')->taste->equals(R::enum('flavour:banana')), FALSE); asrt($coffee->flavour->equals(R::enum('flavour:mocca')), TRUE); //no additional mocca enum... asrt(R::count('flavour'), 2); $drink = R::dispense('drink'); $drink->flavour = R::enum('flavour:choco'); R::store($drink); //now we have three! asrt(R::count('flavour'), 3); $drink = R::load('drink', $drink->id); asrt($drink->flavour->equals(R::enum('flavour:mint')), FALSE); asrt($drink->flavour->equals(R::enum('flavour:choco')), TRUE); asrt(R::count('flavour'), 4); //trash should not affect flavour! R::trash($drink); asrt(R::count('flavour'), 4); }
/** * Tests dependencies (variation). * * @return void */ public function testDependency4() { R::nuke(); $can = $this->createBeanInCan(TRUE); R::store($can); R::trash($can); $can = $this->createCanForBean(); asrt(R::count('bean'), 1); R::trash($can); asrt(R::count('bean'), 0); $can = $this->createBeanInCan(TRUE); R::store($can); R::trash($can); $can = $this->createCanForBean(); asrt(R::count('bean'), 1); R::trash($can); asrt(R::count('bean'), 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")); }
public function testUploadFile() { $this->migrate(); $this->login(); // Preparation: Create an image entry. $image = $this->app->createModel('image', array('position' => 0)); R::store($image); $this->assertNotEmpty($image->getID()); // upload to aws s3 $filename = 'original.jpg'; $filepath = realpath(__DIR__ . '/' . $filename); $_SERVER['REQUEST_METHOD'] = "POST"; $params = array('image_id' => $image->getID(), 'data' => '{ "id": ' . $image->getID() . ', "type": "uploadOnce", "settingName": "multiple_many_types" }', 'settingName' => 'multiple_many_types', 'uploadOnce' => 1); $size = filesize($filepath); $_FILES = array('file-0' => array('name' => $filename, 'type' => 'image/jpeg', 'tmp_name' => $filepath, 'error' => 0, 'size' => $size)); ob_start(); $this->app->runControllerFromRawUrl('admin/cRUDImages/uploadImage', $params); $output = ob_get_clean(); echo "\nOutput of uploadImage:\n"; echo $output; $output_r = json_decode($output); print_r($output_r); $this->assertFalse(property_exists($output_r[0], 'error')); // See if file uploaded successfully. echo "\nFind image with id " . $image->getID(); $imageBean = R::findOne('image', 'id = ?', array($image->getID())); $this->assertNotEmpty($imageBean); echo "\n...Found!"; $data = json_decode($imageBean['multiple_many_types']); echo "\nUploaded images:\n"; print_r($data); $this->assertEquals(1, count($data)); // Uploaded data must contain 'images/Image/1/original-smartphone' $this->assertNotEquals(-1, strpos(SkullyAwsS3\Helpers\S3Helpers::key($this->app->config('publicDir'), $data[0]->smartphone), 'public/images/Image/' . $image->getID() . '/original-smartphone')); // Now see if file does exist in Amazon S3 repository. $amazonS3Config = $this->app->config('amazonS3'); $client = \Aws\S3\S3Client::factory($amazonS3Config['settings']); $result = $client->getObject(array('Bucket' => $amazonS3Config['bucket'], 'Key' => SkullyAwsS3\Helpers\S3Helpers::key($this->app->config('publicDir'), $data[0]->smartphone))); $this->assertNotEmpty($result['Body']); // $this->app->getLogger()->log("result get object : " . print_r($result, true)); // TEST ACL $resultAcl = $client->getObjectAcl(array('Bucket' => $amazonS3Config['bucket'], 'Key' => SkullyAwsS3\Helpers\S3Helpers::key($this->app->config('publicDir'), $data[0]->smartphone))); // $this->app->getLogger()->log("result get object ACL : " . print_r($resultAcl, true)); $this->assertNotEmpty($resultAcl["Grants"]); $dataAcl = array(); foreach (\Aws\S3\Model\Acp::fromArray($resultAcl->toArray()) as $grant) { $grantee = $grant->getGrantee(); $dataAcl[$grantee->getGroupUri()] = array($grantee->getType(), $grant->getPermission()); } $this->assertEquals(2, count($dataAcl)); $this->assertArrayHasKey('http://acs.amazonaws.com/groups/global/AllUsers', $dataAcl); $this->assertEquals(array('Group', 'READ'), $dataAcl['http://acs.amazonaws.com/groups/global/AllUsers']); // ---- end of test ACL ---- // Local file must have been deleted. $filepath = \Skully\App\Helpers\FileHelper::replaceSeparators($this->app->getTheme()->getBasePath() . $data[0]->smartphone); echo "\nChecking if file {$filepath} is deleted..."; $this->assertFalse(file_exists($filepath)); echo "\nYep"; // Delete Models try { R::trash($imageBean); } catch (\Exception $e) { echo 'failed to delete image bean. reason: ' . $e->getMessage(); } // // Cleanup by removing files in bucket. // $client->deleteObject(array( // 'Bucket' => $amazonS3Config['bucket'], // 'Key' => SkullyAwsS3\Helpers\S3Helpers::key($this->app->config('publicDir'), $data[0]->smartphone)) // ); // // $client->deleteObject(array( // 'Bucket' => $amazonS3Config['bucket'], // 'Key' => SkullyAwsS3\Helpers\S3Helpers::key($this->app->config('publicDir'), $data[0]->desktop)) // ); // Check existence of deleted files on s3 server $result = $client->doesObjectExist($amazonS3Config['bucket'], SkullyAwsS3\Helpers\S3Helpers::key($this->app->config('publicDir'), $data[0]->smartphone)); $this->assertFalse($result); $result = $client->doesObjectExist($amazonS3Config['bucket'], SkullyAwsS3\Helpers\S3Helpers::key($this->app->config('publicDir'), $data[0]->desktop)); $this->assertFalse($result); }
/** * @return void */ public function logout() { if ($this->getCookieData() != false) { $userLog = Facade::findOne('rememberme', 'hash = :hash', [':hash' => $this->getCookieData()['token']]); Facade::trash($userLog); } $this->unsetCookie(); session_destroy(); }
/** * Removes an user by his id * * @param integer $id id of user */ public static function removeById($id) { if ($user = R::findOne('user', ' id = ? ', [$id])) { R::trash($user); } }
/** * Test error handling options FUSE. */ public function testModelErrorHandling() { $test = R::dispense('feed'); $test->nonExistantMethod(); pass(); $old = R::setErrorHandlingFUSE(OODBBean::C_ERR_LOG); asrt(is_array($old), TRUE); asrt(count($old), 2); asrt($old[0], FALSE); asrt($old[1], NULL); $test->nonExistantMethod(); //we cant really test this... :( pass(); $old = R::setErrorHandlingFUSE(OODBBean::C_ERR_NOTICE); asrt(is_array($old), TRUE); asrt(count($old), 2); asrt($old[0], OODBBean::C_ERR_LOG); asrt($old[1], NULL); set_error_handler(function ($error, $str) { asrt($str, 'FUSE: method does not exist in model: nonExistantMethod'); }, E_USER_NOTICE); $test->nonExistantMethod(); restore_error_handler(); $old = OODBBean::setErrorHandlingFUSE(OODBBean::C_ERR_WARN); asrt(is_array($old), TRUE); asrt(count($old), 2); asrt($old[0], OODBBean::C_ERR_NOTICE); asrt($old[1], NULL); set_error_handler(function ($error, $str) { asrt($str, 'FUSE: method does not exist in model: nonExistantMethod'); }, E_USER_WARNING); $test->nonExistantMethod(); restore_error_handler(); $old = OODBBean::setErrorHandlingFUSE(OODBBean::C_ERR_FATAL); asrt(is_array($old), TRUE); asrt(count($old), 2); asrt($old[0], OODBBean::C_ERR_WARN); asrt($old[1], NULL); set_error_handler(function ($error, $str) { asrt($str, 'FUSE: method does not exist in model: nonExistantMethod'); }, E_USER_ERROR); $test->nonExistantMethod(); restore_error_handler(); $old = OODBBean::setErrorHandlingFUSE(OODBBean::C_ERR_EXCEPTION); asrt(is_array($old), TRUE); asrt(count($old), 2); asrt($old[0], OODBBean::C_ERR_FATAL); asrt($old[1], NULL); try { $test->nonExistantMethod(); fail(); } catch (\Exception $e) { pass(); } global $test_bean; $test_bean = $test; global $has_executed_error_func_fuse; $has_executed_error_func_fuse = FALSE; $old = OODBBean::setErrorHandlingFUSE(OODBBean::C_ERR_FUNC, function ($info) { global $has_executed_error_func_fuse; global $test_bean; $has_executed_error_func_fuse = TRUE; asrt(is_array($info), TRUE); asrt($info['method'], 'nonExistantMethod'); asrt(json_encode($info['bean']->export()), json_encode($test_bean->export())); asrt($info['message'], 'FUSE: method does not exist in model: nonExistantMethod'); }); asrt(is_array($old), TRUE); asrt(count($old), 2); asrt($old[0], OODBBean::C_ERR_EXCEPTION); asrt($old[1], NULL); $test->nonExistantMethod(); asrt($has_executed_error_func_fuse, TRUE); $old = OODBBean::setErrorHandlingFUSE(OODBBean::C_ERR_IGNORE); asrt(is_array($old), TRUE); asrt(count($old), 2); asrt($old[0], OODBBean::C_ERR_FUNC); asrt(is_callable($old[1]), TRUE); $old = OODBBean::setErrorHandlingFUSE(OODBBean::C_ERR_IGNORE); asrt(is_array($old), TRUE); asrt(count($old), 2); asrt($old[0], OODBBean::C_ERR_IGNORE); asrt($old[1], NULL); try { OODBBean::setErrorHandlingFUSE(900); fail(); } catch (\Exception $e) { pass(); asrt($e->getMessage(), 'Invalid error mode selected'); } try { OODBBean::setErrorHandlingFUSE(OODBBean::C_ERR_FUNC, 'hello'); fail(); } catch (\Exception $e) { pass(); asrt($e->getMessage(), 'Invalid error handler'); } OODBBean::setErrorHandlingFUSE(OODBBean::C_ERR_EXCEPTION); //make sure ignore FUSE events $test = R::dispense('feed'); R::store($test); $test = $test->fresh(); R::trash($test); pass(); OODBBean::setErrorHandlingFUSE(OODBBean::C_ERR_IGNORE); }
public function destroy() { $instance = $this->findInstance(true); // $instance = $this->setInstanceAttributes($instance); $error = true; if (!$instance->hasError()) { try { $this->app->getLogger()->log("trying to delete data.."); R::trash($instance); $error = false; $this->successAction($this->app->getTranslator()->translate('deleted'), $this->app->getRouter()->getUrl($this->indexPath), 'destroy', $instance); } catch (\Exception $e) { $message = $e->getMessage(); // When message is unrelated to instance validation, instance // has no error, but error message is not empty. if (!empty($message) && !$instance->hasError()) { $instance->addError($message); } $this->app->getLogger()->log("Cannot delete data : " . $e->getMessage()); } } if ($error) { $instance = $this->setupAssigns($instance); if ($this->destroyPath != null) { $this->app->getTemplateEngine()->assign(array('destroyPath' => $this->destroyPath)); } if ($this->app->isAjax()) { $this->displayInstanceErrors($instance, $this->instanceName, $this->deleteAjaxTpl); } else { $this->displayInstanceErrors($instance, $this->instanceName, $this->deleteNoAjaxTpl); } } }
/** * 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); }
/** * 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); }
/** * 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); }
/** * Test Ghost beans.... * * @return void */ public function testGhostBeans() { testpack('Testing ghost beans'); $bean = R::dispense('bean'); $bean->title = 'abc'; $id1 = R::store($bean); R::trash($bean); $bean = R::load('bean', $id1); asrt((int) $bean->id, 0); }
public static function deleteRBrelation($beans) { try { if (is_array($beans)) { foreach ($beans as $bean) { self::deleteRBrelation($bean); } } else { foreach ($beans as $key => $value) { if (is_array($value)) { self::deleteRBrelation($value); } } R::trash($beans); } } catch (Exception $e) { throw new Exception($e->getMessage()); } }
/** * Test don't try to store other things in shared list. * * @return void */ public function testDontTryToStoreOtherThingsInSharedList() { $book = R::dispense('book'); $book->sharedPage[] = 'nonsense'; try { R::store($book); fail(); } catch (RedException $exception) { pass(); } $book->sharedPageList = R::dispense('page', 2); R::store($book); $book->sharedPageList; R::trash($book); asrt(R::count('page'), 2); }
/** * Tests the effects of unsetting on the shadow of a list. * * @return void */ public function testUnsettingAListAndShadow() { $book = $this->_createBook(); //should work with ownPage and ownPageList as well... unset($book->ownPageList); R::store($book); $book = $book->fresh(); asrt(count($book->ownPage), 2); unset($book->ownPage); //shadow should be reloaded as well... $book->with(' LIMIT 1 ')->ownPage; R::store($book); $book = $book->fresh(); asrt(count($book->ownPage), 2); asrt(count($book->getMeta('sys.shadow.ownPage')), 2); unset($book->ownPage); asrt($book->getMeta('sys.shadow.ownPage'), NULL); //no load must clear shadow as well... $book->noLoad()->ownPage[] = R::dispense('page'); asrt(count($book->getMeta('sys.shadow.ownPage')), 0); R::store($book); $book = $book->fresh(); asrt(count($book->ownPage), 3); $lists = array('ownPage', 'ownPageList', 'xownPage', 'xownPageList', 'sharedPage', 'sharedPageList'); foreach ($lists as $list) { $book = R::dispense('book'); $book->{$list}; $shadowKey = $list; if (strpos($list, 'x') === 0) { $shadowKey = substr($shadowKey, 1); } $shadowKey = preg_replace('/List$/', '', $shadowKey); asrt(is_array($book->getMeta('sys.shadow.' . $shadowKey)), TRUE); unset($book->{$list}); asrt($book->getMeta('sys.shadow.' . $shadowKey), NULL); $book->{$list}; //reloading brings back shadow asrt(is_array($book->getMeta('sys.shadow.' . $shadowKey)), TRUE); $book->{$list} = array(); //keeps shadow (very important to compare deletions!) asrt(is_array($book->getMeta('sys.shadow.' . $shadowKey)), TRUE); R::store($book); //clears shadow $book->alias('magazine')->{$list}; //reloading with alias also brings back shadow unset($book->{$list}); asrt($book->getMeta('sys.shadow.' . $shadowKey), NULL); $book = $book->fresh(); //clears shadow, reload asrt($book->getMeta('sys.shadow.' . $shadowKey), NULL); $book->noLoad()->{$list}; //reloading with noload also brings back shadow asrt(is_array($book->getMeta('sys.shadow.' . $shadowKey)), TRUE); asrt(count($book->getMeta('sys.shadow.' . $shadowKey)), 0); $book = $book->fresh(); //clears shadow, reload asrt($book->getMeta('sys.shadow.' . $shadowKey), NULL); $book->all()->{$list}; //reloading with all also brings back shadow asrt(is_array($book->getMeta('sys.shadow.' . $shadowKey)), TRUE); $book = $book->fresh(); //clears shadow, reload asrt($book->getMeta('sys.shadow.' . $shadowKey), NULL); $book->with(' LIMIT 1 ')->{$list}; //reloading with with- all also brings back shadow asrt(is_array($book->getMeta('sys.shadow.' . $shadowKey)), TRUE); $book = $book->fresh(); //clears shadow, reload asrt($book->getMeta('sys.shadow.' . $shadowKey), NULL); $book->{$list} = array(); //keeps shadow (very important to compare deletions!) asrt(is_array($book->getMeta('sys.shadow.' . $shadowKey)), TRUE); $book = $book->fresh(); //clears shadow, reload asrt($book->getMeta('sys.shadow.' . $shadowKey), NULL); $book->{$list} = array(); //keeps shadow (very important to compare deletions!) asrt(is_array($book->getMeta('sys.shadow.' . $shadowKey)), TRUE); R::trash($book); asrt($book->getMeta('sys.shadow.' . $shadowKey), NULL); } //no shadow for parent bean $book = $book->fresh(); $book->author = R::dispense('author'); asrt($book->getMeta('sys.shadow.author'), NULL); R::store($book); $book = $book->fresh(); unset($book->author); //we can unset and it does not remove R::store($book); $book = $book->fresh(); asrt(is_object($book->author), TRUE); //but we can also remove $book->author = NULL; R::store($book); $book = $book->fresh(); asrt($book->author, NULL); }
/** * @param int $id * * @return void */ public function deleteAnswer($id) { Facade::trash('answers', $id); }