Completed
Push — master ( 1671b1...4e9462 )
by Kamil
23:39
created

it_creates_promotion_with_coupon()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 23
rs 9.0856
cc 1
eloc 18
nc 1
nop 7
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\Behat\Context\Setup;
13
14
use Behat\Behat\Context\Context;
15
use Doctrine\Common\Persistence\ObjectManager;
16
use PhpSpec\ObjectBehavior;
17
use Sylius\Behat\Context\Setup\PromotionContext;
18
use Sylius\Component\Core\Factory\ActionFactoryInterface;
19
use Sylius\Component\Core\Factory\RuleFactoryInterface;
20
use Sylius\Component\Core\Model\ChannelInterface;
21
use Sylius\Component\Core\Model\CouponInterface;
22
use Sylius\Component\Core\Model\PromotionInterface;
23
use Sylius\Component\Core\Model\TaxonInterface;
24
use Sylius\Component\Core\Test\Factory\TestPromotionFactoryInterface;
25
use Sylius\Component\Core\Test\Services\SharedStorageInterface;
26
use Sylius\Component\Promotion\Factory\CouponFactoryInterface;
27
use Sylius\Component\Promotion\Model\ActionInterface;
28
use Sylius\Component\Promotion\Model\RuleInterface;
29
use Sylius\Component\Promotion\Repository\PromotionRepositoryInterface;
30
31
/**
32
 * @mixin PromotionContext
33
 *
34
 * @author Mateusz Zalewski <[email protected]>
35
 */
