Skip to content

Commit

Permalink
Update unit tests to expect the arrays being returned by the jsonSeri…
Browse files Browse the repository at this point in the history
…alize method.
  • Loading branch information
halfpastfouram committed Jan 9, 2020
1 parent a9694fb commit b73938e
Show file tree
Hide file tree
Showing 26 changed files with 246 additions and 175 deletions.
6 changes: 4 additions & 2 deletions test/unit/Collection/DataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
namespace Collection;

use Halfpastfour\PHPChartJS\Collection\Data;
use PHPUnit_Framework_TestCase;

/**
* Class DataTest
*
* @package Collection
*/
class DataTest extends \PHPUnit_Framework_TestCase
class DataTest extends PHPUnit_Framework_TestCase
{
/**
* @var Data
Expand All @@ -28,7 +30,7 @@ public function setUp()
*/
public function testJsonSerializeEmpty()
{
$expected = "[]";
$expected = [];
$result = $this->data->jsonSerialize();
self::assertSame($expected, $result);
}
Expand Down
32 changes: 27 additions & 5 deletions test/unit/DataSetCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,39 @@

namespace Halfpastfour\PHPChartJS;

class DataSetCollectionTest extends \PHPUnit_Framework_TestCase
use PHPUnit_Framework_TestCase;

/**
* Class DataSetCollectionTest
*
* @package Halfpastfour\PHPChartJS
*/
class DataSetCollectionTest extends PHPUnit_Framework_TestCase
{
/** @var DataSetCollection */
private $dataSetCollection;

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

/**
*
*/
public function testGetArrayCopyEmpty()
{
$expected = [];
$result = $this->dataSetCollection->getArrayCopy();
$result = $this->dataSetCollection->getArrayCopy();
self::assertSame($expected, $result);
}

/**
*
*/
public function testGetArrayCopyNonEmpty()
{
$expected = [[]];
Expand All @@ -27,16 +43,22 @@ public function testGetArrayCopyNonEmpty()
self::assertSame($expected, $result);
}

/**
*
*/
public function testJsonSerializeEmpty()
{
$expected = '[]';
$result = $this->dataSetCollection->jsonSerialize();
$expected = [];
$result = $this->dataSetCollection->jsonSerialize();
self::assertSame($expected, $result);
}

/**
*
*/
public function testJsonSerializeNonEmpty()
{
$expected = '[[]]';
$expected = [[]];
$this->dataSetCollection->append(new DataSet());
$result = $this->dataSetCollection->jsonSerialize();
self::assertSame($expected, $result);
Expand Down
35 changes: 19 additions & 16 deletions test/unit/DataSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@
use Halfpastfour\PHPChartJS\ChartOwnedInterface;
use Halfpastfour\PHPChartJS\Collection\Data;
use Halfpastfour\PHPChartJS\DataSet;
use JsonSerializable;
use PHPUnit_Framework_TestCase;

/**
* Class DataSetTest
*
* @package Test
*/
class DataSetTest extends \PHPUnit_Framework_TestCase
class DataSetTest extends PHPUnit_Framework_TestCase
{
/**
*
Expand All @@ -28,7 +31,7 @@ public function testImplementation()
$dataSet,
'Class implements ArraySerializableInterface'
);
$this->assertInstanceOf(\JsonSerializable::class, $dataSet, 'Class implements JsonSerializable');
$this->assertInstanceOf(JsonSerializable::class, $dataSet, 'Class implements JsonSerializable');
}

/**
Expand Down Expand Up @@ -75,7 +78,7 @@ public function testData()
$this->assertInstanceOf(Data::class, $dataCollection, 'The data collection is the right class');
$this->assertInstanceOf(ArrayAccess::class, $dataCollection, 'The data collection extends Collection');
$this->assertInstanceOf(
\JsonSerializable::class,
JsonSerializable::class,
$dataCollection,
'The data collection implements JsonSerializable'
);
Expand Down Expand Up @@ -106,7 +109,7 @@ public function testBackgroundColor()
$dataSet->setBackgroundColor('#fff');
$this->assertEquals('#fff', $dataSet->getBackgroundColor());

$backgroundColorArray = [ '#fff', 'rgb( 255, 255, 255 )', 'rgba( 255, 255, 255, .5 )', 'white' ];
$backgroundColorArray = ['#fff', 'rgb( 255, 255, 255 )', 'rgba( 255, 255, 255, .5 )', 'white'];
$dataSet->setBackgroundColor($backgroundColorArray);
$this->assertEquals(
$backgroundColorArray,
Expand All @@ -127,7 +130,7 @@ public function testBorderColor()
$dataSet->setBorderColor('#fff');
$this->assertEquals('#fff', $dataSet->getBorderColor(), 'The border color is set and returned correctly');

$borderColorArray = [ '#fff', 'rgb( 255, 255, 255 )', 'rgba( 255, 255, 255, .5 )', 'white' ];
$borderColorArray = ['#fff', 'rgb( 255, 255, 255 )', 'rgba( 255, 255, 255, .5 )', 'white'];
$dataSet->setBorderColor($borderColorArray);
$this->assertEquals(
$borderColorArray,
Expand Down Expand Up @@ -168,9 +171,9 @@ public function testBorderWidth()
$this->assertTrue(is_int($dataSet->getBorderWidth()), 'Return type should be int');
$this->assertEquals(0, $dataSet->getBorderWidth(), 'The border width should equal int 0');

$dataSet->setBorderWidth([ 10, '20', '30abc', 40.00, 'abc50' ]);
$dataSet->setBorderWidth([10, '20', '30abc', 40.00, 'abc50']);
$this->assertTrue(is_array($dataSet->getBorderWidth()), 'Return type should be array');
$this->assertEquals([ 10, 20, 30, 40, 0 ], $dataSet->getBorderWidth(), 'Return value should be array of int');
$this->assertEquals([10, 20, 30, 40, 0], $dataSet->getBorderWidth(), 'Return value should be array of int');
}

/**
Expand Down Expand Up @@ -201,10 +204,10 @@ public function testAxes()

$this->assertInstanceOf(DataSet::class, $dataSet->setXAxisID('myXAxis'));
$this->assertEquals('myXAxis', $dataSet->getXAxisID(), 'The correct value is returned');
$this->assertArraySubset([ 'xAxisID' => 'myXAxis' ], $dataSet->getArrayCopy());
$this->assertArraySubset(['xAxisID' => 'myXAxis'], $dataSet->getArrayCopy());
$this->assertArraySubset(
[ 'xAxisID' => 'myXAxis' ],
json_decode($dataSet->jsonSerialize(), true),
['xAxisID' => 'myXAxis'],
$dataSet->jsonSerialize(),
'Serialized data is not correct'
);

Expand All @@ -217,7 +220,7 @@ public function testAxes()
'xAxisID' => 'myXAxis',
'yAxisID' => 'myYAxis',
],
json_decode($dataSet->jsonSerialize(), true),
$dataSet->jsonSerialize(),
'The serialized data is not correct'
);
}
Expand All @@ -234,7 +237,7 @@ public function testHoverBackgroundColor()
$this->assertInstanceOf(DataSet::class, $dataSet->setHoverBackgroundColor('#fff'));
$this->assertEquals('#fff', $dataSet->getHoverBackgroundColor(), 'The correct value is returned');

$newColors = [ 'silver', '#fff', 'rgb( 0, 0, 0 )', 'rgba( 255, 255, 255, .5 )' ];
$newColors = ['silver', '#fff', 'rgb( 0, 0, 0 )', 'rgba( 255, 255, 255, .5 )'];
$dataSet->setHoverBackgroundColor($newColors);
$this->assertEquals($newColors, $dataSet->getHoverBackgroundColor(), 'The correct value is returned');
}
Expand All @@ -251,9 +254,9 @@ public function testHoverBorderColor()
$this->assertInstanceOf(DataSet::class, $dataSet->setHoverBorderColor('#fff'));
$this->assertEquals('#fff', $dataSet->getHoverBorderColor(), 'The correct value is returned');

$dataSet->setHoverBorderColor([ 'silver', '#fff', 'rgb( 0, 0, 0 )', 'rgba( 255, 255, 255, .5 )', 0 ]);
$dataSet->setHoverBorderColor(['silver', '#fff', 'rgb( 0, 0, 0 )', 'rgba( 255, 255, 255, .5 )', 0]);
$this->assertEquals(
[ 'silver', '#fff', 'rgb( 0, 0, 0 )', 'rgba( 255, 255, 255, .5 )', '0' ],
['silver', '#fff', 'rgb( 0, 0, 0 )', 'rgba( 255, 255, 255, .5 )', '0'],
$dataSet->getHoverBorderColor(),
'The correct value is returned'
);
Expand All @@ -271,7 +274,7 @@ public function testHoverBorderWidth()
$this->assertInstanceOf(DataSet::class, $dataSet->setHoverBorderWidth(1));
$this->assertEquals(1, $dataSet->getHoverBorderWidth(), 'The correct value is returned');

$dataSet->setHoverBorderWidth([ 1, 10, '5a', 0 ]);
$this->assertEquals([ 1, 10, 5, 0 ], $dataSet->getHoverBorderWidth(), 'The correct value is returned');
$dataSet->setHoverBorderWidth([1, 10, '5a', 0]);
$this->assertEquals([1, 10, 5, 0], $dataSet->getHoverBorderWidth(), 'The correct value is returned');
}
}
10 changes: 6 additions & 4 deletions test/unit/LabelsCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
namespace Test;

use Halfpastfour\PHPChartJS\LabelsCollection;
use PHPUnit_Framework_TestCase;

/**
* Class LabelsCollectionTest
*
* @package Test
*/
class LabelsCollectionTest extends \PHPUnit_Framework_TestCase
class LabelsCollectionTest extends PHPUnit_Framework_TestCase
{
/**
* @var LabelsCollection
Expand All @@ -23,7 +25,7 @@ class LabelsCollectionTest extends \PHPUnit_Framework_TestCase
/**
* @var array
*/
private $labelsArray = [ 'startingLabel', 'label1', 'label2', 'endLabel' ];
private $labelsArray = ['startingLabel', 'label1', 'label2', 'endLabel'];

/**
*
Expand All @@ -39,7 +41,7 @@ public function setUp()
*/
public function testJsonSerializeEmpty()
{
$expected = '[]';
$expected = [];
$result = $this->labelsCollectionEmpty->jsonSerialize();
self::assertSame($expected, $result);
}
Expand All @@ -49,7 +51,7 @@ public function testJsonSerializeEmpty()
*/
public function testJsonSerialize()
{
$expected = "[\"" . implode("\",\"", $this->labelsArray) . "\"]";
$expected = $this->labelsArray;
$result = $this->labelsCollection->jsonSerialize();
self::assertSame($expected, $result);
}
Expand Down
10 changes: 6 additions & 4 deletions test/unit/Options/ClickTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
namespace Test\Options;

use Halfpastfour\PHPChartJS\Options;
use PHPUnit_Framework_TestCase;
use Test\TestUtils;
use Zend\Json\Expr;

/**
* Class ClickTest
*
* @package Test\Options
*/
class ClickTest extends \PHPUnit_Framework_TestCase
class ClickTest extends PHPUnit_Framework_TestCase
{
/**
* @var Options
Expand All @@ -21,15 +23,15 @@ class ClickTest extends \PHPUnit_Framework_TestCase
'onClick' => null,
];

private $input_data_no_expressions = ['onClick' => null];
private $input_data_no_expressions = ['onClick' => null];
private $input_data_with_expressions = null;

/**
*
*/
public function setUp()
{
$this->options = new Options();
$this->options = new Options();
$this->input_data_with_expressions = ['onClick' => new Expr('function(event, array) { echo "onClick"; }')];
}

Expand Down Expand Up @@ -62,7 +64,7 @@ public function testJsonSerializeWithoutExpressions()
{
$expected = TestUtils::removeNullsFromArray($this->input_data_no_expressions);
TestUtils::setAttributes($this->options, $this->input_data_no_expressions);
$result = json_decode($this->options->jsonSerialize(), true);
$result = $this->options->jsonSerialize();
self::assertSame($expected, $result);
}
}
6 changes: 4 additions & 2 deletions test/unit/Options/Elements/ArcTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
namespace Test\Options\Elements;

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

/**
* Class ArcTest
*
* @package Test\Options\Elements
*/
class ArcTest extends \PHPUnit_Framework_TestCase
class ArcTest extends PHPUnit_Framework_TestCase
{
/**
* @var Arc
Expand Down Expand Up @@ -79,7 +81,7 @@ public function testJsonSerialize()
{
$expected = TestUtils::removeNullsFromArray($this->input_data);
TestUtils::setAttributes($this->arc, $this->input_data);
$result = json_decode($this->arc->jsonSerialize(), true);
$result = $this->arc->jsonSerialize();
self::assertSame($expected, $result);
}
}
Loading

0 comments on commit b73938e

Please sign in to comment.