getRedBean() public static méthode

Sets the database adapter you want to use.
public static getRedBean ( ) : OODB
Résultat OODB
Exemple #1
0
 /**
  * Test tainted.
  *
  * @return void
  */
 public function testTainted()
 {
     testpack('Original Tainted Tests');
     $redbean = R::getRedBean();
     $spoon = $redbean->dispense("spoon");
     asrt($spoon->getMeta("tainted"), TRUE);
     $spoon->dirty = "yes";
     asrt($spoon->getMeta("tainted"), TRUE);
     testpack('Tainted List test');
     $note = R::dispense('note');
     $note->text = 'abc';
     $note->ownNote[] = R::dispense('note')->setAttr('text', 'def');
     $id = R::store($note);
     $note = R::load('note', $id);
     asrt($note->isTainted(), FALSE);
     // Shouldn't affect tainted
     $note->text;
     asrt($note->isTainted(), FALSE);
     $note->ownNote;
     asrt($note->isTainted(), TRUE);
     testpack('Tainted Test Old Value');
     $text = $note->old('text');
     asrt($text, 'abc');
     asrt($note->hasChanged('text'), FALSE);
     $note->text = 'xxx';
     asrt($note->hasChanged('text'), TRUE);
     $text = $note->old('text');
     asrt($text, 'abc');
     testpack('Tainted Non-exist');
     asrt($note->hasChanged('text2'), FALSE);
     testpack('Misc Tainted Tests');
     $bean = R::dispense('bean');
     $bean->hasChanged('prop');
     $bean->old('prop');
 }
Exemple #2
0
 /**
  * Test whether we can override the getModelForBean() method
  * of the BeanHelper and use a custom BeanHelper to attach a model
  * based on type.
  *
  * @return void
  */
 public function testCustomBeanHelper()
 {
     $customBeanHelper = new \SoupBeanHelper(R::getToolbox());
     $oldBeanHelper = R::getRedBean()->getBeanHelper();
     asrt($oldBeanHelper instanceof SimpleFacadeBeanHelper, TRUE);
     R::getRedBean()->setBeanHelper($customBeanHelper);
     $meal = R::dispense('meal');
     asrt($meal->box() instanceof \Model_Soup, TRUE);
     $cake = R::dispense('cake');
     asrt(is_null($cake->box()), TRUE);
     $bean = R::dispense('coffee');
     asrt($bean->box() instanceof \Model_Coffee, TRUE);
     $meal->setFlavour('tomato');
     asrt($meal->getFlavour(), 'tomato');
     $meal->rating = 5;
     R::store($meal);
     asrt($meal->getFlavour(), 'tomato');
     $meal = $meal->unbox();
     asrt($meal->getFlavour(), 'tomato');
     $meal = R::findOne('meal');
     asrt($meal->box() instanceof \Model_Soup, TRUE);
     asrt($meal->getFlavour(), '');
     $meal->setFlavour('tomato');
     asrt($meal->getFlavour(), 'tomato');
     $meal = $meal->unbox();
     asrt($meal->getFlavour(), 'tomato');
     R::getRedBean()->setBeanHelper($oldBeanHelper);
 }
