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

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
davidwindell committed Mar 2, 2013
1 parent 5186730 commit 8ee8264
Show file tree
Hide file tree
Showing 5 changed files with 392 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/CollectionInputFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class CollectionInputFilter extends InputFilter
/*
* @var int
*/
protected $count;
protected $count = null;

/*
* @var array
Expand Down
3 changes: 3 additions & 0 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,9 @@ public function createInputFilter($inputFilterSpecification)
if (isset($inputFilterSpecification['inputfilter'])) {
$inputFilter->setInputFilter($inputFilterSpecification['inputfilter']);
}
if (isset($inputFilterSpecification['count'])) {
$inputFilter->setCount($inputFilterSpecification['count']);
}
return $inputFilter;
}

Expand Down
16 changes: 16 additions & 0 deletions test/BaseInputFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,22 @@ public function testValidateUseExplodeAndInstanceOf()

$filter->setData($data);
$this->assertTrue($filter->isValid());
}

public function testGetInputs()
{
$filter = new InputFilter();

$foo = new Input('foo');
$bar = new Input('bar');

$filter->add($foo);
$filter->add($bar);

$filters = $filter->getInputs();

$this->assertCount(2, $filters);
$this->assertEquals('foo', $filters['foo']->getName());
$this->assertEquals('bar', $filters['bar']->getName());
}
}
357 changes: 357 additions & 0 deletions test/CollectionInputFilterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,357 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendTest\InputFilter;

use PHPUnit_Framework_TestCase as TestCase;
use Zend\InputFilter\BaseInputFilter;
use Zend\InputFilter\CollectionInputFilter;
use Zend\InputFilter\Input;
use Zend\Validator;

class CollectionInputFilterTest extends TestCase
{
/**
* @var \Zend\InputFilter\CollectionInputFilter
*/
protected $filter;

public function setUp()
{
$this->filter = new CollectionInputFilter();
}

public function getBaseInputFilter()
{
$filter = new BaseInputFilter();

$foo = new Input();
$foo->getFilterChain()->attachByName('stringtrim')
->attachByName('alpha');
$foo->getValidatorChain()->attach(new Validator\StringLength(3, 6));

$bar = new Input();
$bar->getFilterChain()->attachByName('stringtrim');
$bar->getValidatorChain()->attach(new Validator\Digits());

$baz = new Input();
$baz->setRequired(false);
$baz->getFilterChain()->attachByName('stringtrim');
$baz->getValidatorChain()->attach(new Validator\StringLength(1, 6));

$filter->add($foo, 'foo')
->add($bar, 'bar')
->add($baz, 'baz')
->add($this->getChildInputFilter(), 'nest');

return $filter;
}

public function getChildInputFilter()
{
$filter = new BaseInputFilter();

$foo = new Input();
$foo->getFilterChain()->attachByName('stringtrim')
->attachByName('alpha');
$foo->getValidatorChain()->attach(new Validator\StringLength(3, 6));

$bar = new Input();
$bar->getFilterChain()->attachByName('stringtrim');
$bar->getValidatorChain()->attach(new Validator\Digits());

$baz = new Input();
$baz->setRequired(false);
$baz->getFilterChain()->attachByName('stringtrim');
$baz->getValidatorChain()->attach(new Validator\StringLength(1, 6));

$filter->add($foo, 'foo')
->add($bar, 'bar')
->add($baz, 'baz');
return $filter;
}

public function getValidCollectionData()
{
return array(
array(
'foo' => ' bazbat ',
'bar' => '12345',
'baz' => '',
'nest' => array(
'foo' => ' bazbat ',
'bar' => '12345',
'baz' => '',
),
),
array(
'foo' => ' batbaz ',
'bar' => '54321',
'baz' => '',
'nest' => array(
'foo' => ' batbaz ',
'bar' => '54321',
'baz' => '',
),
)
);
}

public function testSetInputFilter()
{
$this->filter->setInputFilter(new BaseInputFilter());
$this->assertInstanceOf('Zend\InputFilter\BaseInputFilter', $this->filter->getInputFilter());
}

public function testInputFilterInputsAppliedToCollection()
{
$this->filter->setInputFilter($this->getBaseInputFilter());

$this->assertCount(4, $this->filter->getInputs());
}

public function testGetDefaultInputFilter()
{
$this->assertInstanceOf('Zend\InputFilter\BaseInputFilter', $this->filter->getInputFilter());
}

public function testSetCount()
{
$this->filter->setCount(5);
$this->assertEquals(5, $this->filter->getCount());
}

public function testSetCountBelowZero()
{
$this->filter->setCount(-1);
$this->assertEquals(0, $this->filter->getCount());
}

public function testGetCountUsesCountOfCollectionDataWhenNotSet()
{
$collectionData = array(
array('foo' => 'bar'),
array('foo' => 'baz')
);

$this->filter->setData($collectionData);
$this->assertEquals(2, $this->filter->getCount());
}

