Therefore, we have to change the regular expression
pattern we used and allow . in the name.
Let's redesign the validator as show here:
class EmailValidator
{
public function validateEmail($email)
{
$pattern = "/[A-z0-9\.]{1,64}@[A-z0-9]+\.[A-z0-9]{2,3}/";
preg_match($pattern, $email,$matches);
return (strlen($matches[0])==strlen($email)?true:false);
}
}
Now if you run your test suites again, you will see the following output:
PHPUnit 3.0.5 by Sebastian Bergmann.
........
Time: 00:00
OK (8 tests)
Our test passes.
So what is the benefit? Time after time, when you need to add new validation rules
to your regular expression, this unit test will help to do the regression test so that the
same fault never occurs again.
That's the beauty of Unit Testing.
Reflection and Unit Testing
[ 120 ]
You will find two functions named setUp() and tearDown()
in the above example. setUp() is used for setting up everything for
the test; you can use it to connect to DB, to open a file or something
similar. tearDown() is for cleaning. It is called when the script
finishes executing.
Unit Testing for Everyday Script
Alongside these unit tests for functions and small classes, you will need to write unit
tests for a final result achieved by different functions.
Pages:
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135