Exemple #3
0
 /**
  * Test usage of named parameters in SQL snippets.
  * Issue #299 on Github.
  *
  * @return void
  */
 public function testNamedParamsInSnippets()
 {
     testpack('Test whether we can use named parameters in SQL snippets.');
     R::nuke();
     $book = R::dispense('book');
     $page = R::dispense('page');
     $book->title = 'book';
     $book->sharedPage[] = $page;
     R::store($book);
     //should not give error like: Uncaught [HY093] - SQLSTATE[HY093]: Invalid parameter number: mixed named and positional parameters
     $books = $page->withCondition(' title = :title ', array(':title' => 'book'))->sharedBook;
     asrt(count($books), 1);
     //should not give error...
     $books = $page->withCondition(' title = :title ', array(':title' => 'book'))->sharedBook;
     asrt(count($books), 1);
     R::nuke();
     $book = R::dispense('book');
     $page = R::dispense('page');
     $book->title = 'book';
     $book->comment = 'comment';
     $page->title = 'page';
     $book->ownPage[] = $page;
     R::store($book);
     //should also not give error..
     $count = $book->countOwn('page');
     asrt($count, 1);
     $book = $book->fresh();
     //should also not give error..
     $count = $book->withCondition(' title = ? ', array('page'))->countOwn('page');
     asrt($count, 1);
     $book = $book->fresh();
     //should also not give error..
     $count = $book->withCondition(' title = :title ', array(':title' => 'page'))->countOwn('page');
     asrt($count, 1);
     $book = $book->fresh();
     $pages = $book->withCondition(' title = :title ', array(':title' => 'page'))->ownPage;
     asrt(count($pages), 1);
     //test with duplicate slots...
     $page = reset($pages);
     $page2 = R::dispense('page');
     $page2->ownPage[] = $page;
     R::store($page2);
     $page2 = $page2->fresh();
     $pages = $page2->withCondition(' title = :title ', array(':title' => 'page'))->ownPage;
     asrt(count($pages), 1);
     //test with find()
     $books = R::getRedBean()->find('book', array('title' => array('book')), ' AND title = :title ', array(':title' => 'book'));
     asrt(count($books), 1);
     $books = R::getRedBean()->find('book', array('title' => array('book', 'book2'), 'comment' => array('comment', 'comment2')), ' AND title = :title ', array(':title' => 'book'));
     asrt(count($books), 1);
     //just check numeric works as well...
     $books = R::getRedBean()->find('book', array('title' => array('book', 'book2'), 'comment' => array('comment', 'comment2')), ' AND title = ? ', array('book'));
     asrt(count($books), 1);
     //just extra check to verify glue works
     $books = R::getRedBean()->find('book', array('title' => array('book', 'book2'), 'comment' => array('comment', 'comment2')), ' ORDER BY id ');
     asrt(count($books), 1);
 }
Exemple #4
0
 /**
  * Tests different return values of dispense().
  *
  * @return void
  */
 public function testDispenseArray()
 {
     $oodb = R::getRedBean();
     $array = $oodb->dispense('book', 0, TRUE);
     asrt(is_array($array), TRUE);
     $array = $oodb->dispense('book', 1, TRUE);
     asrt(is_array($array), TRUE);
     $array = $oodb->dispense('book', 2, TRUE);
     asrt(is_array($array), TRUE);
     $array = R::dispense('book', 0, TRUE);
     asrt(is_array($array), TRUE);
     $array = R::dispense('book', 1, TRUE);
     asrt(is_array($array), TRUE);
     $array = R::dispense('book', 2, TRUE);
     asrt(is_array($array), TRUE);
     $array = $oodb->dispense('book', 0, FALSE);
     asrt(is_array($array), FALSE);
     asrt(is_null($array), TRUE);
     $array = $oodb->dispense('book', 1, FALSE);
     asrt(is_array($array), FALSE);
     asrt($array instanceof OODBBean, TRUE);
     $array = $oodb->dispense('book', 2, FALSE);
     asrt(is_array($array), TRUE);
     $array = R::dispense('book', 0, FALSE);
     asrt(is_array($array), FALSE);
     $array = R::dispense('book', 1, FALSE);
     asrt(is_array($array), FALSE);
     $array = R::dispense('book', 2, FALSE);
     asrt(is_array($array), TRUE);
     $array = $oodb->dispense('book', 0);
     asrt(is_array($array), FALSE);
     asrt(is_null($array), TRUE);
     $array = $oodb->dispense('book', 1);
     asrt(is_array($array), FALSE);
     asrt($array instanceof OODBBean, TRUE);
     $array = $oodb->dispense('book', 2);
     asrt(is_array($array), TRUE);
     $array = R::dispense('book', 0);
     asrt(is_array($array), FALSE);
     $array = R::dispense('book', 1);
     asrt(is_array($array), FALSE);
     $array = R::dispense('book', 2);
     asrt(is_array($array), TRUE);
 }
Exemple #5
0
<?php

namespace RedUNIT\Base;

use RedUNIT\Base;
use RedBeanPHP\Facade as R;
use RedBeanPHP\OODBBean;
use RedBeanPHP\QueryWriter\AQueryWriter;
R::ext('xdispense', function ($type) {
    return R::getRedBean()->dispense($type);
});
define('BOOK', 'tbl_book');
define('AUTHOR', 'tbl_author');
define('COAUTHOR', 'coAuthor');
define('FRIEND', 'tbl_friend');
define('PUBLISHER', 'tbl_publisher');
define('BOOKLIST', 'ownTbl_book');
define('FRIENDLIST', 'sharedTbl_friend');
/**
 * Prefixes
 *
 * @file    RedUNIT/Base/Prefixes.php
 * @desc    Tests whether you can use RedBeanPHP with table prefixes.
 * @author  Gabor de Mooij and the RedBeanPHP Community
 * @license New BSD/GPLv2
 *
 * (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community.
 * This source file is subject to the New BSD/GPLv2 License that is bundled
 * with this source code in the file license.txt.
 */
