Exemplo n.º 1
0
    /**
     * @covers \Altamira\ChartRenderer::__construct
     * @covers \Altamira\ChartRenderer::getInstance
     * @covers \Altamira\ChartRenderer::pushRenderer
     * @covers \Altamira\ChartRenderer::render
     * @covers \Altamira\ChartRenderer::reset
     * @covers \Altamira\ChartRenderer::unshiftRenderer
     */
    public function testChartRenderer()
    {
        $method = new ReflectionMethod('\\Altamira\\ChartRenderer', '__construct');
        $this->assertTrue($method->isProtected(), '\\Altamira\\ChartRenderer should implement singleton design pattern');
        $styleOptions = array('float' => 'left', 'border' => '1px solid #cccccc');
        $mockChart = $this->getMock('\\Altamira\\Chart', array('getLibrary', 'getName', 'getTitle'), array('Mock Chart'));
        $mockChart->expects($this->any())->method('getLibrary')->will($this->returnValue('flot'));
        $mockChart->expects($this->any())->method('getName')->will($this->returnValue('foo'));
        $mockChart->expects($this->any())->method('getTitle')->will($this->returnValue('foo'));
        $expectedStyle = 'float: left; border: 1px solid #cccccc; ';
        $defaultOutput = \Altamira\ChartRenderer::render($mockChart, $styleOptions);
        $expectedOutput = <<<ENDOUTPUT
<div class="flot" id="foo" style="{$expectedStyle}"></div>
ENDOUTPUT;
        $this->assertEquals($expectedOutput, $defaultOutput, '\\Altamira\\ChartRenderer::render() should render the chart using only \\Altamira\\ChartRenderer\\DefaultRenderer by default');
        $getInstanceMethod = new ReflectionMethod('\\Altamira\\ChartRenderer', 'getInstance');
        $getInstanceMethod->setAccessible(true);
        $instance = $getInstanceMethod->invoke(null);
        $this->assertEquals($instance, \Altamira\ChartRenderer::pushRenderer('\\Altamira\\ChartRenderer\\TitleRenderer'), '\\Altamira\\ChartRenderer::pushRenderer should provide a fluent interface');
        $exception = null;
        try {
            \Altamira\ChartRenderer::pushRenderer('\\Altamira\\Chart');
        } catch (Exception $exception) {
        }
        $this->assertInstanceOf('UnexpectedValueException', $exception);
        $exception = null;
        try {
            \Altamira\ChartRenderer::unshiftRenderer('\\Altamira\\Chart');
        } catch (Exception $exception) {
        }
        $this->assertInstanceOf('UnexpectedValueException', $exception);
        $rendererChain = new ReflectionProperty('\\Altamira\\ChartRenderer', 'rendererChain');
        $rendererChain->setAccessible('true');
        $this->assertEquals(array('\\Altamira\\ChartRenderer\\DefaultRenderer', '\\Altamira\\ChartRenderer\\TitleRenderer'), $rendererChain->getValue($instance), '\\Altamira\\ChartRenderer::pushRenderer should add the renderer to the end of the renderer chain');
        $fullExpectedOutput = <<<ENDOUTPUT
<div class="altamira-chart-title">
    <h3>foo</h3>
<div class="flot" id="foo" style="{$expectedStyle}"></div></div>
ENDOUTPUT;
        $this->assertEquals($fullExpectedOutput, \Altamira\ChartRenderer::render($mockChart, $styleOptions), '\\Alamira\\ChartRenderer::render() should take a nested approach to parsing, ' . 'prerendering from the top of the stack to the bottom, and postrendering from the bottom to the top.');
        \Altamira\ChartRenderer::reset();
        $this->assertEmpty($rendererChain->getValue($instance), '\\Altamira\\ChartRenderer::reset() should empty the renderer chain');
        \Altamira\ChartRenderer::unshiftRenderer('\\Altamira\\ChartRenderer\\DefaultRenderer');
        \Altamira\ChartRenderer::unshiftRenderer('\\Altamira\\ChartRenderer\\TitleRenderer');
        $this->assertEquals(array('\\Altamira\\ChartRenderer\\TitleRenderer', '\\Altamira\\ChartRenderer\\DefaultRenderer'), $rendererChain->getValue($instance), '\\Altamira\\ChartRenderer::pushRenderer should add the renderer to the beginning of the renderer chain');
        $method = new ReflectionMethod('\\Altamira\\ChartRenderer', 'getInstance');
        $method->setAccessible(true);
        $property = new ReflectionProperty('\\Altamira\\ChartRenderer', 'instance');
        $mockChartRenderer = $this->getMockBuilder('\\Altamira\\ChartRenderer')->disableOriginalConstructor()->getMock();
        $property->setAccessible(true);
        $property->setValue(null);
        $this->assertInstanceOf('\\Altamira\\ChartRenderer', $method->invoke($mockChartRenderer));
        $property = new ReflectionProperty('\\Altamira\\ChartRenderer', 'rendererChain');
        $property->setAccessible(true);
        $property->setValue(array());
        \Altamira\ChartRenderer::render($mockChart);
        $this->assertNotEmpty($property->getValue(), '\\Altamira\\ChartRenderer should push a default renderer by default if it has no renderers set');
    }
