nuke() public static method

This will remove all schema structures from the database. Only works in fluid mode. Be careful with this method.
public static nuke ( ) : void
return void
Esempio n. 1
0
 /**
  * Tests R::getInsertID convenience method.
  *
  * @return void
  */
 public function testGetInsertID()
 {
     R::nuke();
     $id = R::store(R::dispense('book'));
     $id2 = R::getInsertID();
     asrt($id, $id2);
 }
Esempio n. 2
0
 /**
  * Test varchar 191 condition.
  *
  * @return void
  */
 public function testInnoDBIndexLimit()
 {
     R::nuke();
     $book = R::dispense('book');
     $book->text = 'abcd';
     R::store($book);
     $columns = R::inspect('book');
     asrt(isset($columns['text']), TRUE);
     asrt($columns['text'], 'varchar(191)');
     $book = $book->fresh();
     $book->text = str_repeat('x', 190);
     R::store($book);
     $columns = R::inspect('book');
     asrt(isset($columns['text']), TRUE);
     asrt($columns['text'], 'varchar(191)');
     $book = $book->fresh();
     $book->text = str_repeat('x', 191);
     R::store($book);
     $columns = R::inspect('book');
     asrt(isset($columns['text']), TRUE);
     asrt($columns['text'], 'varchar(191)');
     $book = $book->fresh();
     $book->text = str_repeat('x', 192);
     R::store($book);
     $columns = R::inspect('book');
     asrt(isset($columns['text']), TRUE);
     asrt($columns['text'], 'varchar(255)');
 }
Esempio n. 3
0
 /**
  * Setup
  * 
  * @return void
  */
 public function setup()
 {
     R::nuke();
     //Prepare structure
     $book = R::dispense('book');
     $book->title = 'book';
     $pages = R::dispense('page', 10);
     foreach ($pages as $page) {
         $page->content = 'lorem ipsum';
         $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';
     }
     $book->xownPageList = $pages;
     $tags = R::dispense('tag', 6);
     foreach ($tags as $tag) {
         $tag->label = 'tag';
     }
     $book->sharedTagList = $tags;
     R::store($book);
 }
Esempio n. 4
0
 /**
  * Test query counter.
  *
  * @return void
  */
 public function testQueryCount()
 {
     R::nuke();
     R::store(R::dispense('bean'));
     R::resetQueryCount();
     asrt(R::getQueryCount(), 0);
     R::findOne('bean');
     asrt(R::getQueryCount(), 1);
     R::resetQueryCount();
     asrt(R::getQueryCount(), 0);
     R::findOne('bean');
     R::findOne('bean');
     R::findOne('bean');
     asrt(R::getQueryCount(), 0);
     R::store(R::dispense('bean2'));
     R::resetQueryCount();
     R::findOne('bean');
     R::findOne('bean2');
     asrt(R::getQueryCount(), 2);
     R::resetQueryCount();
     R::findOne('bean', ' id < 100');
     R::findOne('bean', ' id < 101');
     R::findOne('bean', ' id < 102');
     R::findOne('bean', ' id < 103');
     asrt(R::getQueryCount(), 4);
     R::findOne('bean', ' id < 100');
     R::findOne('bean', ' id < 101');
     R::findOne('bean', ' id < 102');
     R::findOne('bean', ' id < 103');
     asrt(R::getQueryCount(), 4);
     R::findOne('bean', ' id < 104');
     asrt(R::getQueryCount(), 5);
 }
Esempio n. 5
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');
 }
