load() public static method

It searches for a OODBBean Bean Object in the database. It does not matter how this bean has been stored. RedBean uses the primary key ID $id and the string $type to find the bean. The $type specifies what kind of bean you are looking for; this is the same type as used with the dispense() function. If RedBean finds the bean it will return the OODB Bean object; if it cannot find the bean RedBean will return a new bean of type $type and with primary key ID 0. In the latter case it acts basically the same as dispense(). Important note: If the bean cannot be found in the database a new bean of the specified type will be generated and returned.
public static load ( string $type, integer $id ) : OODBBean
$type string type of bean you want to load
$id integer ID of the bean you want to load
return OODBBean
Exemplo n.º 1
0
 public function testDatabaseInstallation()
 {
     $this->url('index.php');
     //All Fields initially with empty
     $this->fillFields(array('hostname' => 'localhost', 'username' => 'root', 'password' => '', 'database' => 'phpback_test', 'adminemail' => '*****@*****.**', 'adminname' => 'admin', 'adminpass' => 'admin', 'adminrpass' => 'admin'));
     //Submit form
     $this->byName('install-form')->submit();
     //Should delete first intallation files
     $this->assertFileNotExists('install/index.php');
     $this->assertFileNotExists('install/install1.php');
     $this->assertFileNotExists('install/database_tables.sql');
     //Should create configuration file
     $this->assertFileExists('application/config/database.php');
     include 'application/config/database.php';
     $this->assertEquals($db['default']['username'], 'root');
     $this->assertEquals($db['default']['password'], '');
     $this->assertEquals($db['default']['database'], 'phpback_test');
     $this->assertEquals($db['default']['username'], 'root');
     $this->assertEquals($db['default']['dbdriver'], 'mysqli');
     //Should have updated database with new tables
     $this->assertEquals(RedBean::inspect(), array('_sessions', 'categories', 'comments', 'flags', 'ideas', 'logs', 'settings', 'users', 'votes'));
     //Should have created the admin user
     $adminUser = RedBean::load('users', 1);
     $this->assertEquals($adminUser->name, 'admin');
     $this->assertEquals($adminUser->email, '*****@*****.**');
     $this->assertEquals($adminUser->isadmin, '3');
     $this->assertEquals($adminUser->votes, '20');
 }
Exemplo n.º 2
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');
 }
Exemplo n.º 3
0
 /**
  * Test types.
  * Test how RedBeanPHP OODB and OODBBean handle type and type casts.
  * 
  * Rules:
  * 
  * 1. before storing a bean all types are preserved except booleans (they are converted to STRINGS '0' or '1')
  * 2. after store-reload all bean property values are STRINGS or NULL 
  *    (or ARRAYS but that's only from a user perspective because these are lazy loaded)
  * 3. the ID returned by store() is an INTEGER (if possible; on 32 bit systems overflowing values will be cast to STRINGS!)
  * 
  * After loading:
  * ALL VALUES EXCEPT NULL -> STRING
  * NULL -> NULL
  * 
  * @note Why not simply return bean->id in store()? Because not every driver returns the same type:
  * databases without insert_id support require a separate query or a suffix returning STRINGS, not INTEGERS.
  * 
  * @note Why not preserve types? I.e. I store integer, why do I get back a string?
  * Answer: types are handled different across database platforms, would cause overhead to inspect every value for type,
  * also PHP is a dynamically typed language so types should not matter that much. Another reason: due to the nature
  * of RB columns in the database might change (INT -> STRING) this would cause return types to change as well which would
  * cause 'cascading errors', i.e. a column gets widened and suddenly your code would break.
  * 
  * @note Unfortunately the 32/64-bit issue cannot be tested fully. Return-strategy store() is probably the safest
  * solution.
  * 
  * @return void
  */
 public function testTypes()
 {
     testpack('Beans can only contain STRING and NULL after reload');
     R::nuke();
     $bean = R::dispense('bean');
     $bean->number = 123;
     $bean->float = 12.3;
     $bean->bool = false;
     $bean->bool2 = true;
     $bean->text = 'abc';
     $bean->null = null;
     $bean->datetime = new \DateTime('NOW', new \DateTimeZone('Europe/Amsterdam'));
     $id = R::store($bean);
     asrt(is_int($id), TRUE);
     asrt(is_float($bean->float), TRUE);
     asrt(is_integer($bean->number), TRUE);
     asrt(is_string($bean->bool), TRUE);
     asrt(is_string($bean->bool2), TRUE);
     asrt(is_string($bean->datetime), TRUE);
     asrt(is_string($bean->text), TRUE);
     asrt(is_null($bean->null), TRUE);
     $bean = R::load('bean', $id);
     asrt(is_string($bean->id), TRUE);
     asrt(is_string($bean->float), TRUE);
     asrt(is_string($bean->number), TRUE);
     asrt(is_string($bean->bool), TRUE);
     asrt(is_string($bean->bool2), TRUE);
     asrt(is_string($bean->datetime), TRUE);
     asrt(is_string($bean->text), TRUE);
     asrt(is_null($bean->null), TRUE);
     asrt($bean->bool, '0');
     asrt($bean->bool2, '1');
 }
