PageRenderTime 52ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/core/google/tests/cases/helpers/google_static_chart_engine.test.php

https://github.com/dakota/infinitas
PHP | 794 lines | 572 code | 114 blank | 108 comment | 0 complexity | 62b6f42c32b254f07d51afa7065d4909 MD5 | raw file
  1. <?php
  2. App::import('Core', array('Helper', 'AppHelper', 'ClassRegistry', 'Controller', 'Model'));
  3. App::import('Libs', array('Charts.BaseChartEngine'));
  4. App::import('Helper', array('Html', 'Google.GoogleStaticChartEngine'));
  5. /**
  6. * TheGoogleStaticChartEngineTestController class
  7. */
  8. class TheGoogleStaticChartEngineTestController extends Controller {
  9. /**
  10. * name property
  11. *
  12. * @var string 'TheTest'
  13. * @access public
  14. */
  15. var $name = 'TheTest';
  16. /**
  17. * uses property
  18. *
  19. * @var mixed null
  20. * @access public
  21. */
  22. var $uses = null;
  23. }
  24. Mock::generate('View', 'HtmlHelperMockView');
  25. /**
  26. * HtmlHelperTest class
  27. *
  28. * @package cake
  29. * @subpackage cake.tests.cases.libs.view.helpers
  30. */
  31. class GoogleStaticChartEngineHelperTest extends CakeTestCase {
  32. /**
  33. * html property
  34. *
  35. * @var object
  36. * @access public
  37. */
  38. public $GoogleStaticChartEngine = null;
  39. /**
  40. * setUp method
  41. *
  42. * @access public
  43. * @return void
  44. */
  45. function startTest() {
  46. $this->GoogleStaticChartEngine = new GoogleStaticChartEngineHelper();
  47. $this->GoogleStaticChartEngine->Html = new HtmlHelper();
  48. $view =& new View(new TheGoogleStaticChartEngineTestController());
  49. ClassRegistry::addObject('view', $view);
  50. }
  51. /**
  52. * endTest method
  53. *
  54. * @access public
  55. * @return void
  56. */
  57. function endTest() {
  58. ClassRegistry::flush();
  59. unset($this->GoogleStaticChartEngine);
  60. }
  61. /**
  62. * @brief test the implod method
  63. */
  64. public function testFormatImplode(){
  65. $this->GoogleStaticChartEngine->setType('line');
  66. $expected = '1,2,3,4,5';
  67. $this->assertEqual($expected, $this->GoogleStaticChartEngine->_implode('data', array(1,2,3,4,5)));
  68. $expected = '1|2|3|4|5';
  69. $this->assertEqual($expected, $this->GoogleStaticChartEngine->_implode('labels', array(1,2,3,4,5)));
  70. $expected = '1x2x3x4x5';
  71. $this->assertEqual($expected, $this->GoogleStaticChartEngine->_implode('size', array(1,2,3,4,5)));
  72. $this->expectError('Value for size is type string and expecting array');
  73. $this->GoogleStaticChartEngine->_implode('size', 'string');
  74. $this->expectError('No format available for foo');
  75. $this->GoogleStaticChartEngine->_implode('foo', array(1,2,3));
  76. $this->expectError('Array to string conversion');
  77. $this->GoogleStaticChartEngine->_implode('size', array(array(1,2,3)));
  78. }
  79. /**
  80. * @brief test the generic query string builder
  81. */
  82. public function testFormatGeneric(){
  83. $this->GoogleStaticChartEngine->setType('line');
  84. $expected = 'chd=t:1,2,3,4,5';
  85. $this->assertEqual($expected, $this->GoogleStaticChartEngine->_formatGeneric('data', array(1,2,3,4,5)));
  86. $expected = 'chd=t:';
  87. $this->assertEqual($expected, $this->GoogleStaticChartEngine->_formatGeneric('data', array()));
  88. $expected = 'chs=100x200';
  89. $this->assertEqual($expected, $this->GoogleStaticChartEngine->_formatGeneric('size', array(100,200)));
  90. $this->expectError();
  91. $this->GoogleStaticChartEngine->_formatGeneric('data', 'string');
  92. $this->expectError();
  93. $this->GoogleStaticChartEngine->_formatGeneric('data', false);
  94. }
  95. /**
  96. * @brief test building the query data for the chart data
  97. */
  98. public function testFormatData(){
  99. $this->GoogleStaticChartEngine->setType('line');
  100. $expected = 'chd=t:1,2,3,4,5';
  101. $this->assertEqual($expected, $this->GoogleStaticChartEngine->_formatData(array(1,2,3,4,5)));
  102. $expected = 'chd=t:1,2,3,4,5';
  103. $this->assertEqual($expected, $this->GoogleStaticChartEngine->_formatData(array(array(1,2,3,4,5))));
  104. $expected = 'chd=t:1,2,3,4,5|1,2,3,4,5';
  105. $this->assertEqual($expected, $this->GoogleStaticChartEngine->_formatData(array(array(1,2,3,4,5), array(1,2,3,4,5))));
  106. $expected = 'chd=t:1,2,3,4,5|1,2,3,4,5|1,2,3,4,5';
  107. $this->assertEqual($expected, $this->GoogleStaticChartEngine->_formatData(array(array(1,2,3,4,5), array(1,2,3,4,5), array(1,2,3,4,5))));
  108. }
  109. /**
  110. * @brief test building the query string for chart axes lables
  111. */
  112. public function testFormatLabels(){
  113. $this->GoogleStaticChartEngine->setType('line');
  114. $expected = array(
  115. 'labels' => 'chxl=0:|abc|xyz|1:|1|2|3|4|5|6',
  116. 'axes' => 'chxt=x,y'
  117. );
  118. $data = array(
  119. 'x' => array('abc', 'xyz'),
  120. 'y' => array(1,2,3,4,5,6)
  121. );
  122. $this->assertEqual($expected, $this->GoogleStaticChartEngine->_formatLabels($data));
  123. }
  124. /**
  125. * @brief check building the query string for coloring charts
  126. */
  127. public function testFormatColor(){
  128. $this->GoogleStaticChartEngine->setType('line');
  129. $data = array('series' => '03348a');
  130. $expected = array('chco=03348a');
  131. $this->assertEqual($expected, $this->GoogleStaticChartEngine->_formatColor($data));
  132. $data = array(
  133. 'series' => array(
  134. '0d5c05', // dark green
  135. '03348a' // dark blue
  136. )
  137. );
  138. $expected = array('chco=0d5c05,03348a');
  139. $this->assertEqual($expected, $this->GoogleStaticChartEngine->_formatColor($data));
  140. $data = array(
  141. 'foo' => 'bar',
  142. 'series' => array(
  143. '0d5c05', // dark green
  144. '03348a' // dark blue
  145. )
  146. );
  147. $this->assertEqual($expected, $this->GoogleStaticChartEngine->_formatColor($data));
  148. $data = array('series' => array());
  149. $this->assertEqual(array(), $this->GoogleStaticChartEngine->_formatColor($data));
  150. $data = array('series' => array(array('000000'), array('FF0000', '00FF00', '0000FF')));
  151. $expected = array('chco=000000,FF0000|00FF00|0000FF');
  152. $this->assertEqual($expected, $this->GoogleStaticChartEngine->_formatColor($data));
  153. }
  154. /**
  155. * @todo
  156. */
  157. public function testFormatColorFill(){
  158. }
  159. /**
  160. * @brief check building the query string for spacing on charts
  161. */
  162. public function testFormatSpacing(){
  163. $this->GoogleStaticChartEngine->setType('line');
  164. $data = array('type' => 'absolute', 'width' => 10);
  165. $expected = 'chbh=10';
  166. $this->assertEqual($expected, $this->GoogleStaticChartEngine->_formatSpacing($data));
  167. $data = array('type' => 'relative', 'width' => 10);
  168. $expected = 'chbh=10';
  169. $this->assertEqual($expected, $this->GoogleStaticChartEngine->_formatSpacing($data));
  170. $data = array('type' => 'absolute', 'padding' => 10);
  171. $expected = 'chbh=a,10';
  172. $this->assertEqual($expected, $this->GoogleStaticChartEngine->_formatSpacing($data));
  173. $data = array('type' => 'relative', 'padding' => 10);
  174. $expected = 'chbh=r,0.1';
  175. $this->assertEqual($expected, $this->GoogleStaticChartEngine->_formatSpacing($data));
  176. $data = array('type' => 'absolute');
  177. $expected = 'chbh=a';
  178. $this->assertEqual($expected, $this->GoogleStaticChartEngine->_formatSpacing($data));
  179. $data = array('type' => 'foo', 'padding' => 10);
  180. $this->expectError('Spacing type should be absolute or relative, not foo');
  181. $this->GoogleStaticChartEngine->_formatSpacing($data);
  182. }
  183. /**
  184. * @brief test building the spacing on group type charts
  185. */
  186. public function testFormatSpacingForGroupCharts(){
  187. $this->GoogleStaticChartEngine->setType('bar_group');
  188. $data = array('type' => 'absolute', 'padding' => 10);
  189. $expected = 'chbh=a,10,20';
  190. $this->assertEqual($expected, $this->GoogleStaticChartEngine->_formatSpacing($data));
  191. $data = array('padding' => 10);
  192. $expected = 'chbh=a,10,20';
  193. $this->assertEqual($expected, $this->GoogleStaticChartEngine->_formatSpacing($data));
  194. $data = array('type' => 'relative', 'padding' => 10);
  195. $expected = 'chbh=r,0.1,0.2';
  196. $this->assertEqual($expected, $this->GoogleStaticChartEngine->_formatSpacing($data));
  197. }
  198. /**
  199. * @brief test building the size query string
  200. */
  201. public function testFormatSize(){
  202. $this->GoogleStaticChartEngine->setType('line');
  203. $data = array('width' => 200, 'height' => 400);
  204. $expected = 'chs=200x400';
  205. $this->assertEqual($expected, $this->GoogleStaticChartEngine->_formatSize($data));
  206. $data = array('width' => 400, 'height' => 200);
  207. $expected = 'chs=400x200';
  208. $this->assertEqual($expected, $this->GoogleStaticChartEngine->_formatSize($data));
  209. $data = array('width' => 200);
  210. $expected = 'chs=200x200';
  211. $this->assertEqual($expected, $this->GoogleStaticChartEngine->_formatSize($data));
  212. $data = array('width' => 600, 'height' => 200, 'foo' => 123);
  213. $expected = 'chs=600x200';
  214. $this->assertEqual($expected, $this->GoogleStaticChartEngine->_formatSize($data));
  215. $data = array('width' => 600, 'height' => 600);
  216. $this->expectError('Size of 360000px is greater than maximum allowed size 300000px');
  217. $this->GoogleStaticChartEngine->_formatSize($data);
  218. $data = array('width' => 300000, 'height' => 1);
  219. $expected = 'chs=300000x1';
  220. $this->assertEqual($expected, $this->GoogleStaticChartEngine->_formatSize($data));
  221. $data = array('width' => 300001, 'height' => 1);
  222. $this->expectError('Size of 300001px is greater than maximum allowed size 300000px');
  223. $this->GoogleStaticChartEngine->_formatSize($data);
  224. }
  225. /**
  226. * @brief test generating a legend
  227. */
  228. public function testFormatLegend(){
  229. $this->GoogleStaticChartEngine->setType('line');
  230. $data = array('position' => 'top', 'order' => 'auto', 'labels' => array('abc', 'xyz'));
  231. $expected = array('legend_labels' => 'chdl=abc|xyz','legend_position' => 'chdlp=t|a');
  232. $this->assertEqual($expected, $this->GoogleStaticChartEngine->_formatLegend($data));
  233. $data = array('position' => 'top', 'order' => 'auto');
  234. $this->expectError('Skipping legend, no lables specified');
  235. $this->GoogleStaticChartEngine->_formatLegend($data);
  236. $data = array('labels' => array('abc', 'xyz'));
  237. $expected = array('legend_labels' => 'chdl=abc|xyz','legend_position' => 'chdlp=r|l');
  238. $this->assertEqual($expected, $this->GoogleStaticChartEngine->_formatLegend($data));
  239. /**
  240. * Test the ordering
  241. */
  242. $data = array('order' => 'default', 'labels' => array('abc', 'xyz'));
  243. $expected = array('legend_labels' => 'chdl=abc|xyz','legend_position' => 'chdlp=r|l');
  244. $this->assertEqual($expected, $this->GoogleStaticChartEngine->_formatLegend($data));
  245. $data = array('order' => 'reverse', 'labels' => array('abc', 'xyz'));
  246. $expected = array('legend_labels' => 'chdl=abc|xyz','legend_position' => 'chdlp=r|r');
  247. $this->assertEqual($expected, $this->GoogleStaticChartEngine->_formatLegend($data));
  248. $data = array('order' => array(1,0), 'labels' => array('abc', 'xyz'));
  249. $expected = array('legend_labels' => 'chdl=abc|xyz','legend_position' => 'chdlp=r|1,0');
  250. $this->assertEqual($expected, $this->GoogleStaticChartEngine->_formatLegend($data));
  251. $data = array('order' => array(1,0), 'labels' => array('abc', 'xyz', 'foo'));
  252. $expected = array('legend_labels' => 'chdl=abc|xyz|foo','legend_position' => 'chdlp=r|l');
  253. $this->expectError();
  254. $this->assertEqual($expected, $this->GoogleStaticChartEngine->_formatLegend($data));
  255. /**
  256. * test the position
  257. */
  258. $data = array('position' => 'bottom_horizontal', 'labels' => array('abc', 'xyz'));
  259. $expected = array('legend_labels' => 'chdl=abc|xyz','legend_position' => 'chdlp=b|l');
  260. $this->assertEqual($expected, $this->GoogleStaticChartEngine->_formatLegend($data));
  261. $data = array('position' => 'bottom', 'labels' => array('abc', 'xyz'));
  262. $this->assertEqual($expected, $this->GoogleStaticChartEngine->_formatLegend($data));
  263. $data = array('position' => 'bottom_vertical', 'labels' => array('abc', 'xyz'));
  264. $expected = array('legend_labels' => 'chdl=abc|xyz','legend_position' => 'chdlp=bv|l');
  265. $this->assertEqual($expected, $this->GoogleStaticChartEngine->_formatLegend($data));
  266. $data = array('position' => 'top', 'labels' => array('abc', 'xyz'));
  267. $expected = array('legend_labels' => 'chdl=abc|xyz','legend_position' => 'chdlp=t|l');
  268. $this->assertEqual($expected, $this->GoogleStaticChartEngine->_formatLegend($data));
  269. $data = array('position' => 'top_horizontal', 'labels' => array('abc', 'xyz'));
  270. $this->assertEqual($expected, $this->GoogleStaticChartEngine->_formatLegend($data));
  271. $data = array('position' => 'top_vertical', 'labels' => array('abc', 'xyz'));
  272. $expected = array('legend_labels' => 'chdl=abc|xyz','legend_position' => 'chdlp=tv|l');
  273. $this->assertEqual($expected, $this->GoogleStaticChartEngine->_formatLegend($data));
  274. $data = array('position' => 'left', 'labels' => array('abc', 'xyz'));
  275. $expected = array('legend_labels' => 'chdl=abc|xyz','legend_position' => 'chdlp=l|l');
  276. $this->assertEqual($expected, $this->GoogleStaticChartEngine->_formatLegend($data));
  277. $data = array('position' => 'left_vertical', 'labels' => array('abc', 'xyz'));
  278. $this->assertEqual($expected, $this->GoogleStaticChartEngine->_formatLegend($data));
  279. }
  280. /**
  281. * @brief test building the query string for line styles
  282. */
  283. public function testFormatLineStyle(){
  284. $this->GoogleStaticChartEngine->setType('gauge');
  285. $data = array(
  286. array('thickness' => 3,'arrow' => 15),
  287. array('thickness' => 3,'dash' => array(5, 5),'arrow' => 10)
  288. );
  289. $expected = 'chls=3|3,5,5|15|10';
  290. $this->assertEqual($expected, $this->GoogleStaticChartEngine->_formatLineStyle($data));
  291. $data = array(
  292. array('thickness' => 3),
  293. array('thickness' => 3,'dash' => array(5, 5))
  294. );
  295. $expected = 'chls=3|3,5,5';
  296. $this->assertEqual($expected, $this->GoogleStaticChartEngine->_formatLineStyle($data));
  297. $data = array(
  298. array('thickness' => 3),
  299. array('thickness' => 3,'dash' => array(5))
  300. );
  301. $expected = 'chls=3|3,5,5';
  302. $this->assertEqual($expected, $this->GoogleStaticChartEngine->_formatLineStyle($data));
  303. $data = array(
  304. array('thickness' => 3),
  305. array('thickness' => 3,'dash' => 5)
  306. );
  307. $expected = 'chls=3|3,5,5';
  308. $this->assertEqual($expected, $this->GoogleStaticChartEngine->_formatLineStyle($data));
  309. $data = array(
  310. array('thickness' => 3),
  311. array('thickness' => 3,'dash' => array(5, 5, 5, 5, 5))
  312. );
  313. $expected = 'chls=3|3,5,5';
  314. $this->assertEqual($expected, $this->GoogleStaticChartEngine->_formatLineStyle($data));
  315. $data = array(
  316. array('thickness' => 3, 'dash' => array(5, 5)),
  317. array('thickness' => 3, 'dash' => array(5, 5))
  318. );
  319. $expected = 'chls=3,5,5|3,5,5';
  320. $this->assertEqual($expected, $this->GoogleStaticChartEngine->_formatLineStyle($data));
  321. }
  322. /**
  323. * @brief test building the different parts of a query
  324. */
  325. public function testFormatFormatQueryParts(){
  326. $this->GoogleStaticChartEngine->setType('line');
  327. $data = array();
  328. $data['data'] = array(array(1,2,3,4,5), array(1,2,3,4,5));
  329. $expected = 'chd=t:1,2,3,4,5|1,2,3,4,5';
  330. $this->assertEqual($expected, $this->GoogleStaticChartEngine->_formatQueryParts('data', $data['data']));
  331. $data['size'] = array('width' => 123, 'height' => 321);
  332. $expected = 'chs=123x321';
  333. $this->assertEqual($expected, $this->GoogleStaticChartEngine->_formatQueryParts('size', $data['size']));
  334. $data['color'] = array('series' => array('0000ff'));
  335. $expected = array(0 => 'chco=0000ff');
  336. $this->assertEqual($expected, $this->GoogleStaticChartEngine->_formatQueryParts('color', $data['color']));
  337. $data['scale'] = array(0, 300);
  338. $expected = 'chds=0,300';
  339. $this->assertEqual($expected, $this->GoogleStaticChartEngine->_formatQueryParts('scale', $data['scale']));
  340. $this->assertFalse($this->GoogleStaticChartEngine->_formatQueryParts('data', array()));
  341. }
  342. /**
  343. * @brief test for charts that will work
  344. */
  345. public function testFormatBuildChart(){
  346. $this->GoogleStaticChartEngine->setType('line');
  347. $chart = array(
  348. 'data' => array(1,2,3,4,5),
  349. 'color' => array('series' => array('123123')),
  350. 'size' => array('width' => 123, 'height' => 321),
  351. 'extra' => array('return' => 'url')
  352. );
  353. /**
  354. * test building a query
  355. */
  356. $expected = 'http://0.chart.apis.google.com/chart?cht=lc&chd=t:1,2,3,4,5&chco=123123&chs=123x321';
  357. $this->assertEqual($expected, $this->GoogleStaticChartEngine->_buildChart($chart));
  358. /**
  359. * check that the urls are correct for using downloading many images at once
  360. */
  361. $this->GoogleStaticChartEngine->_buildChart($chart);
  362. $this->GoogleStaticChartEngine->_buildChart($chart);
  363. $this->GoogleStaticChartEngine->_buildChart($chart);
  364. $this->GoogleStaticChartEngine->_buildChart($chart);
  365. $this->GoogleStaticChartEngine->_buildChart($chart);
  366. $this->GoogleStaticChartEngine->_buildChart($chart);
  367. $this->GoogleStaticChartEngine->_buildChart($chart);
  368. $this->GoogleStaticChartEngine->_buildChart($chart);
  369. $expected = 'http://9.chart.apis.google.com/chart?cht=lc&chd=t:1,2,3,4,5&chco=123123&chs=123x321';
  370. $this->assertEqual($expected, $this->GoogleStaticChartEngine->_buildChart($chart));
  371. $expected = 'http://0.chart.apis.google.com/chart?cht=lc&chd=t:1,2,3,4,5&chco=123123&chs=123x321';
  372. $this->assertEqual($expected, $this->GoogleStaticChartEngine->_buildChart($chart));
  373. $chart = array(
  374. 'data' => array(1,2,3,4,5),
  375. 'values' => array('min' => 1, 'max' => 5),
  376. 'color' => array('series' => array('123123')),
  377. 'size' => array('width' => 123, 'height' => 321),
  378. 'extra' => array('return' => 'url', 'scale' => 'relative')
  379. );
  380. $expected = 'http://1.chart.apis.google.com/chart?cht=lc&chd=t:1,2,3,4,5&chco=123123&chs=123x321&chds=0,5';
  381. $this->assertEqual($expected, $this->GoogleStaticChartEngine->_buildChart($chart));
  382. $this->GoogleStaticChartEngine->setType('bar');
  383. $chart = array(
  384. 'data' => array(1,2,3,4,5),
  385. 'color' => array('series' => array('123123')),
  386. 'size' => array('width' => 123, 'height' => 321),
  387. 'config' => array('type' => 'horizontal_group'),
  388. 'extra' => array('return' => 'url')
  389. );
  390. $expected = 'http://2.chart.apis.google.com/chart?cht=bhg&chd=t:1,2,3,4,5&chco=123123&chs=123x321';
  391. $this->assertEqual($expected, $this->GoogleStaticChartEngine->_buildChart($chart));
  392. }
  393. /**
  394. * @brief tests for charts that will not work
  395. */
  396. public function testBrokenCharts(){
  397. $this->GoogleStaticChartEngine->setType('line');
  398. $chart = array(
  399. 'data' => array(
  400. 1,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,
  401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,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,
  419. 1,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,
  420. 1,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,
  421. 1,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,
  422. 1,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,
  423. 1,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5,
  424. ),
  425. 'color' => array('series' => array('123123')),
  426. 'size' => array('width' => 123, 'height' => 321),
  427. 'extra' => array('return' => 'url')
  428. );
  429. $this->expectError('The query string is too long (2809 chars)');
  430. $this->GoogleStaticChartEngine->_buildChart($chart);
  431. $this->GoogleStaticChartEngine->setType('foo');
  432. $chart = array(
  433. 'data' => array(1,2,3,4,5),
  434. 'color' => array('series' => array('123123')),
  435. 'size' => array('width' => 123, 'height' => 321)
  436. );
  437. $this->expectError('The chart type "foo" is invalid');
  438. $this->GoogleStaticChartEngine->_buildChart($chart);
  439. }
  440. /**
  441. * @brief test building image markup for charts
  442. */
  443. public function testFormatImage(){
  444. $this->GoogleStaticChartEngine->setType('line');
  445. $chart = array(
  446. 'data' => array(1,2,3,4,5),
  447. 'color' => array('series' => array('123123')),
  448. 'size' => array('width' => 123, 'height' => 321)
  449. );
  450. $expected = '<img src="http://0.chart.apis.google.com/chart?cht=lc&chd=t:1,2,3,4,5&chco=123123&chs=123x321" alt="" />';
  451. $this->assertEqual($expected, $this->GoogleStaticChartEngine->_buildChart($chart));
  452. $chart = array(
  453. 'data' => array(1,2,3,4,5),
  454. 'color' => array('series' => array('123123')),
  455. 'size' => array('width' => 123, 'height' => 321),
  456. 'extra' => array(
  457. 'image' => array(
  458. 'class' => 'something',
  459. 'alt' => 'some chart'
  460. )
  461. )
  462. );
  463. $expected = '<img src="http://1.chart.apis.google.com/chart?cht=lc&chd=t:1,2,3,4,5&chco=123123&chs=123x321" class="something" alt="some chart" />';
  464. $this->assertEqual($expected, $this->GoogleStaticChartEngine->_buildChart($chart));
  465. }
  466. /**
  467. * @brief test building different types of gauge charts
  468. */
  469. public function testBuildGaugeCharts(){
  470. $chart = array(
  471. 'data' => array(70),
  472. 'axes' => array(
  473. 'x',
  474. 'y'
  475. ),
  476. 'labels' => array(
  477. array('Groovy'),
  478. array('slow', 'faster', 'crazy')
  479. ),
  480. 'color' => array('series' => explode(',', 'FF0000,FF8040,FFFF00,00FF00,00FFFF,0000FF,800080')),
  481. 'size' => array('width' => 200, 'height' => 125),
  482. 'extra' => array('return' => 'url')
  483. );
  484. $expected = 'http://0.chart.apis.google.com/chart?cht=gom&chd=t:70&chxl=0:|Groovy|1:|slow|faster|crazy&chxt=0,1&chco=FF0000,FF8040,FFFF00,00FF00,00FFFF,0000FF,800080&chs=200x125';
  485. $this->assertEqual($expected, $this->GoogleStaticChartEngine->gauge($chart));
  486. $chart = array(
  487. 'data' => array(20, 40, 60),
  488. 'legend' => array('position' => 'bottom_horizontal', 'labels' => array(1, 2, 3)),
  489. 'size' => array('width' => 150, 'height' => 100),
  490. 'extra' => array('return' => 'url')
  491. );
  492. $expected = 'http://1.chart.apis.google.com/chart?cht=gom&chd=t:20,40,60&chdl=1|2|3&chdlp=b|l&chs=150x100';
  493. $this->assertEqual($expected, $this->GoogleStaticChartEngine->gauge($chart));
  494. $chart = array(
  495. 'data' => array(array(20, 40), array(60)),
  496. 'size' => array('width' => 150, 'height' => 100),
  497. 'extra' => array(
  498. 'return' => 'url',
  499. 'arrows' => array(
  500. array('thickness' => 3, 'arrow' => 15),
  501. array('thickness' => 3, 'dash' => array(5, 5), 'arrow' => 10)
  502. )
  503. )
  504. );
  505. $expected = 'http://2.chart.apis.google.com/chart?cht=gom&chd=t:20,40|60&chs=150x100&chls=3|3,5,5|15|10';
  506. $this->assertEqual($expected, $this->GoogleStaticChartEngine->gauge($chart));
  507. }
  508. /**
  509. * @brief test building bra charts
  510. */
  511. public function testBuildBarCharts(){
  512. $chart = array(
  513. 'data' => array(12, 23, 10, 40),
  514. 'size' => array('width' => 200, 'height' => 125),
  515. 'config' => array('type' => 'horizontal'),
  516. 'extra' => array('return' => 'url')
  517. );
  518. $expected = 'http://0.chart.apis.google.com/chart?cht=bhs&chd=t:12,23,10,40&chs=200x125';
  519. $this->assertEqual($expected, $this->GoogleStaticChartEngine->bar($chart));
  520. $chart = array(
  521. 'data' => array(explode(',', '10,50,60,80,40'), explode(',', '50,60,100,40,20')),
  522. 'size' => array('width' => 200, 'height' => 125),
  523. 'config' => array('type' => 'horizontal'),
  524. 'color' => array('series' => array('4d89f9', 'c6d9fd')),
  525. 'spacing' => array('width' => 20),
  526. 'scale' => array(0, 160),
  527. 'extra' => array('return' => 'url')
  528. );
  529. $expected = 'http://1.chart.apis.google.com/chart?cht=bhs&chd=t:10,50,60,80,40|50,60,100,40,20&chs=200x125&chco=4d89f9,c6d9fd&chbh=20&chds=0,160';
  530. $this->assertEqual($expected, $this->GoogleStaticChartEngine->bar($chart));
  531. $chart = array(
  532. 'data' => array(explode(',', '10,50,60,80,40'), explode(',', '50,60,100,40,20')),
  533. 'size' => array('width' => 200, 'height' => 125),
  534. 'config' => array('type' => 'vertical'),
  535. 'color' => array('series' => array('4d89f9', 'c6d9fd')),
  536. 'spacing' => array('width' => 20),
  537. 'scale' => array(0, 160),
  538. 'extra' => array('return' => 'url')
  539. );
  540. $expected = 'http://2.chart.apis.google.com/chart?cht=bvs&chd=t:10,50,60,80,40|50,60,100,40,20&chs=200x125&chco=4d89f9,c6d9fd&chbh=20&chds=0,160';
  541. $this->assertEqual($expected, $this->GoogleStaticChartEngine->bar($chart));
  542. $chart = array(
  543. 'data' => array(explode(',', '10,50,60,80,40'), explode(',', '50,60,100,40,20'), explode(',', '30,30,75,20,60')),
  544. 'size' => array('width' => 200, 'height' => 125),
  545. 'config' => array('type' => 'vertical_overlay'),
  546. 'color' => array('series' => array('4d89f9', 'c6d9fd', '0000FF')),
  547. 'labels' => array('y' => range(0, 100, 20)),
  548. 'spacing' => array('width' => 20),
  549. 'extra' => array('return' => 'url')
  550. );
  551. $expected = 'http://3.chart.apis.google.com/chart?cht=bvo&chd=t:10,50,60,80,40|50,60,100,40,20|30,30,75,20,60&chs=200x125&chco=4d89f9,c6d9fd,0000FF&chxl=0:|0|20|40|60|80|100&chxt=y&chbh=20';
  552. $this->assertEqual($expected, $this->GoogleStaticChartEngine->bar($chart));
  553. $chart = array(
  554. 'data' => array(explode(',', '60,74'), explode(',', '80,86')),
  555. 'size' => array('width' => 200, 'height' => 125),
  556. 'config' => array('type' => 'horizontal_group'),
  557. 'color' => array('series' => array('4d89f9', 'c6d9fd')),
  558. 'extra' => array('return' => 'url')
  559. );
  560. $expected = 'http://4.chart.apis.google.com/chart?cht=bhg&chd=t:60,74|80,86&chs=200x125&chco=4d89f9,c6d9fd';
  561. $this->assertEqual($expected, $this->GoogleStaticChartEngine->bar($chart));
  562. $chart = array(
  563. 'data' => array(explode(',', '60,74'), explode(',', '80,86')),
  564. 'size' => array('width' => 200, 'height' => 125),
  565. 'config' => array('type' => 'horizontal_group'),
  566. 'color' => array('series' => array('4d89f9', 'c6d9fd')),
  567. 'spacing' => array('width' => 10, 'padding' => 5, 'grouping' => 20),
  568. 'extra' => array('return' => 'url')
  569. );
  570. $expected = 'http://5.chart.apis.google.com/chart?cht=bhg&chd=t:60,74|80,86&chs=200x125&chco=4d89f9,c6d9fd&chbh=10,5,20';
  571. $this->assertEqual($expected, $this->GoogleStaticChartEngine->bar($chart));
  572. $chart = array(
  573. 'data' => array(explode(',', '60,74,123,23,45')),
  574. 'size' => array('width' => 200, 'height' => 125),
  575. 'config' => array('type' => 'vertical'),
  576. 'color' => array('series' => array(explode('|', 'FFC6A5|FFFF42|DEF3BD|00A5C6|DEBDDE'))),
  577. 'extra' => array('return' => 'url')
  578. );
  579. $expected = 'http://6.chart.apis.google.com/chart?cht=bvs&chd=t:60,74,123,23,45&chs=200x125&chco=FFC6A5|FFFF42|DEF3BD|00A5C6|DEBDDE';
  580. $this->assertEqual($expected, $this->GoogleStaticChartEngine->bar($chart));
  581. $chart = array(
  582. 'data' => array(explode(',', '40,13,35'), explode(',', '60,23,45')),
  583. 'size' => array('width' => 200, 'height' => 125),
  584. 'config' => array('type' => 'horizontal'),
  585. 'color' => array('series' => array(array('000000'), explode('|', 'FF0000|00FF00|0000FF'))),
  586. 'extra' => array('return' => 'url')
  587. );
  588. $expected = 'http://7.chart.apis.google.com/chart?cht=bhs&chd=t:40,13,35|60,23,45&chs=200x125&chco=000000,FF0000|00FF00|0000FF';
  589. $this->assertEqual($expected, $this->GoogleStaticChartEngine->bar($chart));
  590. $chart = array(
  591. 'data' => array(explode(',', '40,13,35'), explode(',', '60,23,45')),
  592. 'size' => array('width' => 200, 'height' => 125),
  593. 'config' => array('type' => 'horizontal'),
  594. 'color' => array('series' => array(explode('|', '0000FF|FF0000|00FF00'), explode('|', 'FF0000|00FF00|0000FF'))),
  595. 'extra' => array('return' => 'url')
  596. );
  597. $expected = 'http://8.chart.apis.google.com/chart?cht=bhs&chd=t:40,13,35|60,23,45&chs=200x125&chco=0000FF|FF0000|00FF00,FF0000|00FF00|0000FF';
  598. $this->assertEqual($expected, $this->GoogleStaticChartEngine->bar($chart));
  599. }
  600. /**
  601. * @brief test bar chart labels
  602. */
  603. public function testBuildBarChartsLables(){
  604. $chart = array(
  605. 'data' => array(12, 23, 10, 40, 69),
  606. 'size' => array('width' => 200, 'height' => 125),
  607. 'config' => array('type' => 'vertical'),
  608. 'extra' => array('return' => 'url'),
  609. 'labels' => array(
  610. 'x' => range(0, 4),
  611. 'y' => range(0,100, 20)
  612. )
  613. );
  614. $expected = 'http://0.chart.apis.google.com/chart?cht=bvs&chd=t:12,23,10,40,69&chs=200x125&chxl=0:|0|1|2|3|4|1:|0|20|40|60|80|100&chxt=x,y';
  615. $this->assertEqual($expected, $this->GoogleStaticChartEngine->bar($chart));
  616. $chart = array(
  617. 'data' => array(12, 23, 10, 40, 69),
  618. 'size' => array('width' => 200, 'height' => 125),
  619. 'config' => array('type' => 'vertical'),
  620. 'extra' => array('return' => 'url'),
  621. 'labels' => array(
  622. 'x' => range('a', 'e'),
  623. 'y' => range(0,100, 20)
  624. )
  625. );
  626. $expected = 'http://1.chart.apis.google.com/chart?cht=bvs&chd=t:12,23,10,40,69&chs=200x125&chxl=0:|a|b|c|d|e|1:|0|20|40|60|80|100&chxt=x,y';
  627. $this->assertEqual($expected, $this->GoogleStaticChartEngine->bar($chart));
  628. $chart = array(
  629. 'data' => array(12, 23, -50, 40, -15),
  630. 'size' => array('width' => 200, 'height' => 125),
  631. 'config' => array('type' => 'vertical'),
  632. 'extra' => array('return' => 'url'),
  633. 'labels' => array(
  634. 'x' => range('a', 'e'),
  635. 'y' => range(-30,80, 20)
  636. ),
  637. 'scale' => array(-50, 80)
  638. );
  639. $expected = 'http://2.chart.apis.google.com/chart?cht=bvs&chd=t:12,23,-50,40,-15&chs=200x125&chxl=0:|a|b|c|d|e|1:|-30|-10|10|30|50|70&chxt=x,y&chds=-50,80';
  640. $this->assertEqual($expected, $this->GoogleStaticChartEngine->bar($chart));
  641. }
  642. /**
  643. * @brief test line charts
  644. */
  645. public function testBuildLineCharts(){
  646. $chart = array(
  647. 'data' => explode(',', '40,60,60,45,47,75,70,72'),
  648. 'size' => array('width' => 200, 'height' => 125),
  649. 'extra' => array('return' => 'url'),
  650. );
  651. $expected = 'http://0.chart.apis.google.com/chart?cht=lc&chd=t:40,60,60,45,47,75,70,72&chs=200x125';
  652. $this->assertEqual($expected, $this->GoogleStaticChartEngine->line($chart));
  653. $chart = array(
  654. 'data' => explode(',', '27,25,60,31,25,39,25,31,26,28,80,28,27,31,27,29,26,35,70,25'),
  655. 'size' => array('width' => 200, 'height' => 125),
  656. 'color' => array('series' => '0077CC'),
  657. 'config' => array('type' => 'spark'),
  658. 'extra' => array('return' => 'url'),
  659. );
  660. $expected = 'http://1.chart.apis.google.com/chart?cht=ls&chd=t:27,25,60,31,25,39,25,31,26,28,80,28,27,31,27,29,26,35,70,25&chs=200x125&chco=0077CC';
  661. $this->assertEqual($expected, $this->GoogleStaticChartEngine->line($chart));
  662. $chart = array(
  663. 'data' => array(explode(',', '10,20,40,80,90,95,99'), explode(',', '20,30,40,50,60,70,80'), array(-1), explode(',', '5,10,22,35,85')),
  664. 'size' => array('width' => 200, 'height' => 125),
  665. 'color' => array('series' => '0077CC'),
  666. 'config' => array('type' => 'xy'),
  667. 'color' => array('series' => explode(',', '3072F3,ff0000,00aaaa')),
  668. 'legend' => array(
  669. 'labels' => array('Ponies', 'Unicorns'),
  670. 'position' => 'top'
  671. ),
  672. 'extra' => array(
  673. 'return' => 'url',
  674. 'line_style' => array(array('thickness' => 2, 'dash' => array(4, 1)))
  675. ),
  676. );
  677. $expected = 'http://2.chart.apis.google.com/chart?cht=lxy&chd=t:10,20,40,80,90,95,99|20,30,40,50,60,70,80|-1|5,10,22,35,85&chs=200x125&chco=3072F3,ff0000,00aaaa&chdl=Ponies|Unicorns&chdlp=t|l';
  678. $this->assertEqual($expected, $this->GoogleStaticChartEngine->line($chart));
  679. }
  680. }