Exemplo n.º 2
0
 /**
  * @covers \Altamira\Chart::__construct
  * @covers \Altamira\Chart::getJsWriter
  * @covers \Altamira\Chart::getName
  * @covers \Altamira\Chart::getTitle
  * @covers \Altamira\Chart::setTitle
  * @covers \Altamira\Chart::useHighlighting
  * @covers \Altamira\Chart::useZooming
  * @covers \Altamira\Chart::useCursor
  * @covers \Altamira\Chart::useDates
  * @covers \Altamira\Chart::setAxisTicks
  * @covers \Altamira\Chart::setAxisOptions
  * @covers \Altamira\Chart::setSeriesColors
  * @covers \Altamira\Chart::setAxisLabel
  * @covers \Altamira\Chart::setType
  * @covers \Altamira\Chart::setTypeOption
  * @covers \Altamira\Chart::setLegend
  * @covers \Altamira\Chart::setGrid
  * @covers \Altamira\Chart::getFiles
  * @covers \Altamira\Chart::getScript
  * @covers \Altamira\Chart::getJsWriter
  * @covers \Altamira\Chart::getLibrary
  * @covers \Altamira\Chart::getSeries
  * @covers \Altamira\Chart::addSeries
  * @covers \Altamira\Chart::addSingleSeries
  * @covers \Altamira\Chart::createManySeries
  * @covers \Altamira\Chart::createSeries
  * @covers \Altamira\Chart::getDiv
  */
 public function testChart()
 {
     $exception = false;
     try {
         $chart = new \Altamira\Chart('');
     } catch (Exception $exception) {
     }
     $this->assertInstanceOf('Exception', $exception, '\\Altamira\\Chart should throw an exception if it passed an empty name');
     $jqplotChart = new \Altamira\Chart('chart 1');
     $flotChart = new \Altamira\Chart('chart2', \Altamira\JsWriter\Flot::LIBRARY);
     $libraryException = false;
     try {
         $crapChart = new \Altamira\Chart('chart3', 'notareallibrary');
     } catch (Exception $libraryException) {
     }
     $this->assertInstanceOf('Exception', $libraryException, 'A chart should throw an exception if we don\'t support the library.');
     $this->assertInstanceOf('\\Altamira\\JsWriter\\JqPlot', $jqplotChart->getJsWriter(), 'Charts should register a JqPlot JsWriter by default');
     $writermethods = array('useHighlighting', 'useZooming', 'useCursor', 'useDates', 'setAxisTicks', 'setAxisOptions', 'setOption', 'getOption', 'setType', 'setTypeOption', 'setLegend', 'setGrid', 'getType', 'getFiles', 'getScript', 'getLibrary');
     $mockJqPlotWriter = $this->getMock('\\Altamira\\JsWriter\\JqPlot', $writermethods, array($jqplotChart));
     $mockFlotWriter = $this->getMock('\\Altamira\\JsWriter\\Flot', $writermethods, array($flotChart));
     $jsWriterReflection = new ReflectionProperty('\\Altamira\\Chart', 'jsWriter');
     $jsWriterReflection->setAccessible(true);
     $jsWriterReflection->setValue($jqplotChart, $mockJqPlotWriter);
     $jsWriterReflection->setValue($flotChart, $mockFlotWriter);
     $this->assertEquals('chart_1', $jqplotChart->getName(), 'Name values should be normalized to turn whitespace into underscores');
     $flotChart->setTitle('This is a flot chart');
     $this->assertEquals('This is a flot chart', $flotChart->getTitle(), '\\Altamira\\Chart::getTitle should return title if set');
     $this->assertEquals('chart_1', $jqplotChart->getTitle(), '\\Altamira\\Chart::getTitle should return name if title not set');
     $mockJqPlotWriter->expects($this->once())->method('useHighlighting')->with(array('size' => 7.5));
     $this->assertEquals($jqplotChart, $jqplotChart->useHighlighting(), '\\Altamira\\Chart::useHighlighting should provide a fluent interface');
     $mockJqPlotWriter->expects($this->once())->method('useZooming');
     $this->assertEquals($jqplotChart, $jqplotChart->useZooming(), '\\Altamira\\Chart::useZooming should provide a fluent interface');
     $mockJqPlotWriter->expects($this->once())->method('useCursor');
     $this->assertEquals($jqplotChart, $jqplotChart->useCursor(), '\\Altamira\\Chart::useCursor should provide a fluent interface');
     $mockJqPlotWriter->expects($this->once())->method('useDates');
     $this->assertEquals($jqplotChart, $jqplotChart->useDates(), '\\Altamira\\Chart::useDates should provide a fluent interface');
     $mockJqPlotWriter->expects($this->once())->method('setAxisTicks')->with('x', array('one', 'two', 'three'));
     $this->assertEquals($jqplotChart, $jqplotChart->setAxisTicks('x', array('one', 'two', 'three')), '\\Altamira\\Chart::setAxisTicks should provide a fluent interface');
     $mockJqPlotWriter->expects($this->once())->method('setAxisOptions')->with('x', 'max', 10);
     $this->assertEquals($jqplotChart, $jqplotChart->setAxisOptions('x', 'max', 10), '\\Altamira\\Chart::setAxisOptions should provide a fluent interface');
     $mockJqPlotWriter->expects($this->at(0))->method('setOption')->with('seriesColors', array('#333333', '#666666'));
     $this->assertEquals($jqplotChart, $jqplotChart->setSeriesColors(array('#333333', '#666666')), '\\Altamira\\Chart::setSeriesColors should provide a fluent interface');
     $mockAxisOptions = array('xaxis' => array('min' => 0, 'max' => 10), 'yaxis' => array('min' => 0, 'max' => 10));
     $mockJqPlotWriter->expects($this->at(0))->method('getOption')->with('axes', array())->will($this->returnValue($mockAxisOptions));
     $mockAxisOptions['xaxis']['label'] = 'x';
     $mockJqPlotWriter->expects($this->at(1))->method('setOption')->with('axes', $mockAxisOptions);
     $this->assertEquals($jqplotChart, $jqplotChart->setAxisLabel('x', 'x'), '\\Altamira\\Chart::setAxisLabel should provide a fluent interface');
     $mockJqPlotWriter->expects($this->once())->method('setType')->with('Donut');
     $this->assertEquals($jqplotChart, $jqplotChart->setType('Donut'), '\\Altamira\\Chart::setType should provide a fluent interface');
     $mockJqPlotWriter->expects($this->once())->method('setTypeOption')->with('hole', '50px', null);
     $this->assertEquals($jqplotChart, $jqplotChart->setTypeOption('hole', '50px'), '\\Altamira\\Chart::setTypeOption should provide a fluent interface');
     $opts = array('on' => 'true', 'location' => 'ne', 'x' => 0, 'y' => 0);
     $mockJqPlotWriter->expects($this->once())->method('setLegend')->with($opts);
     $this->assertEquals($jqplotChart, $jqplotChart->setLegend(), '\\Altamira\\Chart::setLegend should provide a fluent interface');
     $mockJqPlotWriter->expects($this->once())->method('setGrid')->with(array('on' => true));
     $this->assertEquals($jqplotChart, $jqplotChart->setGrid(), '\\Altamira\\Chart::setGrid should provide a fluent interface');
     $mockJqPlotWriter->expects($this->once())->method('getFiles');
     $jqplotChart->getFiles();
     $mockJqPlotWriter->expects($this->once())->method('getScript');
     $jqplotChart->getScript();
     $seriesData = \Altamira\ChartDatum\TwoDimensionalPointFactory::getFromXValues(array(1, 2, 3));
     $series = $jqplotChart->createSeries($seriesData, 'seriesa', 'Donut');
     $this->assertInstanceOf('\\Altamira\\Series', $series, '\\Altamira\\Chart::createSeries should return an instance of \\Altamira\\Series');
     $this->assertEquals('seriesa', $series->getTitle(), '\\Altamira\\Chart::createSeries should set the series title');
     $this->assertEquals($seriesData, $series->getData(), '\\Altamira\\Chart::createSeries should set the series data');
     $jqplotChart->addSeries($series);
     $seriesArray = $jqplotChart->getSeries();
     $this->assertEquals(array($series->getTitle() => $series), $seriesArray);
     $this->assertArrayHasKey($series->getTitle(), $seriesArray);
     $this->assertEquals($jqplotChart->getLibrary(), $mockJqPlotWriter->getLibrary());
     $styleOptions = array('width' => '100px', 'height' => '200px');
     $this->assertEquals(\Altamira\ChartRenderer::render($jqplotChart, $styleOptions), $jqplotChart->getDiv(100, 200));
 }