Completed
Push — 1.5-symfony-insights ( c13c26 )
by Kamil
05:55
created

ChannelDefaultLocaleEnabledValidatorSpec::let()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace spec\Sylius\Bundle\CoreBundle\Validator\Constraints;
15
16
use Doctrine\Common\Collections\ArrayCollection;
17
use PhpSpec\ObjectBehavior;
18
use Sylius\Bundle\CoreBundle\Validator\Constraints\ChannelDefaultLocaleEnabled;
19
use Sylius\Component\Core\Model\ChannelInterface;
20
use Sylius\Component\Locale\Model\LocaleInterface;
21
use Symfony\Component\Validator\Constraint;
22
use Symfony\Component\Validator\ConstraintValidator;
23
use Symfony\Component\Validator\Context\ExecutionContextInterface;
24
use Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface;
25
26
final class ChannelDefaultLocaleEnabledValidatorSpec extends ObjectBehavior
27
{
28
    function let(ExecutionContextInterface $executionContext): void
29
    {
30
        $this->initialize($executionContext);
31
    }
32
33
    function it_is_a_constraint_validator(): void
34
    {
35
        $this->shouldHaveType(ConstraintValidator::class);
36
    }
37
38
    function it_validates_only_a_channel(\stdClass $object): void
39
    {
40
        $constraint = new ChannelDefaultLocaleEnabled();
41
42
        $this->shouldThrow(\InvalidArgumentException::class)->during('validate', [$object, $constraint]);
43
    }
44
45
    function it_is_a_channel_default_locale_enabled_validator(ChannelInterface $channel, Constraint $constraint): void
46
    {
47
        $this->shouldThrow(\InvalidArgumentException::class)->during('validate', [$channel, $constraint]);
48
    }
49
50
    function it_adds_violation_if_default_locale_is_not_enabled_for_a_given_channel(
51
        ExecutionContextInterface $executionContext,
52
        ConstraintViolationBuilderInterface $constraintViolationBuilder,
53
        ChannelInterface $channel,
54
        LocaleInterface $locale
55
    ): void {
56
        $constraint = new ChannelDefaultLocaleEnabled();
57
58
        $channel->getDefaultLocale()->willReturn($locale);
59
        $channel->hasLocale($locale)->willReturn(false);
60
61
        $executionContext->buildViolation($constraint->message)->willReturn($constraintViolationBuilder);
62
        $constraintViolationBuilder->atPath('defaultLocale')->willReturn($constraintViolationBuilder);
63
        $constraintViolationBuilder->addViolation()->shouldBeCalled();
64
65
        $this->validate($channel, $constraint);
66
    }
67
68
    function it_does_nothing_if_default_locale_is_enabled_for_a_given_channel(
69
        ExecutionContextInterface $executionContext,
70
        ChannelInterface $channel,
71
        LocaleInterface $locale
72
    ): void {
73
        $constraint = new ChannelDefaultLocaleEnabled();
74
75
        $channel->getDefaultLocale()->willReturn($locale);
76
        $channel->hasLocale($locale)->willReturn(true);
77
78
        $executionContext->buildViolation($constraint->message)->shouldNotBeCalled();
79
80
        $this->validate($channel, $constraint);
81
    }
82
}
83