Switch Test Cases (basic)
First we write a few functions that will serve as the body for handlers (via callbacks.)
<?
function stop_on_bar($pValue, $pParameters = NULL)
{
echo '<p><b>BAR rached</b></p>';
return TRUE;
}
function stop_on_foo($pValue, $pParameters = NULL)
{
echo '<p><b>FOO rached</b></p>';
return TRUE;
}
function default_echo($pValue, $pParameters = NULL){
echo '<p><i><b>Default Reached</b></i></p>';
return TRUE;
}
?>
Then we write a series of switch usage tests that employ these functions as handlers.
<h2>Case One</h2>
<p>
Determine if the class responds to a default handler.
No other handlers present.
</p>
<blockquote>
<?
// embedding the default handler as part of the constructor.
// also, the event is present as the first parameter of the case.
$case1 = new Zupal_Switch('bar', FALSE, new Zupal_Switch_Handler('default_echo'));
?>
<h3>1a: test responding to switch constructor parameter "Bar"</h3>
<?
$case1->execute();
?>
<h3>1b: test responding to execution parameter "Foo"</h3>
<?
$case1->execute('foo');
?>
</blockquote>
/** the result is below: */
Case One
Determine if the class responds to a default handler. No other handlers present.
1a: test responding to switch constructor parameter "Bar"
Default Reached
1b: test responding to execution parameter "Foo"
Default Reached
Then we add some actual handlers.
A single handler
<h2>Case Two</h2>
<p>Determine whether a keyed handler traps a response.
</p>
<?
/**
* case two --
*/
$case2 = new Zupal_Switch('bar', FALSE, 'default_echo');
$case2->set_handler('stop_on_bar', 'bar');
?>
<blockquote>
<h3>2a: test parameter "Foo"</h3>
<p>Ensure it doesn't respond to the wrong trigger
</p>
<?
$case2->execute('foo');
?>
<h3>2b: test parameter "Bar"</h3>
<p> ensure it responds to the RIGHT trigger</p>
<?
$case2->execute('bar');
?>
</blockquote>
<h2>Case Three</h2>
<p>test multiple handlers</p>
<?
Multiple Handlers
Here's a multi-handler example
$case3 = new Zupal_Switch('', FALSE, 'default_echo');
$case3->set_handler('stop_on_bar', 'bar');
$case3->set_handler('stop_on_foo', 'foo');
?>
<blockquote>
<h3>3a: test parameter "Foo"</h3>
<?
$case3->execute('foo');
?>
<h3>3a: test parameter "Bar"</h3>
<?
$case3->execute('bar');
?>
<h3>3b: test parameter "Fabian" (unhandled)</h3>
<?
$case3->execute('Fabian');
?>
</blockquote>
Case Two
Determine whether a keyed handler traps a response.
2a: test parameter "Foo"
Ensure it doesn't respond to the wrong trigger
Default Reached
2b: test parameter "Bar"
ensure it responds to the RIGHT trigger
BAR rached
Case Three
Test multiple handlers
<?
$case3 = new Zupal_Switch('', FALSE, 'default_echo');
$case3->set_handler('stop_on_bar', 'bar');
$case3->set_handler('stop_on_foo', 'foo');
?>
<blockquote>
<h3>3a: test parameter "Foo"</h3>
<?
$case3->execute('foo');
?>
<h3>3a: test parameter "Bar"</h3>
<?
$case3->execute('bar');
?>
<h3>3b: test parameter "Fabian" (unhandled)</h3>
<?
$case3->execute('Fabian');
?>
</blockquote>
Case Three
test multiple handlers
3a: test parameter "Foo"
FOO rached
3a: test parameter "Bar"
BAR rached
3b: test parameter "Fabian" (unhandled)
Default Reached
So we now have a set of classes that respond to a single named event and a fallthrough to the default handler. This so far qualifies as "reinventing the wheel" -- all the expressed performance is clearly present in the existing switch statement. To find where this class comes into its own we have to go back... TO THE FUTURE!

Post new comment