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