CommandTestCase::assertPropertyInvalid()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace Tactics\CommandBusBundle\Tests\Command;
4
5
use Symfony\Component\Validator\Validation;
6
use Tactics\CommandBusBundle\Command\Command;
7
8
class CommandTestCase extends \PHPUnit_Framework_TestCase
9
{
10
    protected function getValidator()
11
    {
12
        return Validation::createValidatorBuilder()->enableAnnotationMapping()->getValidator();
13
    }
14
15
    protected function validateAndCheckIfErrorExists(Command $command, $propertyName)
16
    {
17
        return in_array($propertyName, $this->getInvalidPropertyNames($this->getValidator()->validate($command)));
18
    }
19
20
    protected function getInvalidPropertyNames($constraintViolationList)
21
    {
22
        $invalidPropertyNames = array();
23
        foreach ($constraintViolationList as $constraintViolation) {
24
            $invalidPropertyNames[] = $constraintViolation->getPropertyPath();
25
        }
26
        return $invalidPropertyNames;
27
    }
28
29
    public function assertPropertyInvalid(Command $command, $propertyName)
30
    {
31
        $this->assertTrue($this->validateAndCheckIfErrorExists($command, $propertyName));
32
    }
33
34
    public function assertPropertyValid(Command $command, $propertyName)
35
    {
36
        $this->assertFalse($this->validateAndCheckIfErrorExists($command, $propertyName));
37
    }
38
}