From 05cd1a531b967fc0170776b4c96caca2f3bbfdba Mon Sep 17 00:00:00 2001 From: "Theodore R. Smith" Date: Sun, 28 Jul 2019 14:10:07 -0500 Subject: [PATCH] Now filters out the "[]" when checking for arrays of something. --- phpunit.xml | 23 +++++++++++++---------- src/DataTypeValidator.php | 1 + tests/DataTypeValidatorAssertTest.php | 6 ++++++ 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/phpunit.xml b/phpunit.xml index ce34605..4f3ce42 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,22 +1,25 @@ - + testdox="true" + stopOnError="true" + stopOnFailure="true"> - - ./tests/ + + ./tests + - - src/ + + ./src + + + + diff --git a/src/DataTypeValidator.php b/src/DataTypeValidator.php index 81f3db7..a5850da 100644 --- a/src/DataTypeValidator.php +++ b/src/DataTypeValidator.php @@ -144,6 +144,7 @@ public function assertIsArrayOfSomething($values, string $dataType): void { $this->assertIsArray($values); + $dataType = substr($dataType, -2) === '[]' ? substr($dataType, 0, -2) : $dataType; foreach ($values as $i => $value) { if (!$this->isA->isType($value, $dataType)) { throw new InvalidDataTypeException("Index '$i' is not a valid '$dataType'."); diff --git a/tests/DataTypeValidatorAssertTest.php b/tests/DataTypeValidatorAssertTest.php index 98cbcbf..ef980b3 100644 --- a/tests/DataTypeValidatorAssertTest.php +++ b/tests/DataTypeValidatorAssertTest.php @@ -118,11 +118,17 @@ public function testWillAssertAnArrayOfSomething() { $goodArrays = [ 'int' => $this->getDataByType('int', DataTypesLists::getValidStrictDataAndTypes()), + 'int[]' => $this->getDataByType('int', DataTypesLists::getValidStrictDataAndTypes()), 'bool' => $this->getDataByType('bool', DataTypesLists::getValidStrictDataAndTypes()), + 'bool[]' => $this->getDataByType('bool', DataTypesLists::getValidStrictDataAndTypes()), 'float' => $this->getDataByType('float', DataTypesLists::getValidStrictDataAndTypes()), + 'float[]' => $this->getDataByType('float', DataTypesLists::getValidStrictDataAndTypes()), 'string' => $this->getDataByType('string', DataTypesLists::getValidStrictDataAndTypes()), + 'string[]' => $this->getDataByType('string', DataTypesLists::getValidStrictDataAndTypes()), 'isAStrictDataType' => [new IsAStrictDataType(), new IsAStrictDataType(), new IsAStrictDataType()], + 'isAStrictDataType[]' => [new IsAStrictDataType(), new IsAStrictDataType(), new IsAStrictDataType()], 'isAFuzzyDataType' => [new IsAFuzzyDataType(), new IsAFuzzyDataType(), new IsAFuzzyDataType()], + 'isAFuzzyDataType[]' => [new IsAFuzzyDataType(), new IsAFuzzyDataType(), new IsAFuzzyDataType()], isAFuzzyDataType::class => [new IsAFuzzyDataType(), new IsAFuzzyDataType(), new IsAFuzzyDataType()], ];