DiscountHelperTest   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 317
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 11
eloc 170
c 4
b 0
f 0
dl 0
loc 317
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetFoxyDiscountType() 0 16 1
A setUp() 0 50 3
A testGetDiscountFieldValue() 0 15 1
A testGetProduct() 0 6 1
A testGetDiscountedPrice() 0 24 1
A testMemberDiscount() 0 33 1
A testGetDiscountedPriceVariation() 0 24 1
A testExcludeProductFromDiscounts() 0 14 1
A testMemberDiscountMultipleMembers() 0 50 1
1
<?php
2
3
namespace Dynamic\Foxy\Discounts\Tests;
4
5
use Dynamic\Foxy\Discounts\DiscountHelper;
6
use Dynamic\Foxy\Discounts\Extension\ProductDataExtension;
7
use Dynamic\Foxy\Discounts\Model\Discount;
8
use Dynamic\Foxy\Discounts\Model\DiscountTier;
9
use Dynamic\Foxy\Discounts\Tests\TestOnly\Extension\TestDiscountExtension;
10
use Dynamic\Foxy\Discounts\Tests\TestOnly\Extension\VariationDataExtension;
11
use Dynamic\Foxy\Discounts\Tests\TestOnly\Page\ProductPage;
12
use Dynamic\Foxy\Extension\Purchasable;
13
use Dynamic\Foxy\Model\Variation;
14
use Dynamic\Foxy\Model\VariationType;
15
use SilverStripe\Core\Config\Config;
16
use SilverStripe\Dev\SapphireTest;
17
use SilverStripe\ORM\ValidationException;
18
use SilverStripe\Security\Member;
19
use SilverStripe\Versioned\Versioned;
20
21
/**
22
 * Class DiscountHelperTest
23
 * @package Dynamic\Foxy\Discounts\Tests
24
 */
