summaryrefslogtreecommitdiffstats
path: root/tests/chart_test.php
blob: 6069146bf5eebcb479eb409138020c6881ec0600 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
/**
 * ezcGraphChartTest 
 * 
 * @package Graph
 * @version //autogen//
 * @subpackage Tests
 * @copyright Copyright (C) 2005-2007 eZ systems as. All rights reserved.
 * @license http://ez.no/licenses/new_bsd New BSD License
 */

require_once dirname( __FILE__ ) . '/test_case.php';
require_once dirname( __FILE__ ) . '/custom_chart.php';

/**
 * Tests for ezcGraph class.
 * 
 * @package ImageAnalysis
 * @subpackage Tests
 */
class ezcGraphChartTest extends ezcGraphTestCase
{
    protected $testFiles = array(
        'jpeg'          => 'jpeg.jpg',
        'nonexistant'   => 'nonexisting.jpg',
        'invalid'       => 'text.txt',
    );

    protected $tempDir;
    protected $basePath;

	public static function suite()
	{
		return new PHPUnit_Framework_TestSuite( "ezcGraphChartTest" );
	}

    protected function setUp()
    {
        static $i = 0;

        $this->tempDir = $this->createTempDir( __CLASS__ . sprintf( '_%03d_', ++$i ) ) . '/';
        $this->basePath = dirname( __FILE__ ) . '/data/';
    }

    protected function tearDown()
    {
        if ( !$this->hasFailed() )
        {
            $this->removeTempDir();
        }
    }

    public function testSetTitle()
    {
        $pieChart = new ezcGraphPieChart();
        $pieChart->title = 'Test title';

        $this->assertSame(
            'Test title',
            $pieChart->title->title
        );

        $this->assertTrue(
            $pieChart->title instanceof ezcGraphChartElementText
        );
    }

    public function testSetOptionsUnknown()
    {
        try
        {
            $pieChart = new ezcGraphPieChart();
            $pieChart->options->unknown = 'unknown';
        }
        catch ( ezcBasePropertyNotFoundException $e )
        {
            return true;
        }

        $this->fail( 'Expected ezcBasePropertyNotFoundException' );
    }

    public function testSetRenderer()
    {
        $pieChart = new ezcGraphPieChart();
        $renderer = $pieChart->renderer = new ezcGraphRenderer2d();

        $this->assertSame(
            $renderer,
            $pieChart->renderer
        );
    }

    public function testSetInvalidRenderer()
    {
        try
        {
            $pieChart = new ezcGraphPieChart();
            $pieChart->renderer = 'invalid';
        }
        catch ( ezcBaseValueException $e )
        {
            return true;
        }

        $this->fail( 'Expected ezcGraphInvalidRendererException' );
    }

    public function testSetDriver()
    {
        if ( !ezcBaseFeatures::hasExtensionSupport( 'gd' ) && 
             ( ezcBaseFeatures::hasFunction( 'imagefttext' ) || ezcBaseFeatures::hasFunction( 'imagettftext' ) ) )
        {
            $this->markTestSkipped( 'This test needs ext/gd with native ttf support or FreeType 2 support.' );
        }

        $pieChart = new ezcGraphPieChart();
        $driver = $pieChart->driver = new ezcGraphGdDriver();

        $this->assertSame(
            $driver,
            $pieChart->driver
        );
    }

    public function testSetInvalidDriver()
    {
        try
        {
            $pieChart = new ezcGraphPieChart();
            $pieChart->driver = 'invalid';
        }
        catch ( ezcBaseValueException $e )
        {
            return true;
        }

        $this->fail( 'Expected ezcGraphInvalidDriverException' );
    }

    public function testPieChartWithoutData()
    {
        try
        {
            $pieChart = new ezcGraphPieChart();
            $pieChart->render( 400, 200 );
        }
        catch ( ezcGraphNoDataException $e )
        {
            return true;
        }
    }

    public function testBarChartWithoutData()
    {
        try
        {
            $barChart = new ezcGraphBarChart();
            $barChart->render( 400, 200 );
        }
        catch ( ezcGraphNoDataException $e )
        {
            return true;
        }
    }

    public function testBarChartWithSingleDataPoint()
    {
        $filename = $this->tempDir . __FUNCTION__ . '.svg';

        $barChart = new ezcGraphBarChart();
        $barChart->data['test'] = new ezcGraphArrayDataSet(
            array( 23 )
        );
        $barChart->render( 400, 200, $filename );

        $this->compare(
            $filename,
            $this->basePath . 'compare/' . __CLASS__ . '_' . __FUNCTION__ . '.svg'
        );
    }

    public function testBarChartWithSingleDataPointNumericAxis()
    {
        $filename = $this->tempDir . __FUNCTION__ . '.svg';

        $barChart = new ezcGraphBarChart();
        $barChart->xAxis = new ezcGraphChartElementNumericAxis();

        $barChart->data['test'] = new ezcGraphArrayDataSet(
            array( 23 )
        );
        $barChart->render( 400, 200, $filename );

        $this->compare(
            $filename,
            $this->basePath . 'compare/' . __CLASS__ . '_' . __FUNCTION__ . '.svg'
        );
    }

    public function testCustomChartClass()
    {
        $filename = $this->tempDir . __FUNCTION__ . '.svg';

        $chart = new ezcCustomTestChart();
        $chart->render( 400, 200, $filename );

        $this->compare(
            $filename,
            $this->basePath . 'compare/' . __CLASS__ . '_' . __FUNCTION__ . '.svg'
        );
    }
}
?>
OpenPOWER on IntegriCloud