Jump to content


Photo

PHP Unit Testing questions


  • Please log in to reply
3 replies to this topic

#1 gannina

gannina
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 26 July 2006 - 05:46 AM

Hi, I am trying to learn how to do unit testing with Simpletest, but there are a few things I don't get.

1. Do you always make your methods return (true, false, or a value)?
- if a method doesn't return anything is there another way to test whats going on in a method or is it standard practice to just make you methods return something?

2. I was reading through a book and it had the following example

If (strlen ($pass) < 5 ) {
	throw new Exception("Password must have 5 or more letters");
}

If (is_null ($name) {
	return false;
}

Why does the first one throw an error message, and not some assert
statement, or both?

The second statement returns false, so it can be tested in a test case.
Why not just display an error message instead?

Like:

echo 'Name is empty';

I'm kinda confused on those issues. Also, can anyone recommend a simple introduction to unit testing. The documentation for simpletest doesn't cover the basics in unit testing. Thanks in advance

#2 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 26 July 2006 - 06:12 AM

Take a look at this and work it out why it does what it does then your understand valadation a little better ok

Good luck.

<?php

$word="my name is redarrow and i love php";

$number_of_charecters=strlen($word);

if($word > 50) {

echo " my good that a long word";

}elseif($word > 100 ) {

echo " you word is kinda all right";

}elseif($word <= 34) {

echo " <br> i love you sentence <br> numbe of chareters was
$number_of_charecters <br> $word <br>";
}
?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#3 gannina

gannina
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 26 July 2006 - 03:18 PM

I guess the user doesn't want to see those error messages?

#4 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 26 July 2006 - 03:47 PM

Why does the first one throw an error message, and not some assert
statement, or both?

Exceptions can be caught. That is there point.

function foo($a=false) {
  if (!$a) {
    throw new exception('your argument is false');
  } else {
    return true;
  }
}

try {
  foo();
} catch (exception $e) {
  echo $e->getmessage();
  exit();
}
echo "your argument passed to foo was true";

I think the reason the example you posted is how it is is it lets you differentiate between the two different types of failures. ie; If the users pass is too short it will throw an exception, if there username is null it will return false, if neither, then you have a valid combination.

Its probably not a good example of exception handling. exceptions are a funny thing, Its hard to understand when to use them. You can... if you wish... use them at every condition, however they become messy.

IMO, its best to use exceptions when the application relies on the effected action to work. ie; your database connection. Exception let you try different approuches to different errors, then hopefully fail gracefully. For general stuff (data validation and the like), I prefer to just have my functins return true / false.

Really, its up to you though. Hope that helps... probably confused the issue even further :-P




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users