Esempio n. 6
0
 /**
  * Tests whether we can NULLify a parent bean
  * page->book if the parent (book) is already
  * NULL. (isset vs array_key_exists bug).
  *
  * @return void
  */
 public function testUnsetParent()
 {
     R::nuke();
     $book = R::dispense('book');
     $book->title = 'My Book';
     $page = R::dispense('page');
     $page->text = 'Lorem Ipsum';
     $book->ownPage[] = $page;
     R::store($book);
     $page = $page->fresh();
     R::freeze(TRUE);
     asrt((int) $page->book->id, (int) $book->id);
     unset($page->book);
     R::store($page);
     $page = $page->fresh();
     asrt((int) $page->book->id, (int) $book->id);
     $page->book = NULL;
     R::store($page);
     $page = $page->fresh();
     asrt($page->book, NULL);
     asrt($page->book_id, NULL);
     asrt($page->bookID, NULL);
     asrt($page->bookId, NULL);
     $page = R::dispense('page');
     $page->text = 'Another Page';
     $page->book = NULL;
     try {
         R::store($page);
         fail();
     } catch (\Exception $exception) {
         pass();
     }
     unset($page->book);
     R::store($page);
     $page = $page->fresh();
     $page->book = NULL;
     //this must set field id to NULL not ADD column!
     try {
         R::store($page);
         pass();
     } catch (\Exception $exception) {
         fail();
     }
     $page = $page->fresh();
     $page->book = NULL;
     R::store($page);
     $page = $page->fresh();
     asrt(is_null($page->book_id), TRUE);
     $page->book = $book;
     R::store($page);
     $page = $page->fresh();
     asrt((int) $page->book->id, (int) $book->id);
     $page->book = NULL;
     R::store($page);
     asrt(is_null($page->book_id), TRUE);
     asrt(is_null($page->book), TRUE);
     R::freeze(FALSE);
 }
Esempio n. 7
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);
 }
Esempio n. 8
0
 /**
  * Tests quick trash method: R::trash( type, id ).
  *
  * @return void
  */
 public function testQuickTrash()
 {
     R::nuke();
     $bean = R::dispense('bean');
     $id = R::store($bean);
     asrt(R::count('bean'), 1);
     R::trash('bean', $id);
     asrt(R::count('bean'), 0);
 }
Esempio n. 9
0
 /**
  * Tests whether we can clear the history of a bean.
  *
  * @return void
  */
 public function testClearHist()
 {
     R::nuke();
     $book = R::dispense('book');
     asrt($book->hasChanged('title'), FALSE);
     $book->title = 'book';
     asrt($book->hasChanged('title'), TRUE);
     R::store($book);
     asrt($book->hasChanged('title'), TRUE);
     $book->clearHistory();
     asrt($book->hasChanged('title'), FALSE);
 }
Esempio n. 10
0
 /**
  * Test recursive imports (formely known as R::graph).
  *
  * @return void
  */
 public function testRecursiveImport()
 {
     $book = R::dispense(array('_type' => 'book', 'title' => 'The magic book', 'ownPageList' => array(array('_type' => 'page', 'content' => 'magic potions'), array('_type' => 'page', 'content' => 'magic spells'))));
     $id = R::store($book);
     $book = R::load('book', $id);
     asrt($book->title, 'The magic book');
     $pages = $book->with(' ORDER BY content ASC ')->ownPageList;
     asrt(count($pages), 2);
     $page1 = array_shift($pages);
     asrt($page1->content, 'magic potions');
     $page2 = array_shift($pages);
     asrt($page2->content, 'magic spells');
     R::nuke();
     $book = R::dispense(array('_type' => 'book', 'title' => 'The magic book', 'author' => array('_type' => 'author', 'name' => 'Dr. Evil'), 'coAuthor' => array('_type' => 'author', 'name' => 'Dr. Creepy'), 'ownPageList' => array(array('_type' => 'page', 'content' => 'magic potions', 'ownRecipe' => array('a' => array('_type' => 'recipe', 'name' => 'Invisibility Salad'), 'b' => array('_type' => 'recipe', 'name' => 'Soup of Madness'), 'c' => array('_type' => 'recipe', 'name' => 'Love cake'))), array('_type' => 'page', 'content' => 'magic spells')), 'sharedCategory' => array(array('_type' => 'category', 'label' => 'wizardry'))));
     $id = R::store($book);
     $book = R::load('book', $id);
     asrt($book->title, 'The magic book');
     $pages = $book->with(' ORDER BY content ASC ')->ownPageList;
     asrt(count($pages), 2);
     $page1 = array_shift($pages);
     asrt($page1->content, 'magic potions');
     $page2 = array_shift($pages);
     asrt($page2->content, 'magic spells');
     $recipes = $page1->with(' ORDER BY name ASC ')->ownRecipeList;
     asrt(count($recipes), 3);
     $recipe1 = array_shift($recipes);
     asrt($recipe1->name, 'Invisibility Salad');
     $recipe2 = array_shift($recipes);
     asrt($recipe2->name, 'Love cake');
     $recipe3 = array_shift($recipes);
     asrt($recipe3->name, 'Soup of Madness');
     $categories = $book->sharedCategoryList;
     asrt(count($categories), 1);
     $category = reset($categories);
     asrt($category->label, 'wizardry');
     asrt($book->author->name, 'Dr. Evil');
     asrt($book->fetchAs('author')->coAuthor->name, 'Dr. Creepy');
     try {
         R::dispense(array());
         fail();
     } catch (RedException $ex) {
         pass();
     }
     try {
         R::dispense(array('property' => 'value'));
         fail();
     } catch (RedException $ex) {
         pass();
     }
 }