36
class PromotionContextSpec extends ObjectBehavior
37
{
38
    function let(
39
        SharedStorageInterface $sharedStorage,
40
        ActionFactoryInterface $actionFactory,
41
        CouponFactoryInterface $couponFactory,
42
        RuleFactoryInterface $ruleFactory,
43
        TestPromotionFactoryInterface $testPromotionFactory,
44
        PromotionRepositoryInterface $promotionRepository,
45
        ObjectManager $objectManager
46
    ) {
47
        $this->beConstructedWith(
48
            $sharedStorage,
49
            $actionFactory,
50
            $couponFactory,
51
            $ruleFactory,
52
            $testPromotionFactory,
53
            $promotionRepository,
54
            $objectManager
55
        );
56
    }
57
58
    function it_is_initializable()
59
    {
60
        $this->shouldHaveType('Sylius\Behat\Context\Setup\PromotionContext');
61
    }
62
63
    function it_implements_context_interface()
64
    {
65
        $this->shouldImplement(Context::class);
66
    }
67
68
    function it_creates_promotion(
69
        ChannelInterface $channel,
70
        PromotionInterface $promotion,
71
        PromotionRepositoryInterface $promotionRepository,
72
        SharedStorageInterface $sharedStorage,
73
        TestPromotionFactoryInterface $testPromotionFactory
74
    ) {
75
        $sharedStorage->get('channel')->willReturn($channel);
76
77
        $testPromotionFactory->createForChannel('Super promotion', $channel)->willReturn($promotion);
78
79
        $promotionRepository->add($promotion)->shouldBeCalled();
80
        $sharedStorage->set('promotion', $promotion)->shouldBeCalled();
81
82
        $this->thereIsPromotion('Super promotion');
83
    }
84
85
    function it_creates_promotion_with_coupon(
86
        SharedStorageInterface $sharedStorage,
87
        CouponFactoryInterface $couponFactory,
88
        TestPromotionFactoryInterface $testPromotionFactory,
89
        PromotionRepositoryInterface $promotionRepository,
90
        ChannelInterface $channel,
91
        CouponInterface $coupon,
92
        PromotionInterface $promotion
93
    ) {
94
        $couponFactory->createNew()->willReturn($coupon);
95
        $coupon->setCode('Coupon galore')->shouldBeCalled();
96
97
        $sharedStorage->get('channel')->willReturn($channel);
98
        $testPromotionFactory->createForChannel('Promotion galore', $channel)->willReturn($promotion);
99
        $promotion->addCoupon($coupon)->shouldBeCalled();
100
        $promotion->setCouponBased(true)->shouldBeCalled();
101
102
        $promotionRepository->add($promotion)->shouldBeCalled();
103
        $sharedStorage->set('promotion', $promotion)->shouldBeCalled();
104
        $sharedStorage->set('coupon', $coupon)->shouldBeCalled();
105
106
        $this->thereIsPromotionWithCoupon('Promotion galore', 'Coupon galore');
107
    }
108
109
    function it_creates_fixed_discount_action_for_promotion(
110
        ActionFactoryInterface $actionFactory,
111
        ActionInterface $action,
112
        ObjectManager $objectManager,
113
        PromotionInterface $promotion
114
    ) {
115
        $actionFactory->createFixedDiscount(1000)->willReturn($action);
116
        $promotion->addAction($action)->shouldBeCalled();
117
118
        $objectManager->flush()->shouldBeCalled();
119
120
        $this->itGivesFixedDiscountToEveryOrder($promotion, 1000);
121
    }
122
123
    function it_creates_percentage_discount_action_for_promotion(
124
        ActionFactoryInterface $actionFactory,
125
        ActionInterface $action,
126
        ObjectManager $objectManager,
127
        PromotionInterface $promotion,
128
        SharedStorageInterface $sharedStorage
129
    ) {
130
        $sharedStorage->get('promotion')->willReturn($promotion);
131
132
        $actionFactory->createPercentageDiscount(0.1)->willReturn($action);
133
        $promotion->addAction($action)->shouldBeCalled();
134
135
        $objectManager->flush()->shouldBeCalled();
136
137
        $this->itGivesPercentageDiscountToEveryOrder($promotion, 0.1);
138
    }
139
140
    function it_creates_fixed_discount_promotion_for_cart_with_specified_quantity(
141
        ActionFactoryInterface $actionFactory,
142
        ActionInterface $action,
143
        ObjectManager $objectManager,
144
        PromotionInterface $promotion,
145
        RuleFactoryInterface $ruleFactory,
146
        RuleInterface $rule,
147
        SharedStorageInterface $sharedStorage
148
    ) {
149
        $sharedStorage->get('promotion')->willReturn($promotion);
150
151
        $actionFactory->createFixedDiscount(1000)->willReturn($action);
152
        $promotion->addAction($action)->shouldBeCalled();
153
154
        $ruleFactory->createCartQuantity(5)->willReturn($rule);
155
        $promotion->addRule($rule)->shouldBeCalled();
156
157
        $objectManager->flush()->shouldBeCalled();
158
159
        $this->itGivesFixedDiscountToEveryOrderWithQuantityAtLeast($promotion, 1000, '5');
160
    }
161
162
    function it_creates_fixed_discount_promotion_for_cart_with_specified_items_total(
163
        ActionFactoryInterface $actionFactory,
164
        ActionInterface $action,
165
        ObjectManager $objectManager,
166
        PromotionInterface $promotion,
167
        RuleFactoryInterface $ruleFactory,
168
        RuleInterface $rule
169
    ) {
170
        $actionFactory->createFixedDiscount(1000)->willReturn($action);
171
        $promotion->addAction($action)->shouldBeCalled();
172
173
        $ruleFactory->createItemTotal(5000)->willReturn($rule);
174
        $promotion->addRule($rule)->shouldBeCalled();
175
176
        $objectManager->flush()->shouldBeCalled();
177
178
        $this->itGivesFixedDiscountToEveryOrderWithItemsTotalAtLeast($promotion, 1000, 5000);
179
    }
180
181
    function it_creates_percentage_shipping_discount_action_for_promotion(
182
        ActionFactoryInterface $actionFactory,
183
        ActionInterface $action,
184
        ObjectManager $objectManager,
185
        PromotionInterface $promotion
186
    ) {
187
        $actionFactory->createPercentageShippingDiscount(0.1)->willReturn($action);
188
        $promotion->addAction($action)->shouldBeCalled();
189
190
        $objectManager->flush()->shouldBeCalled();
191
192
        $this->itGivesPercentageDiscountOnShippingToEveryOrder($promotion, 0.1);
193
    }
194
195
    function it_creates_item_percentage_discount_action_for_promotion_products_with_specific_taxon(
196
        ActionFactoryInterface $actionFactory,
197
        ActionInterface $action,
198
        ObjectManager $objectManager,
199
        PromotionInterface $promotion,
200
        TaxonInterface $taxon
201
    ) {
202
        $taxon->getCode()->willReturn('scottish_kilts');
203
204
        $actionFactory->createItemPercentageDiscount(0.1)->willReturn($action);
205
        $action->getConfiguration()->willReturn(['percentage' => 0.1]);
206
        $action->setConfiguration([
207
            'percentage' => 0.1,
208
            'filters' => [
209
                'taxons' => ['scottish_kilts'],
210
            ],
211
        ])->shouldBeCalled();
212
        
213
        $promotion->addAction($action)->shouldBeCalled();
214
215
        $objectManager->flush()->shouldBeCalled();
216
217
        $this->itGivesPercentageOffEveryProductClassifiedAs($promotion, 0.1, $taxon);
218
    }
219
}
220