1717use Zend \InputFilter \ArrayInput ;
1818use Zend \InputFilter \BaseInputFilter as InputFilter ;
1919use Zend \InputFilter \Exception \InvalidArgumentException ;
20+ use Zend \InputFilter \Exception \RuntimeException ;
2021use Zend \InputFilter \FileInput ;
2122use Zend \InputFilter \Input ;
2223use 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 ' )) {
0 commit comments