Esempio n. 11
0
 /**
  * Test empty collections (NULLCursor).
  *
  * @return void
  */
 public function testEmptyCollection()
 {
     R::nuke();
     $page = R::dispense('page');
     $page->content = 'aaa';
     R::store($page);
     $collection = R::findCollection('page');
     asrt(get_class($collection), 'RedBeanPHP\\BeanCollection');
     $collection = R::findCollection('page', ' content  =  ?', array('bbb'));
     asrt(get_class($collection), 'RedBeanPHP\\BeanCollection');
     asrt(is_null($collection->next()), TRUE);
     $collection = R::findCollection('something');
     asrt(get_class($collection), 'RedBeanPHP\\BeanCollection');
     asrt(is_null($collection->next()), TRUE);
     $collection->close();
 }
Esempio n. 12
0
 protected function setUp()
 {
     $config = new Config();
     $config->setProtected('basePath', BASE_PATH);
     setCommonConfig($config);
     setUniqueConfig($config);
     Application::setupRedBean('sqlite:test.db', 'user', 'password', $this->frozen, 'sqlite');
     R::freeze(false);
     R::nuke();
     R::freeze($this->frozen);
     $this->app = __setupApp();
     /** $http Mock Http object. */
     $http = $this->getMock('Skully\\Core\\Http');
     $http->expects($this->any())->method('redirect')->will($this->returnCallback('stubRedirect'));
     $this->app->setHttp($http);
 }
Esempio n. 13
0
 /**
  * Nuclear test suite.
  * 
  * @return void
  */
 public function testNuke()
 {
     $bean = R::dispense('bean');
     R::store($bean);
     asrt(count(R::getWriter()->getTables()), 1);
     R::nuke();
     asrt(count(R::getWriter()->getTables()), 0);
     $bean = R::dispense('bean');
     R::store($bean);
     asrt(count(R::getWriter()->getTables()), 1);
     R::freeze();
     R::nuke();
     // No effect
     asrt(count(R::getWriter()->getTables()), 1);
     R::freeze(FALSE);
 }
