mirror of
https://github.com/Respect/Validation.git
synced 2026-03-17 23:59:51 +01:00
Due to the current status of the development of the library, it seems like we will be supporting version 1.1 for a long time. Even when we release version 2.0 we will still give support for version 1.1 for a while. This commit will make sure that version 1.1 is fully supported for PHP 7.2 and 7.3. Also, it will remove the support for HHVM since it will not keep the compatibility with PHP anymore [1]. In order to make that happen, this commit will create a TestCase from Validation so we can use the same API to create mocks in both PHPUnit versions 4.0 and 5.0. During the development of this commit, I noticed that PHPUnit 4.0 had issues to mock "SplFileInfo" and for that reason, this commit will also replace those mocks by "SplFileInfo" instances. [1]: https://hhvm.com/blog/2018/09/12/end-of-php-support-future-of-hack.html Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
185 lines
4.7 KiB
PHP
185 lines
4.7 KiB
PHP
<?php
|
|
|
|
/*
|
|
* This file is part of Respect/Validation.
|
|
*
|
|
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
|
*
|
|
* For the full copyright and license information, please view the "LICENSE.md"
|
|
* file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace Respect\Validation\Rules;
|
|
|
|
use Respect\Validation\TestCase;
|
|
use ArrayObject;
|
|
|
|
/**
|
|
* @group rule
|
|
* @covers Respect\Validation\Rules\KeyNested
|
|
* @covers Respect\Validation\Exceptions\KeyNestedException
|
|
*/
|
|
class KeyNestedTest extends TestCase
|
|
{
|
|
public function testArrayWithPresentKeysWillReturnTrueForFullPathValidator()
|
|
{
|
|
$array = [
|
|
'bar' => [
|
|
'foo' => [
|
|
'baz' => 'hello world!',
|
|
],
|
|
'foooo' => [
|
|
'boooo' => 321,
|
|
],
|
|
],
|
|
];
|
|
|
|
$rule = new KeyNested('bar.foo.baz');
|
|
|
|
$this->assertTrue($rule->validate($array));
|
|
}
|
|
|
|
public function testArrayWithNumericKeysWillReturnTrueForFullPathValidator()
|
|
{
|
|
$array = [
|
|
0 => 'Zero, the hero!',
|
|
];
|
|
|
|
$rule = new KeyNested(0, new Equals('Zero, the hero!'));
|
|
|
|
$this->assertTrue($rule->check($array));
|
|
}
|
|
|
|
public function testArrayWithPresentKeysWillReturnTrueForHalfPathValidator()
|
|
{
|
|
$array = [
|
|
'bar' => [
|
|
'foo' => [
|
|
'baz' => 'hello world!',
|
|
],
|
|
'foooo' => [
|
|
'boooo' => 321,
|
|
],
|
|
],
|
|
];
|
|
|
|
$rule = new KeyNested('bar.foo');
|
|
|
|
$this->assertTrue($rule->validate($array));
|
|
}
|
|
|
|
public function testObjectWithPresentPropertiesWillReturnTrueForDirtyPathValidator()
|
|
{
|
|
$object = (object) [
|
|
'bar' => (object) [
|
|
'foo' => (object) [
|
|
'baz' => 'hello world!',
|
|
],
|
|
'foooo' => (object) [
|
|
'boooo' => 321,
|
|
],
|
|
],
|
|
];
|
|
|
|
$rule = new KeyNested('bar.foooo.');
|
|
|
|
$this->assertTrue($rule->validate($object));
|
|
}
|
|
|
|
public function testEmptyInputMustReturnFalse()
|
|
{
|
|
$rule = new KeyNested('bar.foo.baz');
|
|
|
|
$this->assertFalse($rule->validate(''));
|
|
}
|
|
|
|
/**
|
|
* @expectedException Respect\Validation\Exceptions\KeyNestedException
|
|
*/
|
|
public function testEmptyInputMustNotAssert()
|
|
{
|
|
$rule = new KeyNested('bar.foo.baz');
|
|
$rule->assert('');
|
|
}
|
|
|
|
/**
|
|
* @expectedException Respect\Validation\Exceptions\KeyNestedException
|
|
*/
|
|
public function testEmptyInputMustNotCheck()
|
|
{
|
|
$rule = new KeyNested('bar.foo.baz');
|
|
$rule->check('');
|
|
}
|
|
|
|
public function testArrayWithEmptyKeyShouldReturnTrue()
|
|
{
|
|
$rule = new KeyNested('emptyKey');
|
|
$input = ['emptyKey' => ''];
|
|
|
|
$this->assertTrue($rule->validate($input));
|
|
}
|
|
|
|
/**
|
|
* @expectedException Respect\Validation\Exceptions\KeyNestedException
|
|
*/
|
|
public function testArrayWithAbsentKeyShouldThrowNestedKeyException()
|
|
{
|
|
$validator = new KeyNested('bar.bar');
|
|
$object = [
|
|
'baraaaaaa' => [
|
|
'bar' => 'foo',
|
|
],
|
|
];
|
|
$this->assertTrue($validator->assert($object));
|
|
}
|
|
|
|
/**
|
|
* @expectedException Respect\Validation\Exceptions\KeyNestedException
|
|
*/
|
|
public function testNotArrayShouldThrowKeyException()
|
|
{
|
|
$validator = new KeyNested('baz.bar');
|
|
$object = 123;
|
|
$this->assertFalse($validator->assert($object));
|
|
}
|
|
|
|
public function testExtraValidatorShouldValidateKey()
|
|
{
|
|
$subValidator = new Length(3, 7);
|
|
$validator = new KeyNested('bar.foo.baz', $subValidator);
|
|
$object = [
|
|
'bar' => [
|
|
'foo' => [
|
|
'baz' => 'example',
|
|
],
|
|
],
|
|
];
|
|
$this->assertTrue($validator->assert($object));
|
|
}
|
|
|
|
public function testNotMandatoryExtraValidatorShouldPassWithAbsentKey()
|
|
{
|
|
$subValidator = new Length(1, 3);
|
|
$validator = new KeyNested('bar.rab', $subValidator, false);
|
|
$object = new \stdClass();
|
|
$this->assertTrue($validator->validate($object));
|
|
}
|
|
|
|
public function testArrayAccessWithPresentKeysWillReturnTrue()
|
|
{
|
|
$arrayAccess = new ArrayObject([
|
|
'bar' => [
|
|
'foo' => [
|
|
'baz' => 'hello world!',
|
|
],
|
|
'foooo' => [
|
|
'boooo' => 321,
|
|
],
|
|
],
|
|
]);
|
|
|
|
$rule = new KeyNested('bar.foo.baz');
|
|
|
|
$this->assertTrue($rule->validate($arrayAccess));
|
|
}
|
|
}
|