Exemplo n.º 4
0
 /**
  * Via specific tests.
  * 
  * @return void
  */
 public function testViaAndSQL()
 {
     R::nuke();
     list($p1, $p2) = R::dispense('participant', 2);
     list($e1, $e2) = R::dispense('employee', 2);
     list($x1, $x2) = R::dispense('project', 2);
     $e1->name = 'Anna';
     $e2->name = 'John';
     $p1->project = $x1;
     $p1->employee = $e1;
     $p1->arole = 'designer';
     $p2->project = $x1;
     $p2->employee = $e2;
     $p2->arole = 'coder';
     R::storeAll(array($p1, $p2));
     $project = R::load('project', $x1->id);
     $designers = $project->withCondition(' participant.arole = ? ', array('designer'))->via('participant')->sharedEmployeeList;
     $anna = reset($designers);
     asrt(count($designers), 1);
     asrt($anna->name, 'Anna');
     $coders = $project->withCondition(' participant.arole = ? ', array('coder'))->via('participant')->sharedEmployeeList;
     $john = reset($coders);
     asrt(count($coders), 1);
     asrt($john->name, 'John');
 }
Exemplo n.º 5
0
 /**
  * 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);
 }
Exemplo n.º 6
0
 /**
  * Test NULL handling, setting a property to NULL must
  * cause a change.
  * 
  * @return void
  */
 public function testBasicNullHandling()
 {
     // NULL can change bean
     $bean = R::dispense('bean');
     $bean->bla = 'a';
     R::store($bean);
     $bean = $bean->fresh();
     asrt($bean->hasChanged('bla'), FALSE);
     $bean->bla = NULL;
     asrt($bean->hasChanged('bla'), TRUE);
     // NULL test
     $page = R::dispense('page');
     $book = R::dispense('book');
     $page->title = 'a NULL page';
     $page->book = $book;
     $book->title = 'Why NUll is painful..';
     R::store($page);
     $bookid = $page->book->id;
     unset($page->book);
     $id = R::store($page);
     $page = R::load('page', $id);
     $page->title = 'another title';
     R::store($page);
     pass();
     $page = R::load('page', $id);
     $page->title = 'another title';
     $page->book_id = NULL;
     R::store($page);
     pass();
 }
Exemplo n.º 7
0
 /**
  * Some basic tests.
  * 
  * @return void
  */
 public function testTags()
 {
     list($c, $d, $e, $f) = R::dispense('coffee', 4);
     R::tag($c, 'strong,black');
     R::tag($d, 'black');
     R::tag($e, 'strong,sweet');
     R::tag($f, 'black,strong');
     asrt(count(R::taggedAll('coffee', 'strong,sweet')), 1);
     asrt(count(R::taggedAll('coffee', 'strong')), 3);
     asrt(count(R::taggedAll('coffee', '')), 0);
     asrt(count(R::taggedAll('coffee', 'sweet')), 1);
     asrt(count(R::taggedAll('coffee', 'sweet,strong')), 1);
     asrt(count(R::taggedAll('coffee', 'black,strong')), 2);
     asrt(count(R::taggedAll('coffee', array('black', 'strong'))), 2);
     asrt(count(R::taggedAll('coffee', 'salty')), 0);
     $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::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 (RedException $e) {
         fail();
     }
     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, "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(R::hasTag($blog, "horror,commic,halloween", TRUE), FALSE);
     asrt(count(R::tag($blog)), 3);
     testpack("fetch tagged items");
 }
Exemplo n.º 8
0
 /**
  * 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);
 }
Exemplo n.º 9
0
 public function testDisableVoteIdea()
 {
     Scripts::LoginUser('*****@*****.**', 'Gates123');
     $this->url('home/profile/4');
     $this->byLinkText('Delete votes')->click();
     $voteidea = RedBean::load('ideas', 3);
     $this->assertEquals($voteidea->votes, '0');
 }
Exemplo n.º 10
0
 /**
  * Test UTF8 handling.
  *
  * @return void
  */
 public function testUTF8()
 {
     $str = '𠜎ὃ𠻗𠻹𠻺𠼭𠼮𠽌𠾴𠾼𠿪𡁜';
     $bean = R::dispense('bean');
     $bean->bla = $str;
     R::store($bean);
     $bean = R::load('bean', $bean->id);
     asrt($bean->bla, $str);
     pass();
 }
Exemplo n.º 11
0
 /**
  * Store a new record.
  *
  * @param  Request  $request
  * @return Response
  */
 public function store(Request $request)
 {
     $item_id = $request->input('item_id');
     $item = R::load('item', $item_id);
     $attribute = R::dispense('attribute');
     $attribute->key = $request->input('key');
     $attribute->value = $request->input('value');
     $item->ownAttributeList[] = $attribute;
     R::store($item);
     echo "<pre>\n";
     var_dump($attribute->export());
     var_dump($item->export());
 }
Exemplo n.º 12
0
 /**
  * Test to make sure stash cache works with recursively opening models
  * with FUSE.
  *
  * @return void
  */
 public function testIssue259()
 {
     testpack('Testing Issue #259 - Stash Cache breaks model delegation in open().');
     $mother = R::dispense('mother');
     $mother->desc = 'I am mother';
     R::store($mother);
     $child = R::dispense('child');
     $child->mother = $mother;
     $child->desc = 'I am child';
     $id = R::store($child);
     R::findOne('child', ' id = ?', array($id));
     R::find('child', ' id = ? ', array($id));
     R::load('child', $id);
 }
