Passed
Pull Request — master (#49)
by Nic
02:53
created

DiscountHelperTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
eloc 84
c 2
b 0
f 0
dl 0
loc 150
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 48 2
A testGetProduct() 0 6 1
A testGetDiscountedPrice() 0 24 1
A testGetDiscountedPriceVariation() 0 24 1
1
<?php
2
3
namespace Dynamic\Foxy\Discounts\Tests;
4
5
use Dynamic\Foxy\API\Client\APIClient;
0 ignored issues
show
Bug introduced by
The type Dynamic\Foxy\API\Client\APIClient was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Dynamic\Foxy\Discounts\DiscountHelper;
7
use Dynamic\Foxy\Discounts\Extension\ProductDataExtension;
8
use Dynamic\Foxy\Discounts\Model\Discount;
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\Versioned\Versioned;
18
19
/**
20
 * Class DiscountHelperTest
21
 * @package Dynamic\Foxy\Discounts\Tests
22
 */
23
class DiscountHelperTest extends SapphireTest
24
{
25
    /**
26
     * @var string[]
27
     */
28
    protected static $fixture_file = [
29
        'products.yml',
30
        'discounts.yml',
31
    ];
32
33
    /**
34
     * @var string[]
35
     */
36
    protected static $extra_dataobjects = [
37
        ProductPage::class,
38
    ];
39
40
    /**
41
     * @var \string[][]
42
     */
43
    protected static $required_extensions = [
44
        ProductPage::class => [
45
            Purchasable::class,
46
            ProductDataExtension::class,
47
        ],
48
        Discount::class => [
49
            TestDiscountExtension::class,
50
        ],
51
        Variation::class => [
52
            VariationDataExtension::class,
53
        ],
54
    ];
55
56
    /**
57
     *
58
     */
59
    protected function setUp()
60
    {
61
        APIClient::config()->set('enable_api', false);
62
        if (class_exists('Dynamic\Foxy\SingleSignOn\Client\CustomerClient')) {
63
            Config::modify()->set('Dynamic\Foxy\SingleSignOn\Client\CustomerClient', 'foxy_sso_enabled', false);
64
        }
65
66
        parent::setUp();
67
68
        $product = $this->objFromFixture(ProductPage::class, 'productthree');
69
        $product->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
70
        $productTwo = $this->objFromFixture(ProductPage::class, 'productfiveandvariations');
71
        $productTwo->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
72
        $discountOne = $this->objFromFixture(Discount::class, 'simplediscountpercentage');
73
        $discountOne->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
74
        $discountTwo = $this->objFromFixture(Discount::class, 'tierdiscountpercentage');
75
        $discountTwo->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
76
        $discountThree = $this->objFromFixture(Discount::class, 'tierdiscountamount');
77
        $discountThree->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
78
79
        Versioned::set_stage(Versioned::LIVE);
80
81
        $variationOne = Variation::create();
82
        $variationOne->Title = 'Variation One';
83
        $variationOne->PriceModifier = 10;
84
        $variationOne->PriceModifierAction = 'Add';
85
        $variationOne->Available = true;
86
        $variationOne->VariationTypeID = VariationType::get()->first()->ID;
87
        $variationOne->ProductID = $productTwo->ID;
88
        $variationOne->write();
89
90
        $variationTwo = Variation::create();
91
        $variationTwo->Title = 'Variation Two';
92
        $variationTwo->PriceModifier = 150;
93
        $variationTwo->PriceModifierAction = 'Set';
94
        $variationTwo->Available = true;
95
        $variationTwo->VariationTypeID = VariationType::get()->first()->ID;
96
        $variationTwo->ProductID = $productTwo->ID;
97
        $variationTwo->write();
98
99
        $variationThree = Variation::create();
100
        $variationThree->Title = 'Variation Three';
101
        $variationThree->PriceModifier = 20;
102
        $variationThree->PriceModifierAction = 'Subtract';
103
        $variationThree->Available = true;
104
        $variationThree->VariationTypeID = VariationType::get()->first()->ID;
105
        $variationThree->ProductID = $productTwo->ID;
106
        $variationThree->write();
107
    }
108
109
    public function testGetProduct()
110
    {
111
        $product = $this->objFromFixture(ProductPage::class, 'productthree');
112
        $helper = DiscountHelper::create($product);
113
114
        $this->assertEquals($product->ID, $helper->getProduct()->ID);
115
    }
116
117
    /**
118
     *
119
     */
120
    public function testGetDiscountedPrice()
121
    {
122
        $product = $this->objFromFixture(ProductPage::class, 'productthree');
123
        $helper = DiscountHelper::create($product);
124
125
        $this->assertEquals(75, $helper->getDiscountedPrice()->getValue());
126
127
        $helper->setQuantity(25);
128
        $this->assertEquals(70, $helper->getDiscountedPrice()->getValue());
129
130
        $discountOne = $this->objFromFixture(Discount::class, 'simplediscountpercentage');
131
        $discountOne->doUnpublish();
132
133
        $helper->setDiscountTier();
134
        $helper->setQuantity(1);
135
        $this->assertEquals(95, $helper->getDiscountedPrice()->getValue());
136
137
        $helper->setQuantity(6);
138
        $this->assertEquals(88, $helper->getDiscountedPrice()->getValue());
139
140
        $helper->setQuantity(23);
141
        $this->assertEquals(70, $helper->getDiscountedPrice()->getValue());
142
143
        $product->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
144
    }
145
146
    /**
147
     *
148
     */
149
    public function testGetDiscountedPriceVariation()
150
    {
151
        $product = $this->objFromFixture(ProductPage::class, 'productfiveandvariations');
152
153
        $variationOne = Variation::get()->filter('PriceModifierAction', 'Add')->first();
154
        $variationTwo = Variation::get()->filter('PriceModifierAction', 'Set')->first();
155
        $variationThree = Variation::get()->filter('PriceModifierAction', 'Subtract')->first();
156
157
        //$110 less 25%
158
        $this->assertEquals(
159
            82.5,
160
            DiscountHelper::create($product, 1, $variationOne)->getDiscountedPrice()->getValue()
161
        );
162
163
        //$150 less 25%
164
        $this->assertEquals(
165
            112.5,
166
            DiscountHelper::create($product, 1, $variationTwo)->getDiscountedPrice()->getValue()
167
        );
168
169
        //$80 less 25%
170
        $this->assertEquals(
171
            60,
172
            DiscountHelper::create($product, 1, $variationThree)->getDiscountedPrice()->getValue()
173
        );
174
    }
175
}
176