public function testGetCountUsesSpecifiedCount()
{
$collectionData = array(
array('foo' => 'bar'),
array('foo' => 'baz')
);

$this->filter->setCount(3);
$this->filter->setData($collectionData);
$this->assertEquals(3, $this->filter->getCount());
}

public function testCanValidateValidData()
{
$this->filter->setInputFilter($this->getBaseInputFilter());
$this->filter->setData($this->getValidCollectionData());
$this->assertTrue($this->filter->isValid());
}

public function testInvalidDataReturnsFalse()
{
$invalidCollectionData = array(
array(
'foo' => ' bazbatlong ',
'bar' => '12345',
'baz' => '',
),
array(
'foo' => ' bazbat ',
'bar' => '12345',
'baz' => '',
)
);

$this->filter->setInputFilter($this->getBaseInputFilter());
$this->filter->setData($invalidCollectionData);
$this->assertFalse($this->filter->isValid());
}

public function testDataLessThanCountIsInvalid()
{
$invalidCollectionData = array(
array(
'foo' => ' bazbat ',
'bar' => '12345',
'baz' => '',
'nest' => array(
'foo' => ' bazbat ',
'bar' => '12345',
'baz' => '',
),
),
);

$this->filter->setCount(2);
$this->filter->setInputFilter($this->getBaseInputFilter());
$this->filter->setData($invalidCollectionData);
$this->assertFalse($this->filter->isValid());
}

public function testGetValues()
{
$expectedData = array(
array(
'foo' => 'bazbat',
'bar' => '12345',
'baz' => '',
'nest' => array(
'foo' => 'bazbat',
'bar' => '12345',
'baz' => '',
),
),
array(
'foo' => 'batbaz',
'bar' => '54321',
'baz' => '',
'nest' => array(
'foo' => 'batbaz',
'bar' => '54321',
'baz' => '',
),
)
);

$this->filter->setInputFilter($this->getBaseInputFilter());
$this->filter->setData($this->getValidCollectionData());

$this->assertTrue($this->filter->isValid());
$this->assertEquals($expectedData, $this->filter->getValues());

$this->assertCount(2, $this->filter->getValidInput());
foreach ($this->filter->getValidInput() as $validInputs) {
$this->assertCount(4, $validInputs);
}
}

public function testGetRawValues()
{
$expectedData = array(
array(
'foo' => ' bazbat ',
'bar' => '12345',
'baz' => '',
'nest' => array(
'foo' => ' bazbat ',
'bar' => '12345',
'baz' => '',
),
),
array(
'foo' => ' batbaz ',
'bar' => '54321',
'baz' => '',
'nest' => array(
'foo' => ' batbaz ',
'bar' => '54321',
'baz' => '',
),
)
);

$this->filter->setInputFilter($this->getBaseInputFilter());
$this->filter->setData($this->getValidCollectionData());

$this->assertTrue($this->filter->isValid());
$this->assertEquals($expectedData, $this->filter->getRawValues());
}

public function testGetMessagesForInvalidInputs()
{
$invalidCollectionData = array(
array(
'foo' => ' bazbattoolong ',
'bar' => '12345',
'baz' => '',
'nest' => array(
'foo' => ' bazbat ',
'bar' => '12345',
'baz' => '',
),
),
array(
'foo' => ' bazbat ',
'bar' => 'notstring',
'baz' => '',
'nest' => array(
'foo' => ' bazbat ',
'bar' => '12345',
'baz' => '',
),
),
);

$this->filter->setInputFilter($this->getBaseInputFilter());
$this->filter->setData($invalidCollectionData);

$this->assertFalse($this->filter->isValid());

$this->assertCount(2, $this->filter->getInvalidInput());
foreach ($this->filter->getInvalidInput() as $invalidInputs) {
$this->assertCount(1, $invalidInputs);
}

$messages = $this->filter->getMessages();

$this->assertCount(2, $messages);
$this->assertArrayHasKey('foo', $messages[0]);
$this->assertArrayHasKey('bar', $messages[1]);
}

public function testSetValidationGroupUsingFormStyle()
{
// forms set an array of identical validation groups for each set of data
$formValidationGroup = array(
array(
'foo',
'bar',
),
array(
'foo',
'bar',
),
array(
'foo',
'bar',
)
);

$data = array(
array(
'foo' => ' bazbat ',
'bar' => '12345'
),
array(
'foo' => ' batbaz ',
'bar' => '54321'
),
array(
'foo' => ' batbaz ',
'bar' => '54321'
)
);

$this->filter->setInputFilter($this->getBaseInputFilter());
$this->filter->setData($data);
$this->filter->setValidationGroup($formValidationGroup);

$this->assertTrue($this->filter->isValid());
}
}
Loading

0 comments on commit 8ee8264

Please sign in to comment.