1<?php 2 3require_once dirname(__FILE__).'/ICanvas.php'; 4 5/** 6 * @brief An ICanvas stub for unit testing 7 * 8 * The TestCanvas implements a canvas object that doesn't draw 9 * anything, but keeps a log of which methods have been called. This 10 * is used in unit testing the pChart, since all we care about is to 11 * ensure that the pChart is calling the right methods on the ICanvas 12 * (testing that the canvas does the right thing in response to these 13 * calls belongs in unit testing on the Canvas implementation 14 * 15 * After we run a test, the TestCanvas spits out a list of function 16 * calls and parameters, which we compare against a known good set. To 17 * some extent this is just a way of solving the same problem as Mock 18 * objects, given that the test cases are too large and there are too 19 * many hundreds of calls to manually set up a Mock object script. 20 */ 21class TestCanvas implements ICanvas { 22 function drawRectangle(Point $corner1, Point $corner2, Color $color, $lineWidth, $lineDotSize, ShadowProperties $shadowProperties) { 23 $this->logMethodCall(__METHOD__, func_get_args()); 24 } 25 26 function drawFilledRectangle(Point $corner1, Point $corner2, Color $color, 27 ShadowProperties $shadowProperties, 28 $drawBorder = false, 29 $alpha = 100, 30 $lineWidth = 1, 31 $lineDotSize = 0) { 32 $this->logMethodCall(__METHOD__, func_get_args()); 33 } 34 35 function drawRoundedRectangle(Point $corner1, Point $corner2, $radius, 36 Color $color, $lineWidth, $lineDotSize, 37 ShadowProperties $shadowProperties) { 38 $this->logMethodCall(__METHOD__, func_get_args()); 39 } 40 41 function drawFilledRoundedRectangle(Point $point1, Point $point2, $radius, 42 Color $color, $lineWidth, $lineDotSize, 43 ShadowProperties $shadowProperties) { 44 $this->logMethodCall(__METHOD__, func_get_args()); 45 } 46 47 function drawLine(Point $point1, Point $point2, Color $color, $lineWidth, $lineDotSize, ShadowProperties $shadowProperties, Point $boundingBoxMin = null, Point $boundingBoxMax = null) { 48 $this->logMethodCall(__METHOD__, func_get_args()); 49 } 50 51 function drawDottedLine(Point $point1, Point $point2, $dotSize, $lineWidth, Color $color, ShadowProperties $shadowProperties, Point $boundingBoxMin = null, Point $boundingBoxMax = null) { 52 $this->logMethodCall(__METHOD__, func_get_args()); 53 } 54 55 function drawAntialiasPixel(Point $point, Color $color, ShadowProperties $shadowProperties, $alpha = 100) { 56 $this->logMethodCall(__METHOD__, func_get_args()); 57 } 58 59 function drawText($fontSize, $angle, Point $point, Color $color, $fontName, $text, ShadowProperties $shadowProperties) { 60 $this->logMethodCall(__METHOD__, func_get_args()); 61 } 62 63 /** 64 * @todo The function's called drawCircle(), but you can make it 65 * draw an ellipse by passing in different values for width and 66 * height. This should be changed. 67 */ 68 function drawCircle(Point $center, $height, Color $color, ShadowProperties $shadowProperties, $width = null) { 69 $this->logMethodCall(__METHOD__, func_get_args()); 70 } 71 72 function drawFilledCircle(Point $center, $height, Color $color, ShadowProperties $shadowProperties, $width = null) { 73 $this->logMethodCall(__METHOD__, func_get_args()); 74 } 75 76 function drawFilledPolygon(array $points, $numPoints, Color $color, $alpha = 100) { 77 $this->logMethodCall(__METHOD__, func_get_args()); 78 } 79 80 function setAntialiasQuality($newQuality) { 81 $this->logMethodCall(__METHOD__, func_get_args()); 82 } 83 84 private function logMethodCall($methodName, $args) { 85 $formattedArgs = array(); 86 foreach ($args as $arg) { 87 if (is_array($arg)) { 88 $formattedArgs[] = 'array<' . implode(', ', $arg) . '>'; 89 } 90 else { 91 $formattedArgs[] = $arg; 92 } 93 } 94 $this->actionLog .= $methodName.'('.implode(', ', $formattedArgs).")\n"; 95 } 96 97 public function getActionLog() { 98 return $this->actionLog; 99 } 100 101 private $actionLog = ''; 102}