Exemplo n.º 13
0
 /**
  * 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');
 }
Exemplo n.º 14
0
 /**
  * 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();
     }
 }
Exemplo n.º 15
0
    /**
     * Test BIG INT primary key support.
     *
     * @return void
     */
    public function testBigIntSupport()
    {
        R::nuke();
        $createPageTableSQL = '
			CREATE TABLE
			`page`
			(
				id BIGINT(20) UNSIGNED NOT NULL,
				book_id BIGINT(20) UNSIGNED NOT NULL,
				magazine_id BIGINT(20) UNSIGNED NOT NULL,
				title VARCHAR(255),
				PRIMARY KEY ( id )
			)
			ENGINE = InnoDB DEFAULT
			CHARSET=utf8mb4
			COLLATE=utf8mb4_unicode_ci
			AUTO_INCREMENT = 1223372036854775808';
        $createBookTableSQL = '
			CREATE TABLE
			`book`
			(
				id BIGINT(20) UNSIGNED NOT NULL,
				title VARCHAR(255),
				PRIMARY KEY ( id )
			)
			ENGINE = InnoDB DEFAULT
			CHARSET=utf8mb4
			COLLATE=utf8mb4_unicode_ci
			AUTO_INCREMENT = 2223372036854775808';
        $createPagePageTableSQL = '
			CREATE TABLE
			`page_page`
			(
				id BIGINT(20) UNSIGNED NOT NULL,
				page_id BIGINT(20) UNSIGNED NOT NULL,
				page2_id BIGINT(20) UNSIGNED NOT NULL,
				PRIMARY KEY ( id )
			)
			ENGINE = InnoDB DEFAULT
			CHARSET=utf8mb4
			COLLATE=utf8mb4_unicode_ci
			AUTO_INCREMENT = 3223372036854775808';
        R::exec($createBookTableSQL);
        R::exec($createPageTableSQL);
        R::exec($createPagePageTableSQL);
        //insert some records
        $book1ID = '2223372036854775808';
        $book2ID = '2223372036854775809';
        $page1ID = '1223372036854775808';
        $page2ID = '1223372036854775809';
        $page3ID = '1223372036854775890';
        $pagePage1ID = '3223372036854775808';
        $insertBook1SQL = "\n\t\t\tINSERT INTO book (id, title) VALUES( '{$book1ID}', 'book 1' );\n\t\t";
        $insertBook2SQL = "\n\t\t\tINSERT INTO book (id, title) VALUES( '{$book2ID}', 'book 2' );\n\t\t";
        $insertPage1SQL = "\n\t\t\tINSERT INTO page (id, book_id, title, magazine_id) VALUES( '{$page1ID}', '{$book1ID}', 'page 1 of book 1', '{$book2ID}' );\n\t\t";
        $insertPage2SQL = "\n\t\t\tINSERT INTO page (id, book_id, title) VALUES( '{$page2ID}', '{$book1ID}', 'page 2 of book 1' );\n\t\t";
        $insertPage3SQL = "\n\t\t\tINSERT INTO page (id, book_id, title) VALUES( '{$page3ID}', '{$book2ID}', 'page 1 of book 2' );\n\t\t";
        $insertPagePage1SQL = "\n\t\t\tINSERT INTO page_page (id, page_id, page2_id) VALUES( '{$pagePage1ID}', '{$page2ID}', '{$page3ID}' );\n\t\t";
        R::exec($insertBook1SQL);
        R::exec($insertBook2SQL);
        R::exec($insertPage1SQL);
        R::exec($insertPage2SQL);
        R::exec($insertPage3SQL);
        R::exec($insertPagePage1SQL);
        //basic tour of basic functions....
        $book1 = R::load('book', $book1ID);
        asrt($book1->id, $book1ID);
        asrt($book1->title, 'book 1');
        $book2 = R::load('book', $book2ID);
        asrt($book2->id, $book2ID);
        asrt($book2->title, 'book 2');
        asrt(count($book1->ownPage), 2);
        asrt(count($book1->fresh()->with('LIMIT 1')->ownPage), 1);
        asrt(count($book1->fresh()->withCondition(' title = ? ', array('page 2 of book 1'))->ownPage), 1);
        asrt(count($book2->ownPage), 1);
        asrt($book2->fresh()->countOwn('page'), 1);
        $page1 = R::load('page', $page1ID);
        asrt(count($page1->sharedPage), 0);
        asrt($page1->fetchAs('book')->magazine->id, $book2ID);
        $page2 = R::load('page', $page2ID);
        asrt(count($page2->sharedPage), 1);
        asrt($page2->fresh()->countShared('page'), 1);
        $page3 = R::findOne('page', ' title = ? ', array('page 1 of book 2'));
        asrt($page3->id, $page3ID);
        asrt($page3->book->id, $book2ID);
    }
