Example #1
0
 /**
  * Associates two beans with eachother.
  * @param RedBean_OODBBean $bean1
  * @param RedBean_OODBBean $bean2
  */
 public function associate(RedBean_OODBBean $bean1, RedBean_OODBBean $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"));
     $bean = $this->oodb->dispense($table);
     $property1 = $bean1->getMeta("type") . "_id";
     $property2 = $bean2->getMeta("type") . "_id";
     if ($property1 == $property2) {
         $property2 = $bean2->getMeta("type") . "2_id";
     }
     $bean->setMeta("buildcommand.unique", array(array($property1, $property2)));
     $this->oodb->store($bean1);
     $this->oodb->store($bean2);
     $bean->{$property1} = $bean1->{$idfield1};
     $bean->{$property2} = $bean2->{$idfield2};
     try {
         $this->oodb->store($bean);
     } catch (RedBean_Exception_SQL $e) {
         //If this is a SQLSTATE[23000]: Integrity constraint violation
         //Then just ignore the insert
         if ((int) $e->getSQLState() !== 23000) {
             throw $e;
         }
     }
 }
Example #2
0
 /**
  *
  * Constructor, requires a type name
  * @param string $typeName
  */
 public function __construct($typeName)
 {
     $this->tools = RedBean_Setup::getToolBox();
     $this->redbean = $this->tools->getRedBean();
     $this->bean = $this->redbean->dispense($typeName);
     $this->associationManager = new RedBean_AssociationManager($this->tools);
     $this->treeManager = new RedBean_TreeManager($this->tools);
 }
 /**
  * Associates two beans with eachother.
  * @param RedBean_OODBBean $bean1
  * @param RedBean_OODBBean $bean2
  */
 public function associate(RedBean_OODBBean $bean1, RedBean_OODBBean $bean2)
 {
     $table = $this->getTable(array($bean1->getMeta("type"), $bean2->getMeta("type")));
     $bean = $this->oodb->dispense($table);
     $property1 = $bean1->getMeta("type") . "_id";
     $property2 = $bean2->getMeta("type") . "_id";
     if ($property1 == $property2) {
         $property2 = $bean2->getMeta("type") . "2_id";
     }
     $bean->setMeta("buildcommand.unique", array(array($property1, $property2)));
     $this->oodb->store($bean1);
     $this->oodb->store($bean2);
     $bean->{$property1} = $bean1->id;
     $bean->{$property2} = $bean2->id;
     $this->oodb->store($bean);
 }
 /**
  * Dispenses a new RedBean OODB Bean for use with
  * the rest of the methods.
  *
  * @param string  $type   type
  * @param integer $number number of beans to dispense
  *
  * @return array|RedBean_OODBBean
  *
  * @throws RedBean_Exception_Security
  */
 public function dispense($type, $num = 1)
 {
     if (!preg_match('/^[a-z0-9]+$/', $type) && $this->strictType) {
         throw new RedBean_Exception_Security('Invalid type: ' . $type);
     }
     return $this->redbean->dispense($type, $num);
 }
 /**
  * Handles a REST POST request.
  * Stores the bean described in the payload array in the database.
  * Returns an array formatted according to RedBeanPHP REST BeanCan
  * formatting specifications.
  *
  * Format of the payload array:
  *
  * array(
  *        'bean' => array( property => value pairs )
  * )
  *
  * @return array
  */
 private function post()
 {
     if (!isset($this->payload['bean'])) {
         return $this->resp(NULL, self::C_HTTP_BAD_REQUEST, 'Missing parameter \'bean\'.');
     }
     if (!is_array($this->payload['bean'])) {
         return $this->resp(NULL, self::C_HTTP_BAD_REQUEST, 'Parameter \'bean\' must be object/array.');
     }
     foreach ($this->payload['bean'] as $key => $value) {
         if (!is_string($key) || !is_string($value)) {
             return $this->resp(NULL, self::C_HTTP_BAD_REQUEST, 'Object \'bean\' invalid.');
         }
     }
     $newBean = $this->oodb->dispense($this->type);
     $newBean->import($this->payload['bean']);
     if (strpos($this->list, 'shared-') === FALSE) {
         $listName = 'own' . ucfirst($this->list);
     } else {
         $listName = 'shared' . ucfirst(substr($this->list, 7));
     }
     array_push($this->bean->{$listName}, $newBean);
     $this->oodb->store($this->bean);
     $newBean = $this->oodb->load($newBean->getMeta('type'), $newBean->id);
     return $this->resp($newBean->export());
 }
 /**
  * Creates a copy of bean $bean and copies all primitive properties (not lists)
  * and the parents beans to the newly created bean. Also sets the ID of the bean
  * to 0.
  *
  * @param RedBean_OODBBean $bean bean to copy
  *
  * @return RedBean_OODBBean
  */
 private function createCopy(RedBean_OODBBean $bean)
 {
     $type = $bean->getMeta('type');
     $copy = $this->redbean->dispense($type);
     $copy->importFrom($bean);
     $copy->id = 0;
     return $copy;
 }
