-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from PackageFactory/feature/dataStructureValidator
FEATURE: Add dataStructureValidator
- Loading branch information
Showing
4 changed files
with
226 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
namespace PackageFactory\AtomicFusion\PropTypes\Validators; | ||
|
||
use Neos\Flow\Annotations as Flow; | ||
use Neos\Flow\Validation\Validator\AbstractValidator; | ||
use Neos\Flow\Validation\Validator\ValidatorInterface; | ||
use Neos\Utility\ObjectAccess; | ||
|
||
/** | ||
* Validator for data-structures. | ||
*/ | ||
class DataStructureValidator extends AbstractValidator | ||
{ | ||
protected $acceptsEmptyValues = false; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $supportedOptions = [ | ||
'dataStructure' => array([], 'The expected data-structure for this property', 'array') | ||
]; | ||
|
||
/** | ||
* Checks if the given value is accepted. | ||
* | ||
* @param mixed $value The value that should be validated | ||
* @return void | ||
*/ | ||
protected function isValid($value) | ||
{ | ||
if (is_null($value)) { | ||
return; | ||
} | ||
|
||
if (is_array($value) || ($value instanceof \ArrayAccess) || is_object($value)) { | ||
foreach ($this->options['dataStructure'] as $key => $subValidator) { | ||
if (is_array($value) || ($value instanceof \ArrayAccess)) { | ||
if (array_key_exists($key, $value)) { | ||
$subValue = $value[$key]; | ||
} else { | ||
$subValue = null; | ||
} | ||
} elseif (ObjectAccess::isPropertyGettable($value, $key)) { | ||
$subValue = ObjectAccess::getPropertyPath($value, $key); | ||
} else { | ||
$subValue = null; | ||
} | ||
|
||
if ($subValidator instanceof ValidatorInterface) { | ||
$subResult = $subValidator->validate($subValue); | ||
if ($subResult->hasErrors()) { | ||
$this->addError('DataStructure-Property %s is not valid', 1515003533, [$key]); | ||
} | ||
} | ||
} | ||
} else { | ||
$this->addError('DataStructure is expected to be an array or implement ArrayAccess', 1515070099); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
<?php | ||
namespace PackageFactory\AtomicFusion\PropTypes\Tests\Unit\Validators; | ||
|
||
use Neos\Flow\Tests\Unit\Validation\Validator\AbstractValidatorTestcase; | ||
use PackageFactory\AtomicFusion\PropTypes\Validators\DataStructureValidator; | ||
use Neos\Flow\Validation\Validator\ValidatorInterface; | ||
use Neos\Error\Messages\Result; | ||
|
||
/** | ||
* Testcase for the shape validator | ||
* | ||
*/ | ||
class DataStructureValidatorTest extends AbstractValidatorTestcase | ||
{ | ||
|
||
/** | ||
* @var ValidatorInterface | ||
*/ | ||
protected $mockItemValidator; | ||
|
||
|
||
public function setUp() | ||
{ | ||
$mockSuccessResult = $this->createMock(Result::class); | ||
$mockSuccessResult->expects($this->any())->method('hasErrors')->will($this->returnValue(false)); | ||
|
||
$this->mockItemValidator = $this->createMock(ValidatorInterface::class); | ||
$this->mockItemValidator | ||
->expects($this->any()) | ||
->method('validate') | ||
->will($this->returnValue($mockSuccessResult)); | ||
|
||
$this->validator = new DataStructureValidator(['dataStructure' => [ | ||
'foo' => $this->mockItemValidator, | ||
'bar' => $this->mockItemValidator | ||
]]); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function validatorAcceptsNull() | ||
{ | ||
$this->assertFalse($this->validator->validate(null)->hasErrors()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function validatorAcceptsEmptyArray() | ||
{ | ||
$this->assertFalse($this->validator->validate([])->hasErrors()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function validatorAcceptsEmptyArrayObject() | ||
{ | ||
$arrayObject = new \ArrayObject(); | ||
$this->assertFalse($this->validator->validate($arrayObject)->hasErrors()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function validatorCallsItemValidatorForEachKeyOfShape() | ||
{ | ||
$shape = ['foo' => 123, 'bar' => 'string']; | ||
|
||
$this->mockItemValidator->expects($this->exactly(2))->method('validate'); | ||
$this->mockItemValidator->expects($this->at(0))->method('validate')->with(123); | ||
$this->mockItemValidator->expects($this->at(1))->method('validate')->with('string'); | ||
|
||
$this->validator->validate($shape); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function validatorCallsItemValidatorForEachKeyOfShapeOnArrayObjects() | ||
{ | ||
$shape = new \ArrayObject(['foo' => 123, 'bar' => 'string']); | ||
|
||
$this->mockItemValidator->expects($this->exactly(2))->method('validate'); | ||
$this->mockItemValidator->expects($this->at(0))->method('validate')->with(123); | ||
$this->mockItemValidator->expects($this->at(1))->method('validate')->with('string'); | ||
|
||
$this->validator->validate($shape); | ||
} | ||
|
||
|
||
/** | ||
* @test | ||
*/ | ||
public function validatorCallsItemValidatorForEachKeyOfShapeOnStdClassObjects() | ||
{ | ||
$shape = new \stdClass(); | ||
$shape->foo = 123; | ||
$shape->bar = 'string'; | ||
|
||
$this->mockItemValidator->expects($this->exactly(2))->method('validate'); | ||
$this->mockItemValidator->expects($this->at(0))->method('validate')->with(123); | ||
$this->mockItemValidator->expects($this->at(1))->method('validate')->with('string'); | ||
|
||
$this->validator->validate($shape); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function validatorCallsItemValidatorForEachKeyOfShapeOfClassObjects() | ||
{ | ||
$shape = new class() { | ||
public function getFoo() | ||
{ | ||
return 123; | ||
} | ||
public function getBar() | ||
{ | ||
return 'string'; | ||
} | ||
}; | ||
|
||
$this->mockItemValidator->expects($this->exactly(2))->method('validate'); | ||
$this->mockItemValidator->expects($this->at(0))->method('validate')->with(123); | ||
$this->mockItemValidator->expects($this->at(1))->method('validate')->with('string'); | ||
|
||
$this->validator->validate($shape); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function validatorReturnsErrorIfItemsDoNotValidate() | ||
{ | ||
$shape = ['foo' => 123, 'bar' => 'string']; | ||
|
||
$mockErrorResult = $this->createMock(Result::class); | ||
$mockErrorResult->expects($this->any())->method('hasErrors')->will($this->returnValue(true)); | ||
|
||
$mockItemValidator = $this->createMock(ValidatorInterface::class); | ||
$mockItemValidator->expects($this->any())->method('validate')->will($this->returnValue($mockErrorResult)); | ||
|
||
$validator = new DataStructureValidator(['dataStructure' => [ | ||
'foo' => $mockItemValidator, | ||
'bar' => $mockItemValidator | ||
]]); | ||
|
||
$this->assertTrue($validator->validate($shape)->hasErrors()); | ||
} | ||
} |