Exemplo n.º 16
0
    /**
     * Test BIG INT primary key support.
     *
     * @return void
     */
    public function testBigIntSupport()
    {
        R::nuke();
        $createPageTableSQL = '
			CREATE TABLE
			page
			(
				id BIGSERIAL PRIMARY KEY,
				book_id BIGSERIAL,
				magazine_id BIGSERIAL,
				title VARCHAR(255)
			)';
        $createBookTableSQL = '
			CREATE TABLE
			book
			(
				id BIGSERIAL PRIMARY KEY,
				title VARCHAR(255)
			)';
        $createPagePageTableSQL = '
			CREATE TABLE
			page_page
			(
				id BIGSERIAL PRIMARY KEY,
				page_id BIGSERIAL,
				page2_id BIGSERIAL
			) ';
        R::exec($createBookTableSQL);
        R::exec($createPageTableSQL);
        R::exec($createPagePageTableSQL);
        //insert some records
        $book1ID = '2223372036854775808';
        $book2ID = '2223372036854775809';
        $page1ID = '1223372036854775808';
        $page2ID = '1223372036854775809';
        $page3ID = '1223372036854775890';
        $pagePage1ID = '3223372036854775808';
        R::exec("ALTER SEQUENCE book_id_seq RESTART WITH {$book1ID}");
        R::exec("ALTER SEQUENCE page_id_seq RESTART WITH {$page1ID}");
        R::exec("ALTER SEQUENCE page_page_id_seq RESTART WITH {$pagePage1ID}");
        $insertBook1SQL = "\n\t\t\tINSERT INTO book (title) VALUES( 'book 1' );\n\t\t";
        $insertBook2SQL = "\n\t\t\tINSERT INTO book (title) VALUES( 'book 2' );\n\t\t";
        $insertPage1SQL = "\n\t\t\tINSERT INTO page (id, book_id, title, magazine_id) VALUES( '{$page1ID}', '{$book1ID}', 'page 1 of book 1', '{$book2ID}' );\n\t\t";
        $insertPage2SQL = "\n\t\t\tINSERT INTO page (id, book_id, title) VALUES( '{$page2ID}', '{$book1ID}', 'page 2 of book 1' );\n\t\t";
        $insertPage3SQL = "\n\t\t\tINSERT INTO page (id, book_id, title) VALUES( '{$page3ID}', '{$book2ID}', 'page 1 of book 2' );\n\t\t";
        $insertPagePage1SQL = "\n\t\t\tINSERT INTO page_page (id, page_id, page2_id) VALUES( '{$pagePage1ID}', '{$page2ID}', '{$page3ID}' );\n\t\t";
        R::exec($insertBook1SQL);
        R::exec($insertBook2SQL);
        R::exec($insertPage1SQL);
        R::exec($insertPage2SQL);
        R::exec($insertPage3SQL);
        R::exec($insertPagePage1SQL);
        //basic tour of basic functions....
        $book1 = R::load('book', $book1ID);
        asrt($book1->id, $book1ID);
        asrt($book1->title, 'book 1');
        $book2 = R::load('book', $book2ID);
        asrt($book2->id, $book2ID);
        asrt($book2->title, 'book 2');
        asrt(count($book1->ownPage), 2);
        asrt(count($book1->fresh()->with('LIMIT 1')->ownPage), 1);
        asrt(count($book1->fresh()->withCondition(' title = ? ', array('page 2 of book 1'))->ownPage), 1);
        asrt(count($book2->ownPage), 1);
        asrt($book2->fresh()->countOwn('page'), 1);
        $page1 = R::load('page', $page1ID);
        asrt(count($page1->sharedPage), 0);
        asrt($page1->fetchAs('book')->magazine->id, $book2ID);
        $page2 = R::load('page', $page2ID);
        asrt(count($page2->sharedPage), 1);
        asrt($page2->fresh()->countShared('page'), 1);
        $page3 = R::findOne('page', ' title = ? ', array('page 1 of book 2'));
        asrt($page3->id, $page3ID);
        asrt($page3->book->id, $book2ID);
    }
Exemplo n.º 17
0
 /**
  * Test types of strings.
  *
  * @return void
  */
 public function testTypesStrings()
 {
     $bean = R::dispense('bean');
     $bean->data = 'abcdefghijk';
     R::store($bean);
     $cols = R::getColumns('bean');
     asrt($cols['data'], 'text');
     $bean = R::load('bean', $bean->id);
     asrt($bean->data, 'abcdefghijk');
     $bean->data = '(1,2)';
     R::store($bean);
     $cols = R::getColumns('bean');
     asrt($cols['data'], 'text');
     $bean->data = '[(1.2,1.4),(2.2,34)]';
     R::store($bean);
     $cols = R::getColumns('bean');
     asrt($cols['data'], 'text');
     $bean->data = '<(9.2,1.2),7.9>';
     R::store($bean);
     $cols = R::getColumns('bean');
     asrt($cols['data'], 'text');
     $bean->data = '$25';
     R::store($bean);
     $cols = R::getColumns('bean');
     asrt($cols['data'], 'text');
     $bean->data = '2012-10-10 10:00:00';
     R::store($bean);
     $cols = R::getColumns('bean');
     asrt($cols['data'], 'text');
 }
Exemplo n.º 18
0
 /**
  * Test explicit flush.
  * 
  * @return void
  */
 public function testExplicitCacheFlush()
 {
     testpack('Test cache flush (explicit)');
     R::debug(true, 1);
     $logger = R::getDatabaseAdapter()->getDatabase()->getLogger();
     $bean = R::dispense('bean');
     $bean->title = 'abc';
     $id1 = R::store($bean);
     $logger->clear();
     $bean = R::load('bean', $id1);
     asrt($bean->title, 'abc');
     asrt(count($logger->grep('SELECT *')), 1);
     $bean = R::load('bean', $id1);
     asrt(count($logger->grep('SELECT *')), 1);
     R::getWriter()->flushCache();
     $bean = R::load('bean', $id1);
     asrt(count($logger->grep('SELECT *')), 2);
     R::getWriter()->flushCache();
     R::getWriter()->setUseCache(FALSE);
 }
Exemplo n.º 19
0
 /**
  * Test traversal with aliases.
  *
  * @return void
  */
 public function testTraversalWithAlias()
 {
     R::nuke();
     $book = R::dispense('book');
     $cats = R::dispense('category', 3);
     $cats[0]->gname = 'SF';
     $cats[1]->gname = 'Fantasy';
     $cats[2]->gname = 'Horror';
     $book->genre = $cats[0];
     $book->name = 'Space Story';
     $cats[0]->genre = $cats[1];
     $cats[2]->genre = $cats[1];
     R::store($book);
     $book2 = R::dispense('book');
     $book2->genre = $cats[2];
     $book2->name = 'Ghost Story';
     R::store($book2);
     $fantasy = R::load('category', $cats[1]->id);
     $cats = array();
     $book = $book->fresh();
     $book->fetchAs('category')->traverse('genre', function ($cat) use(&$cats) {
         $cats[] = $cat->gname;
     });
     asrt(implode(',', $cats), 'SF,Fantasy');
     $catList = array();
     $fantasy->alias('genre')->with(' ORDER BY gname ASC ')->traverse('ownCategory', function ($cat) use(&$catList) {
         $catList[] = $cat->gname;
     });
     asrt(implode(',', $catList), 'Horror,SF');
 }
