Completed
Push — scalar-types/taxonomy ( 4d330b )
by Kamil
23:21
created

PromotionProcessorSpec::it_is_initializable()   A

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
rs 10
c 0
b 0
f 0
nc 1
cc 1
eloc 2
nop 0
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\Component\Promotion\Processor;
15
16
use Doctrine\Common\Collections\ArrayCollection;
17
use PhpSpec\ObjectBehavior;
18
use Sylius\Component\Promotion\Action\PromotionApplicatorInterface;
19
use Sylius\Component\Promotion\Checker\Eligibility\PromotionEligibilityCheckerInterface;
20
use Sylius\Component\Promotion\Model\PromotionInterface;
21
use Sylius\Component\Promotion\Model\PromotionSubjectInterface;
22
use Sylius\Component\Promotion\Processor\PromotionProcessorInterface;
23
use Sylius\Component\Promotion\Provider\PreQualifiedPromotionsProviderInterface;
24
25
/**
26
 * @author Saša Stamenković <[email protected]>
27
 */
28
final class PromotionProcessorSpec extends ObjectBehavior
29
{
30
    function let(
31
        PreQualifiedPromotionsProviderInterface $preQualifiedPromotionsProvider,
32
        PromotionEligibilityCheckerInterface $promotionEligibilityChecker,
33
        PromotionApplicatorInterface $promotionApplicator
34
    ): void {
35
        $this->beConstructedWith($preQualifiedPromotionsProvider, $promotionEligibilityChecker, $promotionApplicator);
36
    }
37
38
    function it_is_a_promotion_processor(): void
39
    {
40
        $this->shouldImplement(PromotionProcessorInterface::class);
41
    }
42
43
    function it_does_not_apply_promotions_that_are_not_eligible(
44
        PreQualifiedPromotionsProviderInterface $preQualifiedPromotionsProvider,
45
        PromotionEligibilityCheckerInterface $promotionEligibilityChecker,
46
        PromotionApplicatorInterface $promotionApplicator,
47
        PromotionSubjectInterface $subject,
48
        PromotionInterface $promotion
49
    ): void {
50
        $subject->getPromotions()->willReturn(new ArrayCollection([]));
51
        $preQualifiedPromotionsProvider->getPromotions($subject)->willReturn([$promotion]);
52
53
        $promotion->isExclusive()->willReturn(false);
54
        $promotionEligibilityChecker->isEligible($subject, $promotion)->willReturn(false);
55
56
        $promotionApplicator->apply($subject, $promotion)->shouldNotBeCalled();
57
        $promotionApplicator->revert($subject, $promotion)->shouldNotBeCalled();
58
59
        $this->process($subject);
60
    }
61
62
    function it_applies_promotions_that_are_eligible(
63
        PreQualifiedPromotionsProviderInterface $preQualifiedPromotionsProvider,
64
        PromotionEligibilityCheckerInterface $promotionEligibilityChecker,
65
        PromotionApplicatorInterface $promotionApplicator,
66
        PromotionSubjectInterface $subject,
67
        PromotionInterface $promotion
68
    ): void {
69
        $subject->getPromotions()->willReturn(new ArrayCollection([]));
70
        $preQualifiedPromotionsProvider->getPromotions($subject)->willReturn([$promotion]);
71
72
        $promotion->isExclusive()->willReturn(false);
73
        $promotionEligibilityChecker->isEligible($subject, $promotion)->willReturn(true);
74
75
        $promotionApplicator->apply($subject, $promotion)->shouldBeCalled();
76
        $promotionApplicator->revert($subject, $promotion)->shouldNotBeCalled();
77
78
        $this->process($subject);
79
    }
80
81
    function it_applies_only_exclusive_promotion(
82
        PreQualifiedPromotionsProviderInterface $preQualifiedPromotionsProvider,
83
        PromotionEligibilityCheckerInterface $promotionEligibilityChecker,
84
        PromotionApplicatorInterface $promotionApplicator,
85
        PromotionSubjectInterface $subject,
86
        PromotionInterface $promotion,
87
        PromotionInterface $exclusivePromotion
88
    ): void {
89
        $subject->getPromotions()->willReturn(new ArrayCollection([]));
90
        $preQualifiedPromotionsProvider->getPromotions($subject)->willReturn([$promotion, $exclusivePromotion]);
91
92
        $exclusivePromotion->isExclusive()->willReturn(true);
93
        $promotion->isExclusive()->willReturn(false);
94
        $promotionEligibilityChecker->isEligible($subject, $promotion)->willReturn(true);
95
        $promotionEligibilityChecker->isEligible($subject, $exclusivePromotion)->willReturn(true);
96
97
        $promotionApplicator->apply($subject, $exclusivePromotion)->shouldBeCalled();
98
        $promotionApplicator->apply($subject, $promotion)->shouldNotBeCalled();
99
        $promotionApplicator->revert($subject, $promotion)->shouldNotBeCalled();
100
        $promotionApplicator->revert($subject, $exclusivePromotion)->shouldNotBeCalled();
101
102
        $this->process($subject);
103
    }
104
105
    function it_reverts_promotions_that_are_not_eligible_anymore(
106
        PreQualifiedPromotionsProviderInterface $preQualifiedPromotionsProvider,
107
        PromotionEligibilityCheckerInterface $promotionEligibilityChecker,
108
        PromotionApplicatorInterface $promotionApplicator,
109
        PromotionSubjectInterface $subject,
110
        PromotionInterface $promotion
111
    ): void {
112
        $subject->getPromotions()->willReturn(new ArrayCollection([$promotion->getWrappedObject()]));
113
        $preQualifiedPromotionsProvider->getPromotions($subject)->willReturn([$promotion]);
114
115
        $promotion->isExclusive()->willReturn(false);
116
        $promotionEligibilityChecker->isEligible($subject, $promotion)->willReturn(false);
117
118
        $promotionApplicator->apply($subject, $promotion)->shouldNotBeCalled();
119
        $promotionApplicator->revert($subject, $promotion)->shouldBeCalled();
120
121
        $this->process($subject);
122
    }
123
}
124