ProductDataExtensionTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 45
c 1
b 0
f 0
dl 0
loc 116
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 19 3
A testGetHasDiscount() 0 19 1
A testGetBestDiscount() 0 5 1
A testGetDiscountPrice() 0 10 1
1
<?php
2
3
namespace Dynamic\Foxy\Discounts\Tests\Page;
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\Tests\TestOnly\Extension\TestDiscountExtension;
9
use Dynamic\Foxy\Discounts\Tests\TestOnly\Extension\VariationDataExtension;
10
use Dynamic\Foxy\Discounts\Tests\TestOnly\Page\ProductPage;
11
use Dynamic\Foxy\Extension\Purchasable;
12
use Dynamic\Foxy\Model\Variation;
13
use SilverStripe\Core\Config\Config;
14
use SilverStripe\Dev\SapphireTest;
15
use SilverStripe\Versioned\Versioned;
16
17
/**
18
 * Class ProductPageDiscountTest
19
 * @package Dynamic\Foxy\Discounts\Tests\Page
20
 */
21
class ProductDataExtensionTest extends SapphireTest
22
{
23
    /**
24
     * @var string[]
25
     */
26
    protected static $fixture_file = [
27
        '../products.yml',
28
        '../discounts.yml',
29
    ];
30
31
    /**
32
     * @var string[]
33
     */
34
    protected static $extra_dataobjects = [
35
        ProductPage::class,
36
    ];
37
38
    /**
39
     * @var \string[][]
40
     */
41
    protected static $required_extensions = [
42
        ProductPage::class => [
43
            Purchasable::class,
44
            ProductDataExtension::class,
45
        ],
46
        Discount::class => [
47
            TestDiscountExtension::class,
48
        ],
49
        Variation::class => [
50
            VariationDataExtension::class,
51
        ],
52
53
    ];
54
55
    /**
56
     * @var string[]
57
     */
58
    protected static $illegal_extensions = [
59
        /*Discount::class => [
60
            'Dynamic\\FoxyRecipe\\Extension\\DiscountDataExtension',
61
        ],//*/
62
    ];
63
64
    /**
65
     *
66
     */
67
    protected function setUp()
68
    {
69
        if (class_exists('Dynamic\\Foxy\\API\\Client\\APIClient')) {
70
            Config::modify()->set('Dynamic\\Foxy\\API\\Client\\APIClient', 'enable_api', false);
71
        }
72
        if (class_exists('Dynamic\\Foxy\\SingleSignOn\\Client\\CustomerClient')) {
73
            Config::modify()->set('Dynamic\\Foxy\\SingleSignOn\\Client\\CustomerClient', 'foxy_sso_enabled', false);
74
        }
75
76
        parent::setUp();
77
78
        $product = $this->objFromFixture(ProductPage::class, 'productthree');
79
        $product->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
80
        $discountOne = $this->objFromFixture(Discount::class, 'simplediscountpercentage');
81
        $discountOne->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
82
        $discountTwo = $this->objFromFixture(Discount::class, 'tierdiscountpercentage');
83
        $discountTwo->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
84
85
        Versioned::set_stage(Versioned::LIVE);
86
    }
87
88
    /**
89
     *
90
     */
91
    public function testGetBestDiscount()
92
    {
93
        $product = $product = $this->objFromFixture(ProductPage::class, 'productthree');
0 ignored issues
show
Unused Code introduced by
The assignment to $product is dead and can be removed.
Loading history...
94
95
        $this->assertInstanceOf(DiscountHelper::class, $product->getBestDiscount());
96
97
        //TODO test user_error for a 0 value
98
    }
99
100
    /**
101
     *
102
     */
103
    public function testGetDiscountPrice()
104
    {
105
        $product = $this->objFromFixture(ProductPage::class, 'productthree');
106
107
        $this->assertEquals(75, $product->getDiscountPrice()->getValue());
108
109
        $discountOne = $this->objFromFixture(Discount::class, 'simplediscountpercentage');
110
        $discountOne->doUnpublish();
111
112
        $this->assertEquals(70, $product->getDiscountPrice(23)->getValue());
113
    }
114
115
    /**
116
     * Expected failure in local tests of a foxy-recipe-installer
117
     */
118
    public function testGetHasDiscount()
119
    {
120
        $product = $this->objFromFixture(ProductPage::class, 'productthree');
121
122
        $this->assertTrue($product->getHasDiscount());
123
124
        $newProduct = ProductPage::create();
125
        $newProduct->Title = 'No Discount Product';
126
        $newProduct->Code = 'no-discount-product';
127
        $newProduct->Price = 1000;
128
        $newProduct->writeToStage(Versioned::DRAFT);
129
        $newProduct->publishSingle();
130
131
        /** Expected failure in local tests of a foxy-recipe-installer */
132
        Discount::get()->each(function (Discount $discount) use ($newProduct) {
133
            $discount->ExcludeProducts()->add($newProduct);
0 ignored issues
show
Bug introduced by
The method ExcludeProducts() does not exist on Dynamic\Foxy\Discounts\Model\Discount. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

133
            $discount->/** @scrutinizer ignore-call */ 
134
                       ExcludeProducts()->add($newProduct);
Loading history...
134
        });
135
136
        $this->assertFalse($newProduct->getHasDiscount());
137
    }
138
}
139