Page 59      All Pages  All Books
The Factory Pattern 59
To gain still more confidence in the code, you can test it with more values. This test runs with the code as-is:
function TestGetRgbRandom() {
$color =& new Color(rand(0,255), rand(0,255), rand(0,255)); $this->assertWantedPattern(
‘/^#[0-9A-F]{6}$/’,
$color->getRgb()); $color2 =& new Color($t = rand(0,255), $t, $t); $this->assertWantedPattern(
‘/^#([0-9A-F]{2})\1\1$/’,
$color2->getRgb()); }
®
assertWantedPattern
The assertWantedPattern() assertion tries to match its second parameter to the PCRE expression in the first parameter. If there’s a match, the assertion passes; otherwise it fails.
Building on the power of regular expression matching, the assertWantedPattern() assertion can allow for flexible tests.
All of these tests detail how the Color class behaves under normal, expected circumstances. But every well-designed class should also account for boundary conditions. For example, what should happen if a negative number is passed into the constructor as a color value? What happens for num-bers greater than 255? What happens for non-numeric data? A good test suite for Color would account for these boundary conditions in the tests.
function testColorBoundaries() {
$color =& new Color(-1);
$this->assertErrorPattern(‘/out.*0.*255/i’);
$color =& new Color(1111);
$this->assertErrorPattern(‘/out.*0.*255/i’); }
<D
assertErrorPattern
The assertErrorPattern() assertion allows you to specify a PCRE expression that should match a PHP error. If the error doesn’t materialize or doesn’t match the specified pattern, the assertion fails.

Page 59      All Pages  All Books