Completed
Pull Request — master (#289)
by Hugo
04:55
created

ValidatorStepTest::buildConstraintViolation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
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\ConstraintViolation;
8
use Symfony\Component\Validator\ConstraintViolationList;
9
use Symfony\Component\Validator\Validator\ValidatorInterface;
10
11
class ValidatorStepTest extends \PHPUnit_Framework_TestCase
12
{
13
    protected function setUp()
14
    {
15
        $this->validator = $this->getMock(ValidatorInterface::class);
16
17
        $this->filter = new ValidatorStep($this->validator);
18
    }
19
20
    public function testProcess()
21
    {
22
        $data = ['title' => null];
23
24
        $this->filter->add('title', $constraint = new Constraints\NotNull());
25
26
        $constraintViolations = new ConstraintViolationList();
27
        $constraintViolations->add($this->buildConstraintViolation());
28
29
        $this->validator->expects($this->once())
30
                        ->method('validateValue')
31
                        ->willReturn($constraintViolations);
32
33
        $this->assertFalse($this->filter->process($data));
34
35
        $this->assertEquals([1 => $list], $this->filter->getViolations());
0 ignored issues
show
Bug introduced by
The variable $list does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
36
    }
37
38
    /**
39
     * @expectedException Ddeboer\DataImport\Exception\ValidationException
40
     */
41
    public function testProcessWithExceptions()
42
    {
43
        $data = ['title' => null];
44
45
        $this->filter->add('title', $constraint = new Constraints\NotNull());
46
        $this->filter->throwExceptions();
47
48
        $constraintViolations = new ConstraintViolationList();
49
        $constraintViolations->add($this->buildConstraintViolation());
50
51
        $this->validator->expects($this->once())
52
                        ->method('validateValue')
53
                        ->willReturn($constraintViolations);
54
55
        $this->assertFalse($this->filter->process($data));
56
    }
57
58
    public function testPriority()
59
    {
60
        $this->assertEquals(128, $this->filter->getPriority());
61
    }
62
63
    private function buildConstraintViolation()
64
    {
65
        return $this->getMockBuilder(ConstraintViolation::class)
66
                    ->disableOriginalConstructor()
67
                    ->getMock();
68
    }
69
}
70