php:40
C:\Program Files\Zend\ZendStudio-5.2.0\bin\php5\dummy.php:1
FAILURES!
Tests: 2, Failures: 1.
Here, we found that our foolproof word-count function fails. So what was our test
input? We just add more spaces in our test parameter my name is afif, and then
our function fails. This is because it splits the sentence with white space and returns
the number of split parts. As there are more white spaces, so our function fails
gracefully. That's a pretty nice test case; we found that our function might fail in real
life if we release our code with this version of word counter. PHPUnit has become
useful for us already. Now we will solve our function so that it returns the
correct result if our sentence contains more white spaces. We change our
class.wordcount.php to this new one:
class WordCount
{
public function countWords($sentence)
{
$newsentence = preg_replace("~\s+~"," ",$sentence);
return count(split(" ",$newsentence));
}
}
Reflection and Unit Testing
[ 116 ]
Now if we run our test suite, it will give the following output.
PHPUnit 3.0.5 by Sebastian Bergmann.
..
Time: 00:00
OK (2 tests)
However we want more proof that our function will work better in the wild.
Pages:
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131