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

Add tests for exceptions and improve some messages #49

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
5 changes: 3 additions & 2 deletions src/InputFilterPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,10 @@ public function validatePlugin($plugin)
}

throw new Exception\RuntimeException(sprintf(
'Plugin of type %s is invalid; must implement %s',
'Plugin of type %s is invalid; must implement %s or %s',
(is_object($plugin) ? get_class($plugin) : gettype($plugin)),
InputFilterInterface::class
InputFilterInterface::class,
InputInterface::class
));
}
}
7 changes: 5 additions & 2 deletions test/ArrayInputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ public function testValueIsEmptyArrayByDefault()
$this->assertCount(0, $this->input->getValue());
}

public function testNotArrayValueCannotBeInjected()
public function testSetValueWithInvalidInputTypeThrowsInvalidArgumentException()
{
$this->setExpectedException(InvalidArgumentException::class);
$this->setExpectedException(
InvalidArgumentException::class,
'Value must be an array, string given'
);
$this->input->setValue('bar');
}

Expand Down
165 changes: 152 additions & 13 deletions test/BaseInputFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Zend\InputFilter\ArrayInput;
use Zend\InputFilter\BaseInputFilter as InputFilter;
use Zend\InputFilter\Exception\InvalidArgumentException;
use Zend\InputFilter\Exception\RuntimeException;
use Zend\InputFilter\FileInput;
use Zend\InputFilter\Input;
use Zend\InputFilter\InputFilterInterface;
Expand All @@ -34,6 +35,157 @@ public function testInputFilterIsEmptyByDefault()
$this->assertEquals(0, count($filter));
}

public function testAddWithInvalidInputTypeThrowsInvalidArgumentException()
{
$inputFilter = $this->getInputFilter();

$this->setExpectedException(
InvalidArgumentException::class,
'expects an instance of Zend\InputFilter\InputInterface or Zend\InputFilter\InputFilterInterface ' .
'as its first argument; received "stdClass"'
);
/** @noinspection PhpParamsInspection */
$inputFilter->add(new stdClass());
}

public function testGetThrowExceptionIfInputDoesNotExists()
{
$inputFilter = $this->getInputFilter();

$this->setExpectedException(
InvalidArgumentException::class,
'no input found matching "not exists"'
);
$inputFilter->get('not exists');
}

public function testReplaceWithInvalidInputTypeThrowsInvalidArgumentException()
{
$inputFilter = $this->getInputFilter();
$inputFilter->add(new Input('foo'), 'replace_me');

$this->setExpectedException(
InvalidArgumentException::class,
'expects an instance of Zend\InputFilter\InputInterface or Zend\InputFilter\InputFilterInterface ' .
'as its first argument; received "stdClass"'
);
/** @noinspection PhpParamsInspection */
$inputFilter->replace(new stdClass(), 'replace_me');
}

public function testReplaceThrowExceptionIfInputToReplaceDoesNotExists()
{
$inputFilter = $this->getInputFilter();

$this->setExpectedException(
InvalidArgumentException::class,
'no input found matching "not exists"'
);
$inputFilter->replace(new Input('foo'), 'not exists');
}

public function testGetValueThrowExceptionIfInputDoesNotExists()
{
$inputFilter = $this->getInputFilter();

$this->setExpectedException(
InvalidArgumentException::class,
'"not exists" was not found in the filter'
);
$inputFilter->getValue('not exists');
}

public function testGetRawValueThrowExceptionIfInputDoesNotExists()
{
$inputFilter = $this->getInputFilter();

$this->setExpectedException(
InvalidArgumentException::class,
'"not exists" was not found in the filter'
);
$inputFilter->getRawValue('not exists');
}

public function testSetDataWithInvalidDataTypeThrowsInvalidArgumentException()
{
$inputFilter = $this->getInputFilter();

$this->setExpectedException(
InvalidArgumentException::class,
'expects an array or Traversable argument; received stdClass'
);
/** @noinspection PhpParamsInspection */
$inputFilter->setData(new stdClass());
}