Example #7
0
 /**
  * @see RedBean_Finder::find
  *      Convience method. Tries to find beans of a certain type,
  *      if no beans are found, it dispenses a bean of that type.
  *
  * @param  string $type     the type of bean you are looking for
  * @param  string $sql      SQL query to find the desired bean, starting right after WHERE clause
  * @param  array  $bindings values array of values to be bound to parameters in query
  *
  * @return array
  */
 public function findOrDispense($type, $sql = NULL, $bindings = array())
 {
     $foundBeans = $this->find($type, $sql, $bindings);
     if (empty($foundBeans)) {
         return array($this->redbean->dispense($type));
     } else {
         return $foundBeans;
     }
 }
Example #8
0
 /**
  *
  * Constructor, requires a type name
  * @param string $typeName
  */
 public function __construct($typeName = false)
 {
     /**
      * If no typeName has been specified,
      * figure out the type of this model yourself.
      * In this case the following rule applies:
      * - the name of the model is the LAST part of the
      * namespace.
      * - Within that string, the name of the model is the LAST
      * part of the poorman's name space.
      *
      * So the model name for class: /me/him/her is: her
      * So the model name for class: /me/him/her_lover is: lover
      */
     if (!$typeName) {
         //Fetch the bean type using the class
         $beanTypeName = get_class($this);
         //Get last part of namespace
         $a = explode("\\", $beanTypeName);
         $lastInNameSpace = array_pop($a);
         //Get last part of poorman's namespace (underscores)
         $a = explode("_", $lastInNameSpace);
         $lastInPoormanNameSpace = array_pop($a);
         $beanTypeName = $lastInPoormanNameSpace;
     } else {
         $beanTypeName = $typeName;
     }
     /*
      * Now do a little check to see whether this name
      * can be used. - Just a quick check, we will re-check later on
      */
     if ($beanTypeName && strlen($beanTypeName) > 0) {
         //Fetch us a toolbox.
         $this->tools = RedBean_Setup::getToolBox();
         $this->redbean = $this->tools->getRedBean();
         //Here the bean type is checked properly.
         $this->bean = $this->redbean->dispense(strtolower($beanTypeName));
         //Create some handy modules so you dont have to do the wiring yourself.
         $this->associationManager = new RedBean_AssociationManager($this->tools);
         $this->treeManager = new RedBean_TreeManager($this->tools);
     } else {
         throw new Exception("Invalid Domain Object TypeName");
     }
 }