Esempio n. 14
0
 /**
  * Tests automatic resolvement of parent beans
  * without fetchAs() using inferFetchType (foreign keys).
  *
  * @return void
  */
 public function testAutoResolver()
 {
     R::nuke();
     list($project, $teacher, $student) = R::dispenseAll('project,person,person');
     $teacher->name = 'teacher';
     $student->name = 'student';
     $project->teacher = $teacher;
     $project->student = $student;
     R::store($project);
     $project = $project->fresh();
     asrt($project->teacher instanceof OODBBean, TRUE);
     asrt($project->student instanceof OODBBean, TRUE);
     asrt($project->teacher->name, 'teacher');
     asrt($project->student->name, 'student');
     $project2 = R::dispense('project');
     $teacher2 = R::dispense('person');
     $teacher2->name = 'teacher2';
     $project2->teacher = $teacher2;
     R::store($project2);
     $project2 = $project2->fresh();
     asrt($project2->teacher instanceof OODBBean, TRUE);
     asrt($project2->teacher->name, 'teacher2');
     asrt(is_null($project2->student), TRUE);
     $project = $project->fresh();
     asrt($project->fetchAs('person')->teacher instanceof OODBBean, TRUE);
     asrt($project->fetchAs('person')->student instanceof OODBBean, TRUE);
     asrt($project->fetchAs('person')->teacher->name, 'teacher');
     asrt($project->fetchAs('person')->student->name, 'student');
     $project = $project->fresh();
     $export = R::exportAll(array($project), TRUE);
     asrt(isset($export[0]['teacher']['name']), TRUE);
     asrt(isset($export[0]['student']['name']), TRUE);
     asrt($export[0]['teacher']['name'], 'teacher');
     asrt($export[0]['student']['name'], 'student');
     //Also test the default implementation...
     $nullWriter = new \NullWriter(R::getDatabaseAdapter());
     asrt(is_null($nullWriter->inferFetchType('test', 'test')), TRUE);
     //Realteacher should take precedence over fetchAs-teacher, name conventions first!
     //also: ensure we do not use autoresolv for anything except when truly necessary! (performance)
     $realTeacher = R::dispense('teacher');
     $realTeacher->name = 'real';
     R::store($realTeacher);
     //ID must be same
     asrt($realTeacher->id, $teacher->id);
     $project = $project->fresh();
     asrt($project->teacher->name, 'real');
 }
Esempio n. 15
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');
 }
Esempio n. 16
0
 /**
  * Tests tags with SQL.
  *
  * @return void
  */
 public function testTagsWithSQL()
 {
     R::nuke();
     list($m1, $m2, $m3) = R::dispense('movie', 3);
     $m1->title = 'Frankenstein';
     $m2->title = 'Fall of the House Usher';
     $m3->title = 'Sleepy Hollow';
     R::tag($m1, 'horror,gothic');
     R::tag($m2, 'horror,gothic,short');
     R::tag($m3, 'horror,legend');
     asrt(count(R::tagged('movie', 'horror')), 3);
     asrt(count(R::tagged('movie', 'horror', ' LIMIT 2')), 2);
     asrt(count(R::tagged('movie', 'horror', ' LIMIT ?', array(2))), 2);
     asrt(count(R::tagged('movie', 'horror', ' ORDER BY movie.title DESC LIMIT ?', array(2))), 2);
     asrt(count(R::tagged('movie', 'horror,gothic', ' ORDER BY movie.title DESC LIMIT ?', array(1))), 1);
     asrt(count(R::tagged('movie', 'horror,gothic')), 3);
     asrt(count(R::taggedAll('movie', 'horror,gothic')), 2);
     asrt(count(R::tagged('movie', 'horror,gothic', ' LIMIT ? ', array(2))), 2);
     asrt(count(R::taggedAll('movie', 'horror,gothic', ' LIMIT ? ', array(2))), 2);
     asrt(count(R::tagged('movie', 'horror,gothic', ' LIMIT ? ', array(1))), 1);
     asrt(count(R::taggedAll('movie', 'horror,gothic', ' LIMIT ? ', array(1))), 1);
     asrt(count(R::tagged('movie', 'horror,legend', ' LIMIT ? ', array(1))), 1);
     asrt(count(R::taggedAll('movie', 'horror,legend', ' LIMIT ? ', array(1))), 1);
     asrt(count(R::tagged('movie', 'gothic,legend', ' LIMIT ? ', array(1))), 1);
     asrt(count(R::taggedAll('movie', 'gothic,legend', ' LIMIT ? ', array(1))), 0);
     asrt(count(R::tagged('movie', 'romance', ' LIMIT ? ', array(1))), 0);
     asrt(count(R::taggedAll('movie', 'romance', ' LIMIT ? ', array(1))), 0);
     asrt(count(R::tagged('movie', 'romance,xmas', ' LIMIT ? ', array(1))), 0);
     asrt(count(R::taggedAll('movie', 'romance,xmas', ' LIMIT ? ', array(1))), 0);
     asrt(count(R::tagged('movie', 'gothic,short', ' LIMIT ? ', array(4))), 2);
     asrt(count(R::taggedAll('movie', 'gothic,short', ' LIMIT ? ', array(4))), 1);
     asrt(count(R::tagged('movie', 'gothic,short', ' LIMIT 4 ')), 2);
     asrt(count(R::taggedAll('movie', 'gothic,short', ' LIMIT 4 ')), 1);
     asrt(count(R::tagged('movie', 'gothic,short', ' ORDER BY movie.id DESC LIMIT 4 ')), 2);
     asrt(count(R::taggedAll('movie', 'gothic,short', ' ORDER BY movie.id DESC LIMIT 4 ')), 1);
     asrt(count(R::tagged('movie', 'short', ' LIMIT ? ', array(4))), 1);
     asrt(count(R::taggedAll('movie', 'short', ' LIMIT ? ', array(4))), 1);
     asrt(count(R::tagged('movie', '', ' LIMIT ? ', array(4))), 0);
     asrt(count(R::taggedAll('movie', '', ' LIMIT ? ', array(4))), 0);
     asrt(count(R::tagged('movie', '', ' LIMIT 4 ')), 0);
     asrt(count(R::taggedAll('movie', '', ' LIMIT 4 ')), 0);
     asrt(count(R::tagged('movie', '', '')), 0);
     asrt(count(R::taggedAll('movie', '', '')), 0);
     asrt(count(R::tagged('movie', '')), 0);
     asrt(count(R::taggedAll('movie', '')), 0);
 }
