|
1
|
|
|
<?php |
|
2
|
|
|
namespace Mathielen\ImportEngine\Validation; |
|
3
|
|
|
|
|
4
|
|
|
use Symfony\Component\Validator\Constraints\Email; |
|
5
|
|
|
use Mathielen\DataImport\Filter\ValidatorFilter; |
|
6
|
|
|
use Symfony\Component\Validator\Constraints\NotNull; |
|
7
|
|
|
|
|
8
|
|
|
class ValidatorValidationTest extends \PHPUnit_Framework_TestCase |
|
9
|
|
|
{ |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @var ValidatorValidation |
|
13
|
|
|
*/ |
|
14
|
|
|
private $validatorValidation; |
|
15
|
|
|
|
|
16
|
|
|
protected function setUp() |
|
17
|
|
|
{ |
|
18
|
|
|
$this->validatorMock = $this->createMock('Symfony\Component\Validator\Validator\ValidatorInterface'); |
|
19
|
|
|
|
|
20
|
|
|
$this->validatorValidation = ValidatorValidation::build($this->validatorMock); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function testApply() |
|
24
|
|
|
{ |
|
25
|
|
|
$this->validatorValidation->addSourceConstraint('sourceField', new Email()); |
|
26
|
|
|
$this->validatorValidation->addTargetConstraint('targetField', new NotNull()); |
|
27
|
|
|
|
|
28
|
|
|
$expectedSourceFilter = new ValidatorFilter($this->validatorMock); |
|
29
|
|
|
$expectedSourceFilter->setAllowExtraFields(true); |
|
30
|
|
|
$expectedSourceFilter->add('sourceField', new Email()); |
|
31
|
|
|
|
|
32
|
|
|
$expectedTargetFilter = new ValidatorFilter($this->validatorMock); |
|
33
|
|
|
$expectedTargetFilter->setAllowExtraFields(true); |
|
34
|
|
|
$expectedTargetFilter->add('targetField', new NotNull()); |
|
35
|
|
|
|
|
36
|
|
|
$workflow = $this->getMockBuilder('Ddeboer\DataImport\Workflow')->disableOriginalConstructor()->getMock(); |
|
37
|
|
|
$workflow |
|
38
|
|
|
->expects($this->once()) |
|
39
|
|
|
->method('addFilter') |
|
40
|
|
|
->with($expectedSourceFilter); |
|
41
|
|
|
$workflow |
|
42
|
|
|
->expects($this->once()) |
|
43
|
|
|
->method('addFilterAfterConversion') |
|
44
|
|
|
->with($expectedTargetFilter); |
|
45
|
|
|
|
|
46
|
|
|
$this->validatorValidation->apply($workflow); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function testGetViolations() |
|
50
|
|
|
{ |
|
51
|
|
|
$sourceFilterMock = $this->getMockBuilder('Mathielen\DataImport\Filter\ValidatorFilter')->disableOriginalConstructor()->getMock(); |
|
52
|
|
|
$sourceFilterMock |
|
53
|
|
|
->expects($this->once()) |
|
54
|
|
|
->method('getViolations') |
|
55
|
|
|
->will($this->returnValue('sourceViolation')); |
|
56
|
|
|
|
|
57
|
|
|
$targetFilterMock = $this->getMockBuilder('Mathielen\DataImport\Filter\ValidatorFilter')->disableOriginalConstructor()->getMock(); |
|
58
|
|
|
$targetFilterMock |
|
59
|
|
|
->expects($this->once()) |
|
60
|
|
|
->method('getViolations') |
|
61
|
|
|
->will($this->returnValue('targetViolation')); |
|
62
|
|
|
|
|
63
|
|
|
$this->validatorValidation->setSourceValidatorFilter($sourceFilterMock); |
|
64
|
|
|
$this->validatorValidation->setTargetValidatorFilter($targetFilterMock); |
|
65
|
|
|
|
|
66
|
|
|
$workflow = $this->getMockBuilder('Ddeboer\DataImport\Workflow')->disableOriginalConstructor()->getMock(); |
|
|
|
|
|
|
67
|
|
|
$this->assertEquals( |
|
68
|
|
|
array('source' => 'sourceViolation', 'target' => 'targetViolation'), |
|
69
|
|
|
$this->validatorValidation->getViolations() |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
} |
|
74
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.