class Prefixes extends Base
Exemple #6
0
 /**
  * Test widen for constraint.
  *
  * @return void
  */
 public function testWideningColumnForConstraint()
 {
     testpack('widening column for constraint');
     $bean1 = R::dispense('project');
     $bean2 = R::dispense('invoice');
     $bean3 = R::getRedBean()->dispense('invoice_project');
     $bean3->project_id = false;
     $bean3->invoice_id = true;
     R::store($bean3);
     $cols = R::getColumns('invoice_project');
     asrt($cols['project_id'], "int(11) unsigned");
     asrt($cols['invoice_id'], "int(11) unsigned");
 }
Exemple #7
0
 /**
  * Test beautification of column names.
  *
  * @return void
  */
 public function testBeautifulColumnNames()
 {
     testpack('Beautiful column names');
     $town = R::dispense('town');
     $town->isCapital = FALSE;
     $town->hasTrainStation = TRUE;
     $town->name = 'BeautyVille';
     $houses = R::dispense('house', 2);
     $houses[0]->isForSale = TRUE;
     $town->ownHouse = $houses;
     R::store($town);
     $town = R::load('town', $town->id);
     asrt($town->isCapital == FALSE, TRUE);
     asrt($town->hasTrainStation == TRUE, TRUE);
     asrt($town->name == 'BeautyVille', TRUE);
     testpack('Accept datetime objects.');
     $cal = R::dispense('calendar');
     $cal->when = new \DateTime('2000-01-01', new \DateTimeZone('Pacific/Nauru'));
     asrt($cal->when, '2000-01-01 00:00:00');
     testpack('Affected rows test');
     $currentDriver = $this->currentlyActiveDriverID;
     $toolbox = R::getToolBox();
     $adapter = $toolbox->getDatabaseAdapter();
     $writer = $toolbox->getWriter();
     $redbean = $toolbox->getRedBean();
     $pdo = $adapter->getDatabase();
     $bean = $redbean->dispense('bean');
     $bean->prop = 3;
     //make test run with strict mode as well
     $redbean->store($bean);
     $adapter->exec('UPDATE bean SET prop = 2');
     asrt($adapter->getAffectedRows(), 1);
     testpack('Testing Logger');
     R::getDatabaseAdapter()->getDatabase()->setLogger(new RDefault());
     asrt(R::getDatabaseAdapter()->getDatabase()->getLogger() instanceof Logger, TRUE);
     asrt(R::getDatabaseAdapter()->getDatabase()->getLogger() instanceof RDefault, TRUE);
     $bean = R::dispense('bean');
     $bean->property = 1;
     $bean->unsetAll(array('property'));
     asrt($bean->property, NULL);
     asrt($bean->setAttr('property', 2) instanceof OODBBean, TRUE);
     asrt($bean->property, 2);
     asrt(preg_match('/\\d\\d\\d\\d\\-\\d\\d\\-\\d\\d/', R::isoDate()), 1);
     asrt(preg_match('/\\d\\d\\d\\d\\-\\d\\d\\-\\d\\d\\s\\d\\d:\\d\\d:\\d\\d/', R::isoDateTime()), 1);
     $redbean = R::getRedBean();
     $adapter = R::getDatabaseAdapter();
     $writer = R::getWriter();
     asrt($redbean instanceof OODB, TRUE);
     asrt($adapter instanceof Adapter, TRUE);
     asrt($writer instanceof QueryWriter, TRUE);
     R::setRedBean($redbean);
     pass();
     //cant really test this
     R::setDatabaseAdapter($adapter);
     pass();
     //cant really test this
     R::setWriter($writer);
     pass();
     //cant really test this
     $u1 = R::dispense('user');
     $u1->name = 'Gabor';
     $u1->login = '******';
     $u2 = R::dispense('user');
     $u2->name = 'Eric';
     $u2->login = '******';
     R::store($u1);
     R::store($u2);
     $list = R::getAssoc('select login,' . R::getWriter()->esc('name') . ' from ' . R::getWriter()->esc('user') . ' ');
     asrt($list['e'], 'Eric');
     asrt($list['g'], 'Gabor');
     $painting = R::dispense('painting');
     $painting->name = 'Nighthawks';
     $id = R::store($painting);
     testpack('Testing SQL Error Types');
     foreach ($writer->typeno_sqltype as $code => $text) {
         asrt(is_integer($code), TRUE);
         asrt(is_string($text), TRUE);
     }
     foreach ($writer->sqltype_typeno as $text => $code) {
         asrt(is_integer($code), TRUE);
         asrt(is_string($text), TRUE);
     }
     testpack('Testing Nowhere Pt. 1 (unfrozen)');
     foreach (array('exec', 'getAll', 'getCell', 'getAssoc', 'getRow', 'getCol') as $method) {
         R::$method('select * from nowhere');
         pass();
     }
     testpack('Testing Nowhere Pt. 2 (frozen)');
     R::freeze(TRUE);
     foreach (array('exec', 'getAll', 'getCell', 'getAssoc', 'getRow', 'getCol') as $method) {
         try {
             R::$method('select * from nowhere');
             fail();
         } catch (SQL $e) {
             pass();
         }
     }
     R::freeze(FALSE);
 }