Exemplo n.º 20
0
 /**
  * Test prettier tables using via().
  */
 public function testViaPrettification()
 {
     R::nuke();
     R::renameAssociation('tbl_author_tbl_friend', 'tbl_author_friend');
     $author = R::xdispense(AUTHOR);
     $author->name = 'Mr. Quill';
     $friend = R::xdispense(FRIEND);
     $friend->name = 'Muse';
     $author->{FRIENDLIST}[] = $friend;
     $id = R::store($author);
     //print_r(R::inspect()); exit;
     $author = R::load(AUTHOR, $id);
     $tables = array_flip(R::inspect());
     asrt(isset($tables['tbl_author_friend']), TRUE);
     asrt(isset($tables['tbl_author_tbl_friend']), FALSE);
     asrt(count($author->{FRIENDLIST}), 1);
     AQueryWriter::clearRenames();
 }
Exemplo n.º 21
0
 public function testDisableBanUser()
 {
     Scripts::LoginAdmin();
     $this->byLinkText('Users Management')->click();
     $this->byLinkText('Banned List ')->click();
     $this->byLinkText('Disable ban')->click();
     $passdisbann = RedBean::load('users', 3);
     $this->assertEquals($passdisbann->banned, '0');
 }
Exemplo n.º 22
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"));
 }
Exemplo n.º 23
0
 /**
  * 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);
 }
Exemplo n.º 24
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);
 }
Exemplo n.º 25
0
 /**
  * All kinds of tests for basic CRUD.
  * 
  * Does the data survive?
  * 
  * @return void
  */
 public function testUpdatingBeans()
 {
     testpack('Test basic support UUID/override ID default value');
     $bean = R::dispense('bean');
     R::store($bean);
     if ($this->currentlyActiveDriverID === 'mysql') {
         //otherwise UTF8 causes index overflow in mysql: SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes
         R::exec('alter table bean modify column id char(3);');
     } else {
         R::getWriter()->widenColumn('bean', 'id', R::getWriter()->scanType('abc'));
     }
     $bean->id = 'abc';
     R::store($bean);
     asrt($bean->id, 'abc');
     testpack('Test Update');
     try {
         R::store(array());
         fail();
     } catch (RedException $e) {
         pass();
     }
     $toolbox = R::getToolBox();
     $adapter = $toolbox->getDatabaseAdapter();
     $writer = $toolbox->getWriter();
     $redbean = $toolbox->getRedBean();
     $pdo = $adapter->getDatabase();
     $page = $redbean->dispense("page");
     $page->name = "old name";
     $id = $redbean->store($page);
     asrt($page->getMeta('tainted'), FALSE);
     $page->setAttr('name', "new name");
     asrt($page->getMeta('tainted'), TRUE);
     $id = $redbean->store($page);
     $page = $redbean->load("page", $id);
     asrt($page->name, "new name");
     // Null should == NULL after saving
     $page->rating = NULL;
     $newid = $redbean->store($page);
     $page = $redbean->load("page", $id);
     asrt($page->name, "new name");
     asrt($page->rating === NULL, TRUE);
     asrt(!$page->rating, TRUE);
     $page->rating = FALSE;
     $newid = $redbean->store($page);
     asrt($newid, $id);
     $page = $redbean->load("page", $id);
     asrt($page->name, "new name");
     asrt((bool) $page->rating, FALSE);
     asrt($page->rating == FALSE, TRUE);
     asrt(!$page->rating, TRUE);
     $page->rating = TRUE;
     $newid = $redbean->store($page);
     asrt($newid, $id);
     $page = $redbean->load("page", $id);
     asrt($page->name, "new name");
     asrt((bool) $page->rating, TRUE);
     asrt($page->rating == TRUE, TRUE);
     asrt($page->rating == TRUE, TRUE);
     $page->rating = NULL;
     R::store($page);
     $page = R::load('page', $page->id);
     asrt($page->rating, NULL);
     $page->rating = "1";
     $newid = $redbean->store($page);
     asrt($newid, $id);
     $page = $redbean->load("page", $id);
     asrt($page->name, "new name");
     asrt($page->rating, "1");
     $page->rating = "0";
     $newid = $redbean->store($page);
     asrt($page->rating, "0");
     $page->rating = 0;
     $newid = $redbean->store($page);
     asrt($page->rating, 0);
     $page->rating = "0";
     $newid = $redbean->store($page);
     asrt($newid, $id);
     $page = $redbean->load("page", $id);
     asrt($page->name, "new name");
     asrt(!$page->rating, TRUE);
     asrt($page->rating == 0, TRUE);
     asrt($page->rating == FALSE, TRUE);
     $page->rating = 5;
     $newid = $redbean->store($page);
     asrt($newid, $id);
     $page = $redbean->load("page", $id);
     asrt($page->name, "new name");
     asrt(strval($page->rating), "5");
     $page->rating = 300;
     $newid = $redbean->store($page);
     asrt($newid, $id);
     $page = $redbean->load("page", $id);
     asrt($page->name, "new name");
     asrt(strval($page->rating), "300");
     $page->rating = -2;
     $newid = $redbean->store($page);
     asrt($newid, $id);
     $page = $redbean->load("page", $id);
     asrt($page->name, "new name");
     asrt(strval($page->rating), "-2");
     $page->rating = 2.5;
     $newid = $redbean->store($page);
     asrt($newid, $id);
     $page = $redbean->load("page", $id);
     asrt($page->name, "new name");
     asrt($page->rating == 2.5, TRUE);
     $page->rating = -3.3;
     $newid = $redbean->store($page);
     asrt($newid, $id);
     $page = $redbean->load("page", $id);
     asrt($page->name, "new name");
     asrt($page->rating == -3.3, TRUE);
     $page->rating = "good";
     $newid = $redbean->store($page);
     asrt($newid, $id);
     $page = $redbean->load("page", $id);
     asrt($page->name, "new name");
     asrt($page->rating, "good");
     $longtext = str_repeat('great! because..', 100);
     $page->rating = $longtext;
     $newid = $redbean->store($page);
     asrt($newid, $id);
     $page = $redbean->load("page", $id);
     asrt($page->name, "new name");
     asrt($page->rating, $longtext);
     // Test leading zeros
     $numAsString = "0001";
     $page->numasstring = $numAsString;
     $redbean->store($page);
     $page = $redbean->load("page", $id);
     asrt($page->numasstring, "0001");
     $page->numnotstring = "0.123";
     $redbean->store($page);
     $page = $redbean->load("page", $id);
     asrt($page->numnotstring == 0.123, TRUE);
     $page->numasstring2 = "00.123";
     $redbean->store($page);
     $page = $redbean->load("page", $id);
     asrt($page->numasstring2, "00.123");
     try {
         $redbean->trash(array());
         fail();
     } catch (RedException $e) {
         pass();
     }
     $redbean->trash($page);
     asrt((int) $pdo->GetCell("SELECT count(*) FROM page"), 0);
 }
