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

Commit 0559e24

Browse files
committed
Improve test reuse. Use helper methods for dummy value
1 parent bfc52a6 commit 0559e24

File tree

3 files changed

+131
-178
lines changed

3 files changed

+131
-178
lines changed

test/ArrayInputTest.php

Lines changed: 30 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,8 @@
99

1010
namespace ZendTest\InputFilter;
1111

12-
use PHPUnit_Framework_MockObject_MockObject as MockObject;
13-
use Zend\Filter;
1412
use Zend\InputFilter\ArrayInput;
1513
use Zend\InputFilter\Exception\InvalidArgumentException;
16-
use Zend\Validator;
1714

1815
/**
1916
* @covers Zend\InputFilter\ArrayInput
@@ -30,12 +27,7 @@ public function testNotEmptyValidatorNotInjectedIfContinueIfEmptyIsTrue($value =
3027
parent::testNotEmptyValidatorNotInjectedIfContinueIfEmptyIsTrue([$value]);
3128
}
3229

33-
public function testValueIsNullByDefault()
34-
{
35-
$this->markTestSkipped('Test is not enabled in ArrayInputTest');
36-
}
37-
38-
public function testValueIsEmptyArrayByDefault()
30+
public function testDefaultGetValue()
3931
{
4032
$this->assertCount(0, $this->input->getValue());
4133
}
@@ -49,116 +41,6 @@ public function testSetValueWithInvalidInputTypeThrowsInvalidArgumentException()
4941
$this->input->setValue('bar');
5042
}
5143

52-
public function testValueMayBeInjected()
53-
{
54-
$this->input->setValue(['bar']);
55-
$this->assertEquals(['bar'], $this->input->getValue());
56-
}
57-
58-
public function testRetrievingValueFiltersTheValue()
59-
{
60-
$this->input->setValue(['bar']);
61-
$filter = new Filter\StringToUpper();
62-
$this->input->getFilterChain()->attach($filter);
63-
$this->assertEquals(['BAR'], $this->input->getValue());
64-
}
65-
66-
public function testCanRetrieveRawValue()
67-
{
68-
$this->input->setValue(['bar']);
69-
$filter = new Filter\StringToUpper();
70-
$this->input->getFilterChain()->attach($filter);
71-
$this->assertEquals(['bar'], $this->input->getRawValue());
72-
}
73-
74-
public function testValidationOperatesOnFilteredValue()
75-
{
76-
$this->input->setValue([' 123 ', ' 123']);
77-
$filter = new Filter\StringTrim();
78-
$this->input->getFilterChain()->attach($filter);
79-
$validator = new Validator\Digits();
80-
$this->input->getValidatorChain()->attach($validator);
81-
$this->assertTrue(
82-
$this->input->isValid(),
83-
'isValid() value not match. Detail . ' . json_encode($this->input->getMessages())
84-
);
85-
}
86-
87-
public function testSpecifyingMessagesToInputReturnsThoseOnFailedValidation()
88-
{
89-
$this->input->setValue(['bar']);
90-
$validator = new Validator\Digits();
91-
$this->input->getValidatorChain()->attach($validator);
92-
$this->input->setErrorMessage('Please enter only digits');
93-
$this->assertFalse($this->input->isValid());
94-
$messages = $this->input->getMessages();
95-
$this->assertArrayNotHasKey(Validator\Digits::NOT_DIGITS, $messages);
96-
$this->assertContains('Please enter only digits', $messages);
97-
}
98-
99-
public function testNotEmptyValidatorAddedWhenIsValidIsCalled()
100-
{
101-
$this->assertTrue($this->input->isRequired());
102-
$this->input->setValue(['bar', '']);
103-
$validatorChain = $this->input->getValidatorChain();
104-
$this->assertEquals(0, count($validatorChain->getValidators()));
105-
106-
$this->assertFalse($this->input->isValid());
107-
$messages = $this->input->getMessages();
108-
$this->assertArrayHasKey('isEmpty', $messages);
109-
$this->assertEquals(1, count($validatorChain->getValidators()));
110-
111-
// Assert that NotEmpty validator wasn't added again
112-
$this->assertFalse($this->input->isValid());
113-
$this->assertEquals(1, count($validatorChain->getValidators()));
114-
}
115-
116-
public function testRequiredNotEmptyValidatorNotAddedWhenOneExists()
117-
{
118-
$this->assertTrue($this->input->isRequired());
119-
$this->input->setValue(['bar', '']);
120-
121-
/** @var Validator\NotEmpty|MockObject $notEmptyMock */
122-
$notEmptyMock = $this->getMock(Validator\NotEmpty::class, ['isValid']);
123-
$notEmptyMock->expects($this->exactly(1))
124-
->method('isValid')
125-
->will($this->returnValue(false));
126-
127-
$validatorChain = $this->input->getValidatorChain();
128-
$validatorChain->prependValidator($notEmptyMock);
129-
$this->assertFalse($this->input->isValid());
130-
131-
$validators = $validatorChain->getValidators();
132-
$this->assertEquals(1, count($validators));
133-
$this->assertEquals($notEmptyMock, $validators[0]['instance']);
134-
}
135-
136-
public function testDoNotInjectNotEmptyValidatorIfAnywhereInChain()
137-
{
138-
$this->assertTrue($this->input->isRequired());
139-
$this->input->setValue(['bar', '']);
140-
141-
/** @var Validator\NotEmpty|MockObject $notEmptyMock */
142-
$notEmptyMock = $this->getMock(Validator\NotEmpty::class, ['isValid']);
143-
$notEmptyMock->expects($this->exactly(1))
144-
->method('isValid')
145-
->will($this->returnValue(false));
146-
147-
$validatorChain = $this->input->getValidatorChain();
148-
$validatorChain->attach(new Validator\Digits());
149-
$validatorChain->attach($notEmptyMock);
150-
$this->assertFalse($this->input->isValid());
151-
152-
$validators = $validatorChain->getValidators();
153-
$this->assertEquals(2, count($validators));
154-
$this->assertEquals($notEmptyMock, $validators[1]['instance']);
155-
}
156-
157-
public function testMerge($sourceRawValue = 'bazRawValue')
158-
{
159-
parent::testMerge([$sourceRawValue]);
160-
}
161-
16244
public function fallbackValueVsIsValidProvider()
16345
{
16446
$dataSets = parent::fallbackValueVsIsValidProvider();
@@ -190,4 +72,33 @@ public function mixedValueProvider()
19072

19173
return $dataSets;
19274
}
75+
76+
protected function createFilterChainMock($valueRaw = null, $valueFiltered = null)
77+
{
78+
// ArrayInput filters per each array value
79+
if (is_array($valueRaw)) {
80+
$valueRaw = current($valueRaw);
81+
}
82+
83+
if (is_array($valueFiltered)) {
84+
$valueFiltered = current($valueFiltered);
85+
}
86+
87+
return parent::createFilterChainMock($valueRaw, $valueFiltered);
88+
}
89+
90+
protected function createValidatorChainMock($isValid = null, $value = null, $context = null, $messages = [])
91+
{
92+
// ArrayInput validates per each array value
93+
if (is_array($value)) {
94+
$value = current($value);
95+
}
96+
97+
return parent::createValidatorChainMock($isValid, $value, $context, $messages);
98+
}
99+
100+
protected function getDummyValue($raw = true)
101+
{
102+
return [parent::getDummyValue($raw)];
103+
}
193104
}

