Skip to content

Commit

Permalink
Merge pull request #62: Allow null value as location
Browse files Browse the repository at this point in the history
* pr-62:
  feat: JobDataInputFilter allows null value for location
  feat: validator IsString may allow null value.
  • Loading branch information
TiSiE committed Nov 25, 2020
2 parents 1e57c4c + 64a9e07 commit 93eb4d0
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 7 deletions.
5 changes: 4 additions & 1 deletion src/InputFilter/JobDataInputFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ public function __construct(array $availableClassifications)
],
'validators' => [
[
'name' => IsString::class
'name' => IsString::class,
'options' => [
'allowNull' => true
]
],
],
])->add([
Expand Down
17 changes: 15 additions & 2 deletions src/Validator/IsString.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,34 @@ class IsString extends AbstractValidator
* @var string
*/
const NOT_STRING = 'notString';
const NOT_STRING_OR_NULL = 'notStringOrNull';

private $allowNull = false;

public function setAllowNull(bool $flag)
{
$this->allowNull = $flag;
}

/**
* @var array
*/
protected $messageTemplates = [
self::NOT_STRING => "Expected input to be of type string.",
self::NOT_STRING_OR_NULL => 'Expected input to be of type string or null.',
];

public function isValid($value)
{
if (is_string($value)) {
if (is_string($value) || ($this->allowNull && $value === null)) {
return true;
}

$this->error(self::NOT_STRING, $value);
$this->error(
$this->allowNull ? self::NOT_STRING_OR_NULL : self::NOT_STRING,
$value
);

return false;
}
}
9 changes: 7 additions & 2 deletions test/SimpleImportTest/InputFilter/JobDataInputFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,18 @@ public function __invoke($container, $name, $options)
* [[1,2,3]]
* [{"one":1, "two":2}]
* [[]]
* [null]
* [0]
* [false]
* ["object"]
*/
public function testLocationIsInvalidIfNotString($location)
{
$target = new JobDataInputFilter([]);

if ($location == "object") {
$location = new \stdClass();
}

$data = [
'id' => 'id',
'title' => 'title',
Expand All @@ -199,12 +203,13 @@ public function testLocationIsInvalidIfNotString($location)
$target->setData($data);

static::assertFalse($target->isValid());
static::assertTrue(isset($target->getMessages()['location']['notString']), 'Missing error message');
static::assertTrue(isset($target->getMessages()['location']['notStringOrNull']), 'Missing error message');
}

/**
* @testWith ["finally a string"]
* [""]
* [null]
*/
public function testLocationIsValidIfString($str)
{
Expand Down
57 changes: 55 additions & 2 deletions test/SimpleImportTest/Validator/IsStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,22 @@ class IsStringTest extends TestCase
{
use TestInheritanceTrait, SetupTargetTrait;

private $target = IsString::class;
private $target = [
'create' => [
[
'for' => [
'testReturnsTrueIfValueIsStringOrNull',
'testReturnsFalseIfValueIsNotAStringOrNull',
'testProducesErrorMessageOnFailureWithAllowNull'
],
'target' => IsString::class,
'arguments' => [['allowNull' => true]],
],
[
'target' => IsString::class
],
]
];

private $inheritance = [ AbstractValidator::class ];

Expand All @@ -45,7 +60,8 @@ public function provideNotStringData()
[1234],
[['an', 'array']],
'assoc' => [['one' => 1, 'two' => 2]],
[new \stdClass]
[new \stdClass],
[null]
];
}

Expand All @@ -68,4 +84,41 @@ public function testProducesErrorMessageOnFailure()
static::assertArrayHasKey(IsString::NOT_STRING, $messages);
static::assertEquals($expect, $messages[IsString::NOT_STRING]);
}

public function testReturnsTrueIfValueIsStringOrNull()
{
static::assertTrue($this->target->isValid('thisisastring'), 'String value returns false!');
static::assertTrue($this->target->isValid(null), 'null value returns false');
}

public function provideNotStringOrNullData()
{
return [
[true],
[1234],
[['an', 'array']],
'assoc' => [['one' => 1, 'two' => 2]],
[new \stdClass]
];
}

/**
* @dataProvider provideNotStringOrNullData
*/
public function testReturnsFalseIfValueIsNotStringOrNull($value)
{
static::assertFalse($this->target->isValid($value));
}

public function testProducesErrorMessageOnFailureWithAllowNull()
{
$expect = 'Expected input to be of type string or null.';

$this->target->isValid(false);
$messages = $this->target->getMessages();

static::assertIsArray($messages);
static::assertArrayHasKey(IsString::NOT_STRING_OR_NULL, $messages);
static::assertEquals($expect, $messages[IsString::NOT_STRING_OR_NULL]);
}
}

0 comments on commit 93eb4d0

Please sign in to comment.