summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHannes Kindströmmer <hannes@kindstrommer.se>2017-03-09 16:54:46 +0100
committerHannes Kindströmmer <hannes@kindstrommer.se>2017-03-09 16:54:46 +0100
commit5504639d9d9c9b247fb05c627c85a212b96405e7 (patch)
treee8376c7b462802e495e13067d90a569d95fa75b5
parentc7c8e41696dcf14bf43578cf1b5a2e1c4080267e (diff)
downloadip1-php-sdk-5504639d9d9c9b247fb05c627c85a212b96405e7.zip
ip1-php-sdk-5504639d9d9c9b247fb05c627c85a212b96405e7.tar.gz
ip1-php-sdk-5504639d9d9c9b247fb05c627c85a212b96405e7.tar.bz2
Add documentation and tidy up exception messages
Added phpDoc comments (and some general ones). Fixed typo in function name. Added phpunit tests for the ClassValidationArray. Errors that was found in the tests has been corrected for in the ClassValidationArray class. Signed-off-by: Hannes Kindströmmer <hannes@kindstrommer.se>
-rw-r--r--phpunit.xml3
-rw-r--r--src/Core/ClassValidationArray.php34
-rw-r--r--tests/core/ClassValidationArrayTest.php111
3 files changed, 137 insertions, 11 deletions
diff --git a/phpunit.xml b/phpunit.xml
index 137b316..2c49f33 100644
--- a/phpunit.xml
+++ b/phpunit.xml
@@ -12,5 +12,8 @@
<testsuite name="general">
<directory>tests/general</directory>
</testsuite>
+ <testsuite name="core">
+ <directory>tests/core</directory>
+ </testsuite>
</testsuites>
</phpunit>
diff --git a/src/Core/ClassValidationArray.php b/src/Core/ClassValidationArray.php
index fabe687..1b342c0 100644
--- a/src/Core/ClassValidationArray.php
+++ b/src/Core/ClassValidationArray.php
@@ -16,6 +16,13 @@ class ClassValidationArray extends \ArrayObject implements \JsonSerializable
*/
private $class;
/**
+ * Takes the input and puts it in the contained array.
+ *
+ * If the input is an object it will add the object to the array and set the object's class to the only allowed class
+ * If the input is an array it will verify that all the objects inside the array are of the same class if not it will
+ * throw an InvalidArgumentException. If all the indexes are of the same class it will add them to the array.
+ *
+ * @param $input array|object If array it must only contain a single type of class.
* @throws InvalidArgumentException
*/
public function __construct($input = [])
@@ -23,12 +30,7 @@ class ClassValidationArray extends \ArrayObject implements \JsonSerializable
if (is_array($input)) {
foreach ($input as $key => $value) {
if (!is_object($value)) {
- throw new \InvalidArgumentException(
- get_class($value).
- " is not the same class as ".
- $this->class .
- " given in the first index of argument array."
- );
+ throw new \InvalidArgumentException("Non object found at index $key. Not allowed.");
}
if (!isset($this->class)) {
$this->class = get_class($value);
@@ -36,7 +38,12 @@ class ClassValidationArray extends \ArrayObject implements \JsonSerializable
if ($this->class === get_class($value)) {
parent::offsetSet(null, $value);
} else {
- throw new \InvalidArgumentException($this->class. " expected as set by the first index in given array. " . get_class($value) . " given in index $key of given array.");
+ throw new \InvalidArgumentException(
+ $this->class.
+ " expected in array. Got" .
+ get_class($value) .
+ " at index $key"
+ );
}
}
} elseif (is_object($input)) {
@@ -48,13 +55,14 @@ class ClassValidationArray extends \ArrayObject implements \JsonSerializable
}
/**
- * Sets the value at the specified index to value if
+ * Sets the value at the specified index to value if value's class matches the one set.
+ * If a class has not been set it will set the given object's class as the only allowed class.
* @throws InvalidArgumentException
*/
public function offsetSet($index, $value): void
{
if (!is_object($value)) {
- throw new \InvalidArgumentException("Excepcted object, ". gettype($value) ." given.");
+ throw new \InvalidArgumentException("Excepcted object, got ". gettype($value));
}
if (!isset($this->class)) {
$this->class = get_class($value);
@@ -62,9 +70,13 @@ class ClassValidationArray extends \ArrayObject implements \JsonSerializable
if (get_class($value) === $this->class) {
parent::offsetSet($index, $value);
} else {
- throw new \InvalidArgumentException("Excepcted $this->class, ". get_class($value) ." given.");
+ throw new \InvalidArgumentException("Excepcted $this->class, got ". get_class($value));
}
}
+ /**
+ * Returns the only class that is allowed to be added.
+ * @return string
+ */
public function getContainedClass(): string
{
return $this->class;
@@ -72,7 +84,7 @@ class ClassValidationArray extends \ArrayObject implements \JsonSerializable
/**
* {@inheritDoc}
*/
- public function JsonSerialize(): array
+ public function jsonSerialize(): array
{
$returnArray = [];
foreach ($this->getArrayCopy() as $value) {
diff --git a/tests/core/ClassValidationArrayTest.php b/tests/core/ClassValidationArrayTest.php
new file mode 100644
index 0000000..5c49c59
--- /dev/null
+++ b/tests/core/ClassValidationArrayTest.php
@@ -0,0 +1,111 @@
+<?php
+use PHPUnit\Framework\TestCase;
+use IP1\RESTClient\Core\ClassValidationArray;
+
+class ClassValidationArrayTest extends TestCase
+{
+
+ /**
+ * @dataProvider getValidNonArrayInputs
+ * @covers ClassValidationArray::__construct
+ */
+ public function testConstructorNonArrayInputs($input)
+ {
+ $this->assertEquals([$input], (new ClassValidationArray($input))->getArrayCopy());
+ }
+ /**
+ * @dataProvider getValidArrayInputs
+ * @covers ClassValidationArray::__construct
+ */
+ public function testConstructorArrayInputs($inputs)
+ {
+ $this->assertEquals($inputs, (new ClassValidationArray($inputs))->getArrayCopy());
+ }
+ /**
+ * @dataProvider getInValidArrays
+ * @covers ClassValidationArray::__construct
+ */
+ public function testInvalidArrayInput($invalidArray)
+ {
+ $this->expectException(InvalidArgumentException::class);
+ new ClassValidationArray($invalidArray);
+ }
+ /**
+ * @dataProvider getScalarTypes
+ * @covers ClassValidationArray::__construct
+ */
+ public function testScalarVariables($scalarVariable)
+ {
+ $this->expectException(InvalidArgumentException::class);
+ $array = new ClassValidationArray($scalarVariable);
+ }
+ /**
+ * @dataProvider getNonEmptyValidArrays
+ * @covers ClassValidationArray::offsetSet
+ * @throws InvalidArgumentException
+ */
+ public function testAddWrongObjectClass($validArray)
+ {
+ $this->expectException(InvalidArgumentException::class);
+ $array = new ClassValidationArray($validArray);
+ $array[] = new Mysqli();
+ }
+
+ /**
+ * @dataProvider getScalarTypes
+ * @covers ClassValidationArray::offsetSet
+ */
+ public function testAddScalarTypes($scalarVariable)
+ {
+ $this->expectException(InvalidArgumentException::class);
+ $array = new ClassValidationArray([]);
+ $array[] = $scalarVariable;
+ }
+ public function getNonEmptyValidArrays()
+ {
+ return [
+ [
+ [new stdClass,new stdClass,new stdClass,new stdClass,],
+ [new DateTime(),new DateTime(),new DateTime(),new DateTime(),],
+ [new stdClass],
+ ]
+ ];
+ }
+ public function getValidArrayInputs()
+ {
+ return [[
+ [],
+ [new stdClass,new stdClass,new stdClass,new stdClass,],
+ [new DateTime(),new DateTime(),new DateTime(),new DateTime(),],
+ [new stdClass],
+ ]];
+ }
+ public function getInValidArrays()
+ {
+ return [
+ [
+ [new stdClass, new DateTime],
+ [new stdClass, new stdClass, 1],
+ [new stdClass, new stdClass, true],
+ ['Jack', new stdClass],
+ [1,2,1,2,3,5,6,3,1,32],
+ ["","1231", "Jack",],
+ [false, false, false, false,],
+ [true, true, true, true],
+ [false, true, true, false],
+ ]
+ ];
+ }
+ public function getValidNonArrayInputs()
+ {
+ return [
+ [new DateTime(), new stdClass(), new ClassValidationArray()]
+ ];
+ }
+ public function getScalarTypes()
+ {
+ return [
+ [0,1,1.1, "", "filledString", false, true,],
+ ];
+ }
+}