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

PromotionApplicatorSpec   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A let() 0 4 1
A it_should_be_a_promotion_applicator() 0 4 1
A it_executes_all_actions_registered() 0 20 1
B it_applies_promotion_if_at_least_one_action_was_executed_even_if_the_last_one_was_not() 0 29 1
B it_applies_promotion_if_at_least_one_action_was_executed() 0 29 1
B it_does_not_add_promotion_if_no_action_has_been_applied() 0 29 1
A it_reverts_all_actions_registered() 0 20 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\Component\Promotion\Action;
15
16
use Doctrine\Common\Collections\ArrayCollection;
17
use PhpSpec\ObjectBehavior;
18
use Sylius\Component\Promotion\Action\PromotionActionCommandInterface;
19
use Sylius\Component\Promotion\Action\PromotionApplicatorInterface;
20
use Sylius\Component\Promotion\Model\PromotionActionInterface;
21
use Sylius\Component\Promotion\Model\PromotionInterface;
22
use Sylius\Component\Promotion\Model\PromotionSubjectInterface;
23
use Sylius\Component\Registry\ServiceRegistryInterface;
24
25
/**
26
 * @author Saša Stamenković <[email protected]>
27
 */
28
final class PromotionApplicatorSpec extends ObjectBehavior
29
{
30
    function let(ServiceRegistryInterface $registry): void
31
    {
32
        $this->beConstructedWith($registry);
33
    }
34
35
    function it_should_be_a_promotion_applicator(): void
36
    {
37
        $this->shouldImplement(PromotionApplicatorInterface::class);
38
    }
39
40
    function it_executes_all_actions_registered(
41
        ServiceRegistryInterface $registry,
42
        PromotionActionCommandInterface $actionCommand,
43
        PromotionSubjectInterface $subject,
44
        PromotionInterface $promotion,
45
        PromotionActionInterface $action
46
    ): void {
47
        $configuration = [];
48
49
        $registry->get('test_action')->willReturn($actionCommand);
50
        $promotion->getActions()->willReturn(new ArrayCollection([$action->getWrappedObject()]));
51
        $action->getType()->willReturn('test_action');
52
        $action->getConfiguration()->willReturn($configuration);
53
54
        $actionCommand->execute($subject, $configuration, $promotion)->willReturn(true);
55
56
        $subject->addPromotion($promotion)->shouldBeCalled();
57
58
        $this->apply($subject, $promotion);
59
    }
60
61
    function it_applies_promotion_if_at_least_one_action_was_executed_even_if_the_last_one_was_not(
62
        ServiceRegistryInterface $registry,
63
        PromotionActionCommandInterface $firstActionCommand,
64
        PromotionActionCommandInterface $secondActionCommand,
65
        PromotionSubjectInterface $subject,
66
        PromotionInterface $promotion,
67
        PromotionActionInterface $firstAction,
68
        PromotionActionInterface $secondAction
69
    ): void {
70
        $promotion->getActions()->willReturn(
71
            new ArrayCollection([$firstAction->getWrappedObject(), $secondAction->getWrappedObject()])
72
        );
73
74
        $firstAction->getType()->willReturn('first_action');
75
        $firstAction->getConfiguration()->willReturn([]);
76
77
        $secondAction->getType()->willReturn('second_action');
78
        $secondAction->getConfiguration()->willReturn([]);
79
80
        $registry->get('first_action')->willReturn($firstActionCommand);
81
        $registry->get('second_action')->willReturn($secondActionCommand);
82
83
        $firstActionCommand->execute($subject, [], $promotion)->willReturn(true);
84
        $secondActionCommand->execute($subject, [], $promotion)->willReturn(false);
85
86
        $subject->addPromotion($promotion)->shouldBeCalled();
87
88
        $this->apply($subject, $promotion);
89
    }
90
91
    function it_applies_promotion_if_at_least_one_action_was_executed(
92
        ServiceRegistryInterface $registry,
93
        PromotionActionCommandInterface $firstActionCommand,
94
        PromotionActionCommandInterface $secondActionCommand,
95
        PromotionSubjectInterface $subject,
96
        PromotionInterface $promotion,
97
        PromotionActionInterface $firstAction,
98
        PromotionActionInterface $secondAction
99
    ): void {
100
        $promotion->getActions()->willReturn(
101
            new ArrayCollection([$firstAction->getWrappedObject(), $secondAction->getWrappedObject()])
102
        );
103
104
        $firstAction->getType()->willReturn('first_action');
105
        $firstAction->getConfiguration()->willReturn([]);
106
107
        $secondAction->getType()->willReturn('second_action');
108
        $secondAction->getConfiguration()->willReturn([]);
109
110
        $registry->get('first_action')->willReturn($firstActionCommand);
111
        $registry->get('second_action')->willReturn($secondActionCommand);
112
113
        $firstActionCommand->execute($subject, [], $promotion)->willReturn(false);
114
        $secondActionCommand->execute($subject, [], $promotion)->willReturn(true);
115
116
        $subject->addPromotion($promotion)->shouldBeCalled();
117
118
        $this->apply($subject, $promotion);
119
    }
120
121
    function it_does_not_add_promotion_if_no_action_has_been_applied(
122
        ServiceRegistryInterface $registry,
123
        PromotionActionCommandInterface $firstActionCommand,
124
        PromotionActionCommandInterface $secondActionCommand,
125
        PromotionSubjectInterface $subject,
126
        PromotionInterface $promotion,
127
        PromotionActionInterface $firstAction,
128
        PromotionActionInterface $secondAction
129
    ): void {
130
        $promotion->getActions()->willReturn(
131
            new ArrayCollection([$firstAction->getWrappedObject(), $secondAction->getWrappedObject()])
132
        );
133
134
        $firstAction->getType()->willReturn('first_action');
135
        $firstAction->getConfiguration()->willReturn([]);
136
137
        $secondAction->getType()->willReturn('second_action');
138
        $secondAction->getConfiguration()->willReturn([]);
139
140
        $registry->get('first_action')->willReturn($firstActionCommand);
141
        $registry->get('second_action')->willReturn($secondActionCommand);
142
143
        $firstActionCommand->execute($subject, [], $promotion)->willReturn(false);
144
        $secondActionCommand->execute($subject, [], $promotion)->willReturn(false);
145
146
        $subject->addPromotion($promotion)->shouldNotBeCalled();
147
148
        $this->apply($subject, $promotion);
149
    }
150
151
    function it_reverts_all_actions_registered(
152
        ServiceRegistryInterface $registry,
153
        PromotionActionCommandInterface $actionCommand,
154
        PromotionSubjectInterface $subject,
155
        PromotionInterface $promotion,
156
        PromotionActionInterface $action
157
    ): void {
158
        $configuration = [];
159
160
        $registry->get('test_action')->willReturn($actionCommand);
161
        $promotion->getActions()->willReturn(new ArrayCollection([$action->getWrappedObject()]));
162
        $action->getType()->willReturn('test_action');
163
        $action->getConfiguration()->willReturn($configuration);
164
165
        $actionCommand->revert($subject, $configuration, $promotion)->shouldBeCalled();
166
167
        $subject->removePromotion($promotion)->shouldBeCalled();
168
169
        $this->revert($subject, $promotion);
170
    }
171
}
172