Exemple #8
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"));
 }
Exemple #9
0
 /**
  * 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);
 }
Exemple #10
0
 /**
  * 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);
 }
Exemple #11
0
 /**
  * ExportAll.
  *
  * @return void
  */
 public function testExportAll()
 {
     testpack('Test exportAll');
     $redbean = R::getRedBean();
     $bean = new OODBBean();
     $bean->import(array("a" => 1, "b" => 2));
     $bean->setMeta("justametaproperty", "hellothere");
     $arr = $bean->export();
     asrt(is_array($arr), TRUE);
     asrt(isset($arr["a"]), TRUE);
     asrt(isset($arr["b"]), TRUE);
     asrt($arr["a"], 1);
     asrt($arr["b"], 2);
     asrt(isset($arr["__info"]), FALSE);
     $arr = $bean->export(TRUE);
     asrt(isset($arr["__info"]), TRUE);
     asrt($arr["a"], 1);
     asrt($arr["b"], 2);
     $exportBean = $redbean->dispense("abean");
     $exportBean->setMeta("metaitem.bla", 1);
     $exportedBean = $exportBean->export(TRUE);
     asrt($exportedBean["__info"]["metaitem.bla"], 1);
     asrt($exportedBean["__info"]["type"], "abean");
     // Can we determine whether a bean is empty?
     testpack('test $bean->isEmpty() function');
     $bean = R::dispense('bean');
     asrt($bean->isEmpty(), TRUE);
     asrt(count($bean) > 0, TRUE);
     $bean->property = 1;
     asrt($bean->isEmpty(), FALSE);
     asrt(count($bean) > 0, TRUE);
     $bean->property = 0;
     asrt($bean->isEmpty(), TRUE);
     asrt(count($bean) > 0, TRUE);
     $bean->property = FALSE;
     asrt($bean->isEmpty(), TRUE);
     asrt(count($bean) > 0, TRUE);
     $bean->property = NULL;
     asrt($bean->isEmpty(), TRUE);
     asrt(count($bean) > 0, TRUE);
     unset($bean->property);
     asrt($bean->isEmpty(), TRUE);
     asrt(count($bean) > 0, TRUE);
     // Export bug I found
     $bandmember = R::dispense('bandmember');
     $bandmember->name = 'Duke';
     $instrument = R::dispense('instrument');
     $instrument->name = 'Piano';
     $bandmember->ownInstrument[] = $instrument;
     $a = R::exportAll($bandmember);
     pass();
     asrt(isset($a[0]), TRUE);
     asrt((int) $a[0]['id'], 0);
     asrt($a[0]['name'], 'Duke');
     asrt($a[0]['ownInstrument'][0]['name'], 'Piano');
     R::nuke();
     $v = R::dispense('village');
     $b = R::dispense('building');
     $v->name = 'a';
     $b->name = 'b';
     $v->ownBuilding[] = $b;
     $id = R::store($v);
     $a = R::exportAll($v);
     asrt($a[0]['name'], 'a');
     asrt($a[0]['ownBuilding'][0]['name'], 'b');
     $v = R::load('village', $id);
     $b2 = R::dispense('building');
     $b2->name = 'c';
     $v->ownBuilding[] = $b2;
     $a = R::exportAll($v);
     asrt($a[0]['name'], 'a');
     asrt($a[0]['ownBuilding'][0]['name'], 'b');
     asrt(count($a[0]['ownBuilding']), 2);
     list($r1, $r2) = R::dispense('army', 2);
     $r1->name = '1';
     $r2->name = '2';
     $v->sharedArmy[] = $r2;
     $a = R::exportAll($v);
     asrt(count($a[0]['sharedArmy']), 1);
     R::store($v);
     $v = R::load('village', $id);
     $a = R::exportAll($v);
     asrt(count($a[0]['sharedArmy']), 1);
     asrt($a[0]['name'], 'a');
     asrt($a[0]['ownBuilding'][0]['name'], 'b');
     asrt(count($a[0]['ownBuilding']), 2);
     $v->sharedArmy[] = $r1;
     $a = R::exportAll($v);
     asrt(count($a[0]['sharedArmy']), 2);
     $v = R::load('village', $id);
     $a = R::exportAll($v);
     asrt(count($a[0]['sharedArmy']), 1);
     $v->sharedArmy[] = $r1;
     R::store($v);
     $v = R::load('village', $id);
     $a = R::exportAll($v);
     asrt(count($a[0]['sharedArmy']), 2);
 }
 /**
  * Test bean type checking.
  * 
  * @return void
  */
 public function testBeanTypeChecking()
 {
     $redbean = R::getRedBean();
     $bean = $redbean->dispense("page");
     // Set some illegal values in the bean; this should trigger Security exceptions.
     // Arrays are not allowed.
     $bean->name = array("1");
     try {
         $redbean->store($bean);
         fail();
     } catch (RedException $e) {
         pass();
     }
     try {
         $redbean->check($bean);
         fail();
     } catch (RedException $e) {
         pass();
     }
     $bean->name = new OODBBean();
     try {
         $redbean->check($bean);
         fail();
     } catch (RedException $e) {
         pass();
     }
     // Property names should be alphanumeric
     $prop = ".";
     $bean->{$prop} = 1;
     try {
         $redbean->store($bean);
         fail();
     } catch (RedException $e) {
         pass();
     }
     try {
         $redbean->check($bean);
         fail();
     } catch (RedException $e) {
         pass();
     }
     // Really...
     $prop = "-";
     $bean->{$prop} = 1;
     try {
         $redbean->store($bean);
         fail();
     } catch (RedException $e) {
         pass();
     }
     try {
         $redbean->check($bean);
         fail();
     } catch (RedException $e) {
         pass();
     }
 }
