Skip to content

Commit

Permalink
Cooker now throws exceptions in case of unexpected input or array str…
Browse files Browse the repository at this point in the history
…ucture
  • Loading branch information
= committed Dec 15, 2011
1 parent 0eb0876 commit b41edea
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 2 deletions.
5 changes: 4 additions & 1 deletion RedBean/Cooker.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ public function graph( $array, $filterEmpty = false ) {
elseif (is_array($array)) {
foreach($array as $key=>$value) {
$listBean = $this->graph($value,$filterEmpty);
if (!($listBean instanceof RedBean_OODBBean)) {
throw new RedBean_Exception_Security('Expected bean but got :'.gettype($listBean));
}
if ($listBean->isEmpty()) {
if (!$filterEmpty) {
$beans[$key] = $listBean;
Expand All @@ -102,7 +105,7 @@ public function graph( $array, $filterEmpty = false ) {
return $beans;
}
else {
return $array;
throw new RedBean_Exception_Security('Expected array but got :'.gettype($array));
}
}
}
61 changes: 60 additions & 1 deletion testing/RedUNIT/Base/Graph.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,39 @@ public function run() {
$adapter = $toolbox->getDatabaseAdapter();
$writer = $toolbox->getWriter();
$redbean = $toolbox->getRedBean();

try{
R::graph(array(array(array('a'=>'b'))));
fail();
}
catch(RedBean_Exception_Security $e){
pass();
}

try{
R::graph('ABC');
fail();
}
catch(RedBean_Exception_Security $e){
pass();
}

try{
R::graph(123);
fail();
}
catch(RedBean_Exception_Security $e){
pass();
}

try{
R::graph(array(new stdClass));
fail();
}
catch(RedBean_Exception_Security $e){
pass();
}

list($v1,$v2,$v3) = R::dispense('village',3);
list($b1,$b2,$b3,$b4,$b5,$b6) = R::dispense('building',6);
list($f1,$f2,$f3,$f4,$f5,$f6) = R::dispense('farmer',6);
Expand Down Expand Up @@ -334,6 +367,32 @@ public function run() {
asrt(end($order->ownCustomer)->name,'Bill');
asrt($order->sharedCoupon[$couponID]->name,'567');


//save a form using graph and ignore empty beans, wrong nesting
R::nuke();
$product = R::dispense('product');
$product->name = 'shampoo';
$productID = R::store($product);
$coupon = R::dispense('coupon');
$coupon->name = '567';
$couponID = R::store($coupon);

$form = array(
'type'=>'order',
'ownProduct'=>array(
array(
array('id'=>$productID,'type'=>'product')
),
),
);
try{
$order = R::graph($form, true);
fail();
}
catch(RedBean_Exception_Security $e){
pass();
}

//without ignore empty beans
R::nuke();
$product = R::dispense('product');
Expand All @@ -357,7 +416,7 @@ public function run() {
array('type'=>'coupon','id'=>$couponID)
)
);
$order = R::graph($form, true);
$order = R::graph($form);
asrt($order->getMeta('type'),'order');
asrt(count($order->ownProduct),1);
asrt(count($order->ownCustomer),2);
Expand Down
25 changes: 25 additions & 0 deletions testing/RedUNIT/Blackhole/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class RedUNIT_Blackhole_Export extends RedUNIT_Blackhole {
* @return void
*/
public function run() {


$redbean = R::$redbean;
$bean = new RedBean_OODBBean;
$bean->import(array("a"=>1,"b"=>2));
Expand All @@ -40,6 +42,29 @@ public function run() {
$exportedBean = $exportBean->export(true);
asrt($exportedBean["__info"]["metaitem.bla"],1);
asrt($exportedBean["__info"]["type"],"abean");

//can we determine whether a bean is empty?
testpack('test $bean->isEmpty() function');
$bean = R::dispense('bean');
asrt($bean->isEmpty(),true);
asrt((count($bean)>0),true);
$bean->property = 1;
asrt($bean->isEmpty(),false);
asrt((count($bean)>0),true);
$bean->property = 0;
asrt($bean->isEmpty(),true);
asrt((count($bean)>0),true);
$bean->property = false;
asrt($bean->isEmpty(),true);
asrt((count($bean)>0),true);
$bean->property = null;
asrt($bean->isEmpty(),true);
asrt((count($bean)>0),true);
unset($bean->property);
asrt($bean->isEmpty(),true);
asrt((count($bean)>0),true);


}

}
2 changes: 2 additions & 0 deletions testing/RedUNIT/Blackhole/Misc.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ public function run() {
$bean->hello = 'hi';
$bean->world = 'planet';
asrt($bean['hello'],'hi');
asrt(isset($bean['hello']),true);
asrt(isset($bean['bye']),false);
$bean['world'] = 'sphere';
asrt($bean->world,'sphere');
foreach($bean as $key=>$el) {
Expand Down

0 comments on commit b41edea

Please sign in to comment.