Skip to content

Commit

Permalink
Adds some test cases concerning validation groups and collections
Browse files Browse the repository at this point in the history
Signed-off-by: George Steel <[email protected]>
  • Loading branch information
gsteel committed Jul 6, 2022
1 parent 8c663d3 commit c716cf8
Show file tree
Hide file tree
Showing 2 changed files with 278 additions and 0 deletions.
200 changes: 200 additions & 0 deletions test/ValidationGroup/InputFilterCollectionsValidationGroupTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
<?php

declare(strict_types=1);

namespace LaminasTest\InputFilter\ValidationGroup;

use Laminas\InputFilter\CollectionInputFilter;
use Laminas\InputFilter\Input;
use Laminas\InputFilter\InputFilter;
use PHPUnit\Framework\TestCase;

final class InputFilterCollectionsValidationGroupTest extends TestCase
{
private InputFilter $inputFilter;

protected function setUp(): void
{
parent::setUp();

$collection = new CollectionInputFilter();
$collection->setIsRequired(true);

$first = new Input('first');
$first->setRequired(true);
$second = new Input('second');
$second->setRequired(true);

$nestedFilter = new InputFilter();
$nestedFilter->add($first);
$nestedFilter->add($second);
$collection->setInputFilter($nestedFilter);

$this->inputFilter = new InputFilter();
$this->inputFilter->add($collection, 'stuff');
}

/** @return array<string, array{0: int|null}> */
public function collectionCountProvider(): array
{
return [
'Collection Count: None' => [null],
'Collection Count: One' => [1],
'Collection Count: Two' => [2],
'Collection Count: Three' => [3],
'Collection Count: Four' => [4],
];
}

private function setCollectionCount(?int $count): void
{
if ($count === null) {
return;
}

$collection = $this->inputFilter->get('stuff');
self::assertInstanceOf(CollectionInputFilter::class, $collection);
$collection->setCount($count);
}

/** @dataProvider collectionCountProvider */
public function testIncompleteDataFailsValidation(?int $count): void
{
$this->setCollectionCount($count);
$this->inputFilter->setData([
'stuff' => [
['first' => 'Foo'],
],
]);
self::assertFalse($this->inputFilter->isValid());
}

/** @dataProvider collectionCountProvider */
public function testCompleteDataPassesValidation(?int $count): void
{
$this->setCollectionCount($count);
$this->inputFilter->setData([
'stuff' => [
['first' => 'Foo', 'second' => 'Bar'],
['first' => 'Foo', 'second' => 'Bar'],
['first' => 'Foo', 'second' => 'Bar'],
['first' => 'Foo', 'second' => 'Bar'],
],
]);

self::assertTrue($this->inputFilter->isValid());
}

/** @dataProvider collectionCountProvider */
public function testValidationFailsForCollectionItemValidity(?int $count): void
{
$this->setCollectionCount($count);
$this->inputFilter->setData([
'stuff' => [
['first' => 'Foo', 'second' => 'Bar'],
['first' => '', 'second' => 'Bar'],
['first' => 'Foo', 'second' => ''],
['first' => '', 'second' => ''],
],
]);

self::assertFalse($this->inputFilter->isValid());
}

/** @dataProvider collectionCountProvider */
public function testValidationGroupWithCollectionInputFilter(?int $count): void
{
$this->setCollectionCount($count);
$collection = $this->inputFilter->get('stuff');
self::assertInstanceOf(CollectionInputFilter::class, $collection);
$collection->getInputFilter()->setValidationGroup('first');

$this->inputFilter->setData([
'stuff' => [
['first' => 'Foo'],
['first' => 'Foo'],
['first' => 'Foo'],
['first' => 'Foo'],
],
]);

self::assertTrue($this->inputFilter->isValid());
}

/** @dataProvider collectionCountProvider */
public function testValidationGroupViaCollection(?int $count): void
{
$this->setCollectionCount($count);
$collection = $this->inputFilter->get('stuff');
self::assertInstanceOf(CollectionInputFilter::class, $collection);
$collection->setValidationGroup([
0 => 'first',
1 => 'second',
2 => 'first',
3 => 'first',
]);

$this->inputFilter->setData([
'stuff' => [
['first' => 'Foo'],
['second' => 'Foo'],
['first' => 'Foo'],
['first' => 'Foo'],
],
]);

self::assertTrue($this->inputFilter->isValid());
}

/**
* This test fails because of an undefined offset - the validation group must be set for elements 0 through 3
*
* @dataProvider collectionCountProvider
*/
public function testValidationGroupViaCollectionMustSpecifyAllKeys(?int $count): void
{
$this->setCollectionCount($count);
$collection = $this->inputFilter->get('stuff');
self::assertInstanceOf(CollectionInputFilter::class, $collection);

$collection->setValidationGroup([
0 => 'first',
]);

$this->inputFilter->setData([
'stuff' => [
['first' => 'Foo'],
['first' => 'Foo'],
['first' => 'Foo'],
['first' => 'Foo'],
],
]);

self::assertTrue($this->inputFilter->isValid());
}

/** @dataProvider collectionCountProvider */
public function testValidationGroupViaTopLevelInputFilter(?int $count): void
{
$this->setCollectionCount($count);
$this->inputFilter->setValidationGroup([
'stuff' => [
0 => 'first',
1 => 'second',
2 => 'first',
3 => 'first',
],
]);

$this->inputFilter->setData([
'stuff' => [
['first' => 'Foo'],
['second' => 'Foo'],
['first' => 'Foo'],
['first' => 'Foo'],
],
]);

self::assertTrue($this->inputFilter->isValid());
}
}
78 changes: 78 additions & 0 deletions test/ValidationGroup/InputFilterStringValidationGroupTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

