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

Commit 256a6d6

Browse files
committed
Merge pull request #49 from Maks3w/feature/exceptions-improvements
Add tests for exceptions and improve some messages
2 parents 65bc263 + 8c3bb64 commit 256a6d6

7 files changed

+250
-31
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

+6-3
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

@@ -208,7 +211,7 @@ public function mixedValueProvider()
208211
{
209212
$dataSets = parent::mixedValueProvider();
210213
array_walk($dataSets, function (&$set) {
211-
$set[0] = [$set[0]]; // Wrap value into an array.
214+
$set['raw'] = [$set['raw']]; // Wrap value into an array.
212215
});
213216

214217
return $dataSets;

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/FileInputTest.php

+21
Original file line numberDiff line numberDiff line change
@@ -456,4 +456,25 @@ public function emptyValueProvider()
456456
],
457457
];
458458
}
459+
460+
public function mixedValueProvider()
461+
{
462+
$fooUploadErrOk = [
463+
'tmp_name' => 'foo',
464+
'error' => UPLOAD_ERR_OK,
465+
];
466+
467+
return [
468+
'single' => [
469+
'raw' => $fooUploadErrOk,
470+
'filtered' => $fooUploadErrOk,
471+
],
472+
'multi' => [
473+
'raw' => [
474+
$fooUploadErrOk,
475+
],
476+
'filtered' => $fooUploadErrOk,
477+
],
478+
];
479+
}
459480
}

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

test/InputTest.php

+50-12
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,8 @@ public function testIsRequiredVsAllowEmptyVsContinueIfEmptyVsIsValid(
350350
'isValid() value not match. Detail: ' . json_encode($this->input->getMessages())
351351
);
352352
$this->assertEquals($expectedMessages, $this->input->getMessages(), 'getMessages() value not match');
353+
$this->assertEquals($value, $this->input->getRawValue(), 'getRawValue() must return the value always');
354+
$this->assertEquals($value, $this->input->getValue(), 'getValue() must return the filtered value always');
353355
}
354356

355357
/**
@@ -551,19 +553,55 @@ public function mixedValueProvider()
551553
{
552554
return [
553555
// Description => [$value]
554-
'"0"' => ['0'],
555-
'0' => [0],
556-
'0.0' => [0.0],
557-
'false' => [false],
558-
'php' => ['php'],
559-
'whitespace' => [' '],
560-
'1' => [1],
561-
'1.0' => [1.0],
562-
'true' => [true],
563-
'["php"]' => [['php']],
564-
'object' => [new stdClass()],
556+
'"0"' => [
557+
'raw' => '0',
558+
'filtered' => '0',
559+
],
560+
'0' => [
561+
'raw' => 0,
562+
'filtered' => 0,
563+
],
564+
'0.0' => [
565+
'raw' => 0.0,
566+
'filtered' => 0.0,
567+
],
568+
'false' => [
569+
'raw' => false,
570+
'filtered' => false,
571+
],
572+
'php' => [
573+
'raw' => 'php',
574+
'filtered' => 'php',
575+
],
576+
'whitespace' => [
577+
'raw' => ' ',
578+
'filtered' => ' ',
579+
],
580+
'1' => [
581+
'raw' => 1,
582+
'filtered' => 1,
583+
],
584+
'1.0' => [
585+
'raw' => 1.0,
586+
'filtered' => 1.0,
587+
],
588+
'true' => [
589+
'raw' => true,
590+
'filtered' => true,
591+
],
592+
'["php"]' => [
593+
'raw' => ['php'],
594+
'filtered' => ['php'],
595+
],
596+
'object' => [
597+
'raw' => new stdClass(),
598+
'filtered' => new stdClass(),
599+
],
565600
// @codingStandardsIgnoreStart
566-
'callable' => [function () {}],
601+
'callable' => [
602+
'raw' => function () {},
603+
'filtered' => function () {},
604+
],
567605
// @codingStandardsIgnoreEnd
568606
];
569607
}

0 commit comments

Comments
 (0)