Skip to content

Commit

Permalink
Merge 75a0d86 into d2fa3f6
Browse files Browse the repository at this point in the history
  • Loading branch information
IvBre committed Nov 8, 2016
2 parents d2fa3f6 + 75a0d86 commit 1e4da18
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
24 changes: 22 additions & 2 deletions src/Step/ValidatorStep.php
Expand Up @@ -37,6 +37,12 @@ class ValidatorStep implements PriorityStep
*/
private $validator;

/**
* Possible options to set for Constraints\Collection
* @var array
*/
private $possibleOptions = [ 'groups', 'allowExtraFields', 'allowMissingFields', 'extraFieldsMessage', 'missingFieldsMessage' ];

/**
* @param ValidatorInterface $validator
*/
Expand All @@ -54,10 +60,10 @@ public function __construct(ValidatorInterface $validator)
public function add($field, Constraint $constraint)
{
if (!isset($this->constraints[$field])) {
$this->constraints[$field] = [];
$this->constraints['fields'][$field] = [];
}

$this->constraints[$field][] = $constraint;
$this->constraints['fields'][$field][] = $constraint;

return $this;
}
Expand All @@ -78,6 +84,20 @@ public function getViolations()
return $this->violations;
}

/**
* Add additional options for the constraints
* @param string $option
* @param $optionValue
*/
public function addOption($option, $optionValue)
{
if (!isset($this->possibleOptions[$option])) {
return;
}

$this->constraints[$option] = $optionValue;
}

/**
* {@inheritdoc}
*/
Expand Down
31 changes: 28 additions & 3 deletions tests/Step/ValidatorStepTest.php
Expand Up @@ -4,15 +4,20 @@

use Ddeboer\DataImport\Step\ValidatorStep;
use Symfony\Component\Validator\Constraints;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\ConstraintViolationList;
use Symfony\Component\Validator\Validator\ValidatorInterface;

class ValidatorStepTest extends \PHPUnit_Framework_TestCase
{
/** @var ValidatorInterface */
private $validator;

/** @var ValidatorStep */
private $filter;

protected function setUp()
{
$this->validator = $this->getMock('Symfony\Component\Validator\Validator\ValidatorInterface');

$this->filter = new ValidatorStep($this->validator);
}

Expand All @@ -35,7 +40,7 @@ public function testProcess()
}

/**
* @expectedException Ddeboer\DataImport\Exception\ValidationException
* @expectedException \Ddeboer\DataImport\Exception\ValidationException
*/
public function testProcessWithExceptions()
{
Expand All @@ -54,6 +59,26 @@ public function testProcessWithExceptions()
$this->assertFalse($this->filter->process($data));
}

public function testProcessWithAllowedExtraFields()
{
$this->filter->addOption('allowExtraFields', true);

$data = ['title' => null, 'telephone' => '0155/555-555'];

$this->filter->add('title', $constraint = new Constraints\NotNull());

$list = new ConstraintViolationList();
$list->add($this->buildConstraintViolation());

$this->validator->expects($this->once())
->method('validate')
->willReturn($list);

$this->assertFalse($this->filter->process($data));

$this->assertEquals([1 => $list], $this->filter->getViolations());
}

public function testPriority()
{
$this->assertEquals(128, $this->filter->getPriority());
Expand Down

0 comments on commit 1e4da18

Please sign in to comment.