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

Commit 8c3bb64

Browse files
committed
Add tests for exceptions and improve some messages
1 parent 801f0f9 commit 8c3bb64

5 files changed

+178
-18
lines changed

src/InputFilterPluginManager.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,10 @@ public function validatePlugin($plugin)
8383
}
8484

8585
throw new Exception\RuntimeException(sprintf(
86-
'Plugin of type %s is invalid; must implement %s',
86+
'Plugin of type %s is invalid; must implement %s or %s',
8787
(is_object($plugin) ? get_class($plugin) : gettype($plugin)),
88-
InputFilterInterface::class
88+
InputFilterInterface::class,
89+
InputInterface::class
8990
));
9091
}
9192
}

test/ArrayInputTest.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,12 @@ public function testValueIsEmptyArrayByDefault()
3636
$this->assertCount(0, $this->input->getValue());
3737
}
3838

39-
public function testNotArrayValueCannotBeInjected()
39+
public function testSetValueWithInvalidInputTypeThrowsInvalidArgumentException()
4040
{
41-
$this->setExpectedException(InvalidArgumentException::class);
41+
$this->setExpectedException(
42+
InvalidArgumentException::class,
43+
'Value must be an array, string given'
44+
);
4245
$this->input->setValue('bar');
4346
}
4447

test/BaseInputFilterTest.php

+152-13
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Zend\InputFilter\ArrayInput;
1818
use Zend\InputFilter\BaseInputFilter as InputFilter;
1919
use Zend\InputFilter\Exception\InvalidArgumentException;
20+
use Zend\InputFilter\Exception\RuntimeException;
2021
use Zend\InputFilter\FileInput;
2122
use Zend\InputFilter\Input;
2223
use Zend\InputFilter\InputFilterInterface;
@@ -34,6 +35,157 @@ public function testInputFilterIsEmptyByDefault()
3435
$this->assertEquals(0, count($filter));
3536
}
3637

38+
public function testAddWithInvalidInputTypeThrowsInvalidArgumentException()
39+
{
40+
$inputFilter = $this->getInputFilter();
41+
42+
$this->setExpectedException(
43+
InvalidArgumentException::class,
44+
'expects an instance of Zend\InputFilter\InputInterface or Zend\InputFilter\InputFilterInterface ' .
45+
'as its first argument; received "stdClass"'
46+
);
47+
/** @noinspection PhpParamsInspection */
48+
$inputFilter->add(new stdClass());
49+
}
50+
51+
public function testGetThrowExceptionIfInputDoesNotExists()
52+
{
53+
$inputFilter = $this->getInputFilter();
54+
55+
$this->setExpectedException(
56+
InvalidArgumentException::class,
57+
'no input found matching "not exists"'
58+
);
59+
$inputFilter->get('not exists');
60+
}
61+
62+
public function testReplaceWithInvalidInputTypeThrowsInvalidArgumentException()
63+
{
64+
$inputFilter = $this->getInputFilter();
65+
$inputFilter->add(new Input('foo'), 'replace_me');
66+
67+
$this->setExpectedException(
68+
InvalidArgumentException::class,
69+
'expects an instance of Zend\InputFilter\InputInterface or Zend\InputFilter\InputFilterInterface ' .
70+
'as its first argument; received "stdClass"'
71+
);
72+
/** @noinspection PhpParamsInspection */
73+
$inputFilter->replace(new stdClass(), 'replace_me');
74+
}
75+
76+
public function testReplaceThrowExceptionIfInputToReplaceDoesNotExists()
77+
{
78+
$inputFilter = $this->getInputFilter();
79+
80+
$this->setExpectedException(
81+
InvalidArgumentException::class,
82+
'no input found matching "not exists"'
83+
);
84+
$inputFilter->replace(new Input('foo'), 'not exists');
85+
}
86+
87+
public function testGetValueThrowExceptionIfInputDoesNotExists()
88+
{
89+
$inputFilter = $this->getInputFilter();
90+
91+
$this->setExpectedException(
92+
InvalidArgumentException::class,
93+
'"not exists" was not found in the filter'
94+
);
95+
$inputFilter->getValue('not exists');
96+
}
97+
98+
public function testGetRawValueThrowExceptionIfInputDoesNotExists()
99+
{
100+
$inputFilter = $this->getInputFilter();
101+
102+
$this->setExpectedException(
103+
InvalidArgumentException::class,
104+
'"not exists" was not found in the filter'
105+
);
106+
$inputFilter->getRawValue('not exists');
107+
}
108+
109+
public function testSetDataWithInvalidDataTypeThrowsInvalidArgumentException()
110+
{
111+
$inputFilter = $this->getInputFilter();
112+
113+
$this->setExpectedException(
114+
InvalidArgumentException::class,
115+
'expects an array or Traversable argument; received stdClass'
116+
);
117+
/** @noinspection PhpParamsInspection */
118+
$inputFilter->setData(new stdClass());
119+
}
120+
121+
public function testIsValidThrowExceptionIfDataWasNotSetYet()
122+
{
123+
$inputFilter = $this->getInputFilter();
124+
125+
$this->setExpectedException(
126+
RuntimeException::class,
127+
'no data present to validate'
128+
);
129+
$inputFilter->isValid();
130+
}
131+
132+
public function testSetValidationGroupThrowExceptionIfInputIsNotAnInputFilter()
133+
{
134+
$inputFilter = $this->getInputFilter();
135+
136+
/** @var InputInterface|MockObject $nestedInput */
137+
$nestedInput = $this->getMock(InputInterface::class);
138+
$inputFilter->add($nestedInput, 'fooInput');
139+
140+
$this->setExpectedException(
141+
InvalidArgumentException::class,
142+
'Input "fooInput" must implement InputFilterInterface'
143+
);
144+
$inputFilter->setValidationGroup(['fooInput' => 'foo']);
145+
}
146+
147+
public function testSetValidationGroupThrowExceptionIfInputFilterNotExists()
148+
{
149+
$inputFilter = $this->getInputFilter();
150+
151+
$this->setExpectedException(
152+
InvalidArgumentException::class,
153+
'expects a list of valid input names; "anotherNotExistsInputFilter" was not found'
154+
);
155+
$inputFilter->setValidationGroup(['notExistInputFilter' => 'anotherNotExistsInputFilter']);
156+
}
157+
158+
public function testSetValidationGroupThrowExceptionIfInputFilterInArgumentListNotExists()
159+
{
160+
$inputFilter = $this->getInputFilter();
161+
162+
$this->setExpectedException(
163+
InvalidArgumentException::class,
164+
'expects a list of valid input names; "notExistInputFilter" was not found'
165+
);
166+
$inputFilter->setValidationGroup('notExistInputFilter');
167+
}
168+
169+
public function testHasUnknownThrowExceptionIfDataWasNotSetYet()
170+
{
171+
$inputFilter = $this->getInputFilter();
172+
173+
$this->setExpectedException(
174+
RuntimeException::class
175+
);
176+
$inputFilter->hasUnknown();
177+
}
178+
179+
public function testGetUnknownThrowExceptionIfDataWasNotSetYet()
180+
{
181+
$inputFilter = $this->getInputFilter();
182+
183+
$this->setExpectedException(
184+
RuntimeException::class
185+
);
186+
$inputFilter->getUnknown();
187+
}
188+
37189
public function testAddingInputsIncreasesCountOfFilter()
38190
{
39191
$filter = new InputFilter();
@@ -254,19 +406,6 @@ public function testResetEmptyValidationGroupRecursively()
254406
$this->assertEquals($data, $filter->getValues());
255407
}
256408