Example #9
0
 /**
  * Dispenses a new RedBean OODB Bean for use with
  * the rest of the methods.
  *
  * @param string $type type
  *
  * @return RedBean_OODBBean $bean a new bean
  */
 public static function dispense($type, $num = 1)
 {
     if ($num == 1) {
         return self::$redbean->dispense($type);
     } else {
         $beans = array();
         for ($v = 0; $v < $num; $v++) {
             $beans[] = self::$redbean->dispense($type);
         }
         return $beans;
     }
 }
 /**
  * Associates two beans in a many-to-many relation.
  * This method will associate two beans and store the connection between the
  * two in a link table. Instead of two single beans this method also accepts
  * two sets of beans. Returns the ID or the IDs of the linking beans.
  * 
  * @param RedBean_OODBBean|array $beans1 one or more beans to form the association
  * @param RedBean_OODBBean|array $beans2 one or more beans to form the association
  *
  * @return array
  */
 public function associate($beans1, $beans2)
 {
     if (!is_array($beans1)) {
         $beans1 = array($beans1);
     }
     if (!is_array($beans2)) {
         $beans2 = array($beans2);
     }
     $results = array();
     foreach ($beans1 as $bean1) {
         foreach ($beans2 as $bean2) {
             $table = $this->getTable(array($bean1->getMeta('type'), $bean2->getMeta('type')));
             $bean = $this->oodb->dispense($table);
             $results[] = $this->associateBeans($bean1, $bean2, $bean);
         }
     }
     return count($results) > 1 ? $results : reset($results);
 }
Example #11
0
 /**
  * Loads bean, recurses if one of the property appears to be a list.
  *
  * @param array   $array       data array to import as a bean
  * @param boolean $filterEmpty if TRUE empty STRING values are converted to NULL (default FALSE)
  *
  * @return RedBean_OODBBean
  *
  * @throws RedBean_Exception_Security
  */
 private function loadBean(&$array, $filterEmpty)
 {
     $type = $array['type'];
     unset($array['type']);
     if (isset($array['id'])) {
         // Do we need to load the bean?
         if (self::$loadBeans) {
             $bean = $this->redbean->load($type, (int) $array['id']);
         } else {
             throw new RedBean_Exception_Security('Attempt to load a bean in Cooker. Use enableBeanLoading to override but please read security notices first.');
         }
     } else {
         $bean = $this->redbean->dispense($type);
     }
     foreach ($array as $property => $value) {
         if (is_array($value)) {
             $bean->{$property} = $this->graph($value, $filterEmpty);
         } else {
             $bean->{$property} = $value == '' && self::$useNULLForEmptyString ? NULL : $value;
         }
     }
     return $bean;
 }
Example #12
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);
}
Example #13
0
 /**
  * Associates two beans with eachother.
  *
  * @param RedBean_OODBBean $bean1 bean1
  * @param RedBean_OODBBean $bean2 bean2
  */
 public function associate(RedBean_OODBBean $bean1, RedBean_OODBBean $bean2)
 {
     $table = $this->getTable(array($bean1->getMeta("type"), $bean2->getMeta("type")));
     $bean = $this->oodb->dispense($table);
     return $this->associateBeans($bean1, $bean2, $bean);
 }
Example #14
0
 $b->texture = "wood";
 $a->associate($g, $b);
 $a = new RedBean_AssociationManager($toolbox);
 $writer = $oldwriter;
 $redbean = $oldredbean;
 testpack("Test Custom ID Field");
 class MyWriter extends RedBean_QueryWriter_PostgreSQL
 {
     public function getIDField($type)
     {
         return $type . "_id";
     }
 }
 $writer2 = new MyWriter($adapter);
 $redbean2 = new RedBean_OODB($writer2);
 $movie = $redbean2->dispense("movie");
 asrt(isset($movie->movie_id), true);
 $movie->name = "movie 1";
 $movieid = $redbean2->store($movie);
 asrt($movieid > 0, true);
 $columns = array_keys($writer->getColumns("movie"));
 asrt(in_array("movie_id", $columns), true);
 asrt(in_array("id", $columns), false);
 $movie2 = $redbean2->dispense("movie");
 asrt(isset($movie2->movie_id), true);
 $movie2->name = "movie 2";
 $movieid2 = $redbean2->store($movie2);
 $movie1 = $redbean2->load("movie", $movieid);
 asrt($movie->name, "movie 1");
 $movie2 = $redbean2->load("movie", $movieid2);
 asrt($movie2->name, "movie 2");
