Skip to content

Commit

Permalink
Add unit test making sure the ArraySerializable trait is working well.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bob Kruithof committed Sep 2, 2018
1 parent 44d1b87 commit ac39cc7
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 146 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
/composer.phar
composer.phar
/vendor/
codeCoverage
build
29 changes: 14 additions & 15 deletions src/Delegate/ArraySerializable.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

/**
* Class ArraySerializable
*
* @package Halfpastfour\PHPChartJS\Model
*/
trait ArraySerializable
Expand All @@ -15,6 +16,7 @@ trait ArraySerializable
* Will loop through class properties and try and assign their values to an array of data that will be returned.
*
* @return array
* @throws \ReflectionException
*/
public function getArrayCopy()
{
Expand All @@ -29,39 +31,33 @@ public function getArrayCopy()

// Only process properties that aren't null
if (! is_null($value)) {
// Gather phpdoc from property
$phpDoc = $reflectionClass->getProperty($property)->getDocComment();
$type = $phpDoc->getTag('var')->getDescription();
$object = false;
$object = (is_object($value) && ! $this->$property instanceof Expr);

// Prepend 'get' to the getter method.
$getter = 'get' . ucfirst($property);
if (is_object($value) && ! $this->$property instanceof Expr) {
$object = true;
if (! method_exists($this, $getter)) {
// If 'getSomething' doesn't exist, try to use 'isSomething'
$getter = 'is' . ucfirst($property);
}

// Determine whether the getter method is equal to the property name or is prepended by 'is' or 'get'
if (strcmp($type, 'bool') === 0 || strcmp($type, 'boolean') === 0) {
// Prepend 'is' to the getter method
$getter = 'is' . ucfirst($property);
} elseif (method_exists($this, $property) && is_object($value)) {
// The getter method is equal to the property name and the value is an actual object
// Finally simply use the property
if (! method_exists($this, $getter)) {
$getter = $property;
$object = true;
}

// Abort if the method does not exist
// Abort if none of the above methods exit
if (! method_exists($this, $getter)) {
continue;
}

// Assign the contents of the property to the data array
$data[ $property ] = $object ? $this->$getter()->getArrayCopy() : $this->$getter();
$data[$property] = $object ? $this->$getter()->getArrayCopy() : $this->$getter();
}
}

return $data;
}
<<<<<<< HEAD
}
=======
/**
Expand Down Expand Up @@ -105,3 +101,6 @@ public function getArrayCopy()
}
}
>>>>>>> 6d743af... Simplified getArrayCopy method
=======
}
>>>>>>> e69d8b3... Add unit test making sure the ArraySerializable trait is working well.
131 changes: 0 additions & 131 deletions test/unit/Delegate/ArraySerializableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
*/
class ArraySerializableTest extends \PHPUnit_Framework_TestCase
{
<<<<<<< HEAD
/**
* @var A
*/
Expand Down Expand Up @@ -40,46 +39,6 @@ public function testSuperclass()
$result = $this->classA->getArrayCopy();
ksort($result);
self::assertSame($expected, $result);
=======
/**
* @var A
*/
private $classA;

/**
* @var B
*/
private $classB;

/**
*
*/
public function setUp()
{
$this->classA = new A( 1, 2 );
$this->classB = new B( 3, 4, 5, 6 );
}

/**
*
*/
public function testSuperclass()
{
$expected = [ 'a' => 1, 'b' => 2 ];
ksort( $expected );
$result = $this->classA->getArrayCopy();
ksort( $result );
self::assertSame( $expected, $result );
}

public function testBoolean()
{
$expected = [ 'a' => true, 'b' => 2 ];
$class = new A(true, 2);
ksort( $expected );
$result = $class->getArrayCopy();
ksort( $result );
>>>>>>> 6d743af... Simplified getArrayCopy method
}
}

Expand All @@ -89,7 +48,6 @@ public function testBoolean()
*/
class A
{
<<<<<<< HEAD
use ArraySerializable;

/**
Expand Down Expand Up @@ -177,95 +135,6 @@ public function getY()
{
return $this->y;
}
=======
use ArraySerializable;

/**
* @var bool|int
*/
protected $a;

/**
* @var int
*/
protected $b;

/**
*
* should not show (private)
* @var int
*/
private $x;

/**
* should not show (private)
* @var int
*/
private $y;

/**
* A constructor.
*
* @param $a int
* @param $b int
*/
public function __construct( $a, $b )
{
$this->a = $a;
$this->b = $b;
}

/**
* @return int
*/
public function isA()
{
return $this->a;
}

/**
* @param int $a
*/
public function setA( $a )
{
$this->a = $a;
}

/**
* @return int
*/
public function getB()
{
return $this->b;
}

/**
* @param int $b
*/
public function setB( $b )
{
$this->b = $b;
}

/**
* @return int
*/
public function getX()
{
return $this->x;
}

/**
* this method should never be called by jsonSerialize because it is not
* a boolean.
*
* @return int
*/
public function getY()
{
return $this->y;
}
>>>>>>> 6d743af... Simplified getArrayCopy method
}

/**
Expand Down

0 comments on commit ac39cc7

Please sign in to comment.