function updatePackageCost($id, $cost, $label)
{
    $packageCost = R::load('packagecost', $id);
    $packageCost->cost = $cost;
    $packageCost->label = $label;
    R::store($packageCost);
    return json_encode(R::exportAll($packageCost));
}
Exemplo n.º 27
0
 public function testDeclinedIdea()
 {
     Scripts::ApproveIdea();
     $this->url('/home/idea/1/Relativity-Theory');
     $this->select($this->byName('Changestatusbutton'))->selectOptionByValue('Declined');
     $declinedidea = RedBean::load('ideas', 2);
     $this->assertEquals($declinedidea->status, 'Declined');
     $this->byName('deleteidea')->click();
     $this->assertNotContains('Relativity Theory', $this->byTag('body')->text());
     //la unica manera que podia demostrar que se habia echo lo queria
 }
Exemplo n.º 28
0
 /**
  * Tests whether aliasing plays nice with beautification.
  *
  * @return void
  */
 public function testAliasWithBeautify()
 {
     /**
      * Ensure that aliased column aren't beautified
      */
     $points = R::dispense('point', 2);
     $line = R::dispense('line');
     $line->pointA = $points[0];
     $line->pointB = $points[1];
     R::store($line);
     $line2 = R::dispense('line');
     $line2->pointA = $line->fetchAs('point')->pointA;
     $line2->pointB = R::dispense('point');
     R::store($line2);
     //now we have two points per line (1-to-x)
     //I want to know which lines cross A:
     $a = R::load('point', $line->pointA->id);
     //reload A
     $lines = $a->alias('pointA')->ownLine;
     asrt(count($lines), 2);
 }
