Skip to content

Commit

Permalink
added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joerg-rauscher committed Feb 15, 2019
1 parent 3de5469 commit a37c739
Show file tree
Hide file tree
Showing 9 changed files with 546 additions and 12 deletions.
18 changes: 18 additions & 0 deletions src/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Halfpastfour\PHPChartJS\Delegate\ArraySerializable;
use Halfpastfour\PHPChartJS\Options\Animation;
use Halfpastfour\PHPChartJS\Options\Elements;
use Halfpastfour\PHPChartJS\Options\Hover;
use Halfpastfour\PHPChartJS\Options\Layout;
use Halfpastfour\PHPChartJS\Options\Legend;
Expand Down Expand Up @@ -31,6 +32,11 @@ class Options implements ChartOwnedInterface, ArraySerializableInterface, \JsonS
*/
protected $title;

/**
* @var Elements
*/
protected $elements;

/**
* @var Hover
*/
Expand Down Expand Up @@ -68,6 +74,18 @@ public function getLayout()
return $this->layout;
}

/**
* @return Elements
*/
public function getElements()
{
if (is_null($this->elements)) {
$this->elements = new Elements();
}

return $this->elements;
}

/**
* @return Title
*/
Expand Down
12 changes: 6 additions & 6 deletions src/Options/Elements/Line.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Line implements ArraySerializableInterface, \JsonSerializable
* @default 'rgba(0,0,0,0.1)'
* @var string
*/
private $backGroundColor;
private $backgroundColor;

/**
* Line stroke width.
Expand Down Expand Up @@ -129,18 +129,18 @@ public function setTension($tension)
/**
* @return string
*/
public function getBackGroundColor()
public function getBackgroundColor()
{
return $this->backGroundColor;
return $this->backgroundColor;
}

/**
* @param string $backGroundColor
* @param string $backgroundColor
* @return Line
*/
public function setBackGroundColor($backGroundColor)
public function setBackgroundColor($backgroundColor)
{
$this->backGroundColor = is_null($backGroundColor) ? null : strval($backGroundColor);
$this->backgroundColor = is_null($backgroundColor) ? null : strval($backgroundColor);
return $this;
}

Expand Down
12 changes: 6 additions & 6 deletions src/Options/Elements/Point.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class Point implements ArraySerializableInterface, \JsonSerializable
* @default 'rgba(0,0,0,0.1)'
* @var string
*/
private $backGroundColor;
private $backgroundColor;

/**
* Point stroke width.
Expand Down Expand Up @@ -142,18 +142,18 @@ public function setRotation($rotation)
/**
* @return string
*/
public function getBackGroundColor()
public function getBackgroundColor()
{
return $this->backGroundColor;
return $this->backgroundColor;
}

/**
* @param string $backGroundColor
* @param string $backgroundColor
* @return Point
*/
public function setBackGroundColor($backGroundColor)
public function setBackgroundColor($backgroundColor)
{
$this->backGroundColor = is_null($backGroundColor) ? null : strval($backGroundColor);
$this->backgroundColor = is_null($backgroundColor) ? null : strval($backgroundColor);
return $this;
}

Expand Down
85 changes: 85 additions & 0 deletions test/unit/Options/Elements/ArcTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace Test\Options\Elements;

use Halfpastfour\PHPChartJS\Options\Elements\Arc;
use Test\TestUtils;

/**
* Class ArcTest
* @package Test\Options\Elements
*/
class ArcTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Arc
*/
protected $arc;

/**
* @var array
*/
private $data_types = [
'backgroundColor' => '', /* string */
'borderColor' => '', /* string */
'borderWidth' => 1, /* int */
];

/**
* @var array
*/
private $empty_data = [
'backgroundColor' => null, /* string */
'borderColor' => null, /* string */
'borderWidth' => null, /* int */
];

/**
* @var array
*/
private $input_data = [
'backgroundColor' => 'backgroundColor', /* string */
'borderColor' => 'borderColor', /* string */
'borderWidth' => 2, /* int */
];

/**
*
*/
public function setUp()
{
$this->arc = new Arc();
}

/**
*
*/
public function testEmpty()
{
$expected = $this->empty_data;
$result = TestUtils::getAttributes($this->arc, $this->data_types);
self::assertSame($expected, $result);
}

/**
*
*/
public function testGetAndSetNoObjects()
{
$expected = $this->input_data;
TestUtils::setAttributes($this->arc, $this->input_data);
$result = TestUtils::getAttributes($this->arc, $this->data_types);
self::assertSame($expected, $result);
}

/**
*
*/
public function testJsonSerialize()
{
$expected = TestUtils::removeNullsFromArray($this->input_data);
TestUtils::setAttributes($this->arc, $this->input_data);
$result = json_decode($this->arc->jsonSerialize(), true);
self::assertSame($expected, $result);
}
}
109 changes: 109 additions & 0 deletions test/unit/Options/Elements/LineTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

namespace Test\Options\Elements;

use Halfpastfour\PHPChartJS\Options\Elements\Line;
use Test\TestUtils;

/**
* Class LineTest
* @package Test\Options\Elements
*/
class LineTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Line
*/
protected $line;

/**
* @var array
*/
private $data_types = [
'tension' => 1.0, /* float */
'backgroundColor' => '', /* string */
'borderWidth' => 1, /* int */
'borderColor' => '', /* string */
'borderCapStyle' => '', /* string */
'borderDash' => [1, 2], /* int[] */
'borderDashOffset' => 1.0, /* float */
'borderJoinStyle' => '', /* string */
'capBezierPoints' => false, /* bool */
'fill' => '', /* string */
'stepped' => false, /* bool */
];

/**
* @var array
*/
private $empty_data = [
'tension' => null, /* float */
'backgroundColor' => null, /* string */
'borderWidth' => null, /* int */
'borderColor' => null, /* string */
'borderCapStyle' => null, /* string */
'borderDash' => null, /* int[] */
'borderDashOffset' => null, /* float */
'borderJoinStyle' => null, /* string */
'capBezierPoints' => null, /* bool */
'fill' => null, /* bool */
'stepped' => null, /* bool */
];

/**
* @var array
*/
private $input_data = [
'tension' => 0.2, /* float */
'backgroundColor' => 'backgroundColor', /* string */
'borderWidth' => 2, /* int */
'borderColor' => 'borderColor', /* string */
'borderCapStyle' => 'borderCapStyle', /* string */
'borderDash' => [5, 6], /* int[] */
'borderDashOffset' => 0.1, /* float */
'borderJoinStyle' => 'borderJoinStyle', /* string */
'capBezierPoints' => true, /* bool */
'fill' => true, /* bool */
'stepped' => true, /* bool */
];

/**
*
*/
public function setUp()
{
$this->line = new Line();
}

/**
*
*/
public function testEmpty()
{
$expected = $this->empty_data;
$result = TestUtils::getAttributes($this->line, $this->data_types);
self::assertSame($expected, $result);
}

/**
*
*/
public function testGetAndSetNoObjects()
{
$expected = $this->input_data;
TestUtils::setAttributes($this->line, $this->input_data);
$result = TestUtils::getAttributes($this->line, $this->data_types);
self::assertSame($expected, $result);
}

/**
*
*/
public function testJsonSerialize()
{
$expected = TestUtils::removeNullsFromArray($this->input_data);
TestUtils::setAttributes($this->line, $this->input_data);
$result = json_decode($this->line->jsonSerialize(), true);
self::assertSame($expected, $result);
}
}
Loading

0 comments on commit a37c739

Please sign in to comment.