Example #15
0
File: rb.php Project: u007/FlexiPHP
 /**
  * Dispenses a bean, just like oodb does
  * @param string $type
  * @return RedBean_OODBBean $bean
  */
 public function dispense($type)
 {
     return $this->oodb->dispense($type);
 }
 /**
  * Dispenses a new RedBean OODB Bean for use with
  * the rest of the methods.
  *
  * @param string  $type   type
  * @param integer $number number of beans to dispense
  * 
  * @return array $oneOrMoreBeans
  */
 public static function dispense($type, $num = 1)
 {
     if (!preg_match('/^[a-z0-9]+$/', $type) && self::$strictType) {
         throw new RedBean_Exception_Security('Invalid type: ' . $type);
     }
     if ($num == 1) {
         return self::$redbean->dispense($type);
     } else {
         $beans = array();
         for ($v = 0; $v < $num; $v++) {
             $beans[] = self::$redbean->dispense($type);
         }
         return $beans;
     }
 }
Example #17
0
 /**
  * @see RedBean_DuplicationManager::dup
  *
  * @param RedBean_OODBBean $bean  bean to be copied
  * @param array            $trail trail to prevent infinite loops
  * @param boolean          $pid   preserve IDs
  *
  * @return array $copiedBean the duplicated bean
  */
 protected function duplicate($bean, $trail = array(), $pid = false)
 {
     $type = $bean->getMeta('type');
     $key = $type . $bean->getID();
     if (isset($trail[$key])) {
         return $bean;
     }
     $trail[$key] = $bean;
     $copy = $this->redbean->dispense($type);
     $copy->importFrom($bean);
     $copy->id = 0;
     $tables = $this->tables;
     foreach ($tables as $table) {
         if (is_array($this->filters) && count($this->filters) && !in_array($table, $this->filters)) {
             continue;
         }
         if ($table == $type) {
             continue;
         }
         $owned = 'own' . ucfirst($table);
         $shared = 'shared' . ucfirst($table);
         if ($this->hasSharedList($type, $table)) {
             if ($beans = $bean->{$shared}) {
                 $copy->{$shared} = array();
                 foreach ($beans as $subBean) {
                     array_push($copy->{$shared}, $subBean);
                 }
             }
         } elseif ($this->hasOwnList($type, $table)) {
             if ($beans = $bean->{$owned}) {
                 $copy->{$owned} = array();
                 foreach ($beans as $subBean) {
                     array_push($copy->{$owned}, $this->duplicate($subBean, $trail, $pid));
                 }
             }
             $copy->setMeta('sys.shadow.' . $owned, null);
         }
         $copy->setMeta('sys.shadow.' . $shared, null);
     }
     if ($pid) {
         $copy->id = $bean->id;
     }
     return $copy;
 }
Example #18
0
 /**
  * Dispenses a new RedBean OODB Bean for use with
  * the rest of the methods.
  * @param string $type
  * @return RedBean_OODBBean $bean
  */
 public static function dispense($type)
 {
     return self::$redbean->dispense($type);
 }
Example #19
0
 /**
  * Constructor, loads directly from main table
  * @param $type
  * @param $id
  * @return unknown_type
  */
 public function __construct($type = false, $id = 0, $lock = false)
 {
     $id = floatval($id);
     if (!$type) {
         throw new Exception("Undefined bean type");
     } else {
         $this->type = preg_replace("[\\W_]", "", strtolower($type));
         //echo $this->type;
         if ($id > 0) {
             //if the id is higher than 0 load data
             $this->data = RedBean_OODB::getById($this->type, $id, $lock);
         } else {
             //otherwise, dispense a regular empty OODBBean
             $this->data = RedBean_OODB::dispense($this->type);
         }
     }
 }
Example #20
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();
}