Switch Test Cases (Back To The Future!)
Lets look at a truly event oriented use case. Say you have a bunch of users. Some can travel in time, some are banned. You want to send those that are time travellers back to the future, and you want to get alerts when banned users pass through the machine. (you don't want to stop them, what fun would that be?)
So you create a series of user tokens and an event handler that takes care of both the warning and time travelling activity.
<?
$biff = array('name' => "Biff",
'status' => 'banned',
'time_traveller' => TRUE
);
$marty = array('name' => "Marty",
'status' => 'user',
'time_traveller' => TRUE
);
$doc = array('name' => 'Doc',
'status' => 'admin',
'time_traveller' => TRUE
);
$dad = array('name' => 'dad',
'status' => 'user',
'time_traveller' => FALSE
);
$users = array($biff, $marty, $doc,$dad);
function bttf_missing_user_error($activity){
echo '<p><i><font color="red">attempt to ', $activity, '; no user present</font></i></p>';
return FALSE;
}
function bttf_do_nothing($pValue, $pParameters)
{
if (!array_key_exists('user', $pParameters)):
bttf_missing_user_error($pValue);
return FALSE;
endif;
echo '<p>Done attempting to ', $pValue, ' with ', $pParameters['user']['name'];
}
$back_to_the_future_1 = new Zupal_Switch(NULL, NULL, 'bttf_do_nothing');
?>
Note at this point you haven't "hooked in" the time machine or warning events. Again we are just validating the default handler -- as well as the error trigger. Also in this case there are TWO arguments -- we pass the event AND our user in a parameter array.
<h2>Case one: only the default handler is present.</h2>
<p>Time machine AWOL; all cases fall through to the default</p>
<ol>
<? foreach($users as $user): ?>
<li>
<?
$back_to_the_future_1->execute('time_travel', array('user' => $user));
?>
</li>
<? endforeach; ?>
<li>
<?
$back_to_the_future_1->execute('time_travel', array());
?>
</li>
</ol>
Outside of our loop-through of users we also validate that the system works even if we don't pass a user.
Case one: only the default handler is present.
Time machine AWOL; all cases fall through to the default
-
Done attempting to time_travel with Biff
-
Done attempting to time_travel with Marty
-
Done attempting to time_travel with Doc
-
Done attempting to time_travel with dad
-
attempt to time_travel; no user present
<h2>Case two: attempting to time travel; alert AND time travel handlers present. </h2>
<p>alert if a banned user time travels <b>AND</b>
send all time travellers through.</p>
<?
function bttf_warn_banned($pValue, $pParameters)
{
if (array_key_exists('user' , $pParameters)):
$user = $pParameters['user'];
else:
bttf_missing_user_error($pValue);
return FALSE;
endif;
if ($user['status'] == 'banned'):
echo '<p><font color="red">!!!WARNING!!!</font>: banned user ', $user['name'] , '! present</p>';
endif;
return FALSE; // cascade activity
}
function bttf_time_travel($pValue, $pParameters)
{
if (array_key_exists('user' , $pParameters)):
$user = $pParameters['user'];
else:
bttf_missing_user_error('Time Travel');
return FALSE;
endif;
if ($user['time_traveller']):
echo '<p>sending ', $user['name'] , ' back ... <b><i><font color="green">TO THE FUTURE!</font></i></b></p>';
else:
echo '<p>Sorry, ', $user['name'], ' cannot time travel, and is stuck in the present.</p>';
endif;
return FALSE; // cascade activity
}
$back_to_the_future_2 = new Zupal_Switch(NULL, NULL, 'bttf_do_nothing');
$warn_banned = new Zupal_Switch_Handler('bttf_warn_banned');
$warn_banned->set_responds_to(array('time_travel', 'warn_banned'));
$back_to_the_future_2->set_handler($warn_banned, 'warn_banned');
$back_to_the_future_2->set_handler('bttf_time_travel', 'time_travel');
?>
<ol>
<? foreach($users as $user): ?>
<li>
<?
$back_to_the_future_2->execute('time_travel', array('user' => $user));
?>
</li>
<? endforeach; ?>
<li>
<?
$back_to_the_future_2->execute('time_travel', array());
?>
</li>
</ol>
<p>Note triple error at the end -- all three handlers will fail out becuase of missing user.</p>
</blockquote>Case two: attempting to time travel; alert AND time travel handlers present.
alert if a banned user time travels AND send all time travellers through.
-
!!!WARNING!!!: banned user Biff! present
sending Biff back ... TO THE FUTURE!
Done attempting to time_travel with Biff
-
sending Marty back ... TO THE FUTURE!
Done attempting to time_travel with Marty
-
sending Doc back ... TO THE FUTURE!
Done attempting to time_travel with Doc
-
Sorry, dad cannot time travel, and is stuck in the present.
Done attempting to time_travel with dad
-
attempt to time_travel; no user present
attempt to Time Travel; no user present
attempt to time_travel; no user present
Note triple error at the end -- all three handlers will fail out becuase of missing user.
Now the utility of an OOP based event handler comes into its own. Instead of a binary result set, you get a broad array of possible results; time travellers go to the future, bad users are flagged, and errors are trapped. You aren't forced to manage the order of the handlers to get the results, becuase the functions return FALSE which allows the other handlers to recieve every event -- which is why the default handler always fires off.
As further proof, you can send a "warn" event through the system and ONLY get back the Biff message, the default handler and the errors.
<h2>Case Three: just use the warn case to detect bad users</h2>
<p>say, a "dry run" case to ensure all your users are good.</p>
<ol>
<? foreach($users as $user): ?>
<li>
<?
$back_to_the_future_2->execute('warn_banned', array('user' => $user));
?>
</li>
<? endforeach; ?>
<li>
<?
$back_to_the_future_2->execute('warn_banned', array());
?>
</li>
</ol>
<p>Note -- only TWO errors tripped becuase time travel handler doesn't take place.</p>
Case Three: just use the warn case to detect bad users
say, a "dry run" case to ensure all your users are good.
-
!!!WARNING!!!: banned user Biff! present
Done attempting to warn_banned with Biff
-
Done attempting to warn_banned with Marty
-
Done attempting to warn_banned with Doc
-
Done attempting to warn_banned with dad
-
attempt to warn_banned; no user present
attempt to warn_banned; no user present
Note -- only TWO errors tripped becuase time travel handler doesn't take place.

Post new comment