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

Commit

Permalink
Merge branch 'hotfix/64'
Browse files Browse the repository at this point in the history
Close #64
  • Loading branch information
weierophinney committed Sep 8, 2015
2 parents 1cd1aa7 + 9da818d commit a05fa0b
Show file tree
Hide file tree
Showing 7 changed files with 479 additions and 1,114 deletions.
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
"zendframework/zend-stdlib": "~2.5"
},
"require-dev": {
"zendframework/zend-i18n": "~2.5",
"zendframework/zend-servicemanager": "~2.5",
"fabpot/php-cs-fixer": "1.7.*",
"phpunit/PHPUnit": "^4.5"
Expand Down
180 changes: 40 additions & 140 deletions test/ArrayInputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@

namespace ZendTest\InputFilter;

use PHPUnit_Framework_MockObject_MockObject as MockObject;
use Zend\Filter;
use Zend\InputFilter\ArrayInput;
use Zend\InputFilter\Exception\InvalidArgumentException;
use Zend\Validator;

/**
* @covers Zend\InputFilter\ArrayInput
Expand All @@ -25,12 +22,7 @@ public function setUp()
$this->input = new ArrayInput('foo');
}

public function testValueIsNullByDefault()
{
$this->markTestSkipped('Test is not enabled in ArrayInputTest');
}

public function testValueIsEmptyArrayByDefault()
public function testDefaultGetValue()
{
$this->assertCount(0, $this->input->getValue());
}
Expand All @@ -44,137 +36,6 @@ public function testSetValueWithInvalidInputTypeThrowsInvalidArgumentException()
$this->input->setValue('bar');
}

public function testValueMayBeInjected()
{
$this->input->setValue(['bar']);
$this->assertEquals(['bar'], $this->input->getValue());
}

public function testRetrievingValueFiltersTheValue()
{
$this->input->setValue(['bar']);
$filter = new Filter\StringToUpper();
$this->input->getFilterChain()->attach($filter);
$this->assertEquals(['BAR'], $this->input->getValue());
}

public function testCanRetrieveRawValue()
{
$this->input->setValue(['bar']);
$filter = new Filter\StringToUpper();
$this->input->getFilterChain()->attach($filter);
$this->assertEquals(['bar'], $this->input->getRawValue());
}

public function testValidationOperatesOnFilteredValue()
{
$this->input->setValue([' 123 ', ' 123']);
$filter = new Filter\StringTrim();
$this->input->getFilterChain()->attach($filter);
$validator = new Validator\Digits();
$this->input->getValidatorChain()->attach($validator);
$this->assertTrue(
$this->input->isValid(),
'isValid() value not match. Detail . ' . json_encode($this->input->getMessages())
);
}

public function testSpecifyingMessagesToInputReturnsThoseOnFailedValidation()
{
$this->input->setValue(['bar']);
$validator = new Validator\Digits();
$this->input->getValidatorChain()->attach($validator);
$this->input->setErrorMessage('Please enter only digits');
$this->assertFalse($this->input->isValid());
$messages = $this->input->getMessages();
$this->assertArrayNotHasKey(Validator\Digits::NOT_DIGITS, $messages);
$this->assertContains('Please enter only digits', $messages);
}

public function testNotEmptyValidatorAddedWhenIsValidIsCalled()
{
$this->assertTrue($this->input->isRequired());
$this->input->setValue(['bar', '']);
$validatorChain = $this->input->getValidatorChain();
$this->assertEquals(0, count($validatorChain->getValidators()));

$this->assertFalse($this->input->isValid());
$messages = $this->input->getMessages();
$this->assertArrayHasKey('isEmpty', $messages);
$this->assertEquals(1, count($validatorChain->getValidators()));

// Assert that NotEmpty validator wasn't added again
$this->assertFalse($this->input->isValid());
$this->assertEquals(1, count($validatorChain->getValidators()));
}

public function testRequiredNotEmptyValidatorNotAddedWhenOneExists()
{
$this->assertTrue($this->input->isRequired());
$this->input->setValue(['bar', '']);

/** @var Validator\NotEmpty|MockObject $notEmptyMock */
$notEmptyMock = $this->getMock(Validator\NotEmpty::class, ['isValid']);
$notEmptyMock->expects($this->exactly(1))
->method('isValid')
->will($this->returnValue(false));

$validatorChain = $this->input->getValidatorChain();
$validatorChain->prependValidator($notEmptyMock);
$this->assertFalse($this->input->isValid());

$validators = $validatorChain->getValidators();
$this->assertEquals(1, count($validators));
$this->assertEquals($notEmptyMock, $validators[0]['instance']);
}

public function testDoNotInjectNotEmptyValidatorIfAnywhereInChain()
{
$this->assertTrue($this->input->isRequired());
$this->input->setValue(['bar', '']);

/** @var Validator\NotEmpty|MockObject $notEmptyMock */
$notEmptyMock = $this->getMock(Validator\NotEmpty::class, ['isValid']);
$notEmptyMock->expects($this->exactly(1))
->method('isValid')
->will($this->returnValue(false));

$validatorChain = $this->input->getValidatorChain();
$validatorChain->attach(new Validator\Digits());
$validatorChain->attach($notEmptyMock);
$this->assertFalse($this->input->isValid());

$validators = $validatorChain->getValidators();
$this->assertEquals(2, count($validators));
$this->assertEquals($notEmptyMock, $validators[1]['instance']);
}

public function testNotAllowEmptyWithFilterConvertsNonemptyToEmptyIsNotValid()
{
$this->input->setValue(['nonempty'])
->getFilterChain()->attach(new Filter\Callback(function () {
return '';
}));
$this->assertFalse($this->input->isValid());
}

public function testNotAllowEmptyWithFilterConvertsEmptyToNonEmptyIsValid()
{
$this->input->setValue([''])
->getFilterChain()->attach(new Filter\Callback(function () {
return 'nonempty';
}));
$this->assertTrue(
$this->input->isValid(),
'isValid() value not match. Detail . ' . json_encode($this->input->getMessages())
);
}

public function testMerge($sourceRawValue = 'bazRawValue')
{
parent::testMerge([$sourceRawValue]);
}

public function fallbackValueVsIsValidProvider()
{
$dataSets = parent::fallbackValueVsIsValidProvider();
Expand Down Expand Up @@ -206,4 +67,43 @@ public function mixedValueProvider()

return $dataSets;
}

protected function createFilterChainMock($valueRaw = null, $valueFiltered = null)
{
// ArrayInput filters per each array value
if (is_array($valueRaw)) {
$valueRaw = current($valueRaw);
}

if (is_array($valueFiltered)) {
$valueFiltered = current($valueFiltered);
}

return parent::createFilterChainMock($valueRaw, $valueFiltered);
}

protected function createValidatorChainMock($isValid = null, $value = null, $context = null, $messages = [])
{
// ArrayInput validates per each array value
if (is_array($value)) {
$value = current($value);
}

return parent::createValidatorChainMock($isValid, $value, $context, $messages);
}

protected function createNonEmptyValidatorMock($isValid, $value, $context = null)
{
// ArrayInput validates per each array value
if (is_array($value)) {
$value = current($value);
}

return parent::createNonEmptyValidatorMock($isValid, $value, $context);
}

protected function getDummyValue($raw = true)
{
return [parent::getDummyValue($raw)];
}
}
Loading

0 comments on commit a05fa0b

Please sign in to comment.