ValidatorSpec.php
2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
namespace spec\Cardinity\Method;
use Cardinity\Method\MethodInterface;
use PhpSpec\ObjectBehavior;
use Symfony\Component\Validator\ConstraintViolationList;
use Symfony\Component\Validator\Validator\ValidatorInterface;
class ValidatorSpec extends ObjectBehavior
{
function let(ValidatorInterface $validator)
{
$this->beConstructedWith($validator);
}
function it_implements_validator_interface()
{
$this->shouldImplement('Cardinity\Method\ValidatorInterface');
}
function it_validates_given_method_instance(MethodInterface $method, ValidatorInterface $validator)
{
$attributes = ['field' => 'value'];
$constraints = ['constraints'];
$method->getAttributes()->shouldBeCalled()->willReturn($attributes);
$method->getValidationConstraints()->shouldBeCalled()->willReturn($constraints);
$validator->validate($attributes, $constraints)->shouldBeCalled();
$this->validate($method);
}
function it_throws_exception_on_validation_failure(
MethodInterface $method,
ValidatorInterface $validator,
ConstraintViolationList $violations
) {
$attributes = ['field' => 'value'];
$constraints = ['constraints'];
$method->getValidationConstraints()->shouldBeCalled()->willReturn($constraints);
$method->getAttributes()->shouldBeCalled()->willReturn($attributes);
$violations->count()->willReturn(1);
$violations->__toString()->willReturn('');
$validator->validate($attributes, $constraints)->willReturn($violations);
$this->shouldThrow('Cardinity\Exception\InvalidAttributeValue')->duringValidate($method);
}
function it_does_not_validate_method_with_no_constraints(MethodInterface $method, ValidatorInterface $validator)
{
$constraints = [];
$method->getValidationConstraints()->shouldBeCalled()->willReturn($constraints);
$validator->validate([], $constraints)->shouldNotBeCalled();
$this->validate($method);
}
}