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

ProductDataExtensionTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 12
rs 10
cc 1
nc 1
nop 0
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\Page\ProductPage;
9
use Dynamic\Foxy\Extension\Purchasable;
10
use SilverStripe\Dev\SapphireTest;
11
use SilverStripe\Versioned\Versioned;
12
13
/**
14
 * Class ProductPageDiscountTest
15
 * @package Dynamic\Foxy\Discounts\Tests\Page
16
 */
17
class ProductDataExtensionTest extends SapphireTest
18
{
19
    /**
20
     * @var string[]
21
     */
22
    protected static $fixture_file = [
23
        '../products.yml',
24
        '../discounts.yml',
25
    ];
26
27
    /**
28
     * @var string[]
29
     */
30
    protected static $extra_dataobjects = [
31
        ProductPage::class,
32
    ];
33
34
    /**
35
     * @var \string[][]
36
     */
37
    protected static $required_extensions = [
38
        ProductPage::class => [
39
            Purchasable::class,
40
            ProductDataExtension::class,
41
        ],
42
    ];
43
44
    /**
45
     *
46
     */
47
    protected function setUp()
48
    {
49
        parent::setUp();
50
51
        $product = $this->objFromFixture(ProductPage::class, 'productthree');
52
        $product->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
53
        $discountOne = $this->objFromFixture(Discount::class, 'simplediscountpercentage');
54
        $discountOne->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
55
        $discountTwo = $this->objFromFixture(Discount::class, 'tierdiscountpercentage');
56
        $discountTwo->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
57
58
        Versioned::set_stage(Versioned::LIVE);
59
    }
60
61
    /**
62
     *
63
     */
64
    public function testGetBestDiscount()
65
    {
66
        $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...
67
68
        $this->assertInstanceOf(DiscountHelper::class, $product->getBestDiscount());
69
70
        //TODO test user_error for a 0 value
71
    }
72
73
    /**
74
     *
75
     */
76
    public function testGetDiscountPrice()
77
    {
78
        $product = $this->objFromFixture(ProductPage::class, 'productthree');
79
80
        $this->assertEquals(75, $product->getDiscountPrice()->getValue());
81
82
        $discountOne = $this->objFromFixture(Discount::class, 'simplediscountpercentage');
83
        $discountOne->doUnpublish();
84
85
        $this->assertEquals(70, $product->getDiscountPrice(23)->getValue());
86
    }
87
88
    /**
89
     *
90
     */
91
    public function testGetHasDiscount()
92
    {
93
        $product = $this->objFromFixture(ProductPage::class, 'productthree');
94
95
        $this->assertTrue($product->getHasDiscount());
96
97
        $newProduct = ProductPage::create();
98
        $newProduct->Title = 'No Discount Product';
99
        $newProduct->Code = 'no-discount-product';
100
        $newProduct->Price = 1000;
101
        $newProduct->writeToStage(Versioned::DRAFT);
102
        $newProduct->publishSingle();
103
104
        Discount::get()->each(function (Discount $discount) use ($newProduct) {
105
            $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

105
            $discount->/** @scrutinizer ignore-call */ 
106
                       ExcludeProducts()->add($newProduct);
Loading history...
106
        });
107
108
        $this->assertFalse($newProduct->getHasDiscount());
109
    }
110
}
111