Esempio n. 17
0
 /**
  * Setup
  *
  * @return void
  */
 private function _createBook()
 {
     R::nuke();
     $book = R::dispense('book');
     $pages = R::dispense('page', 2);
     $ads = R::dispense('ad', 3);
     $tags = R::dispense('tag', 2);
     $author = R::dispense('author');
     $coauthor = R::dispense('author');
     $book->alias('magazine')->ownAd = $ads;
     $book->ownPage = $pages;
     $book->sharedTag = $tags;
     $book->via('connection')->sharedUser = array(R::dispense('user'));
     $book->author = $author;
     $book->coauthor = $coauthor;
     R::store($book);
     return $book->fresh();
 }
Esempio n. 18
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();
     }
 }
Esempio n. 19
0
 /**
  * Test whether we cannot add unique constraints on chilled tables,
  * otherwise you cannot avoid this from happening when adding beans to the
  * shared list :) -- this is almost a theoretical issue however we want it
  * to work according to specifications!
  *
  * @return void
  */
 public function testDontAddUniqueConstraintForChilledBeanTypes()
 {
     R::nuke();
     $person = R::dispense('person');
     $role = R::dispense('role');
     $person->sharedRole[] = $role;
     R::store($person);
     $person->sharedRole[] = R::dispense('role');
     R::store($person);
     $bean = R::getRedBean()->dispense('person_role');
     $bean->personId = $person->id;
     $bean->roleId = $role->id;
     try {
         R::store($bean);
         fail();
     } catch (\Exception $e) {
         pass();
     }
     asrt(R::count('person_role'), 2);
     R::nuke();
     $link = R::getRedBean()->dispense('person_role');
     $person = R::dispense('person');
     $role = R::dispense('role');
     $link->person = $person;
     $link->role = $role;
     R::store($link);
     R::freeze(array('person_role'));
     $person->sharedRole[] = R::dispense('role');
     R::store($person);
     $bean = R::getRedBean()->dispense('person_role');
     $bean->personId = $person->id;
     $bean->roleId = $role->id;
     try {
         R::store($bean);
         pass();
     } catch (\Exception $e) {
         fail();
     }
     asrt(R::count('person_role'), 3);
     R::freeze(array());
     //set freeze to FALSE and clear CHILL LIST!
 }
