Completed
Push — symfony3 ( 405d0c...88ded0 )
by Kamil
32:03 queued 12:32
created

HasTaxonRuleCheckerSpec   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 7
dl 0
loc 77
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A it_is_initializable() 0 4 1
A it_is_a_rule_checker() 0 4 1
A it_recognizes_a_subject_as_eligible_if_product_taxon_is_matched() 0 15 1
A it_recognizes_a_subject_as_eligible_if_a_product_taxon_is_matched_to_one_of_required_taxons() 0 15 1
A it_recognizes_a_subject_as_not_eligible_if_a_product_taxon_is_not_matched() 0 15 1
A it_does_nothing_if_a_configuration_is_invalid() 0 6 1
A it_throws_an_exception_if_promotion_subject_is_not_order() 0 9 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
namespace spec\Sylius\Component\Core\Promotion\Checker\Rule;
13
14
use Doctrine\Common\Collections\Collection;
15
use PhpSpec\ObjectBehavior;
16
use Sylius\Component\Core\Model\OrderInterface;
17
use Sylius\Component\Core\Model\OrderItemInterface;
18
use Sylius\Component\Core\Model\ProductInterface;
19
use Sylius\Component\Core\Model\TaxonInterface;
20
use Sylius\Component\Core\Promotion\Checker\Rule\HasTaxonRuleChecker;
21
use Sylius\Component\Promotion\Checker\Rule\RuleCheckerInterface;
22
use Sylius\Component\Promotion\Exception\UnsupportedTypeException;
23
use Sylius\Component\Promotion\Model\PromotionSubjectInterface;
24
25
/**
26
 * @author Joseph Bielawski <[email protected]>
27
 * @author Mateusz Zalewski <[email protected]>
28
 */
29
final class HasTaxonRuleCheckerSpec extends ObjectBehavior
30
{
31
    function it_is_initializable()
32
    {
33
        $this->shouldHaveType(HasTaxonRuleChecker::class);
34
    }
35
36
    function it_is_a_rule_checker()
37
    {
38
        $this->shouldImplement(RuleCheckerInterface::class);
39
    }
40
41
    function it_recognizes_a_subject_as_eligible_if_product_taxon_is_matched(
42
        OrderInterface $subject,
43
        OrderItemInterface $item,
44
        ProductInterface $bastardSword,
45
        TaxonInterface $swords
46
    ) {
47
        $configuration = ['taxons' => ['swords']];
48
49
        $swords->getCode()->willReturn('swords');
50
        $bastardSword->getTaxons()->willReturn([$swords]);
51
        $item->getProduct()->willReturn($bastardSword);
52
        $subject->getItems()->willReturn([$item]);
53
54
        $this->isEligible($subject, $configuration)->shouldReturn(true);
55
    }
56
57
    function it_recognizes_a_subject_as_eligible_if_a_product_taxon_is_matched_to_one_of_required_taxons(
58
        OrderInterface $subject,
59
        OrderItemInterface $item,
60
        ProductInterface $bastardSword,
61
        TaxonInterface $swords
62
    ) {
63
        $configuration = ['taxons' => ['swords', 'axes']];
64
65
        $swords->getCode()->willReturn('swords');
66
        $bastardSword->getTaxons()->willReturn([$swords]);
67
        $item->getProduct()->willReturn($bastardSword);
68
        $subject->getItems()->willReturn([$item]);
69
70
        $this->isEligible($subject, $configuration)->shouldReturn(true);
71
    }
72
73
    function it_recognizes_a_subject_as_not_eligible_if_a_product_taxon_is_not_matched(
74
        OrderInterface $subject,
75
        OrderItemInterface $item,
76
        ProductInterface $reflexBow,
77
        TaxonInterface $bows
78
    ) {
79
        $configuration = ['taxons' => ['swords', 'axes']];
80
81
        $bows->getCode()->willReturn('bows');
82
        $reflexBow->getTaxons()->willReturn([$bows]);
83
        $item->getProduct()->willReturn($reflexBow);
84
        $subject->getItems()->willReturn([$item]);
85
86
        $this->isEligible($subject, $configuration)->shouldReturn(false);
87
    }
88
89
    function it_does_nothing_if_a_configuration_is_invalid(OrderInterface $subject)
90
    {
91
        $subject->getItems()->shouldNotBeCalled();
92
93
        $this->isEligible($subject, []);
94
    }
95
96
    function it_throws_an_exception_if_promotion_subject_is_not_order(
97
        Collection $taxonsCollection,
98
        PromotionSubjectInterface $subject
99
    ) {
100
        $this
101
            ->shouldThrow(new UnsupportedTypeException($subject->getWrappedObject(), OrderInterface::class))
102
            ->during('isEligible', [$subject, ['taxons' => $taxonsCollection]])
103
        ;
104
    }
105
}
106