Exemple #13
0
 /**
  * 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);
 }
Exemple #14
0
 public function testCountShared()
 {
     R::nuke();
     $book = R::dispense('book');
     $book->sharedPageList = R::dispense('page', 5);
     R::store($book);
     asrt($book->countShared('page'), 5);
     asrt($book->countShared('leaflet'), 0);
     asrt(R::dispense('book')->countShared('page'), 0);
     $am = R::getRedBean()->getAssociationManager();
     asrt($am->relatedCount(R::dispense('book'), 'page'), 0);
     try {
         $am->relatedCount('not a bean', 'type');
         fail();
     } catch (RedException $e) {
         pass();
     }
 }
Exemple #15
0
 /**
  * Test list beautifications.
  *
  * @return void
  */
 public function testListBeautifications()
 {
     testpack('Test list beautifications');
     $book = R::dispense('book');
     $page = R::dispense('page')->setAttr('name', 'a');
     $book->sharedPage[] = $page;
     $id = R::store($book);
     $book = R::load('book', $id);
     $p = reset($book->ownBookPage);
     asrt($p->page->name, 'a');
     $bean = R::dispense('bean');
     $bean->sharedAclRole[] = R::dispense('role')->setAttr('name', 'x');
     R::store($bean);
     asrt(R::count('role'), 1);
     $aclrole = R::getRedBean()->dispense('acl_role');
     $aclrole->name = 'role';
     $bean = R::dispense('bean');
     $bean->sharedAclRole[] = $aclrole;
     R::store($bean);
     asrt(count($bean->sharedAclRole), 1);
 }
Exemple #16
0
 public function testErrorHandling()
 {
     R::nuke();
     list($book, $page) = R::dispenseAll('book,page');
     $book->sharedPage[] = $page;
     R::store($page);
     $redbean = R::getRedBean();
     $am = $redbean->getAssociationManager();
     //SQLite and CUBRID do not comply with ANSI SQLState codes.
     $catchAll = $this->currentlyActiveDriverID == 'sqlite' || $this->currentlyActiveDriverID === 'CUBRID';
     try {
         $am->related($book, 'page', 'invalid SQL');
         if ($catchAll) {
             pass();
         } else {
             fail();
         }
     } catch (SQL $e) {
         if ($catchAll) {
             fail();
         } else {
             pass();
         }
     }
     try {
         $am->related($book, 'cover');
         pass();
     } catch (SQL $e) {
         fail();
     }
     try {
         $am->related(R::dispense('cover'), 'book');
         pass();
     } catch (SQL $e) {
         fail();
     }
 }