Esempio n. 20
0
 protected function setUp()
 {
     $config = new Config();
     $config->setProtected('basePath', BASE_PATH);
     setCommonConfig($config);
     setUniqueConfig($config);
     $dbConfig = $config->getProtected('dbConfig');
     if ($dbConfig['type'] == 'mysql') {
         Application::setupRedBean("mysql:host={$dbConfig['host']};dbname={$dbConfig['dbname']};port={$dbConfig['port']}", $dbConfig['user'], $dbConfig['password'], $config->getProtected('isDevMode'));
     } elseif ($dbConfig['type'] == 'sqlite') {
         Application::setupRedBean("sqlite:{$dbConfig['dbname']}", $dbConfig['user'], $dbConfig['password'], $config->getProtected('isDevMode'));
     }
     R::freeze(false);
     R::nuke();
     R::freeze($this->frozen);
     $this->app = __setupApp();
     /** $http Mock Http object. */
     $http = $this->getMock('Skully\\Core\\Http');
     $http->expects($this->any())->method('redirect')->will($this->returnCallback('stubRedirect'));
     $this->app->setHttp($http);
 }
Esempio n. 21
0
 /**
  * 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);
 }
Esempio n. 22
0
 /**
  * Run the actual test
  */
 public function run()
 {
     $class = new \ReflectionClass($this);
     $skip = array('run', 'getTargetDrivers', 'onEvent');
     // Call all methods except run automatically
     foreach ($class->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
         // Skip methods inherited from parent class
         if ($method->class != $class->getName()) {
             continue;
         }
         if (in_array($method->name, $skip)) {
             continue;
         }
         $classname = str_replace($class->getParentClass()->getName() . '_', '', $method->class);
         printtext("\n\t" . $classname . "->" . $method->name . " [" . $method->class . "->" . $method->name . "]" . " \n\t");
         $call = $method->name;
         $this->{$call}();
         try {
             R::nuke();
         } catch (\PDOException $e) {
             // Some tests use a broken database on purpose, so an exception is ok
         }
     }
 }
Esempio n. 23
0
 /**
  * Test special data types.
  *
  * @return void
  */
 public function testSpecialDataTypes()
 {
     testpack('Special data types');
     $bean = R::dispense('bean');
     $bean->date = 'someday';
     R::store($bean);
     $cols = R::getColumns('bean');
     asrt($cols['date'], 'TEXT');
     $bean = R::dispense('bean');
     $bean->date = '2011-10-10';
     R::nuke();
     $bean = R::dispense('bean');
     $bean->date = '2011-10-10';
     R::store($bean);
     $cols = R::getColumns('bean');
     asrt($cols['date'], 'NUMERIC');
 }
Esempio n. 24
0
 /**
  * Various.
  * Tests whether writer correctly handles keyword 'group' and SQL state 23000 issue.
  * These tests remain here to make sure issues 9 and 10 never happen again.
  * However this bug will probably never re-appear due to changed architecture.
  *
  * @return void
  */
 public function testIssue9and10()
 {
     $toolbox = R::getToolBox();
     $redbean = $toolbox->getRedBean();
     $adapter = $toolbox->getDatabaseAdapter();
     $a = new AssociationManager($toolbox);
     $book = $redbean->dispense("book");
     $author1 = $redbean->dispense("author");
     $author2 = $redbean->dispense("author");
     $book->title = "My First Post";
     $author1->name = "Derek";
     $author2->name = "Whoever";
     $a->associate($book, $author1);
     $a->associate($book, $author2);
     pass();
     testpack("Test Association Issue Group keyword (Issues 9 and 10)");
     R::nuke();
     $group = $redbean->dispense("group");
     $group->name = "mygroup";
     $redbean->store($group);
     try {
         $a->associate($group, $book);
         pass();
     } catch (SQL $e) {
         fail();
     }
     // Test issue SQL error 23000
     try {
         $a->associate($group, $book);
         pass();
     } catch (SQL $e) {
         fail();
     }
     asrt((int) $adapter->getCell("select count(*) from book_group"), 1);
     //just 1 rec!
 }
