class Product { function getList() { $db =& DbConnectionBroker::getConnection(); //... } } class DbConnectionBroker { function &getConnection() { return new MysqlConnection(DB_USER, DB_PW, DB_NAME); } }
class Color { var $r=0; var $g=0; var $b=0; function Color($red=0, $green=0, $blue=0) { $this->r =$red; $this->g = $green; $this->b = $blue; } function getRgb() { return sprintf(‘#%02X%02X%02X’, $this->r, $this->g, $this->b); } }
function testColorBoundaries() { $color =& new Color(-1); $this->assertErrorPattern(‘/out.*0.*255/i’); $color =& new Color(1111); $this->assertErrorPattern(‘/out.*0.*255/i’); }
class Color { var $r=0; var $g=0; var $b=0; function Color($red=0, $green=0, $blue=0) { $red = (int)$red; if ($red < 0 || $red > 255) { trigger_error(“color ‘$color’ out of bounds, “ .”please specify a number between 0 and 255”); } $this->r = $red; $green = (int)$green; if ($green < 0 || $green > 255) { trigger_error(“color ‘$color’ out of bounds, “ .”please specify a number between 0 and 255”); } $this->g = $green; $blue = (int)$blue; if ($blue < 0 || $blue > 255) { trigger_error(“color ‘$color’ out of bounds, “ .”please specify a number between 0 and 255”); } $this->b = $blue; } function getRgb() { return sprintf(‘#%02X%02X%02X’, $this->r, $this->g, $this->b); } }
class Color { var $r=0; var $g=0; var $b=0; function Color($red=0, $green=0, $blue=0) { $this->r = $this->validateColor($red); $this->g = $this->validateColor($green); $this->b = $this->validateColor($blue); } function validateColor($color) { $check = (int)$color; if ($check < 0 || $check > 255) { trigger_error(“color ‘$color’ out of bounds, “ .”please specify a number between 0 and 255”); } else { return $check; } } function getRgb() { return sprintf(‘#%02X%02X%02X’, $this->r, $this->g, $this->b); } }
function TestBadColor() { $this->assertIsA($o =& CrayonBox::getColor(‘Lemon’), ‘Color’); $this->assertErrorPattern(‘/lemon/i’); // got black instead $this->assertEqual(‘#000000’, $o->getRgb()); }
以下是一个可以满足测试的CrayonBox类:
class CrayonBox { /** * Return valid colors as color name => array(red, green, blue) * * Note the array is returned from function call * because we want to have getColor able to be called statically * so we can’t have instance variables to store the array * @return array */ function colorList() { return array( ‘black’ => array(0, 0, 0) ,’green’ => array(0, 128, 0) // the rest of the colors ... ,’aqua’ => array(0, 255, 255) ); } /** * Factory method to return a Color * @param string $color_name the name of the desired color * @return Color */ function &getColor($color_name) { $color_name = strtolower($color_name); if (array_key_exists($color_name, $colors = CrayonBox::colorList())) { $color = $colors[$color_name]; return new Color($color[0], $color[1], $color[2]); } trigger_error(“No color ‘$color_name’ available”); // default to black return new Color; }
class Street extends Property { protected $base_rent; public $color; public function setRent($rent) { $this->base_rent = new Dollar($rent); } protected function calcRent() { if ($this->game->hasMonopoly($this->owner, $this->color)) { return $this->base_rent->add($this->base_rent); } return $this->base_rent; } } class RailRoad extends Property { protected function calcRent() { switch($this->game->railRoadCount($this->owner)) { case 1: return new Dollar(25); case 2: return new Dollar(50); case 3: return new Dollar(100); case 4: return new Dollar(200); default: return new Dollar; } } } class Utility extends Property { protected function calcRent() { switch ($this->game->utilityCount($this->owner)) { case 1: return new Dollar(4*$this->game->lastRoll()); case 2: return new Dollar(10*$this->game->lastRoll()); default: return new Dollar; } } }
class Assessor { protected $game; public function setGame($game) { $this->game = $game; } public function getProperty($name) { $prop_info = new PropertyInfo($this->prop_info[$name]); switch($prop_info->type) { case ‘Street’: $prop = new Street($this->game, $name, $prop_info->price); $prop->color = $prop_info->color; $prop->setRent($prop_info->rent); return $prop; case ‘RailRoad’: return new RailRoad($this->game, $name, $prop_info->price); break; case ‘Utility’: return new Utility($this->game, $name, $prop_info->price); break; default: //should not be able to get here } } protected $prop_info = array(/* ... */); }
class Assessor { protected $game; public function setGame($game) { $this->game = $game; } public function getProperty($name) { $prop_info = $this->getPropInfo($name); switch($prop_info->type) { case ‘Street’: $prop = new Street($this->game, $name, $prop_info->price); $prop->color = $prop_info->color; $prop->setRent($prop_info->rent); return $prop; case ‘RailRoad’: return new RailRoad($this->game, $name, $prop_info->price); break; case ‘Utility’: return new Utility($this->game, $name, $prop_info->price); break; default: //should not be able to get here } } protected $prop_info = array(/* ... */); protected function getPropInfo($name) { if (!array_key_exists($name, $this->prop_info)) { throw new InvalidPropertyNameException($name); } return new PropertyInfo($this->prop_info[$name]); } }