Completed
Push — master ( d2fa3f...fff6b7 )
by Markus
8s
created

testProcessWithAllowedExtraFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 19
rs 9.4285
cc 1
eloc 11
nc 1
nop 0
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\ConstraintViolationList;
8
use Symfony\Component\Validator\Validator\ValidatorInterface;
9
10
class ValidatorStepTest extends \PHPUnit_Framework_TestCase
11
{
12
    /** @var ValidatorInterface */
13
    private $validator;
14
15
    /** @var ValidatorStep */
16
    private $filter;
17
18
    protected function setUp()
19
    {
20
        $this->validator = $this->getMock('Symfony\Component\Validator\Validator\ValidatorInterface');
21
        $this->filter = new ValidatorStep($this->validator);
22
    }
23
24
    public function testProcess()
25
    {
26
        $data = ['title' => null];
27
28
        $this->filter->add('title', $constraint = new Constraints\NotNull());
29
30
        $list = new ConstraintViolationList();
31
        $list->add($this->buildConstraintViolation());
32
33
        $this->validator->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Symfony\Component...tor\ValidatorInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
34
                        ->method('validate')
35
                        ->willReturn($list);
36
37
        $this->assertFalse($this->filter->process($data));
38
39
        $this->assertEquals([1 => $list], $this->filter->getViolations());
40
    }
41
42
    /**
43
     * @expectedException \Ddeboer\DataImport\Exception\ValidationException
44
     */
45
    public function testProcessWithExceptions()
46
    {
47
        $data = ['title' => null];
48
49
        $this->filter->add('title', $constraint = new Constraints\NotNull());
50
        $this->filter->throwExceptions();
51
52
        $list = new ConstraintViolationList();
53
        $list->add($this->buildConstraintViolation());
54
55
        $this->validator->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Symfony\Component...tor\ValidatorInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
56
                        ->method('validate')
57
                        ->willReturn($list);
58
59
        $this->assertFalse($this->filter->process($data));
60
    }
61
62
    public function testProcessWithAllowedExtraFields()
63
    {
64
        $this->filter->addOption('allowExtraFields', true);
65
66
        $data = ['title' => null, 'telephone' => '0155/555-555'];
67
68
        $this->filter->add('title', $constraint = new Constraints\NotNull());
69
70
        $list = new ConstraintViolationList();
71
        $list->add($this->buildConstraintViolation());
72
73
        $this->validator->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Symfony\Component...tor\ValidatorInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
74
            ->method('validate')
75
            ->willReturn($list);
76
77
        $this->assertFalse($this->filter->process($data));
78
79
        $this->assertEquals([1 => $list], $this->filter->getViolations());
80
    }
81
82
    public function testPriority()
83
    {
84
        $this->assertEquals(128, $this->filter->getPriority());
85
    }
86
87
    private function buildConstraintViolation()
88
    {
89
        return $this->getMockBuilder('Symfony\Component\Validator\ConstraintViolation')
90
                    ->disableOriginalConstructor()
91
                    ->getMock();
92
    }
93
}
94