/**
  * Breaks the association between two beans. This method unassociates two beans. If the
  * method succeeds the beans will no longer form an association. In the database
  * this means that the association record will be removed. This method uses the
  * OODB trash() method to remove the association links, thus giving FUSE models the
  * opportunity to hook-in additional business logic. If the $fast parameter is
  * set to boolean TRUE this method will remove the beans without their consent,
  * bypassing FUSE. This can be used to improve performance.
  *
  * @param RedBean_OODBBean $bean1 first bean
  * @param RedBean_OODBBean $bean2 second bean
  * @param boolean          $fast  If TRUE, removes the entries by query without FUSE
  *
  * @return void
  */
 public function unassociate($beans1, $beans2, $fast = NULL)
 {
     $beans1 = !is_array($beans1) ? array($beans1) : $beans1;
     $beans2 = !is_array($beans2) ? array($beans2) : $beans2;
     foreach ($beans1 as $bean1) {
         foreach ($beans2 as $bean2) {
             try {
                 $this->oodb->store($bean1);
                 $this->oodb->store($bean2);
                 $type1 = $bean1->getMeta('type');
                 $type2 = $bean2->getMeta('type');
                 $row = $this->writer->queryRecordLink($type1, $type2, $bean1->id, $bean2->id);
                 $linkType = $this->getTable(array($type1, $type2));
                 if ($fast) {
                     $this->writer->deleteRecord($linkType, array('id' => $row['id']));
                     return;
                 }
                 $beans = $this->oodb->convertToBeans($linkType, array($row));
                 if (count($beans) > 0) {
                     $bean = reset($beans);
                     $this->oodb->trash($bean);
                 }
             } catch (RedBean_Exception_SQL $exception) {
                 $this->handleException($exception);
             }
         }
     }
 }
Beispiel #2
0
 /**
  * Breaks the association between two beans. This method unassociates two beans. If the
  * method succeeds the beans will no longer form an association. In the database
  * this means that the association record will be removed. This method uses the
  * OODB trash() method to remove the association links, thus giving FUSE models the
  * opportunity to hook-in additional business logic. If the $fast parameter is
  * set to boolean TRUE this method will remove the beans without their consent,
  * bypassing FUSE. This can be used to improve performance.
  *
  * @param RedBean_OODBBean $bean1 first bean
  * @param RedBean_OODBBean $bean2 second bean
  * @param boolean          $fast  If TRUE, removes the entries by query without FUSE
  */
 public function unassociate(RedBean_OODBBean $bean1, RedBean_OODBBean $bean2, $fast = null)
 {
     $this->oodb->store($bean1);
     $this->oodb->store($bean2);
     $table = $this->getTable(array($bean1->getMeta("type"), $bean2->getMeta("type")));
     $idfield1 = $this->writer->getIDField($bean1->getMeta("type"));
     $idfield2 = $this->writer->getIDField($bean2->getMeta("type"));
     $type = $bean1->getMeta("type");
     if ($type == $bean2->getMeta("type")) {
         $type .= "2";
         $cross = 1;
     } else {
         $cross = 0;
     }
     $property1 = $type . "_id";
     $property2 = $bean2->getMeta("type") . "_id";
     $value1 = (int) $bean1->{$idfield1};
     $value2 = (int) $bean2->{$idfield2};
     try {
         $rows = $this->writer->selectRecord($table, array($property1 => array($value1), $property2 => array($value2)), null, $fast);
         if ($cross) {
             $rows2 = $this->writer->selectRecord($table, array($property2 => array($value1), $property1 => array($value2)), null, $fast);
             if ($fast) {
                 return;
             }
             $rows = array_merge($rows, $rows2);
         }
         if ($fast) {
             return;
         }
         $beans = $this->oodb->convertToBeans($table, $rows);
         foreach ($beans as $link) {
             $link->setMeta("assoc." . $bean1->getMeta("type"), $bean1);
             $link->setMeta("assoc." . $bean2->getMeta("type"), $bean2);
             $this->oodb->trash($link);
         }
     } catch (RedBean_Exception_SQL $e) {
         if (!$this->writer->sqlStateIn($e->getSQLState(), array(RedBean_QueryWriter::C_SQLSTATE_NO_SUCH_COLUMN, RedBean_QueryWriter::C_SQLSTATE_NO_SUCH_TABLE))) {
             throw $e;
         }
     }
     return;
 }
Beispiel #3
0
 /**
  * Deletes the specified bean.
  *
  * @param RedBean_OODBBean|RedBean_SimpleModel $bean bean to be deleted
  *
  * @return mixed
  */
 public static function trash($bean)
 {
     return self::$redbean->trash($bean);
 }
Beispiel #4
0
 /**
  * Trashes a RedBean OODBBean and removes it from cache.
  *
  * @param RedBean_OODBBean $bean bean
  *
  * @return mixed
  */
 public function trash($bean)
 {
     $type = $bean->getMeta('type');
     $id = $bean->id;
     if (isset($this->cache[$type][$id])) {
         unset($this->cache[$type][$id]);
     }
     parent::trash($bean);
 }
Beispiel #5
0
 /**
  * Deletes the inner bean from the database.
  */
 public function delete()
 {
     $this->redbean->trash($this->bean);
 }
 /**
  * Removes a bean from the database.
  * This function will remove the specified RedBean_OODBBean
  * Bean Object from the database.
  *
  * @param RedBean_OODBBean|RedBean_SimpleModel $bean bean you want to remove from database
  *
  * @return void
  *
  * @throws RedBean_Exception_Security
  */
 public function trash($bean)
 {
     $this->redbean->trash($bean);
 }
Beispiel #7
0
 /**
  * Deletes the specified bean.
  * @param RedBean_OODBBean $bean
  * @return mixed
  */
 public static function trash(RedBean_OODBBean $bean)
 {
     return self::$redbean->trash($bean);
 }
Beispiel #8
0
$movies = $a2->related($genre, "movie");
asrt(count($movies), 1);
asrt((int) $movies[0], (int) $movieid);
$a2->unassociate($movie, $genre);
$movies = $a2->related($genre, "movie");
asrt(count($movies), 0);
$a2->clearRelations($movie, "movie");
$movies = $a2->related($movie1, "movie");
asrt(count($movies), 0);
$pdo->setDebugMode(0);
$t2 = new RedBean_TreeManager($toolbox2);
$t2->attach($movie1, $movie2);
$movies = $t2->children($movie1);
asrt(count($movies), 1);
asrt($movies[$movieid2]->name, "movie 2");
$redbean2->trash($movie1);
asrt((int) $adapter->getCell("SELECT count(*) FROM movie"), 1);
$redbean2->trash($movie2);
asrt((int) $adapter->getCell("SELECT count(*) FROM movie"), 0);
$columns = array_keys($writer->getColumns("movie_movie"));
asrt(in_array("movie_movie_id", $columns), true);
//test locking
$logger = new RedBean_Plugin_ChangeLogger($toolbox);
$redbean->addEventListener("open", $logger);
$redbean->addEventListener("update", $logger);
$redbean->addEventListener("delete", $logger);
testpack("Test RedBean Locking: Change Logger method ");
$observers = RedBean_Setup::getAttachedObservers();
$page = $redbean->dispense("page");
$page->name = "a page";
$id = $redbean->store($page);
Beispiel #9
0
 /**
  * Deletes the bean
  * @param $deco
  * @return unknown_type
  */
 public static function delete($deco)
 {
     RedBean_OODB::trash($deco->getData());
 }
Beispiel #10
0
 /**
  * Trashes a bean and removes the bean from cache.
  * @param RedBean_OODBBean $bean
  */
 public function trash(RedBean_OODBBean $bean)
 {
     $this->removeFromCache($bean);
     return $this->oodb->trash($bean);
 }