Exemplo n.º 29
0
 /**
  * More variations...
  * 
  * @return void
  */
 public function testEmbeddedSQLPart2()
 {
     list($book1, $book2, $book3) = R::dispense('book', 3);
     $book1->position = 1;
     $book2->position = 2;
     $book3->position = 3;
     $shelf = R::dispense('shelf');
     $shelf->ownBook = array($book1, $book2, $book3);
     $id = R::store($shelf);
     $shelf = R::load('shelf', $id);
     $books = $shelf->with(' ORDER BY position ASC ')->ownBook;
     $book1 = array_shift($books);
     asrt((int) $book1->position, 1);
     $book2 = array_shift($books);
     asrt((int) $book2->position, 2);
     $book3 = array_shift($books);
     asrt((int) $book3->position, 3);
     $books = $shelf->with(' ORDER BY position DESC ')->ownBook;
     $book1 = array_shift($books);
     asrt((int) $book1->position, 3);
     $book2 = array_shift($books);
     asrt((int) $book2->position, 2);
     $book3 = array_shift($books);
     asrt((int) $book3->position, 1);
     //R::debug(1);
     $shelf = R::load('shelf', $id);
     $books = $shelf->with(' AND position > 2 ')->ownBook;
     asrt(count($books), 1);
     $shelf = R::load('shelf', $id);
     $books = $shelf->with(' AND position < ? ', array(3))->ownBook;
     asrt(count($books), 2);
     $shelf = R::load('shelf', $id);
     $books = $shelf->with(' AND position = 1 ')->ownBook;
     asrt(count($books), 1);
     $shelf = R::load('shelf', $id);
     $books = $shelf->withCondition(' position > -1 ')->ownBook;
     asrt(count($books), 3);
     // With-condition should not affect storing
     $shelf = R::load('shelf', $id);
     $books = $shelf->with(' AND position = 1 ')->ownBook;
     asrt(count($books), 1);
     asrt(count($shelf->ownBook), 1);
     $book = reset($shelf->ownBook);
     $book->title = 'Trees and other Poems';
     R::store($shelf);
     $books = $shelf->withCondition(' position > -1 ')->ownBook;
     asrt(count($books), 3);
     asrt(count($shelf->ownBook), 3);
     $shelf = R::load('shelf', $id);
     $books = $shelf->with(' AND position = 1 ')->ownBook;
     // Also with trashing -- just trash one!
     $shelf->ownBook = array();
     R::store($shelf);
     $books = $shelf->withCondition(' position > -1 ')->ownBook;
     asrt(count($books), 2);
     // With should cause a reload of a list
     $shelf = R::load('shelf', $id);
     $books = $shelf->with(' AND position = 2 ')->ownBook;
     asrt(count($books), 1);
     $books = $shelf->withCondition(' position > -1 ')->ownBook;
     asrt(count($books), 2);
     $book = reset($books);
     $book->title = 'Venetian Music';
     // Should not affect storage (fact that we used with twice, unsetting prop)
     R::store($shelf);
     $shelf = R::load('shelf', $id);
     asrt(count($shelf->ownBook), 2);
     // Alias
     list($game1, $game2, $game3) = R::dispense('game', 3);
     list($t1, $t2, $t3) = R::dispense('team', 3);
     $t1->name = 'Bats';
     $t2->name = 'Tigers';
     $t3->name = 'Eagles';
     $game1->name = 'a';
     $game1->team1 = $t1;
     $game1->team2 = $t2;
     $game2->name = 'b';
     $game2->team1 = $t1;
     $game2->team2 = $t3;
     $game3->name = 'c';
     $game3->team1 = $t2;
     $game3->team2 = $t3;
     R::storeAll(array($game1, $game2, $game3));
     $team1 = R::load('team', $t1->id);
     $team2 = R::load('team', $t2->id);
     $team3 = R::load('team', $t3->id);
     asrt(count($team1->alias('team1')->ownGame), 2);
     asrt(count($team2->alias('team1')->ownGame), 1);
     $team1 = R::load('team', $t1->id);
     $team2 = R::load('team', $t2->id);
     asrt(count($team1->alias('team2')->ownGame), 0);
     asrt(count($team2->alias('team2')->ownGame), 1);
     asrt(count($team3->alias('team1')->ownGame), 0);
     $team3 = R::load('team', $t3->id);
     asrt(count($team3->alias('team2')->ownGame), 2);
     $team1 = R::load('team', $t1->id);
     $games = $team1->alias('team1')->ownGame;
     $game4 = R::dispense('game');
     $game4->name = 'd';
     $game4->team2 = $t3;
     $team1->alias('team1')->ownGame[] = $game4;
     R::store($team1);
     $team1 = R::load('team', $t1->id);
     asrt(count($team1->alias('team1')->ownGame), 3);
     foreach ($team1->ownGame as $g) {
         if ($g->name == 'a') {
             $game = $g;
         }
     }
     $game->name = 'match';
     R::store($team1);
     $team1 = R::load('team', $t1->id);
     asrt(count($team1->alias('team1')->ownGame), 3);
     $found = 0;
     foreach ($team1->ownGame as $g) {
         if ($g->name == 'match') {
             $found = 1;
         }
     }
     if ($found) {
         pass();
     }
     $team1->ownGame = array();
     R::store($team1);
     $team1 = R::load('team', $t1->id);
     asrt(count($team1->alias('team1')->ownGame), 0);
     $team1->ownBook[] = $book1;
     R::store($team1);
     $team1 = R::load('team', $t1->id);
     asrt(count($team1->alias('team1')->ownGame), 0);
     asrt(count($team1->ownBook), 1);
 }
