That's the plan, hopefully sooner rather than later. Thanks for your time.
No real need for debate or comments (unless of course you want to) but it seems where people have debated with me regarding echo vs true debugger I could probably debate along the same lines with unit testing vs my current approach.
By way of quick PHP example, I'm putting together a collection of a few functions for IP related issues at the moment. As per, I tend to run things through a little test script, the script in this case is as follows...
Code: Select all
<?php
// filename : $app_path . '/tests/ip.tst.php'
header('Content-Type: text/plain');
function show_text($text, $cr = true, $pad = 70)
{
$text = $pad ? str_pad($text, $pad, '.') : $text;
echo !$cr ? $text : $text . "\x0a";
}
include('../lib/ip.cls.php');
$ips[0] = '232.106.10.7';
$ips[1] = '232.106.010.007';
$ips[2] = '232.106.010.000';
$ips[3] = '1080:0:0:0:8:800:200C:417A';
$ips[4] = '123.56..001';
$ips[5] = 'FEDC:BA98:7654:3210:FEDC:BA98:224.74.56.18';
$ips[6] = 'A0EC::FEDC:BA98:7654:3210';
$ips[7] = '::'; // compressed hex representation of 0:0:0:0:0:0:0:0
$ips[8] = '1080::8:123.231.4.99';
$ips[9] = '294967299';
$compare[0] = '244.66.10.27';
$compare[1] = '244.66.12.32';
$compare[2] = '244.66.17.32';
$compare[3] = '244.25.10.27';
$compare[4] = '133.66.10.27';
$compare[5] = '133.66.10.34';
$compare[6] = '2235697691';
$compare[7] = '244.66.12.27';
$compare[8] = '4097968667';
foreach ($ips as $ip)
{
// there is potential here to loop within the loop, fine for testing but may
// then require additional example scripts. leaving 'as is' as this script
// should serve as both test and example for even the most braindead
show_text("Testing IP address {$ip} ", false);
($type = IP::is_ip($ip)) ? show_text('Passed ', false, false) : show_text('Failed', true,false);
if ($type & IP_V4)
{
show_text('(IPv4)', true, false);
}
if ($type & IP_V6)
{
show_text('(IPv6)', true, false);
}
show_text('Testing as IPv4 ', false);
($type = IP::is_ip($ip, IP_V4)) ? show_text('Passed ', false, false) : show_text('Failed', true,false);
switch($type)
{
case V4_QUAD:
show_text('(dotted quad)', true, false);
break;
case V4_INT:
show_text('(integer)', true, false);
break;
}
show_text('Testing as IPv4 strict ', false);
IP::is_ip($ip, IP_V4, V4_STRICT) ? show_text('Passed', true, false) : show_text('Failed', true,false);
show_text('Testing as IPv4 quad ', false);
IP::is_ip($ip, V4_QUAD) ? show_text('Passed', true, false) : show_text('Failed', true,false);
show_text('Testing as IPv4 integer ', false);
IP::is_ip($ip, V4_INT) ? show_text('Passed', true, false) : show_text('Failed', true,false);
show_text('Testing as IPv6 ', false);
($type = IP::is_ip($ip, IP_V6)) ? show_text('Passed ', false, false) : show_text('Failed', true,false);
switch($type)
{
case V6_HEX:
show_text('(hex)', true, false);
break;
case V6_ZHEX:
show_text('(compressed hex)', true, false);
break;
case V6_HEXDEC:
show_text('(hex/dec)', true, false);
break;
case V6_ZHEXDEC:
show_text('(compressed hex/dec)', true, false);
break;
}
show_text('Testing as IPv6 hex ', false);
IP::is_ip($ip, V6_HEX) ? show_text('Passed', true, false) : show_text('Failed', true,false);
show_text('Testing as IPv6 compresed hex ', false);
IP::is_ip($ip, V6_ZHEX) ? show_text('Passed', true, false) : show_text('Failed', true,false);
show_text('Testing as IPv6 hex/dec ', false);
IP::is_ip($ip, V6_HEXDEC) ? show_text('Passed', true, false) : show_text('Failed', true,false);
show_text('Testing as IPv6 compressed hex/dec ', false);
IP::is_ip($ip, V6_ZHEXDEC) ? show_text('Passed', true, false) : show_text('Failed', true,false);
show_text('Converting to IPv4 integer ', false);
($int = IP::ip2int($ip)) ? show_text('Passed (' . $int . ')', true, false) : show_text('Failed', true, false);
show_text('Converting to IPv4 quad ', false);
($quad = IP::int2ip($ip)) ? show_text('Passed (' . $quad . ')', true, false) : show_text('Failed', true, false);
// SMA? acronym for Slightly More Advanced, best I could think of at the time
show_text('SMA testing as any except IPv4 integer ', false);
IP::is_ip($ip, IP_ALL ^ V4_INT) ? show_text('Passed', true, false) : show_text('Failed', true,false);
show_text('SMA testing as IPv6 hex or IPv4 any ', false);
IP::is_ip($ip, V6_HEX, IP_V4) ? show_text('Passed', true, false) : show_text('Failed', true,false);
show_text('SMA testing as IPv6 any or IPv4 integer ', false);
IP::is_ip($ip, IP_V6, V4_INT) ? show_text('Passed', true, false) : show_text('Failed', true,false);
show_text('SMA testing as IPv4 quad or IPv6 any except compressed hex ', false);
IP::is_ip($ip, V4_QUAD, IP_V6 ^ V6_ZHEX) ? show_text('Passed', true, false) : show_text('Failed', true,false);
show_text('', true, false);
}
show_text('Checking for client IP ', false);
($client = IP::client_ip()) ? show_text("Passed ({$client})", true, false) : show_text("Failed", true, false);
show_text('', true, false);
show_text("Comparison checking...", true, false);
for ($i = 0, $max = count($compare); $i < $max; $i++)
{
$ip1 = $compare[$i];
$ip2 = (isset($compare[$i + 1])) ? $compare[$i + 1] : $compare[0];
show_text("Comparing {$ip1} against {$ip2} :", true, false);
show_text('32-bit compare ', false);
IP::ip_cmp($ip1, $ip2, 32) ? show_text('Passed', true, false) : show_text('Failed', true,false);
show_text('24-bit compare ', false);
IP::ip_cmp($ip1, $ip2, 24) ? show_text('Passed', true, false) : show_text('Failed', true,false);
show_text('16-bit compare ', false);
IP::ip_cmp($ip1, $ip2, 16) ? show_text('Passed', true, false) : show_text('Failed', true,false);
show_text(' 8-bit compare ', false);
IP::ip_cmp($ip1, $ip2, ? show_text('Passed', true, false) : show_text('Failed', true,false);
show_text('', true, false);
}
show_text('Done', false, false);
?>
This test script allows for feeding the functions with various data types/values and getting a good overview of the results, as the results look like this....
Granted my test script probably doesn't offer the same level of granularity that unit testing may, however, should there be errors within the code the output of this script will help me as I feel it provides a relatively good insight as to *where* and *when* the problem lies. From that I can examine the code base and optionally crank up a debugger if required as a means to resolve the issue.
Much as I'd love to say that my code works first time every time that's not the case, and I did indeed end up running parts of the code through a debugger. Whilst in there resolving the issue the debugger also highlighted a completely unrelated issue, a problem with my flag/option handling logic, so even though all seemed good on the outside and the desired results were being acheived, there was potential under the hood that under certain circumstances there could be additional and unnessecary function calls being made.
Hopefully soon I'll be able to draw my own conclusions but certainly for the moment, I'm still unclear as to what exact benefits I could gain from adding unit testing to my toolbox.