Beispiel #11
0
function testsperengine()
{
    global $tests;
    SmartTest::instance()->progress();
    SmartTest::instance()->testPack = "perform generic bean manipulation";
    $ok = 1;
    $bean = RedBean_OODB::dispense("note");
    $bean->message = "hai";
    $bean->color = 3;
    $bean->date = time();
    $bean->special = 'n';
    $bean->state = 90;
    RedBean_OODB::set($bean);
    $bean2 = RedBean_OODB::getById("note", 1);
    if ($bean2->state != 90 || $bean2->special != 'n' || $bean2->message != 'hai') {
        $ok = 0;
        SmartTest::failedTest();
    }
    SmartTest::instance()->progress();
    $bean->message = "lorem ipsum";
    RedBean_OODB::set($bean);
    $bean->message = 1;
    $bean->color = "green";
    $bean->date = str_repeat("BLABLA", 100);
    RedBean_OODB::set($bean);
    $note = $bean;
    SmartTest::instance()->progress();
    $person = RedBean_OODB::dispense("person");
    $person->age = 50;
    $person->name = "Bob";
    $person->gender = "m";
    RedBean_OODB::set($person);
    RedBean_OODB::associate($person, $note);
    $memo = RedBean_OODB::getById("note", 1);
    $authors = RedBean_OODB::getAssoc($memo, "person");
    if (count($authors) !== 1) {
        SmartTest::failedTest();
    }
    RedBean_OODB::trash($authors[1]);
    $authors = RedBean_OODB::getAssoc($memo, "person");
    if (count($authors) > 0) {
        $ok = 0;
    }
    if (!$ok) {
        SmartTest::failedTest();
    }
    //unit tests
    //drop the note table
    SmartTest::instance()->progress();
    SmartTest::instance()->testPack = "dispense an RedBean_OODB Bean";
    $oBean = RedBean_OODB::dispense();
    if (!$oBean instanceof OODBBean) {
        SmartTest::failedTest();
    }
    SmartTest::instance()->progress();
    SmartTest::instance()->testPack = "put a bean in the database";
    $person = RedBean_OODB::dispense("person");
    $person->name = "John";
    $person->age = 35;
    $person->gender = "m";
    $person->hasJob = true;
    $id = RedBean_OODB::set($person);
    $johnid = $id;
    $person2 = RedBean_OODB::getById("person", $id);
    if ($person2->age != $person->age) {
        SmartTest::failedTest();
    }
    $person2->anotherprop = 2;
    RedBean_OODB::set($person2);
    $person = RedBean_OODB::dispense("person");
    $person->name = "Bob";
    $person->age = 50;
    $person->gender = "m";
    $person->hasJob = false;
    $bobid = RedBean_OODB::set($person);
    SmartTest::instance()->progress();
    SmartTest::instance()->testPack = "find records on basis of similarity";
    $ids = RedBean_OODB::getBySQL("`gender`={gender} order by `name` asc", array("gender" => "m"), "person");
    if (count($ids) != 2) {
        SmartTest::failedTest();
    }
    SmartTest::instance()->progress();
    $ids = RedBean_OODB::getBySQL("`gender`={gender} OR `color`={clr} ", array("gender" => "m", "clr" => "red"), "person");
    if (count($ids) != 0) {
        SmartTest::failedTest();
    }
    SmartTest::instance()->progress();
    $ids = RedBean_OODB::getBySQL("`gender`={gender} AND `color`={clr} ", array("gender" => "m", "clr" => "red"), "person");
    if (count($ids) != 0) {
        SmartTest::failedTest();
    }
    SmartTest::instance()->progress();
    R::gen("Person");
    $dummy = new Person();
    $dummy->age = 40;
    SmartTest::instance()->test(count(Person::find($dummy, array("age" => ">"))), 1);
    $dummy->age = 20;
    SmartTest::instance()->test(count(Person::find($dummy, array("age" => ">"))), 2);
    $dummy->age = 100;
    SmartTest::instance()->test(count(Person::find($dummy, array("age" => ">"))), 0);
    $dummy->age = 100;
    SmartTest::instance()->test(count(Person::find($dummy, array("age" => "<="))), 2);
    $dummy->name = "ob";
    SmartTest::instance()->test(count(Person::find($dummy, array("name" => "LIKE"))), 1);
    $dummy->name = "o";
    SmartTest::instance()->test(count(Person::find($dummy, array("name" => "LIKE"))), 2);
    $dummy->gender = "m";
    SmartTest::instance()->test(count(Person::find($dummy, array("gender" => "="))), 2);
    $dummy->gender = "f";
    SmartTest::instance()->test(count(Person::find($dummy, array("gender" => "="))), 0);
    SmartTest::instance()->test(count(Person::listAll()), 2);
    SmartTest::instance()->test(count(Person::listAll(0, 1)), 1);
    SmartTest::instance()->test(count(Person::listAll(1)), 1);
    $can = Person::where("`gender`={gender} order by `name`  asc", array("gender" => "m"), "person");
    //test array access
    foreach ($can as $item) {
        if ($item->getName() == "Bob" || $item->getName() == "John") {
            SmartTest::instance()->progress();
        } else {
            SmartTest::failedTest();
        }
    }
    //test array access
    $bean = $can[0];
    if ($bean->name == "Bob") {
        SmartTest::instance()->progress();
    } else {
        SmartTest::failedTest();
    }
    if ($can->count() != 2) {
        SmartTest::failedTest();
    }
    SmartTest::instance()->progress();
    $can->rewind();
    SmartTest::instance()->test($can->key(), 0);
    SmartTest::instance()->test($can->valid(), true);
    SmartTest::instance()->test($can->current()->getName(), "Bob");
    $can->next();
    SmartTest::instance()->test($can->key(), 1);
    SmartTest::instance()->test($can->valid(), false);
    SmartTest::instance()->test($can->current()->getName(), "John");
    $can->seek(0);
    SmartTest::instance()->test($can->key(), 0);
    $beans = $can->getBeans();
    if (count($beans) != 2) {
        SmartTest::failedTest();
    }
    SmartTest::instance()->progress();
    //test slicing
    $can->slice(0, 1);
    $can->rewind();
    SmartTest::instance()->test($can->current()->getName(), "Bob");
    SmartTest::instance()->test($can->count(), 1);
    $b1 = array_shift($beans);
    if ($b1->name != "Bob") {
        SmartTest::failedTest();
    }
    SmartTest::instance()->progress();
    //basic functionality where()
    $beans = Person::where("`gender`={gender} order by `name` asc", array("gender" => "m"), "person")->getBeans();
    if (count($beans) != 2) {
        SmartTest::failedTest();
    }
    SmartTest::instance()->progress();
    //without backticks should still work
    $beans = Person::where("gender={person} order by `name` asc", array("person" => "m"), "person")->getBeans();
    if (count($beans) != 2) {
        SmartTest::failedTest();
    }
    SmartTest::instance()->progress();
    //like comparing should still work
    $beans = Person::where("gender={gender} and `name` LIKE {name} order by `name` asc", array("gender" => "m", "name" => "B%"), "person")->getBeans();
    if (count($beans) != 1) {
        SmartTest::failedTest();
    }
    SmartTest::instance()->progress();
    $searchBean = RedBean_OODB::dispense("person");
    $searchBean->gender = "m";
    SmartTest::instance()->progress();
    SmartTest::instance()->testPack = "associate beans with eachother?";
    $app = RedBean_OODB::dispense("appointment");
    $app->kind = "dentist";
    RedBean_OODB::set($app);
    RedBean_OODB::associate($person2, $app);
    $appforbob = array_shift(RedBean_OODB::getAssoc($person2, "appointment"));
    if (!$appforbob || $appforbob->kind != "dentist") {
        SmartTest::failedTest();
    }
    SmartTest::instance()->progress();
    SmartTest::instance()->testPack = "delete a bean?";
    $person = RedBean_OODB::getById("person", $bobid);
    RedBean_OODB::trash($person);
    try {
        $person = RedBean_OODB::getById("person", $bobid);
        $ok = 0;
    } catch (ExceptionFailedAccessBean $e) {
        $ok = true;
    }
    if (!$ok) {
        SmartTest::failedTest();
    }
    SmartTest::instance()->progress();
    SmartTest::instance()->testPack = "unassociate two beans?";
    $john = RedBean_OODB::getById("person", $johnid);
    //hmmmmmm gaat mis bij innodb
    $app = RedBean_OODB::getById("appointment", 1);
    RedBean_OODB::unassociate($john, $app);
    $john2 = RedBean_OODB::getById("person", $johnid);
    $appsforjohn = RedBean_OODB::getAssoc($john2, "appointment");
    if (count($appsforjohn) > 0) {
        SmartTest::failedTest();
    }
    SmartTest::instance()->progress();
    SmartTest::instance()->testPack = "unassociate by deleting a bean?";
    $anotherdrink = RedBean_OODB::dispense("whisky");
    $anotherdrink->name = "bowmore";
    $anotherdrink->age = 18;
    $anotherdrink->singlemalt = 'y';
    RedBean_OODB::set($anotherdrink);
    RedBean_OODB::associate($anotherdrink, $john);
    $hisdrinks = RedBean_OODB::getAssoc($john, "whisky");
    if (count($hisdrinks) !== 1) {
        SmartTest::failedTest();
    }
    RedBean_OODB::trash($anotherdrink);
    $hisdrinks = RedBean_OODB::getAssoc($john, "whisky");
    if (count($hisdrinks) !== 0) {
        SmartTest::failedTest();
    }
    SmartTest::instance()->progress();
    SmartTest::instance()->testPack = "create parent child relationships?";
    $pete = RedBean_OODB::dispense("person");
    $pete->age = 48;
    $pete->gender = "m";
    $pete->name = "Pete";
    $peteid = RedBean_OODB::set($pete);
    $rob = RedBean_OODB::dispense("person");
    $rob->age = 19;
    $rob->name = "Rob";
    $rob->gender = "m";
    $saskia = RedBean_OODB::dispense("person");
    $saskia->age = 20;
    $saskia->name = "Saskia";
    $saskia->gender = "f";
    RedBean_OODB::set($saskia);
    RedBean_OODB::set($rob);
    RedBean_OODB::addChild($pete, $rob);
    RedBean_OODB::addChild($pete, $saskia);
    $children = RedBean_OODB::getChildren($pete);
    $names = 0;
    if (is_array($children) && count($children) === 2) {
        foreach ($children as $child) {
            if ($child->name === "Rob") {
                $names++;
            }
            if ($child->name === "Saskia") {
                $names++;
            }
        }
    }
    if (!$names) {
        SmartTest::failedTest();
    }
    $daddies = RedBean_OODB::getParent($saskia);
    $daddy = array_pop($daddies);
    if ($daddy->name === "Pete") {
        $ok = 1;
    } else {
        $ok = 0;
    }
    if (!$ok) {
        SmartTest::failedTest();
    }
    SmartTest::instance()->progress();
    SmartTest::instance()->testPack = "remove a child from a parent-child tree?";
    RedBean_OODB::removeChild($daddy, $saskia);
    $children = RedBean_OODB::getChildren($pete);
    $ok = 0;
    if (count($children) === 1) {
        $only = array_pop($children);
        if ($only->name === "Rob") {
            $ok = 1;
        }
    }
    SmartTest::instance()->progress();
    SmartTest::instance()->testPack = "save on the fly while associating?";
    $food = RedBean_OODB::dispense("dish");
    $food->name = "pizza";
    RedBean_OODB::associate($food, $pete);
    $petesfood = RedBean_OODB::getAssoc($pete, "food");
    if (is_array($petesfood) && count($petesfood) === 1) {
        $ok = 1;
    }
    if (!$ok) {
        SmartTest::failedTest();
    }
    //some extra tests... quick without further notice.
    $food = RedBean_OODB::dispense("dish");
    $food->name = "spaghetti";
    RedBean_OODB::trash($food);
    //test aggregation functions
    //insert stat table
    $s = RedBean_OODB::dispense("stattest");
    $s->amount = 1;
    RedBean_OODB::set($s);
    $s = RedBean_OODB::dispense("stattest");
    $s->amount = 2;
    RedBean_OODB::set($s);
    $s = RedBean_OODB::dispense("stattest");
    $s->amount = 3;
    RedBean_OODB::set($s);
    SmartTest::instance()->testPack = "can we use aggr functions using Redbean?";
    if (RedBean_OODB::numberof("stattest") != 3) {
        SmartTest::failedTest();
    } else {
        SmartTest::instance()->progress();
    }
    if (RedBean_OODB::maxof("stattest", "amount") != 3) {
        SmartTest::failedTest();
    } else {
        SmartTest::instance()->progress();
    }
    if (RedBean_OODB::minof("stattest", "amount") != 1) {
        SmartTest::failedTest();
    } else {
        SmartTest::instance()->progress();
    }
    if (RedBean_OODB::avgof("stattest", "amount") != 2) {
        SmartTest::failedTest();
    } else {
        SmartTest::instance()->progress();
    }
    if (RedBean_OODB::sumof("stattest", "amount") != 6) {
        SmartTest::failedTest();
    } else {
        SmartTest::instance()->progress();
    }
    if (count(RedBean_OODB::distinct("stattest", "amount")) != 3) {
        SmartTest::failedTest();
    } else {
        SmartTest::instance()->progress();
    }
    RedBean_OODB::setLocking(true);
    $i = 3;
    SmartTest::instance()->testPack = "generate only valid classes?";
    try {
        $i += RedBean_OODB::gen("");
        SmartTest::instance()->progress();
    } catch (Exception $e) {
        SmartTest::failedTest();
    }
    //nothing
    try {
        $i += RedBean_OODB::gen(".");
        SmartTest::instance()->progress();
    } catch (Exception $e) {
        SmartTest::failedTest();
    }
    //illegal chars
    try {
        $i += RedBean_OODB::gen(",");
        SmartTest::instance()->progress();
    } catch (Exception $e) {
        SmartTest::failedTest();
    }
    //illegal chars
    try {
        $i += RedBean_OODB::gen("null");
        SmartTest::instance()->progress();
    } catch (Exception $e) {
        SmartTest::failedTest();
    }
    //keywords
    try {
        $i += RedBean_OODB::gen("Exception");
        SmartTest::instance()->progress();
    } catch (Exception $e) {
        SmartTest::failedTest();
    }
    //reserved
    SmartTest::instance()->progress();
    SmartTest::instance()->testPack = "generate classes using Redbean?";
    if (!class_exists("Bug")) {
        $i += RedBean_OODB::gen("Bug");
        if ($i !== 4) {
            SmartTest::failedTest();
        }
    } else {
        if ($i !== 3) {
            SmartTest::failedTest();
        }
    }
    if (!class_exists("Bug")) {
        SmartTest::failedTest();
    }
    SmartTest::instance()->progress();
    SmartTest::instance()->testPack = "use getters and setters";
    $bug = new Bug();
    $bug->setSomething(sha1("abc"));
    if ($bug->getSomething() != sha1("abc")) {
        SmartTest::failedTest();
    }
    //can we use non existing props? --triggers fatal..
    $bug->getHappy();
    SmartTest::instance()->progress();
    SmartTest::instance()->testPack = "Use boolean values and retrieve them with is()?";
    if ($bug->isHappy()) {
        SmartTest::failedTest();
    }
    SmartTest::instance()->progress();
    $bug->setHappy(true);
    $bug->save();
    $bug = new Bug(1);
    if (!$bug->isHappy()) {
        SmartTest::failedTest();
    }
    SmartTest::instance()->progress();
    $bug->setHappy(false);
    if ($bug->isHappy()) {
        SmartTest::failedTest();
    }
    SmartTest::instance()->progress();
    SmartTest::instance()->testPack = "avoid race-conditions by locking?";
    RedBean_OODB::gen("Cheese,Wine");
    $cheese = new Cheese();
    $cheese->setName('Brie');
    $cheese->save();
    $cheese = new Cheese(1);
    //try to mess with the locking system...
    $oldkey = RedBean_OODB::$pkey;
    RedBean_OODB::$pkey = 1234;
    $cheese = new Cheese(1);
    $cheese->setName("Camembert");
    $ok = 0;
    try {
        $cheese->save();
    } catch (ExceptionFailedAccessBean $e) {
        $ok = 1;
    }
    if (!$ok) {
        SmartTest::failedTest();
    }
    SmartTest::instance()->progress();
    $bordeaux = new Wine();
    $bordeaux->setRegion("Bordeaux");
    try {
        $bordeaux->add($cheese);
    } catch (ExceptionFailedAccessBean $e) {
        $ok = 1;
    }
    if (!$ok) {
        SmartTest::failedTest();
    }
    SmartTest::instance()->progress();
    try {
        $bordeaux->attach($cheese);
    } catch (ExceptionFailedAccessBean $e) {
        $ok = 1;
    }
    if (!$ok) {
        SmartTest::failedTest();
    }
    SmartTest::instance()->progress();
    try {
        $bordeaux->add(new Wine());
        $ok = 1;
    } catch (ExceptionFailedAccessBean $e) {
        $ok = 0;
    }
    if (!$ok) {
        SmartTest::failedTest();
    }
    SmartTest::instance()->progress();
    RedBean_OODB::$pkey = $oldkey;
    $cheese = new Cheese(1);
    $cheese->setName("Camembert");
    $ok = 0;
    try {
        $cheese->save();
        $ok = 1;
    } catch (ExceptionFailedAccessBean $e) {
        $ok = 0;
    }
    if (!$ok) {
        SmartTest::failedTest();
    }
    SmartTest::instance()->progress();
    try {
        RedBean_OODB::$pkey = 999;
        RedBean_OODB::setLockingTime(0);
        $cheese = new Cheese(1);
        $cheese->setName("Cheddar");
        $cheese->save();
        RedBean_OODB::setLockingTime(10);
        SmartTest::instance()->progress();
    } catch (Exception $e) {
        SmartTest::failedTest();
    }
    try {
        RedBean_OODB::$pkey = 123;
        RedBean_OODB::setLockingTime(100);
        $cheese = new Cheese(1);
        $cheese->setName("Cheddar2");
        $cheese->save();
        RedBean_OODB::setLockingTime(10);
        SmartTest::failedTest();
    } catch (Exception $e) {
        SmartTest::instance()->progress();
    }
    try {
        RedBean_OODB::$pkey = 42;
        RedBean_OODB::setLockingTime(0);
        $cheese = new Cheese(1);
        $cheese->setName("Cheddar3");
        $cheese->save();
        RedBean_OODB::setLockingTime(10);
        SmartTest::instance()->progress();
    } catch (Exception $e) {
        SmartTest::failedTest();
    }
    //test value ranges
    SmartTest::instance()->progress();
    SmartTest::instance()->testPack = "protect inner state of RedBean";
    try {
        RedBean_OODB::setLockingTime(-1);
        SmartTest::failedTest();
    } catch (ExceptionInvalidArgument $e) {
    }
    SmartTest::instance()->progress();
    SmartTest::instance()->testPack = "protect inner state of RedBean";
    try {
        RedBean_OODB::setLockingTime(1.5);
        SmartTest::failedTest();
    } catch (ExceptionInvalidArgument $e) {
    }
    SmartTest::instance()->progress();
    SmartTest::instance()->testPack = "protect inner state of RedBean";
    try {
        RedBean_OODB::setLockingTime("aaa");
        SmartTest::failedTest();
    } catch (ExceptionInvalidArgument $e) {
    }
    SmartTest::instance()->progress();
    SmartTest::instance()->testPack = "protect inner state of RedBean";
    try {
        RedBean_OODB::setLockingTime(null);
        SmartTest::failedTest();
    } catch (ExceptionInvalidArgument $e) {
    }
    SmartTest::instance()->progress();
    //test convenient tree functions
    SmartTest::instance()->testPack = "convient tree functions";
    if (!class_exists("Person")) {
        RedBean_OODB::gen("person");
    }
    $donald = new Person();
    $donald->setName("Donald");
    $donald->save();
    $kwik = new Person();
    $kwik->setName("Kwik");
    $kwik->save();
    $kwek = new Person();
    $kwek->setName("Kwek");
    $kwek->save();
    $kwak = new Person();
    $kwak->setName("Kwak");
    $kwak->save();
    $donald->attach($kwik);
    $donald->attach($kwek);
    $donald->attach($kwak);
    if (count($donald->children()) != 3) {
        SmartTest::failedTest();
    } else {
        SmartTest::instance()->progress();
    }
    if (count($kwik->siblings()) != 2) {
        SmartTest::failedTest();
    } else {
        SmartTest::instance()->progress();
    }
    //todo
    if ($kwik->hasParent($donald) != true) {
        SmartTest::failedTest();
    } else {
        SmartTest::instance()->progress();
    }
    if ($donald->hasParent($kwak) != false) {
        SmartTest::failedTest();
    } else {
        SmartTest::instance()->progress();
    }
    if ($donald->hasChild($kwak) != true) {
        SmartTest::failedTest();
    } else {
        SmartTest::instance()->progress();
    }
    if ($donald->hasChild($donald) != false) {
        SmartTest::failedTest();
    } else {
        SmartTest::instance()->progress();
    }
    if ($kwak->hasChild($kwik) != false) {
        SmartTest::failedTest();
    } else {
        SmartTest::instance()->progress();
    }
    if ($kwak->hasSibling($kwek) != true) {
        SmartTest::failedTest();
    } else {
        SmartTest::instance()->progress();
    }
    if ($kwak->hasSibling($kwak) != false) {
        SmartTest::failedTest();
    } else {
        SmartTest::instance()->progress();
    }
    if ($kwak->hasSibling($donald) != false) {
        SmartTest::failedTest();
    } else {
        SmartTest::instance()->progress();
    }
    //copy
    SmartTest::instance()->testPack = "copy functions";
    $kwak2 = $kwak->copy();
    $id = $kwak2->save();
    $kwak2 = new Person($id);
    if ($kwak->getName() != $kwak2->getName()) {
        SmartTest::failedTest();
    } else {
        SmartTest::instance()->progress();
    }
    SmartTest::instance()->testPack = "countRelated";
    R::gen("Blog,Comment");
    $blog = new Blog();
    $blog2 = new Blog();
    $blog->setTitle("blog1");
    $blog2->setTitle("blog2");
    for ($i = 0; $i < 5; $i++) {
        $comment = new Comment();
        $comment->setText("comment no.  {$i} ");
        $blog->add($comment);
    }
    for ($i = 0; $i < 3; $i++) {
        $comment = new Comment();
        $comment->setText("comment no.  {$i} ");
        $blog2->add($comment);
    }
    if ($blog->numofComment() !== 5) {
        SmartTest::failedTest();
    } else {
        SmartTest::instance()->progress();
    }
    if ($blog2->numofComment() !== 3) {
        SmartTest::failedTest();
    } else {
        SmartTest::instance()->progress();
    }
    SmartTest::instance()->testPack = "associate tables of the same name";
    $blog = new Blog();
    $blogb = new Blog();
    $blog->title = 'blog a';
    $blogb->title = 'blog b';
    $blog->add($blogb);
    $b = $blog->getRelatedBlog();
    if (count($b) !== 1) {
        SmartTest::failedTest();
    } else {
        SmartTest::instance()->progress();
    }
    $b = array_pop($b);
    if ($b->title != 'blog b') {
        SmartTest::failedTest();
    } else {
        SmartTest::instance()->progress();
    }
    SmartTest::instance()->testPack = "inferTypeII patch";
    $blog->rating = 4294967295.0;
    $blog->save();
    $id = $blog->getID();
    $blog2->rating = -1;
    $blog2->save();
    $blog = new Blog($id);
    if ($blog->getRating() != 4294967295.0) {
        SmartTest::failedTest();
    } else {
        SmartTest::instance()->progress();
    }
    SmartTest::instance()->testPack = "Longtext column type";
    $blog->message = str_repeat("x", 65535);
    $blog->save();
    $blog = new Blog($id);
    if (strlen($blog->message) != 65535) {
        SmartTest::failedTest();
    } else {
        SmartTest::instance()->progress();
    }
    $rows = RedBean_OODB::$db->get("describe blog");
    if ($rows[3]["Type"] != "text") {
        SmartTest::failedTest();
    } else {
        SmartTest::instance()->progress();
    }
    $blog->message = str_repeat("x", 65536);
    $blog->save();
    $blog = new Blog($id);
    if (strlen($blog->message) != 65536) {
        SmartTest::failedTest();
    } else {
        SmartTest::instance()->progress();
    }
    $rows = RedBean_OODB::$db->get("describe blog");
    if ($rows[3]["Type"] != "longtext") {
        SmartTest::failedTest();
    } else {
        SmartTest::instance()->progress();
    }
    Redbean_OODB::clean();
    SmartTest::instance()->testPack = "Export";
    RedBean_OODB::gen("justabean");
    $oBean = new JustABean();
    $oBean->setA("a");
    $oOtherBean = new OODBBean();
    $oOtherBean->a = "b";
    $oBean2 = new OODBBean();
    $oBean->exportTo($oBean2);
    if ($oBean2->a !== "a") {
        SmartTest::failedTest();
    } else {
        SmartTest::instance()->progress();
    }
    $oBean2 = $oBean->exportTo($oBean2);
    if ($oBean2->a !== "a") {
        SmartTest::failedTest();
    } else {
        SmartTest::instance()->progress();
    }
    $oBean->exportTo($oBean2, $oOtherBean);
    if ($oBean2->a !== "b") {
        SmartTest::failedTest();
    } else {
        SmartTest::instance()->progress();
    }
    $arr = array();
    $oBean->exportTo($arr);
    if ($arr["a"] !== "a") {
        SmartTest::failedTest();
    } else {
        SmartTest::instance()->progress();
    }
    $arr = array();
    $arr = $oBean->exportTo($arr);
    if ($arr["a"] !== "a") {
        SmartTest::failedTest();
    } else {
        SmartTest::instance()->progress();
    }
    $arr = $oBean->exportTo($arr, $oOtherBean);
    if ($arr["a"] !== "b") {
        SmartTest::failedTest();
    } else {
        SmartTest::instance()->progress();
    }
    SmartTest::instance()->testPack = "Export Array";
    $oBean->a = "a";
    $oInnerBean = new JustABean();
    $oInnerBean->setID(123);
    $oBean->innerbean = $oInnerBean;
    $arr = $oBean->exportAsArr();
    if (!is_array($arr)) {
        SmartTest::failedTest();
    } else {
        SmartTest::instance()->progress();
    }
    if ($arr["innerbean"] !== 123) {
        SmartTest::failedTest();
    } else {
        SmartTest::instance()->progress();
    }
    if ($arr["a"] !== "a") {
        SmartTest::failedTest();
    } else {
        SmartTest::instance()->progress();
    }
    //test 1-to-n
    SmartTest::instance()->testPack = "1-to-n relations";
    R::gen("Track,Disc");
    $cd1 = new Disc();
    $cd1->name = 'first';
    $cd1->save();
    $cd2 = new Disc();
    $cd2->name = 'second';
    $cd2->save();
    $track = new Track();
    $track->title = "song 1";
    $track->belongsTo($cd1);
    $discs = $track->getRelatedDisc();
    SmartTest::instance()->test(count($discs), 1);
    $track->belongsTo($cd2);
    $discs = $track->getRelatedDisc();
    SmartTest::instance()->test(count($discs), 1);
    $track2 = new Track();
    $track2->title = "song 2";
    $cd1->exclusiveAdd($track2);
    SmartTest::instance()->test(count($track->getRelatedDisc()), 1);
    $cd2->exclusiveAdd($track2);
    SmartTest::instance()->test(count($track->getRelatedDisc()), 1);
}
Beispiel #12
0
function testsperengine()
{
    global $tests;
    SmartTest::instance()->progress();
    SmartTest::instance()->canwe = "perform generic bean manipulation";
    $ok = 1;
    $bean = RedBean_OODB::dispense("note");
    $bean->message = "hai";
    $bean->color = 3;
    $bean->date = time();
    $bean->special = 'n';
    $bean->state = 90;
    RedBean_OODB::set($bean);
    $bean2 = RedBean_OODB::getById("note", 1);
    if ($bean2->state != 90 || $bean2->special != 'n' || $bean2->message != 'hai') {
        $ok = 0;
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    SmartTest::instance()->progress();
    $bean->message = "lorem ipsum";
    RedBean_OODB::set($bean);
    $bean->message = 1;
    $bean->color = "green";
    $bean->date = str_repeat("BLABLA", 100);
    RedBean_OODB::set($bean);
    unset($bean);
    $bean = RedBean_OODB::dispense("note");
    $bean->color = "green";
    //print_r($bean);
    $bean3 = RedBean_OODB::find($bean, array("color" => "="));
    if (count($bean3) !== 1) {
        die("<b style='color:red'>Error CANNOT1:" . SmartTest::instance()->canwe);
    }
    unset($bean);
    $bean = RedBean_OODB::dispense("note");
    $bean->state = 80;
    $bean3 = RedBean_OODB::find($bean, array("state" => ">"));
    if (count($bean3) !== 1) {
        die("<b style='color:red'>Error CANNOT2:" . SmartTest::instance()->canwe);
    }
    SmartTest::instance()->progress();
    try {
        $bla = RedBean_OODB::find($bean, array("undefined" => ">"));
    } catch (Exception $e) {
        //dont fail if prop does not exist
        die("<b style='color:red'>Error CANNOT3:" . SmartTest::instance()->canwe);
    }
    SmartTest::instance()->progress();
    $note = $bean3[1];
    $person = RedBean_OODB::dispense("person");
    $person->age = 50;
    $person->name = "Bob";
    $person->gender = "m";
    RedBean_OODB::set($person);
    RedBean_OODB::associate($person, $note);
    $memo = RedBean_OODB::getById("note", 1);
    $authors = RedBean_OODB::getAssoc($memo, "person");
    if (count($authors) !== 1) {
        die("<b style='color:red'>Error CANNOT4:" . SmartTest::instance()->canwe);
    }
    RedBean_OODB::trash($authors[1]);
    //$ok=false;
    //try{
    $authors = RedBean_OODB::getAssoc($memo, "person");
    if (count($authors) > 0) {
        $ok = 0;
    }
    //}
    //catch(Exception $e){
    //	$ok=1;
    //}
    if (!$ok) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    //unit tests
    //drop the note table
    SmartTest::instance()->progress();
    SmartTest::instance()->canwe = "dispense an RedBean_OODB Bean";
    $oBean = RedBean_OODB::dispense();
    if (!$oBean instanceof OODBBean) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    SmartTest::instance()->progress();
    SmartTest::instance()->canwe = "put a bean in the database";
    $person = RedBean_OODB::dispense("person");
    $person->name = "John";
    $person->age = 35;
    $person->gender = "m";
    $person->hasJob = true;
    $id = RedBean_OODB::set($person);
    $johnid = $id;
    //echo "--->$id";
    $person2 = RedBean_OODB::getById("person", $id);
    if ($person2->age != $person->age) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    $person2->anotherprop = 2;
    RedBean_OODB::set($person2);
    $person = RedBean_OODB::dispense("person");
    $person->name = "Bob";
    $person->age = 50;
    $person->gender = "m";
    $person->hasJob = false;
    $bobid = RedBean_OODB::set($person);
    SmartTest::instance()->progress();
    SmartTest::instance()->canwe = "find records on basis of similarity";
    //RedBean_OODB::closeAllBeansOfType("person");
    $searchBean = RedBean_OODB::dispense("person");
    $searchBean->gender = "m";
    $persons = RedBean_OODB::find($searchBean, array("gender" => "="));
    if (count($persons) != 2) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    //RedBean_OODB::closeAllBeansOfType("person");
    $searchBean = RedBean_OODB::dispense("person");
    $searchBean->name = "John";
    $persons = RedBean_OODB::find($searchBean, array("name" => "LIKE"));
    if (count($persons) != 1) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    $searchBean2 = RedBean_OODB::dispense("person");
    $searchBean2->name = "John";
    $searchBean2->gender = "m";
    $persons2 = RedBean_OODB::find($searchBean2, array("gender" => "="));
    if (count($persons2) != 2) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    //test limits (start end etc..)
    $searchBean2 = RedBean_OODB::dispense("person");
    $searchBean2->name = "John";
    $searchBean2->gender = "m";
    $persons2 = RedBean_OODB::find($searchBean2, array("gender" => "="), 1);
    if (count($persons2) != 1) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    SmartTest::instance()->progress();
    $persons2 = RedBean_OODB::find($searchBean2, array("gender" => "="), 0, 1);
    if (count($persons2) != 1) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    SmartTest::instance()->progress();
    $persons2 = RedBean_OODB::find($searchBean2, array("gender" => "="), 0, 1, "age ASC");
    if (count($persons2) == 1) {
        $who = array_pop($persons2);
        if ($who->name != "John") {
            die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
        }
    } else {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    SmartTest::instance()->progress();
    $persons2 = RedBean_OODB::find($searchBean2, array("gender" => "="), 0, 1, "age DESC");
    if (count($persons2) == 1) {
        $who = array_pop($persons2);
        if ($who->name != "Bob") {
            die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
        }
    } else {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    SmartTest::instance()->progress();
    //test extra sql
    $persons2 = RedBean_OODB::find($searchBean2, array("gender" => "="), 0, 1, "age DESC", "order by age ASC limit 1");
    if (count($persons2) == 1) {
        $who = array_pop($persons2);
        if ($who->name != "John") {
            die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
        }
    } else {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    SmartTest::instance()->progress();
    $searchBean = RedBean_OODB::dispense("person");
    $searchBean->age = "20";
    $searchBean->gender = "m";
    $persons = RedBean_OODB::find($searchBean, array("age" => ">"));
    if (count($persons) != 2) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    SmartTest::instance()->progress();
    $searchBean = RedBean_OODB::dispense("person");
    $searchBean->age = "20";
    $searchBean->gender = "v";
    $persons = RedBean_OODB::find($searchBean, array("age" => ">", "gender" => "="));
    if (count($persons) != 0) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    SmartTest::instance()->progress();
    $searchBean = RedBean_OODB::dispense("person");
    $searchBean->age = "50";
    $searchBean->name = "Bob";
    $searchBean->gender = "m";
    $persons = RedBean_OODB::find($searchBean, array("age" => "=", "name" => "LIKE", "gender" => "="));
    if (count($persons) != 1) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    $whisky = RedBean_OODB::dispense("whisky");
    $whisky->name = "Glen Old";
    $whisky->age = 50;
    RedBean_OODB::set($whisky);
    $searchBean = RedBean_OODB::dispense("whisky");
    $searchBean->age = "12";
    $drinks = RedBean_OODB::find($searchBean, array("age" => ">"));
    if (count($drinks) != 1) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    SmartTest::instance()->progress();
    SmartTest::instance()->canwe = "associate beans with eachother?";
    $app = RedBean_OODB::dispense("appointment");
    $app->kind = "dentist";
    RedBean_OODB::set($app);
    RedBean_OODB::associate($person2, $app);
    $appforbob = array_shift(RedBean_OODB::getAssoc($person2, "appointment"));
    if (!$appforbob || $appforbob->kind != "dentist") {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    SmartTest::instance()->progress();
    SmartTest::instance()->canwe = "delete a bean?";
    $person = RedBean_OODB::getById("person", $bobid);
    RedBean_OODB::trash($person);
    try {
        $person = RedBean_OODB::getById("person", $bobid);
        $ok = 0;
    } catch (ExceptionFailedAccessBean $e) {
        $ok = true;
    }
    if (!$ok) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    SmartTest::instance()->progress();
    SmartTest::instance()->canwe = "unassociate two beans?";
    $john = RedBean_OODB::getById("person", $johnid);
    //hmmmmmm gaat mis bij innodb
    /*
    	SELECT * FROM `person` WHERE id = 2
    	Fatal error: Uncaught exception 'ExceptionFailedAccessBean' with message 'bean not found' in /Applications/xampp/xamppfiles/htdocs/cms/oodb.php:1161 Stack trace: #0 /Applications/xampp/xamppfiles/htdocs/cms/test.php(275): RedBean_OODB::getById('person', 2) #1 {main} thrown in /Applications/xampp/xamppfiles/htdocs/cms/oodb.php on line 1161
    */
    $app = RedBean_OODB::getById("appointment", 1);
    RedBean_OODB::unassociate($john, $app);
    $john2 = RedBean_OODB::getById("person", $johnid);
    $appsforjohn = RedBean_OODB::getAssoc($john2, "appointment");
    if (count($appsforjohn) > 0) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    SmartTest::instance()->progress();
    SmartTest::instance()->canwe = "unassociate by deleting a bean?";
    $anotherdrink = RedBean_OODB::dispense("whisky");
    $anotherdrink->name = "bowmore";
    $anotherdrink->age = 18;
    $anotherdrink->singlemalt = 'y';
    RedBean_OODB::set($anotherdrink);
    RedBean_OODB::associate($anotherdrink, $john);
    $hisdrinks = RedBean_OODB::getAssoc($john, "whisky");
    if (count($hisdrinks) !== 1) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    RedBean_OODB::trash($anotherdrink);
    $hisdrinks = RedBean_OODB::getAssoc($john, "whisky");
    if (count($hisdrinks) !== 0) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    SmartTest::instance()->progress();
    SmartTest::instance()->canwe = "create parent child relationships?";
    $pete = RedBean_OODB::dispense("person");
    $pete->age = 48;
    $pete->gender = "m";
    $pete->name = "Pete";
    $peteid = RedBean_OODB::set($pete);
    $rob = RedBean_OODB::dispense("person");
    $rob->age = 19;
    $rob->name = "Rob";
    $rob->gender = "m";
    $saskia = RedBean_OODB::dispense("person");
    $saskia->age = 20;
    $saskia->name = "Saskia";
    $saskia->gender = "f";
    RedBean_OODB::set($saskia);
    RedBean_OODB::set($rob);
    RedBean_OODB::addChild($pete, $rob);
    RedBean_OODB::addChild($pete, $saskia);
    $children = RedBean_OODB::getChildren($pete);
    $names = 0;
    if (is_array($children) && count($children) === 2) {
        foreach ($children as $child) {
            if ($child->name === "Rob") {
                $names++;
            }
            if ($child->name === "Saskia") {
                $names++;
            }
        }
    }
    if (!$names) {
        die("<b style='color:red'>Error CANNOT1:" . SmartTest::instance()->canwe);
    }
    $daddies = RedBean_OODB::getParent($saskia);
    $daddy = array_pop($daddies);
    if ($daddy->name === "Pete") {
        $ok = 1;
    } else {
        $ok = 0;
    }
    if (!$ok) {
        die("<b style='color:red'>Error CANNOT2:" . SmartTest::instance()->canwe);
    }
    SmartTest::instance()->progress();
    SmartTest::instance()->canwe = "remove a child from a parent-child tree?";
    RedBean_OODB::removeChild($daddy, $saskia);
    $children = RedBean_OODB::getChildren($pete);
    $ok = 0;
    if (count($children) === 1) {
        $only = array_pop($children);
        if ($only->name === "Rob") {
            $ok = 1;
        }
    }
    if (!$ok) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    //test exceptions
    $ok = 0;
    try {
        RedBean_OODB::addChild($daddy, $whisky);
    } catch (ExceptionInvalidParentChildCombination $e) {
        $ok = 1;
    }
    if (!$ok) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    $ok = 0;
    try {
        RedBean_OODB::removeChild($daddy, $whisky);
    } catch (ExceptionInvalidParentChildCombination $e) {
        $ok = 1;
    }
    if (!$ok) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    SmartTest::instance()->progress();
    SmartTest::instance()->canwe = "save on the fly while associating?";
    $food = RedBean_OODB::dispense("dish");
    $food->name = "pizza";
    RedBean_OODB::associate($food, $pete);
    $petesfood = RedBean_OODB::getAssoc($pete, "food");
    if (is_array($petesfood) && count($petesfood) === 1) {
        $ok = 1;
    }
    if (!$ok) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    //some extra tests... quick without further notice.
    $food = RedBean_OODB::dispense("dish");
    $food->name = "spaghetti";
    RedBean_OODB::trash($food);
    //test aggregation functions
    //insert stat table
    $s = RedBean_OODB::dispense("stattest");
    $s->amount = 1;
    RedBean_OODB::set($s);
    $s = RedBean_OODB::dispense("stattest");
    $s->amount = 2;
    RedBean_OODB::set($s);
    $s = RedBean_OODB::dispense("stattest");
    $s->amount = 3;
    RedBean_OODB::set($s);
    SmartTest::instance()->canwe = "can we use aggr functions using Redbean?";
    if (RedBean_OODB::numberof("stattest") != 3) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    } else {
        SmartTest::instance()->progress();
    }
    if (RedBean_OODB::maxof("stattest", "amount") != 3) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    } else {
        SmartTest::instance()->progress();
    }
    if (RedBean_OODB::minof("stattest", "amount") != 1) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    } else {
        SmartTest::instance()->progress();
    }
    if (RedBean_OODB::avgof("stattest", "amount") != 2) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    } else {
        SmartTest::instance()->progress();
    }
    if (RedBean_OODB::sumof("stattest", "amount") != 6) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    } else {
        SmartTest::instance()->progress();
    }
    if (count(RedBean_OODB::distinct("stattest", "amount")) != 3) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    } else {
        SmartTest::instance()->progress();
    }
    //test list function
    SmartTest::instance()->canwe = "can we use list functions using Redbean?";
    if (count(RedBean_OODB::listAll("stattest")) != 3) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    } else {
        SmartTest::instance()->progress();
    }
    if (count(RedBean_OODB::listAll("stattest", 1)) != 2) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    } else {
        SmartTest::instance()->progress();
    }
    if (count(RedBean_OODB::listAll("stattest", 1, 1)) != 1) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    } else {
        SmartTest::instance()->progress();
    }
    if (count(RedBean_OODB::listAll("stattest", 1, 2)) != 2) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    } else {
        SmartTest::instance()->progress();
    }
    if (count(RedBean_OODB::listAll("stattest", 1, 1, "", " limit 100 ")) != 3) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    } else {
        SmartTest::instance()->progress();
    }
    //now test with decorator =======================================
    RedBean_OODB::setLocking(true);
    $i = 3;
    SmartTest::instance()->canwe = "generate only valid classes?";
    try {
        $i += RedBean_OODB::gen("");
        SmartTest::instance()->progress();
    } catch (Exception $e) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    //nothing
    try {
        $i += RedBean_OODB::gen(".");
        SmartTest::instance()->progress();
    } catch (Exception $e) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    //illegal chars
    try {
        $i += RedBean_OODB::gen(",");
        SmartTest::instance()->progress();
    } catch (Exception $e) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    //illegal chars
    try {
        $i += RedBean_OODB::gen("null");
        SmartTest::instance()->progress();
    } catch (Exception $e) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    //keywords
    try {
        $i += RedBean_OODB::gen("Exception");
        SmartTest::instance()->progress();
    } catch (Exception $e) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    //reserved
    SmartTest::instance()->progress();
    SmartTest::instance()->canwe = "generate classes using Redbean?";
    if (!class_exists("Bug")) {
        $i += RedBean_OODB::gen("Bug");
        if ($i !== 4) {
            die("<b style='color:red'>Error CANNOT {$i}:" . SmartTest::instance()->canwe);
        }
    } else {
        if ($i !== 3) {
            die("<b style='color:red'>Error CANNOT {$i}:" . SmartTest::instance()->canwe);
        }
    }
    if (!class_exists("Bug")) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    SmartTest::instance()->progress();
    SmartTest::instance()->canwe = "use getters and setters";
    $bug = new Bug();
    $bug->setSomething(sha1("abc"));
    if ($bug->getSomething() != sha1("abc")) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    //can we use non existing props? --triggers fatal..
    $bug->getHappy();
    SmartTest::instance()->progress();
    SmartTest::instance()->canwe = "use oget and oset?";
    RedBean_OODB::gen("Project");
    $proj = new Project();
    $proj->setName("zomaar");
    $bug->osetProject($proj);
    $bug->save();
    $oldbug = new Bug(1);
    $oldproj = $oldbug->ogetProject();
    if ($oldproj->getName() != "zomaar") {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    SmartTest::instance()->progress();
    SmartTest::instance()->canwe = "Use boolean values and retrieve them with is()?";
    if ($bug->isHappy()) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    SmartTest::instance()->progress();
    $bug->setHappy(true);
    $bug->save();
    $bug = new Bug(1);
    if (!$bug->isHappy()) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    SmartTest::instance()->progress();
    $bug->setHappy(false);
    if ($bug->isHappy()) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    SmartTest::instance()->progress();
    SmartTest::instance()->canwe = "break oget/oset assoc?";
    $bug->osetProject(null);
    $bug->save();
    $bug = null;
    $bug = new Bug(1);
    $proj = $bug->ogetProject();
    if ($proj->getID() > 0) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    SmartTest::instance()->progress();
    SmartTest::instance()->canwe = "Use the decorator to associate items?";
    $bug = null;
    $bug1 = new Bug();
    $bug2 = new Bug();
    $bug1->setName("b1");
    $bug2->setName("b2");
    $p = new Project();
    $p->setProjectNum(42);
    $p->add($bug1);
    $p->add($bug2);
    $p = null;
    $b = new Project();
    $b->setProjectNum(42);
    //also fck up case...
    $arr = RedBean_Decorator::find($b, array("PRoJECTnuM" => "="));
    $proj = array_pop($arr);
    $bugs = $proj->getRelatedBug();
    if (count($bugs) !== 2) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    SmartTest::instance()->progress();
    SmartTest::instance()->canwe = "use hierarchies?";
    $sub1 = new Project();
    $sub2 = new Project();
    $sub3 = new Project();
    $sub1->setName("a");
    $sub2->setName("b");
    $sub2->setDate(time());
    $sub3->setName("c");
    $sub2->attach($sub3);
    $proj->attach($sub1)->attach($sub2);
    $arr = RedBean_Decorator::find($b, array("PRoJECTnuM" => "="));
    $proj = array_pop($arr);
    $c = $proj->children();
    foreach ($c as $c1) {
        if ($c1->getName() == "b") {
            break;
        }
    }
    $c2 = $c1->children();
    $sub3 = array_pop($c2);
    if ($sub3->getName() != "c") {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    SmartTest::instance()->progress();
    SmartTest::instance()->canwe = "make our own models";
    if (!class_exists("Customer")) {
        class Customer extends RedBean_Decorator
        {
            public function __construct($id = 0)
            {
                parent::__construct("Customer", $id);
            }
            //each customer may only have one project
            public function setProject(Project $project)
            {
                $this->clearRelatedProject();
                $this->command("add", array($project));
            }
            public function add($what)
            {
                if ($what instanceof Project) {
                    return false;
                }
                $this->command("add", array($what));
            }
        }
    }
    $p2 = new Project();
    $p2->setName("hihi");
    $cust = new Customer();
    $cust->setProject($p2);
    $cust->add($p2);
    $cust->setProject($proj);
    $ps = $cust->getRelatedProject();
    if (count($ps) > 1) {
        die("<b style='color:red'>Error CANNOT1:" . SmartTest::instance()->canwe);
    }
    SmartTest::instance()->progress();
    $p = array_pop($ps);
    if ($p->getName() == "hihi") {
        die("<b style='color:red'>Error CANNOT2:" . SmartTest::instance()->canwe);
    }
    SmartTest::instance()->progress();
    SmartTest::instance()->canwe = "delete all assoc";
    $cust->clearAllRelations();
    $ps = $cust->getRelatedProject();
    if (count($ps) > 0) {
        die("<b style='color:red'>Error CANNOT1:" . SmartTest::instance()->canwe);
    }
    SmartTest::instance()->progress();
    SmartTest::instance()->canwe = "not mess with redbean";
    $bla = new Customer();
    Redbean_Decorator::find($bla, array());
    $ok = 0;
    try {
        Redbean_Decorator::find($bla, array("bkaa" => "q="));
    } catch (ExceptionInvalidFindOperator $e) {
        $ok = 1;
    }
    if (!$ok) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    SmartTest::instance()->progress();
    SmartTest::instance()->canwe = "manipulate hierarchies";
    $cust2 = new Customer();
    $cust->attach($cust2);
    $c = $cust->children();
    if (count($c) !== 1) {
        die("<b style='color:red'>Error CANNOT1:" . SmartTest::instance()->canwe);
    }
    SmartTest::instance()->progress();
    $cust->remove($cust2);
    $c = $cust->children();
    if (count($c) !== 0) {
        die("<b style='color:red'>Error CANNOT2:" . SmartTest::instance()->canwe);
        exit;
    }
    SmartTest::instance()->progress();
    $cust->attach($cust2);
    Customer::delete($cust2);
    $c = $cust->children();
    if (count($c) !== 0) {
        die("<b style='color:red'>Error CANNOT3:" . SmartTest::instance()->canwe);
        exit;
    }
    SmartTest::instance()->progress();
    SmartTest::instance()->canwe = "remove associations";
    $cust3 = new Customer();
    $cust4 = new Customer();
    $cust3->add($cust4);
    $c = $cust3->getRelatedCustomer();
    if (count($c) !== 1) {
        die("<b style='color:red'>Error CANNOT1:" . SmartTest::instance()->canwe);
        exit;
    }
    SmartTest::instance()->progress();
    $cust3->remove($cust4);
    $c = $cust3->getRelatedCustomer();
    if (count($c) !== 0) {
        die("<b style='color:red'>Error CANNOT2:" . SmartTest::instance()->canwe);
        exit;
    }
    SmartTest::instance()->progress();
    $cust3 = new Customer();
    $cust4 = new Customer();
    $cust3->add($cust4);
    $cust3->add($cust4);
    //also test multiple assoc
    $c = $cust3->getRelatedCustomer();
    if (count($c) !== 1) {
        die("<b style='color:red'>Error CANNOT3:" . SmartTest::instance()->canwe);
        exit;
    }
    SmartTest::instance()->progress();
    $cust4->remove($cust3);
    $c = $cust3->getRelatedCustomer();
    if (count($c) !== 0) {
        die("<b style='color:red'>Error CANNOT4:" . SmartTest::instance()->canwe);
        exit;
    }
    SmartTest::instance()->progress();
    $cust3 = new Customer();
    $cust4 = new Customer();
    $cust3->add($cust4);
    $cust3->add($cust4);
    //also test multiple assoc
    $c = $cust3->getRelatedCustomer();
    if (count($c) !== 1) {
        die("<b style='color:red'>Error CANNOT5:" . SmartTest::instance()->canwe);
        exit;
    }
    SmartTest::instance()->progress();
    Customer::delete($cust4);
    $c = $cust3->getRelatedCustomer();
    if (count($c) !== 0) {
        die("<b style='color:red'>Error CANNOT6:" . SmartTest::instance()->canwe);
        exit;
    }
    SmartTest::instance()->progress();
    SmartTest::instance()->canwe = "import from post";
    $_POST["hallo"] = 123;
    $_POST["there"] = 456;
    $_POST["nope"] = 789;
    $cust = new Customer();
    $cust->importFromPost(array("hallo", "there"));
    if ($cust->getHallo() == 123 && $cust->getThere() == 456 && !$cust->getNope()) {
        SmartTest::instance()->progress();
    } else {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
        exit;
    }
    foreach ($cust->problems() as $p) {
        if ($p) {
            die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
        }
    }
    SmartTest::instance()->progress();
    $_POST["hallo"] = 123;
    $_POST["there"] = 456;
    $_POST["nope"] = 789;
    $cust = new Customer();
    $cust->importFromPost("hallo,there");
    if ($cust->getHallo() == 123 && $cust->getThere() == 456 && !$cust->getNope()) {
        SmartTest::instance()->progress();
    } else {
        die("<b style='color:red'>Error CANNOT2:" . SmartTest::instance()->canwe);
        exit;
    }
    foreach ($cust->problems() as $p) {
        if ($p) {
            die("<b style='color:red'>Error CANNOT3:" . SmartTest::instance()->canwe);
        }
    }
    SmartTest::instance()->progress();
    $_POST["hallo"] = 123;
    $_POST["there"] = 456;
    $_POST["nope"] = 789;
    $cust = new Customer();
    $cust->importFromPost();
    if ($cust->getHallo() == 123 && $cust->getThere() == 456 && $cust->getNope()) {
        SmartTest::instance()->progress();
    } else {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
        exit;
    }
    foreach ($cust->problems() as $p) {
        if ($p) {
            die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
        }
    }
    SmartTest::instance()->progress();
    if (!class_exists("Trick")) {
        class Trick extends RedBean_Decorator
        {
            public function __construct($id = 0)
            {
                parent::__construct("Customer", $id);
            }
            public function setHallo()
            {
                return "hallodaar";
            }
        }
    }
    $trick = new Trick();
    $trick->importFromPost(array("hallo", "there"));
    $message = array_shift($trick->problems());
    if ($message === "hallodaar") {
        SmartTest::instance()->progress();
    } else {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
        exit;
    }
    SmartTest::instance()->progress();
    SmartTest::instance()->canwe = "avoid race-conditions by locking?";
    RedBean_OODB::gen("Cheese,Wine");
    $cheese = new Cheese();
    $cheese->setName('Brie');
    $cheese->save();
    $cheese = new Cheese(1);
    //try to mess with the locking system...
    $oldkey = RedBean_OODB::$pkey;
    RedBean_OODB::$pkey = 1234;
    $cheese = new Cheese(1);
    $cheese->setName("Camembert");
    $ok = 0;
    try {
        $cheese->save();
    } catch (ExceptionFailedAccessBean $e) {
        $ok = 1;
    }
    if (!$ok) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    SmartTest::instance()->progress();
    $bordeaux = new Wine();
    $bordeaux->setRegion("Bordeaux");
    try {
        $bordeaux->add($cheese);
    } catch (ExceptionFailedAccessBean $e) {
        $ok = 1;
    }
    if (!$ok) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    SmartTest::instance()->progress();
    try {
        $bordeaux->attach($cheese);
    } catch (ExceptionFailedAccessBean $e) {
        $ok = 1;
    }
    if (!$ok) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    SmartTest::instance()->progress();
    try {
        $bordeaux->add(new Wine());
        $ok = 1;
    } catch (ExceptionFailedAccessBean $e) {
        $ok = 0;
    }
    if (!$ok) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    SmartTest::instance()->progress();
    RedBean_OODB::$pkey = $oldkey;
    $cheese = new Cheese(1);
    $cheese->setName("Camembert");
    $ok = 0;
    try {
        $cheese->save();
        $ok = 1;
    } catch (ExceptionFailedAccessBean $e) {
        $ok = 0;
    }
    if (!$ok) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    SmartTest::instance()->progress();
    try {
        RedBean_OODB::$pkey = 999;
        RedBean_OODB::setLockingTime(0);
        $cheese = new Cheese(1);
        $cheese->setName("Cheddar");
        $cheese->save();
        RedBean_OODB::setLockingTime(10);
        SmartTest::instance()->progress();
    } catch (Exception $e) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    try {
        RedBean_OODB::$pkey = 123;
        RedBean_OODB::setLockingTime(100);
        $cheese = new Cheese(1);
        $cheese->setName("Cheddar2");
        $cheese->save();
        RedBean_OODB::setLockingTime(10);
        die("<b style='color:red'>Error CANNOT-C:" . SmartTest::instance()->canwe);
    } catch (Exception $e) {
        SmartTest::instance()->progress();
    }
    try {
        RedBean_OODB::$pkey = 42;
        RedBean_OODB::setLockingTime(0);
        $cheese = new Cheese(1);
        $cheese->setName("Cheddar3");
        $cheese->save();
        RedBean_OODB::setLockingTime(10);
        SmartTest::instance()->progress();
    } catch (Exception $e) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    //test value ranges
    SmartTest::instance()->progress();
    SmartTest::instance()->canwe = "protect inner state of RedBean";
    try {
        RedBean_OODB::setLockingTime(-1);
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    } catch (ExceptionInvalidArgument $e) {
    }
    SmartTest::instance()->progress();
    SmartTest::instance()->canwe = "protect inner state of RedBean";
    try {
        RedBean_OODB::setLockingTime(1.5);
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    } catch (ExceptionInvalidArgument $e) {
    }
    SmartTest::instance()->progress();
    SmartTest::instance()->canwe = "protect inner state of RedBean";
    try {
        RedBean_OODB::setLockingTime("aaa");
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    } catch (ExceptionInvalidArgument $e) {
    }
    SmartTest::instance()->progress();
    SmartTest::instance()->canwe = "protect inner state of RedBean";
    try {
        RedBean_OODB::setLockingTime(null);
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    } catch (ExceptionInvalidArgument $e) {
    }
    SmartTest::instance()->progress();
    //can we reset logs
    SmartTest::instance()->canwe = "reset the logs?";
    $logs = RedBean_DBAdapter::getLogs();
    //are the logs working?
    if (is_array($logs) && count($logs) > 0) {
        SmartTest::instance()->progress();
    } else {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    RedBean_DBAdapter::resetLogs();
    $logs = RedBean_DBAdapter::getLogs();
    if (is_array($logs) && count($logs) === 0) {
        SmartTest::instance()->progress();
    } else {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    SmartTest::instance()->progress();
    SmartTest::instance()->canwe = "freeze the database?";
    RedBean_OODB::freeze();
    $joop = new Project();
    $joop->setName("Joop");
    $joopid = $joop->save();
    if (!is_numeric($joopid) || $joopid < 1) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    SmartTest::instance()->progress();
    $joop->setBlaaataap("toppiedoe");
    $joop->save();
    $joop2 = new Project($joopid);
    $name = $joop2->getName();
    $blaataap = $joop2->getBlaataap();
    if ($name !== "Joop") {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    SmartTest::instance()->progress();
    if (!is_null($blaataap)) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    SmartTest::instance()->progress();
    try {
        RedBean_OODB::gen("haas");
        $haas = new Haas();
        $haas->setHat("redhat");
        $id = $haas->save();
        if ($id !== 0) {
            die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
        }
        SmartTest::instance()->progress();
    } catch (Exception $e) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    SmartTest::instance()->progress();
    $cheese = new Cheese();
    $cheese->setName("bluecheese");
    $cheeseid = $cheese->save();
    if (!$cheeseid) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    $anothercheese = new Cheese();
    $cheese->add($anothercheese);
    $cheese->attach($anothercheese);
    $a1 = $cheese->getRelatedCheese();
    $a2 = $cheese->children();
    if (!is_array($a1) || is_array($a1) && count($a1) !== 0) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    SmartTest::instance()->progress();
    if (!is_array($a2) || is_array($a2) && count($a2) !== 0) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    SmartTest::instance()->progress();
    //now scan the logs for database modifications
    $logs = strtolower(implode(",", RedBean_DBAdapter::getLogs()));
    if (strpos("alter", $logs) !== false) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    SmartTest::instance()->progress();
    if (strpos("truncate", $logs) !== false) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    SmartTest::instance()->progress();
    if (strpos("drop", $logs) !== false) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    SmartTest::instance()->progress();
    if (strpos("change", $logs) !== false) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    SmartTest::instance()->progress();
    if (strpos("show", $logs) !== false) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    SmartTest::instance()->progress();
    if (strpos("describe", $logs) !== false) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    SmartTest::instance()->progress();
    if (strpos("drop database", $logs) !== false) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    SmartTest::instance()->progress();
    //should be unable to do gc() and optimize() and clean() and resetAll()
    RedBean_DBAdapter::resetLogs();
    if (RedBean_OODB::gc() && count(RedBean_DBAdapter::getLogs()) > 0) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    } else {
        SmartTest::instance()->progress();
    }
    if (RedBean_OODB::optimizeIndexes() && count(RedBean_DBAdapter::getLogs()) > 0) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    } else {
        SmartTest::instance()->progress();
    }
    if (RedBean_OODB::clean() && count(RedBean_DBAdapter::getLogs()) > 0) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    } else {
        SmartTest::instance()->progress();
    }
    if (RedBean_OODB::registerUpdate("cheese") && count(RedBean_DBAdapter::getLogs()) < 1) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    } else {
        SmartTest::instance()->progress();
    }
    if (RedBean_OODB::registerSearch("cheese") && count(RedBean_DBAdapter::getLogs()) < 1) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    } else {
        SmartTest::instance()->progress();
    }
    SmartTest::instance()->canwe = "can we unfreeze the database";
    try {
        RedBean_OODB::unfreeze();
    } catch (Exception $e) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    }
    SmartTest::instance()->progress();
    //should be ABLE to do gc() and optimize() and clean() and resetAll()
    RedBean_DBAdapter::resetLogs();
    if (!RedBean_OODB::gc() && count(RedBean_DBAdapter::getLogs()) < 1) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    } else {
        SmartTest::instance()->progress();
    }
    if (!RedBean_OODB::optimizeIndexes() && count(RedBean_DBAdapter::getLogs()) < 1) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    } else {
        SmartTest::instance()->progress();
    }
    if (!RedBean_OODB::clean() && count(RedBean_DBAdapter::getLogs()) < 1) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    } else {
        SmartTest::instance()->progress();
    }
    if (!RedBean_OODB::registerUpdate("cheese") && count(RedBean_DBAdapter::getLogs()) < 1) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    } else {
        SmartTest::instance()->progress();
    }
    if (!RedBean_OODB::registerSearch("cheese") && count(RedBean_DBAdapter::getLogs()) < 1) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    } else {
        SmartTest::instance()->progress();
    }
    //test convenient tree functions
    SmartTest::instance()->canwe = "convient tree functions";
    if (!class_exists("Person")) {
        RedBean_OODB::gen("person");
    }
    $donald = new Person();
    $donald->setName("Donald");
    $donald->save();
    $kwik = new Person();
    $kwik->setName("Kwik");
    $kwik->save();
    $kwek = new Person();
    $kwek->setName("Kwek");
    $kwek->save();
    $kwak = new Person();
    $kwak->setName("Kwak");
    $kwak->save();
    $donald->attach($kwik);
    $donald->attach($kwek);
    $donald->attach($kwak);
    if (count($donald->children()) != 3) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    } else {
        SmartTest::instance()->progress();
    }
    if (count($kwik->siblings()) != 2) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    } else {
        SmartTest::instance()->progress();
    }
    //todo
    if ($kwik->hasParent($donald) != true) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    } else {
        SmartTest::instance()->progress();
    }
    if ($donald->hasParent($kwak) != false) {
        die("<b style='color:red'>Error CANNOT2:" . SmartTest::instance()->canwe);
    } else {
        SmartTest::instance()->progress();
    }
    if ($donald->hasChild($kwak) != true) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    } else {
        SmartTest::instance()->progress();
    }
    if ($donald->hasChild($donald) != false) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    } else {
        SmartTest::instance()->progress();
    }
    if ($kwak->hasChild($kwik) != false) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    } else {
        SmartTest::instance()->progress();
    }
    if ($kwak->hasSibling($kwek) != true) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    } else {
        SmartTest::instance()->progress();
    }
    if ($kwak->hasSibling($kwak) != false) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    } else {
        SmartTest::instance()->progress();
    }
    if ($kwak->hasSibling($donald) != false) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    } else {
        SmartTest::instance()->progress();
    }
    //copy
    SmartTest::instance()->canwe = "copy functions";
    $kwak2 = $kwak->copy();
    $id = $kwak2->save();
    $kwak2 = new Person($id);
    if ($kwak->getName() != $kwak2->getName()) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    } else {
        SmartTest::instance()->progress();
    }
    SmartTest::instance()->canwe = "countRelated";
    R::gen("Blog,Comment");
    $blog = new Blog();
    $blog2 = new Blog();
    $blog->setTitle("blog1");
    $blog2->setTitle("blog2");
    for ($i = 0; $i < 5; $i++) {
        $comment = new Comment();
        $comment->setText("comment no.  {$i} ");
        $blog->add($comment);
    }
    for ($i = 0; $i < 3; $i++) {
        $comment = new Comment();
        $comment->setText("comment no.  {$i} ");
        $blog2->add($comment);
    }
    if ($blog->numofComment() !== 5) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    } else {
        SmartTest::instance()->progress();
    }
    if ($blog2->numofComment() !== 3) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    } else {
        SmartTest::instance()->progress();
    }
    SmartTest::instance()->canwe = "associate tables of the same name";
    $blog = new Blog();
    $blogb = new Blog();
    $blog->title = 'blog a';
    $blogb->title = 'blog b';
    $blog->add($blogb);
    $b = $blog->getRelatedBlog();
    if (count($b) !== 1) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    } else {
        SmartTest::instance()->progress();
    }
    $b = array_pop($b);
    if ($b->title != 'blog b') {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    } else {
        SmartTest::instance()->progress();
    }
    SmartTest::instance()->canwe = "inferTypeII patch";
    $blog->rating = 4294967295.0;
    $blog->save();
    $id = $blog->getID();
    $blog2->rating = -1;
    $blog2->save();
    $blog = new Blog($id);
    if ($blog->getRating() != 4294967295.0) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    } else {
        SmartTest::instance()->progress();
    }
    SmartTest::instance()->canwe = "Longtext column type";
    $blog->message = str_repeat("x", 65535);
    $blog->save();
    $blog = new Blog($id);
    if (strlen($blog->message) != 65535) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    } else {
        SmartTest::instance()->progress();
    }
    $rows = RedBean_OODB::$db->get("describe blog");
    if ($rows[3]["Type"] != "text") {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    } else {
        SmartTest::instance()->progress();
    }
    $blog->message = str_repeat("x", 65536);
    $blog->save();
    $blog = new Blog($id);
    if (strlen($blog->message) != 65536) {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    } else {
        SmartTest::instance()->progress();
    }
    $rows = RedBean_OODB::$db->get("describe blog");
    if ($rows[3]["Type"] != "longtext") {
        die("<b style='color:red'>Error CANNOT:" . SmartTest::instance()->canwe);
    } else {
        SmartTest::instance()->progress();
    }
    Redbean_OODB::clean();
}
 /**
  * Handles a REST DELETE request.
  * Deletes the selected bean.
  * Returns an array formatted according to RedBeanPHP REST BeanCan
  * formatting specifications. Internal method.
  *
  * @return array
  */
 private function delete()
 {
     $this->oodb->trash($this->bean);
     return $this->resp('OK');
 }