25
class DiscountHelperTest extends SapphireTest
26
{
27
    /**
28
     * @var string[]
29
     */
30
    protected static $fixture_file = [
31
        'products.yml',
32
        'discounts.yml',
33
        'accounts.yml',
34
    ];
35
36
    /**
37
     * @var string[]
38
     */
39
    protected static $extra_dataobjects = [
40
        ProductPage::class,
41
    ];
42
43
    /**
44
     * @var \string[][]
45
     */
46
    protected static $required_extensions = [
47
        ProductPage::class => [
48
            Purchasable::class,
49
            ProductDataExtension::class,
50
        ],
51
        Discount::class => [
52
            TestDiscountExtension::class,
53
        ],
54
        Variation::class => [
55
            VariationDataExtension::class,
56
        ],
57
    ];
58
59
    /**
60
     * @var string[]
61
     */
62
    protected static $illegal_extensions = [
63
        /*Discount::class => [
64
            'Dynamic\\FoxyRecipe\\Extension\\DiscountDataExtension',
65
        ],//*/
66
    ];
67
68
    /**
69
     *
70
     */
71
    protected function setUp()
72
    {
73
        if (class_exists('Dynamic\\Foxy\\API\\Client\\APIClient')) {
74
            Config::modify()->set('Dynamic\\Foxy\\API\\Client\\APIClient', 'enable_api', false);
75
        }
76
        if (class_exists('Dynamic\\Foxy\\SingleSignOn\\Client\\CustomerClient')) {
77
            Config::modify()->set('Dynamic\\Foxy\\SingleSignOn\\Client\\CustomerClient', 'foxy_sso_enabled', false);
78
        }
79
80
        parent::setUp();
81
82
        $product = $this->objFromFixture(ProductPage::class, 'productthree');
83
        $product->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
84
        $productTwo = $this->objFromFixture(ProductPage::class, 'productfiveandvariations');
85
        $productTwo->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
86
        $discountOne = $this->objFromFixture(Discount::class, 'simplediscountpercentage');
87
        $discountOne->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
88
        $discountTwo = $this->objFromFixture(Discount::class, 'tierdiscountpercentage');
89
        $discountTwo->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
90
        $discountThree = $this->objFromFixture(Discount::class, 'tierdiscountamount');
91
        $discountThree->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
92
93
        Versioned::set_stage(Versioned::LIVE);
94
95
        $variationOne = Variation::create();
96
        $variationOne->Title = 'Variation One';
97
        $variationOne->PriceModifier = 10;
98
        $variationOne->PriceModifierAction = 'Add';
99
        $variationOne->Available = true;
100
        $variationOne->VariationTypeID = VariationType::get()->first()->ID;
101
        $variationOne->ProductID = $productTwo->ID;
102
        $variationOne->write();
103
104
        $variationTwo = Variation::create();
105
        $variationTwo->Title = 'Variation Two';
106
        $variationTwo->PriceModifier = 150;
107
        $variationTwo->PriceModifierAction = 'Set';
108
        $variationTwo->Available = true;
109
        $variationTwo->VariationTypeID = VariationType::get()->first()->ID;
110
        $variationTwo->ProductID = $productTwo->ID;
111
        $variationTwo->write();
112
113
        $variationThree = Variation::create();
114
        $variationThree->Title = 'Variation Three';
115
        $variationThree->PriceModifier = 20;
116
        $variationThree->PriceModifierAction = 'Subtract';
117
        $variationThree->Available = true;
118
        $variationThree->VariationTypeID = VariationType::get()->first()->ID;
119
        $variationThree->ProductID = $productTwo->ID;
120
        $variationThree->write();
121
    }
122
123
    public function testGetProduct()
124
    {
125
        $product = $this->objFromFixture(ProductPage::class, 'productthree');
126
        $helper = DiscountHelper::create($product);
127
128
        $this->assertEquals($product->ID, $helper->getProduct()->ID);
129
    }
130
131
    /**
132
     *
133
     */
134
    public function testGetDiscountedPrice()
135
    {
136
        $product = $this->objFromFixture(ProductPage::class, 'productthree');
137
        $helper = DiscountHelper::create($product);
138
139
        $this->assertEquals(75, $helper->getDiscountedPrice()->getValue());
140
141
        $helper->setQuantity(25);
142
        $this->assertEquals(70, $helper->getDiscountedPrice()->getValue());
143
144
        $discountOne = $this->objFromFixture(Discount::class, 'simplediscountpercentage');
145
        $discountOne->doUnpublish();
146
147
        $helper->setDiscountTier();
148
        $helper->setQuantity(1);
149
        $this->assertEquals(95, $helper->getDiscountedPrice()->getValue());
150
151
        $helper->setQuantity(6);
152
        $this->assertEquals(88, $helper->getDiscountedPrice()->getValue());
153
154
        $helper->setQuantity(23);
155
        $this->assertEquals(70, $helper->getDiscountedPrice()->getValue());
156
157
        $product->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
158
    }
159
160
    /**
161
     *
162
     */
163
    public function testGetDiscountedPriceVariation()
164
    {
165
        $product = $this->objFromFixture(ProductPage::class, 'productfiveandvariations');
166
167
        $variationOne = Variation::get()->filter('PriceModifierAction', 'Add')->first();
168
        $variationTwo = Variation::get()->filter('PriceModifierAction', 'Set')->first();
169
        $variationThree = Variation::get()->filter('PriceModifierAction', 'Subtract')->first();
170
171
        //$110 less 25%
172
        $this->assertEquals(
173
            82.5,
174
            DiscountHelper::create($product, 1, $variationOne)->getDiscountedPrice()->getValue()
175
        );
176
177
        //$150 less 25%
178
        $this->assertEquals(
179
            112.5,
180
            DiscountHelper::create($product, 1, $variationTwo)->getDiscountedPrice()->getValue()
181
        );
182
183
        //$80 less 25%
184
        $this->assertEquals(
185
            60,
186
            DiscountHelper::create($product, 1, $variationThree)->getDiscountedPrice()->getValue()
187
        );
188
    }
189
190
    /**
191
     *
192
     */
193
    public function testGetFoxyDiscountType()
194
    {
195
        $product = $this->objFromFixture(ProductPage::class, 'productthree');
196
        $helper = DiscountHelper::create($product);
197
198
        $this->assertEquals('discount_quantity_percentage', $helper->getFoxyDiscountType());
199
200
        $discountOne = $this->objFromFixture(Discount::class, 'simplediscountpercentage');
201
        $discountTwo = $this->objFromFixture(Discount::class, 'tierdiscountpercentage');
202
203
        $discountOne->doUnpublish();
204
        $discountTwo->doUnpublish();
205
206
        $newHelper = DiscountHelper::create($product);
207
208
        $this->assertEquals('discount_quantity_amount', $newHelper->getFoxyDiscountType());
209
    }
210
211
    /**
212
     *
213
     */
214
    public function testGetDiscountFieldValue()
215
    {
216
        $product = $this->objFromFixture(ProductPage::class, 'productthree');
217
        $helper = DiscountHelper::create($product);
218
219
        $this->assertEquals("Unrestricted Discount{allunits|1-25}", $helper->getDiscountFieldValue());
220
221
        $discountOne = $this->objFromFixture(Discount::class, 'simplediscountpercentage');
222
        $discountOne->doUnpublish();
223
224
        $newHelper = DiscountHelper::create($product);
225
226
        $this->assertEquals(
227
            'Unrestricted Discount Tiered Percentage{allunits|1-5|5-10|20-30}',
228
            $newHelper->getDiscountFieldValue()
229
        );
230
    }
231
232
    /**
233
     * @throws ValidationException
234
     */
235
    public function testMemberDiscount()
236
    {
237
        /** @var Member $customer */
238
        $customer = $this->objFromFixture(Member::class, 'default');
239
        $alternateCustomer = $this->objFromFixture(Member::class, 'site-owner');
240
241
        $customerDiscount = Discount::create();
242
        $customerDiscount->Title = 'Amazing Customer Discount';
243
        $customerDiscount->Type = 'Percent';
244
        $customerDiscount->writeToStage(Versioned::DRAFT);
245
        $customerDiscount->publishSingle();
246
247
        $cdTier = DiscountTier::create();
248
        $cdTier->ParentType = $customerDiscount->Type;
249
        $cdTier->Quantity = 1;
250
        $cdTier->Percentage = 90;
251
        $cdTier->DiscountID = $customerDiscount->ID;
252
        $cdTier->write();
253
254
        $customer->DiscountID = $customerDiscount->ID;
255
        $customerDiscount->write();
256
257
        $this->logInAs($customer);
258
259
        $product = $this->objFromFixture(ProductPage::class, 'productthree');
260
        $helper = DiscountHelper::create($product);
261
262
        $this->assertEquals(10, $helper->getDiscountedPrice()->getValue());
263
264
        $this->logInAs($alternateCustomer);
265
        $newHelper = DiscountHelper::create($product);
266
267
        $this->assertEquals(75, $newHelper->getDiscountedPrice()->getValue());
268
    }
269
270
    /**
271
     *
272
     */
273
    public function testExcludeProductFromDiscounts()
274
    {
275
        $product = $this->objFromFixture(ProductPage::class, 'productthree');
276
        $helper = DiscountHelper::create($product);
277
278
        $this->assertInstanceOf(DiscountTier::class, $helper->getDiscountTier());
279
280
        $product->ExcludeFromDiscounts = true;
281
        $product->writeToStage(Versioned::DRAFT);
282
        $product->publishSingle();
283
284
        $newHelper = DiscountHelper::create($product);
285
286
        $this->assertNull($newHelper->getDiscountTier());
287
    }
288
289
    /**
290
     * @throws ValidationException
291
     */
292
    public function testMemberDiscountMultipleMembers()
293
    {
294
        /** @var Member $customer */
295
        $customer = $this->objFromFixture(Member::class, 'default');
296
        /** @var Member $alternateCustomer */
297
        $alternateCustomer = $this->objFromFixture(Member::class, 'site-owner');
298
299
        $customerDiscount = Discount::create();
300
        $customerDiscount->Title = 'Amazing Customer Discount';
301
        $customerDiscount->Type = 'Percent';
302
        $customerDiscount->writeToStage(Versioned::DRAFT);
303
        $customerDiscount->publishSingle();
304
305
        $cdTier = DiscountTier::create();
306
        $cdTier->ParentType = $customerDiscount->Type;
307
        $cdTier->Quantity = 1;
308
        $cdTier->Percentage = 90;
309
        $cdTier->DiscountID = $customerDiscount->ID;
310
        $cdTier->write();
311
312
        $customer->DiscountID = $customerDiscount->ID;
313
        $customer->write();
314
315
        $customerDiscount2 = Discount::create();
316
        $customerDiscount2->Title = 'Amazing Customer Discount 2';
317
        $customerDiscount2->Type = 'Percent';
318
        $customerDiscount2->writeToStage(Versioned::DRAFT);
319
        $customerDiscount2->publishSingle();
320
321
        $cdTier2 = DiscountTier::create();
322
        $cdTier2->ParentType = $customerDiscount2->Type;
323
        $cdTier2->Quantity = 1;
324
        $cdTier2->Percentage = 80;
325
        $cdTier2->DiscountID = $customerDiscount2->ID;
326
        $cdTier2->write();
327
328
        $alternateCustomer->DiscountID = $customerDiscount2->ID;
329
        $alternateCustomer->write();
330
331
        $this->logInAs($customer);
332
333
        $product = $this->objFromFixture(ProductPage::class, 'productthree');
334
        $helper = DiscountHelper::create($product);
335
336
        $this->assertEquals(10, $helper->getDiscountedPrice()->getValue());
337
338
        $this->logInAs($alternateCustomer);
339
        $newHelper = DiscountHelper::create($product);
340
341
        $this->assertEquals(20, $newHelper->getDiscountedPrice()->getValue());
342
    }
343
}
344