DictionaryValidatorSpec   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 29
c 2
b 0
f 0
dl 0
loc 70
rs 10
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A it_adds_violation_for_an_unexisting_keys() 0 16 1
A let() 0 9 1
A it_is_initializable() 0 3 1
A it_valids_existing_keys() 0 7 1
A it_throw_exception_form_unknown_constraints() 0 5 1
A it_transforms_keys_in_string_representation() 0 18 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace spec\Knp\DictionaryBundle\Validator\Constraints;
6
7
use Knp\DictionaryBundle\Dictionary;
8
use Knp\DictionaryBundle\Dictionary\Collection;
9
use Knp\DictionaryBundle\Validator\Constraints\Dictionary as Constraint;
10
use Knp\DictionaryBundle\Validator\Constraints\DictionaryValidator;
11
use PhpSpec\ObjectBehavior;
12
use Prophecy\Argument;
13
use Symfony\Component\Form\Exception\UnexpectedTypeException;
14
use Symfony\Component\Validator\Constraints\NotNull;
15
use Symfony\Component\Validator\Context\ExecutionContextInterface;
16
17
final class DictionaryValidatorSpec extends ObjectBehavior
18
{
19
    function let(ExecutionContextInterface $context, Dictionary $dictionary)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
20
    {
21
        $dictionary->getName()->willReturn('dico');
22
        $dictionary->getKeys()->willReturn(['the_key']);
23
24
        $dictionaries = new Collection($dictionary->getWrappedObject());
0 ignored issues
show
Bug introduced by
The method getWrappedObject() does not exist on Knp\DictionaryBundle\Dictionary. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

24
        $dictionaries = new Collection($dictionary->/** @scrutinizer ignore-call */ getWrappedObject());

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...
25
26
        $this->beConstructedWith($dictionaries);
27
        $this->initialize($context);
0 ignored issues
show
Bug introduced by
The method initialize() does not exist on spec\Knp\DictionaryBundl...DictionaryValidatorSpec. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

27
        $this->/** @scrutinizer ignore-call */ 
28
               initialize($context);
Loading history...
28
    }
29
30
    function it_is_initializable()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
31
    {
32
        $this->shouldHaveType(DictionaryValidator::class);
33
    }
34
35
    function it_valids_existing_keys($context)
36
    {
37
        $constraint = new Constraint(['name' => 'dico']);
38
39
        $context->addViolation(Argument::any())->shouldNotBeCalled();
40
41
        $this->validate('the_key', $constraint);
0 ignored issues
show
Bug introduced by
The method validate() does not exist on spec\Knp\DictionaryBundl...DictionaryValidatorSpec. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

41
        $this->/** @scrutinizer ignore-call */ 
42
               validate('the_key', $constraint);
Loading history...
42
    }
43
44
    function it_adds_violation_for_an_unexisting_keys($context)
45
    {
46
        $constraint = new Constraint(['name' => 'dico']);
47
48
        $context
49
            ->addViolation(
50
                "The key {{ key }} doesn't exist in the given dictionary. {{ keys }} available.",
51
                [
52
                    '{{ key }}'  => '"the_unexisting_key"',
53
                    '{{ keys }}' => '"the_key"',
54
                ]
55
            )
56
            ->shouldBeCalled()
57
        ;
58
59
        $this->validate('the_unexisting_key', $constraint);
60
    }
61
62
    function it_transforms_keys_in_string_representation($dictionary, $context)
63
    {
64
        $dictionary->getKeys()->willReturn(['the_key', true, 12, 3.14, 0.0, null]);
65
66
        $constraint = new Constraint(['name' => 'dico']);
67
68
        $context
69
            ->addViolation(
70
                "The key {{ key }} doesn't exist in the given dictionary. {{ keys }} available.",
71
                [
72
                    '{{ key }}'  => '"the_unexisting_key"',
73
                    '{{ keys }}' => '"the_key", true, 12, 3.14, 0.0, null',
74
                ]
75
            )
76
            ->shouldBeCalled()
77
        ;
78
79
        $this->validate('the_unexisting_key', $constraint);
80
    }
81
82
    function it_throw_exception_form_unknown_constraints()
83
    {
84
        $constraint = new NotNull();
85
86
        $this->shouldThrow(new UnexpectedTypeException($constraint, Constraint::class))->duringValidate('the_key', $constraint);
87
    }
88
}
89