257-
public function testSetDeepValidationGroupToNonInputFilterThrowsException()
258-
{
259-
$filter = $this->getInputFilter();
260-
$filter->add(new Input, 'flat');
261-
// we expect setValidationGroup to throw an exception when flat is treated
262-
// like an inputfilter which it actually isn't
263-
$this->setExpectedException(
264-
InvalidArgumentException::class,
265-
'Input "flat" must implement InputFilterInterface'
266-
);
267-
$filter->setValidationGroup(['flat' => 'foo']);
268-
}
269-
270409
public function testCanRetrieveInvalidInputsOnFailedValidation()
271410
{
272411
if (!extension_loaded('intl')) {

test/CollectionInputFilterTest.php

+14
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
namespace ZendTest\InputFilter;
1111

1212
use PHPUnit_Framework_TestCase as TestCase;
13+
use stdClass;
1314
use Zend\InputFilter\BaseInputFilter;
1415
use Zend\InputFilter\CollectionInputFilter;
16+
use Zend\InputFilter\Exception\RuntimeException;
1517
use Zend\InputFilter\Input;
1618
use Zend\InputFilter\InputFilter;
1719
use Zend\Validator;
@@ -31,6 +33,18 @@ public function setUp()
3133
$this->filter = new CollectionInputFilter();
3234
}
3335

36+
public function testSetDataWithInvalidDataTypeThrowsInvalidArgumentException()
37+
{
38+
$inputFilter = $this->filter;
39+
40+
$this->setExpectedException(
41+
RuntimeException::class,
42+
'expects an instance of Zend\InputFilter\BaseInputFilter; received "stdClass"'
43+
);
44+
/** @noinspection PhpParamsInspection */
45+
$inputFilter->setInputFilter(new stdClass());
46+
}
47+
3448
public function getBaseInputFilter()
3549
{
3650
$filter = new BaseInputFilter();

test/InputFilterPluginManagerTest.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ public function testIsNotSharedByDefault()
4949

5050
public function testRegisteringInvalidElementRaisesException()
5151
{
52-
$this->setExpectedException(RuntimeException::class);
52+
$this->setExpectedException(
53+
RuntimeException::class,
54+
'must implement Zend\InputFilter\InputFilterInterface or Zend\InputFilter\InputInterface'
55+
);
5356
$this->manager->setService('test', $this);
5457
}
5558

0 commit comments

Comments
 (0)