Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

Consolidate tests for InputFilterInterface #57

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 13 additions & 18 deletions src/BaseInputFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class BaseInputFilter implements
ReplaceableInputInterface
{
/**
* @var null|array|ArrayAccess
* @var null|array
*/
protected $data;

Expand Down Expand Up @@ -106,23 +106,13 @@ public function add($input, $name = null)
/**
* Replace a named input
*
* @param InputInterface|InputFilterInterface $input
* @param mixed $input Any of the input types allowed on add() method.
* @param string $name Name of the input to replace
* @throws Exception\InvalidArgumentException
* @throws Exception\InvalidArgumentException If input to replace not exists.
* @return self
*/
public function replace($input, $name)
{
if (!$input instanceof InputInterface && !$input instanceof InputFilterInterface) {
throw new Exception\InvalidArgumentException(sprintf(
'%s expects an instance of %s or %s as its first argument; received "%s"',
__METHOD__,
InputInterface::class,
InputFilterInterface::class,
(is_object($input) ? get_class($input) : gettype($input))
));
}

if (!array_key_exists($name, $this->inputs)) {
throw new Exception\InvalidArgumentException(sprintf(
'%s: no input found matching "%s"',
Expand All @@ -131,7 +121,9 @@ public function replace($input, $name)
));
}

$this->inputs[$name] = $input;
$this->remove($name);
$this->add($input, $name);

return $this;
}

Expand Down Expand Up @@ -186,16 +178,16 @@ public function remove($name)
*/
public function setData($data)
{
if (!is_array($data) && !$data instanceof Traversable) {
if ($data instanceof Traversable) {
$data = ArrayUtils::iteratorToArray($data);
}
if (!is_array($data)) {
throw new Exception\InvalidArgumentException(sprintf(
'%s expects an array or Traversable argument; received %s',
__METHOD__,
(is_object($data) ? get_class($data) : gettype($data))
));
}
if (is_object($data) && !$data instanceof ArrayAccess) {
$data = ArrayUtils::iteratorToArray($data);
}
$this->data = $data;
$this->populate();
return $this;
Expand Down Expand Up @@ -433,6 +425,9 @@ public function getRawValue($name)
));
}
$input = $this->inputs[$name];
if ($input instanceof InputFilterInterface) {
return $input->getRawValues();
}
return $input->getRawValue();
}

Expand Down
2 changes: 2 additions & 0 deletions src/CollectionInputFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ public function getCount()
public function setData($data)
{
$this->data = $data;

return $this;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/InputFilterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ interface InputFilterInterface extends Countable
* raise an exception for any they cannot process.
* @param null|string $name Name used to retrieve this input
* @return InputFilterInterface
* @throws Exception\InvalidArgumentInterface if unable to handle the input type.
* @throws Exception\InvalidArgumentException if unable to handle the input type.
*/
public function add($input, $name = null);

Expand Down
10 changes: 8 additions & 2 deletions test/ArrayInputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ public function testValidationOperatesOnFilteredValue()
$this->input->getFilterChain()->attach($filter);
$validator = new Validator\Digits();
$this->input->getValidatorChain()->attach($validator);
$this->assertTrue($this->input->isValid());
$this->assertTrue(
$this->input->isValid(),
'isValid() value not match. Detail . ' . json_encode($this->input->getMessages())
);
}

public function testSpecifyingMessagesToInputReturnsThoseOnFailedValidation()
Expand Down Expand Up @@ -162,7 +165,10 @@ public function testNotAllowEmptyWithFilterConvertsEmptyToNonEmptyIsValid()
->getFilterChain()->attach(new Filter\Callback(function () {
return 'nonempty';
}));
$this->assertTrue($this->input->isValid());
$this->assertTrue(
$this->input->isValid(),
'isValid() value not match. Detail . ' . json_encode($this->input->getMessages())
);
}

public function testMerge($sourceRawValue = 'bazRawValue')
Expand Down
Loading