declare(strict_types=1);

namespace LaminasTest\InputFilter\ValidationGroup;

use Laminas\InputFilter\Exception\InvalidArgumentException;
use Laminas\InputFilter\Input;
use Laminas\InputFilter\InputFilter;
use Laminas\Validator\StringLength;
use PHPUnit\Framework\TestCase;

final class InputFilterStringValidationGroupTest extends TestCase
{
private InputFilter $inputFilter;

protected function setUp(): void
{
parent::setUp();
$first = new Input('first');
$first->setRequired(true);
$first->getValidatorChain()->attach(new StringLength(['min' => 5]));
$second = new Input('second');
$second->setRequired(true);
$second->getValidatorChain()->attach(new StringLength(['min' => 5]));
$third = new Input('third');
$third->setRequired(true);
$third->getValidatorChain()->attach(new StringLength(['min' => 5]));

$this->inputFilter = new InputFilter();
$this->inputFilter->add($first);
$this->inputFilter->add($second);
$this->inputFilter->add($third);
}

public function testValidationFailsForIncompleteInput(): void
{
$this->inputFilter->setData(['first' => 'Freddy']);
self::assertFalse($this->inputFilter->isValid());
}

public function testValidationSucceedsForCompleteInput(): void
{
$this->inputFilter->setData(['first' => 'Freddy', 'second' => 'Fruit Bat', 'third' => 'Muppet']);
self::assertTrue($this->inputFilter->isValid());
}

public function testValidationSucceedsForIncompleteInputWhenValidationGroupIsProvided(): void
{
$this->inputFilter->setValidationGroup('first');
$this->inputFilter->setData(['first' => 'Freddy']);

self::assertTrue($this->inputFilter->isValid());
}

public function testThatValidationGroupIsVariadic(): void
{
$this->inputFilter->setValidationGroup('first', 'second');
$this->inputFilter->setData(['first' => 'Freddy', 'second' => 'Fruit Bat']);

self::assertTrue($this->inputFilter->isValid());
}

public function testThatValidationGroupAcceptsListOfInputNames(): void
{
$this->inputFilter->setValidationGroup(['first', 'second']);
$this->inputFilter->setData(['first' => 'Freddy', 'second' => 'Fruit Bat']);

self::assertTrue($this->inputFilter->isValid());
}

public function testValidationGroupWithUnknownInput(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('"doughnuts" was not found');
$this->inputFilter->setValidationGroup(['doughnuts']);
}
}

0 comments on commit c716cf8

Please sign in to comment.