1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ddeboer\DataImport\Tests\Step; |
4
|
|
|
|
5
|
|
|
use Ddeboer\DataImport\Step\ValidatorStep; |
6
|
|
|
use Symfony\Component\Validator\Constraints; |
7
|
|
|
use Symfony\Component\Validator\ConstraintViolation; |
8
|
|
|
use Symfony\Component\Validator\ConstraintViolationList; |
9
|
|
|
|
10
|
|
|
class ValidatorStepTest extends \PHPUnit_Framework_TestCase |
11
|
|
|
{ |
12
|
|
|
protected function setUp() |
13
|
|
|
{ |
14
|
|
|
$this->validator = $this->getMock('Symfony\Component\Validator\Validator\ValidatorInterface'); |
15
|
|
|
|
16
|
|
|
$this->filter = new ValidatorStep($this->validator); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function testProcess() |
20
|
|
|
{ |
21
|
|
|
$data = ['title' => null]; |
22
|
|
|
|
23
|
|
|
$this->filter->add('title', $constraint = new Constraints\NotNull()); |
24
|
|
|
|
25
|
|
|
$list = new ConstraintViolationList(); |
26
|
|
|
$list->add($this->buildConstraintViolation()); |
27
|
|
|
|
28
|
|
|
$this->validator->expects($this->once()) |
29
|
|
|
->method('validate') |
30
|
|
|
->willReturn($list); |
31
|
|
|
|
32
|
|
|
$this->assertFalse($this->filter->process($data)); |
33
|
|
|
|
34
|
|
|
$this->assertEquals([1 => $list], $this->filter->getViolations()); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @expectedException Ddeboer\DataImport\Exception\ValidationException |
39
|
|
|
*/ |
40
|
|
|
public function testProcessWithExceptions() |
41
|
|
|
{ |
42
|
|
|
$data = ['title' => null]; |
43
|
|
|
|
44
|
|
|
$this->filter->add('title', $constraint = new Constraints\NotNull()); |
45
|
|
|
$this->filter->throwExceptions(); |
46
|
|
|
|
47
|
|
|
$list = new ConstraintViolationList(); |
48
|
|
|
$list->add($this->buildConstraintViolation()); |
49
|
|
|
|
50
|
|
|
$this->validator->expects($this->once()) |
51
|
|
|
->method('validate') |
52
|
|
|
->willReturn($list); |
53
|
|
|
|
54
|
|
|
$this->assertFalse($this->filter->process($data)); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function testPriority() |
58
|
|
|
{ |
59
|
|
|
$this->assertEquals(128, $this->filter->getPriority()); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
private function buildConstraintViolation() |
63
|
|
|
{ |
64
|
|
|
return $this->getMockBuilder('Symfony\Component\Validator\ConstraintViolation') |
65
|
|
|
->disableOriginalConstructor() |
66
|
|
|
->getMock(); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|