Esempio n. 25
0
 /**
  * Test self-referential N-M relations.
  */
 public function testSelfRefNM()
 {
     R::nuke();
     $friend1 = R::xdispense(FRIEND);
     $friend1->name = 'f1';
     $friend2 = R::xdispense(FRIEND);
     $friend2->name = 'f2';
     $friend3 = R::xdispense(FRIEND);
     $friend3->name = 'f3';
     $friend1->{FRIENDLIST} = array($friend2, $friend3);
     $friend3->{FRIENDLIST} = array($friend1);
     R::storeAll(array($friend1, $friend2, $friend3));
     $friend1 = $friend1->fresh();
     $friend2 = $friend2->fresh();
     $friend3 = $friend3->fresh();
     asrt(count($friend1->{FRIENDLIST}), 2);
     asrt(count($friend2->{FRIENDLIST}), 1);
     asrt(count($friend3->{FRIENDLIST}), 1);
     $friend = reset($friend3->{FRIENDLIST});
     asrt($friend->name, 'f1');
 }
Esempio n. 26
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);
    }
Esempio n. 27
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');
 }
Esempio n. 28
0
 /**
  * Test whether a duplicate bean in the list isnt saved.
  * This was an issue with Postgres while testing the threeway tables.
  * Postgres returned the ID as a string while other drivers returned
  * a numeric value causing different outcome in array_diff when
  * calculating the shared additions.
  * 
  * @return void
  */
 public function testIssueWithDriverReturnID()
 {
     AQueryWriter::clearRenames();
     R::nuke();
     $book = R::dispense('book');
     $page = R::dispense('page');
     $book->sharedPageList[] = $page;
     R::store($book);
     asrt(R::count('page'), 1);
     $book = $book->fresh();
     $book->sharedPageList[] = $page;
     R::store($book);
     //don't save the duplicate bean!
     asrt(R::count('page'), 1);
     $book = $book->fresh();
     $page->item = 2;
     //even if we change a property ?
     $book->sharedPageList[] = $page;
     R::store($book);
     foreach ($book->sharedPageList as $listItem) {
         asrt(is_string($listItem->id), TRUE);
     }
     //same test but for own-list
     R::nuke();
     $book = R::dispense('book');
     $page = R::dispense('page');
     $book->ownPageList[] = $page;
     R::store($book);
     asrt(R::count('page'), 1);
     $book = $book->fresh();
     $book->ownPageList[] = $page;
     R::store($book);
     //don't save the duplicate bean!
     asrt(R::count('page'), 1);
     $book = $book->fresh();
     $book->ownPageList[] = $page;
     $page->item = 3;
     R::store($book);
     //don't save the duplicate bean!
     asrt(R::count('page'), 1);
     foreach ($book->ownPageList as $listItem) {
         asrt(is_string($listItem->id), TRUE);
     }
     AQueryWriter::clearRenames();
 }
Esempio n. 29
0
 /**
  * 
  * Same as above.. test keep cache.
  * 
  * @return void
  */
 public function testInstructNoDrop()
 {
     $str = 'SELECT * FROM ' . R::getWriter()->esc('bean', TRUE) . ' -- keep-cache';
     $bean = R::dispense('bean');
     $bean->title = 'abc';
     $id = R::store($bean);
     $bean = R::load('bean', $id);
     $bean->title = 'xxx';
     R::store($bean);
     R::exec($str);
     $bean = R::load('bean', $id);
     asrt($bean->title, 'abc');
     R::nuke();
     // Now INSTRUCT the cache to not drop the cache CASE 2
     $str = 'SELECT * FROM ' . R::getWriter()->esc('bean', TRUE) . ' -- keep-cache';
     $bean = R::dispense('bean');
     $bean->title = 'abc';
     $id = R::store($bean);
     $bean = R::load('bean', $id);
     $bean->title = 'xxx';
     R::store($bean);
     R::findOne('bean', ' title = ? ', array('cache'));
     $bean = R::load('bean', $id);
     asrt($bean->title, 'xxx');
 }
Esempio n. 30
0
 /**
  * Test effect of via on shared list removal of beans.
  *
  * @return void
  */
 public function testViaAndRemove()
 {
     R::nuke();
     $project = R::dispense('project');
     $employees = R::dispense('employee', 2);
     $project->via('partcipant')->sharedEmployeeList = $employees;
     R::store($project);
     asrt(R::count('employee'), 2);
     asrt(R::count('participant'), 2);
     $project = $project->fresh();
     $project->sharedEmployee = array();
     R::store($project);
     asrt(R::count('employee'), 2);
     asrt(R::count('participant'), 0);
 }