test/FileInputTest.php

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,6 @@ public function setUp()
2929
$this->input->setAutoPrependUploadValidator(false);
3030
}
3131

32-
public function testValueMayBeInjected()
33-
{
34-
$value = ['tmp_name' => 'bar'];
35-
$this->input->setValue($value);
36-
$this->assertEquals($value, $this->input->getValue());
37-
}
38-
3932
public function testRetrievingValueFiltersTheValue()
4033
{
4134
$this->markTestSkipped('Test are not enabled in FileInputTest');
@@ -215,18 +208,6 @@ public function testCanValidateArrayOfMultiFileData()
215208
$this->assertFalse($this->input->isValid());
216209
}
217210

218-
public function testSpecifyingMessagesToInputReturnsThoseOnFailedValidation()
219-
{
220-
$this->input->setValue(['tmp_name' => 'bar']);
221-
$validator = new Validator\Digits();
222-
$this->input->getValidatorChain()->attach($validator);
223-
$this->input->setErrorMessage('Please enter only digits');
224-
$this->assertFalse($this->input->isValid());
225-
$messages = $this->input->getMessages();
226-
$this->assertArrayNotHasKey(Validator\Digits::NOT_DIGITS, $messages);
227-
$this->assertContains('Please enter only digits', $messages);
228-
}
229-
230211
public function testAutoPrependUploadValidatorIsOnByDefault()
231212
{
232213
$input = new FileInput('foo');
@@ -315,12 +296,12 @@ public function testValidationsRunWithoutFileArrayDueToAjaxPost()
315296
$this->assertEquals($uploadMock, $validators[0]['instance']);
316297
}
317298

318-
public function testNotEmptyValidatorAddedWhenIsValidIsCalled()
299+
public function testNotEmptyValidatorAddedWhenIsValidIsCalled($value = null)
319300
{
320301
$this->markTestSkipped('Test is not enabled in FileInputTest');
321302
}
322303

323-
public function testRequiredNotEmptyValidatorNotAddedWhenOneExists()
304+
public function testRequiredNotEmptyValidatorNotAddedWhenOneExists($value = null)
324305
{
325306
$this->markTestSkipped('Test is not enabled in FileInputTest');
326307
}
@@ -338,10 +319,7 @@ public function testFallbackValueVsIsValidRules(
338319

339320
public function testFallbackValueVsIsValidRulesWhenValueNotSet(
340321
$required = null,
341-
$fallbackValue = null,
342-
$originalValue = null,
343-
$isValid = null,
344-
$expectedValue = null
322+
$fallbackValue = null
345323
) {
346324
$this->markTestSkipped('Input::setFallbackValue is not implemented on FileInput');
347325
}
@@ -485,4 +463,9 @@ public function mixedValueProvider()
485463
],
486464
];
487465
}
466+
467+
protected function getDummyValue($raw = true)
468+
{
469+
return ['tmp_name' => 'bar'];
470+
}
488471
}

0 commit comments

Comments
 (0)