public function testIsValidThrowExceptionIfDataWasNotSetYet()
{
$inputFilter = $this->getInputFilter();

$this->setExpectedException(
RuntimeException::class,
'no data present to validate'
);
$inputFilter->isValid();
}

public function testSetValidationGroupThrowExceptionIfInputIsNotAnInputFilter()
{
$inputFilter = $this->getInputFilter();

/** @var InputInterface|MockObject $nestedInput */
$nestedInput = $this->getMock(InputInterface::class);
$inputFilter->add($nestedInput, 'fooInput');

$this->setExpectedException(
InvalidArgumentException::class,
'Input "fooInput" must implement InputFilterInterface'
);
$inputFilter->setValidationGroup(['fooInput' => 'foo']);
}

public function testSetValidationGroupThrowExceptionIfInputFilterNotExists()
{
$inputFilter = $this->getInputFilter();

$this->setExpectedException(
InvalidArgumentException::class,
'expects a list of valid input names; "anotherNotExistsInputFilter" was not found'
);
$inputFilter->setValidationGroup(['notExistInputFilter' => 'anotherNotExistsInputFilter']);
}

public function testSetValidationGroupThrowExceptionIfInputFilterInArgumentListNotExists()
{
$inputFilter = $this->getInputFilter();

$this->setExpectedException(
InvalidArgumentException::class,
'expects a list of valid input names; "notExistInputFilter" was not found'
);
$inputFilter->setValidationGroup('notExistInputFilter');
}

public function testHasUnknownThrowExceptionIfDataWasNotSetYet()
{
$inputFilter = $this->getInputFilter();

$this->setExpectedException(
RuntimeException::class
);
$inputFilter->hasUnknown();
}

public function testGetUnknownThrowExceptionIfDataWasNotSetYet()
{
$inputFilter = $this->getInputFilter();

$this->setExpectedException(
RuntimeException::class
);
$inputFilter->getUnknown();
}

public function testAddingInputsIncreasesCountOfFilter()
{
$filter = new InputFilter();
Expand Down Expand Up @@ -254,19 +406,6 @@ public function testResetEmptyValidationGroupRecursively()
$this->assertEquals($data, $filter->getValues());
}

public function testSetDeepValidationGroupToNonInputFilterThrowsException()
{
$filter = $this->getInputFilter();
$filter->add(new Input, 'flat');
// we expect setValidationGroup to throw an exception when flat is treated
// like an inputfilter which it actually isn't
$this->setExpectedException(
InvalidArgumentException::class,
'Input "flat" must implement InputFilterInterface'
);
$filter->setValidationGroup(['flat' => 'foo']);
}

public function testCanRetrieveInvalidInputsOnFailedValidation()
{
if (!extension_loaded('intl')) {
Expand Down
14 changes: 14 additions & 0 deletions test/CollectionInputFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
namespace ZendTest\InputFilter;

use PHPUnit_Framework_TestCase as TestCase;
use stdClass;
use Zend\InputFilter\BaseInputFilter;
use Zend\InputFilter\CollectionInputFilter;
use Zend\InputFilter\Exception\RuntimeException;
use Zend\InputFilter\Input;
use Zend\InputFilter\InputFilter;
use Zend\Validator;
Expand All @@ -31,6 +33,18 @@ public function setUp()
$this->filter = new CollectionInputFilter();
}

public function testSetDataWithInvalidDataTypeThrowsInvalidArgumentException()
{
$inputFilter = $this->filter;

$this->setExpectedException(
RuntimeException::class,
'expects an instance of Zend\InputFilter\BaseInputFilter; received "stdClass"'
);
/** @noinspection PhpParamsInspection */
$inputFilter->setInputFilter(new stdClass());
}

public function getBaseInputFilter()
{
$filter = new BaseInputFilter();
Expand Down
5 changes: 4 additions & 1 deletion test/InputFilterPluginManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ public function testIsNotSharedByDefault()

public function testRegisteringInvalidElementRaisesException()
{
$this->setExpectedException(RuntimeException::class);
$this->setExpectedException(
RuntimeException::class,
'must implement Zend\InputFilter\InputFilterInterface or Zend\InputFilter\InputInterface'
);
$this->manager->setService('test', $this);
}

Expand Down