Exemplo n.º 30
0
 /**
  * Test Full fluid UUID support.
  *
  */
 public function testFullSupport()
 {
     //Rewire objects to support UUIDs.
     $oldToolBox = R::getToolBox();
     $oldAdapter = $oldToolBox->getDatabaseAdapter();
     $uuidWriter = new \UUIDWriterPostgres($oldAdapter);
     $newRedBean = new OODB($uuidWriter);
     $newToolBox = new ToolBox($newRedBean, $oldAdapter, $uuidWriter);
     R::configureFacadeWithToolbox($newToolBox);
     list($mansion, $rooms, $ghosts, $key) = R::dispenseAll('mansion,room*3,ghost*4,key');
     $mansion->name = 'Haunted Mansion';
     $mansion->xownRoomList = $rooms;
     $rooms[0]->name = 'Green Room';
     $rooms[1]->name = 'Red Room';
     $rooms[2]->name = 'Blue Room';
     $ghosts[0]->name = 'zero';
     $ghosts[1]->name = 'one';
     $ghosts[2]->name = 'two';
     $ghosts[3]->name = 'three';
     $rooms[0]->noLoad()->sharedGhostList = array($ghosts[0], $ghosts[1]);
     $rooms[1]->noLoad()->sharedGhostList = array($ghosts[0], $ghosts[2]);
     $rooms[2]->noLoad()->sharedGhostList = array($ghosts[1], $ghosts[3], $ghosts[2]);
     $rooms[2]->xownKey = array($key);
     //Can we store a bean hierachy with UUIDs?
     R::debug(1);
     $id = R::store($mansion);
     //exit;
     asrt(is_string($id), TRUE);
     asrt(strlen($id), 36);
     $haunted = R::load('mansion', $id);
     asrt($haunted->name, 'Haunted Mansion');
     asrt(is_string($haunted->id), TRUE);
     asrt(strlen($haunted->id), 36);
     asrt(is_array($haunted->xownRoomList), TRUE);
     asrt(count($haunted->ownRoom), 3);
     $rooms = $haunted->xownRoomList;
     //Do some counting...
     $greenRoom = NULL;
     foreach ($rooms as $room) {
         if ($room->name === 'Green Room') {
             $greenRoom = $room;
             break;
         }
     }
     asrt(!is_null($greenRoom), TRUE);
     asrt(is_array($greenRoom->with(' ORDER BY id ')->sharedGhostList), TRUE);
     asrt(count($greenRoom->sharedGhostList), 2);
     $names = array();
     foreach ($greenRoom->sharedGhost as $ghost) {
         $names[] = $ghost->name;
     }
     sort($names);
     $names = implode(',', $names);
     asrt($names, 'one,zero');
     $rooms = $haunted->xownRoomList;
     $blueRoom = NULL;
     foreach ($rooms as $room) {
         if ($room->name === 'Blue Room') {
             $blueRoom = $room;
             break;
         }
     }
     asrt(!is_null($blueRoom), TRUE);
     asrt(is_array($blueRoom->sharedGhostList), TRUE);
     asrt(count($blueRoom->sharedGhostList), 3);
     $names = array();
     foreach ($blueRoom->sharedGhost as $ghost) {
         $names[] = $ghost->name;
     }
     sort($names);
     $names = implode(',', $names);
     asrt($names, 'one,three,two');
     $rooms = $haunted->xownRoomList;
     $redRoom = NULL;
     foreach ($rooms as $room) {
         if ($room->name === 'Red Room') {
             $redRoom = $room;
             break;
         }
     }
     $names = array();
     foreach ($redRoom->sharedGhost as $ghost) {
         $names[] = $ghost->name;
     }
     sort($names);
     $names = implode(',', $names);
     asrt($names, 'two,zero');
     asrt(!is_null($redRoom), TRUE);
     asrt(is_array($redRoom->sharedGhostList), TRUE);
     asrt(count($redRoom->sharedGhostList), 2);
     //Can we repaint a room?
     $redRoom->name = 'Yellow Room';
     $id = R::store($redRoom);
     $yellowRoom = R::load('room', $id);
     asrt($yellowRoom->name, 'Yellow Room');
     asrt(!is_null($yellowRoom), TRUE);
     asrt(is_array($yellowRoom->sharedGhostList), TRUE);
     asrt(count($yellowRoom->sharedGhostList), 2);
     //Can we throw one ghost out?
     array_pop($yellowRoom->sharedGhost);
     R::store($yellowRoom);
     $yellowRoom = $yellowRoom->fresh();
     asrt($yellowRoom->name, 'Yellow Room');
     asrt(!is_null($yellowRoom), TRUE);
     asrt(is_array($yellowRoom->sharedGhostList), TRUE);
     asrt(count($yellowRoom->sharedGhostList), 1);
     //can we remove one of the rooms?
     asrt(R::count('key'), 1);
     $list = $mansion->withCondition(' "name" = ? ', array('Blue Room'))->xownRoomList;
     $room = reset($list);
     unset($mansion->xownRoomList[$room->id]);
     R::store($mansion);
     asrt(R::count('room'), 2);
     //and what about its dependent beans?
     asrt(R::count('key'), 0);
     asrt(R::count('ghost_room'), 3);
     //and can we find ghosts?
     $ghosts = R::find('ghost');
     asrt(count($ghosts), 4);
     $ghosts = R::findAll('ghost', 'ORDER BY id');
     asrt(count($ghosts), 4);
     $ghosts = R::findAll('ghost', 'ORDER BY id LIMIT 2');
     asrt(count($ghosts), 2);
     $ghostZero = R::findOne('ghost', ' "name" = ? ', array('zero'));
     asrt($ghostZero instanceof OODBBean, TRUE);
     //can we create link properties on existing tables?
     $blackRoom = R::dispense('room');
     $blackRoom->name = 'Black Room';
     $ghostZero->link('ghost_room', array('mood' => 'grumpy'))->room = $blackRoom;
     R::store($ghostZero);
     $ghostZero = $ghostZero->fresh();
     $list = $ghostZero->sharedRoomList;
     asrt(count($list), 3);
     $ghostZero = $ghostZero->fresh();
     $list = $ghostZero->withCondition(' ghost_room.mood = ? ', array('grumpy'))->sharedRoomList;
     asrt(count($list), 1);
     //can we load a batch?
     $ids = R::getCol('SELECT id FROM ghost');
     $ghosts = R::batch('ghost', $ids);
     asrt(count($ghosts), 4);
     //can we do an aggregation?
     $ghosts = $greenRoom->aggr('ownGhostRoom', 'ghost', 'ghost');
     asrt(count($ghosts), 2);
     //can we duplicate the mansion?
     asrt(R::count('mansion'), 1);
     asrt(R::count('room'), 3);
     asrt(R::count('ghost'), 4);
     $copy = R::dup($mansion);
     R::store($copy);
     asrt(R::count('mansion'), 2);
     asrt(R::count('room'), 5);
     //black room does not belong to mansion 1
     asrt(R::count('ghost'), 4);
     //can we do some counting using the list?
     asrt($copy->countOwn('room'), 2);
     $rooms = $copy->withCondition(' "name" = ? ', array('Green Room'))->xownRoomList;
     $room = reset($rooms);
     asrt($room->countShared('ghost'), 2);
     //Finally restore old toolbox
     R::configureFacadeWithToolbox($oldToolBox);
 }