Issues (464)

tests/php/Model/Modifiers/OrderModifierTest.php (1 issue)

1
<?php
2
3
namespace SilverShop\Tests\Model\Modifiers;
4
5
use Exception;
6
use SilverShop\Model\Modifiers\Tax\FlatTax;
7
use SilverShop\Model\Order;
8
use SilverShop\Model\OrderModifier;
9
use SilverShop\Page\Product;
10
use SilverShop\Tests\Model\Product\CustomProduct_OrderItem;
11
use SilverShop\Tests\ShopTest;
12
use SilverStripe\Core\Config\Config;
13
use SilverStripe\Dev\FunctionalTest;
14
use SilverStripe\ORM\DB;
15
16
/**
17
 * @package    shop
18
 * @subpackage tests
19
 */
20
class OrderModifierTest extends FunctionalTest
21
{
22
    public static $fixture_file = __DIR__ . '/../../Fixtures/shop.yml';
23
    public static $disable_theme = true;
24
    protected $usesTransactions = false;
25
    protected static $use_draft_site = true;
26
27
    /**
28
     * @var Product
29
     */
30
    protected $mp3player;
31
32
    /**
33
     * @var Product
34
     */
35
    protected $socks;
36
37
    protected static $extra_dataobjects = [
38
        CustomProduct_OrderItem::class,
39
        OrderModifierTest_TestModifier::class
40
    ];
41
42
    public function setUp(): void
43
    {
44
        parent::setUp();
45
        ShopTest::setConfiguration();
46
47
        Config::modify()
48
            ->set(
49
                Order::class,
50
                'modifiers',
51
                [
52
                    FlatTax::class
53
                ]
54
            )
55
            ->set(FlatTax::class, 'rate', 0.25)
56
            ->set(FlatTax::class, 'name', 'GST');
57
58
59
        $this->logInWithPermission('ADMIN');
60
        $this->mp3player = $this->objFromFixture(Product::class, 'mp3player');
61
        $this->socks = $this->objFromFixture(Product::class, 'socks');
62
        $this->mp3player->publishSingle();
63
        $this->socks->publishSingle();
64
    }
65
66
    public function testModifierCalculation()
67
    {
68
        $order = $this->createOrder();
69
        $this->assertEquals(510, $order->calculate(), "Total with 25% tax");
70
71
        //remove modifiers
72
        Order::config()->modifiers = null;
73
        $order->calculate();
74
        $this->assertEquals(408, $order->calculate(), "Total with no modification");
75
    }
76
77
    public function testModifierFailure()
78
    {
79
        if (!DB::get_conn()->supportsTransactions()) {
80
            $this->markTestSkipped(
81
                'The Database doesn\'t support transactions.'
82
            );
83
        }
84
85
        Config::modify()->set(
86
            Order::class,
87
            'modifiers',
88
            [
89
                OrderModifierTest_TestModifier::class,
90
                FlatTax::class
91
            ]
92
        );
93
94
        $order = $this->createOrder();
95
        $order->calculate();
96
        $order->write();
97
98
        // 408 from items + 10 from modifier + 25% from tax
99
        $this->assertEquals('522.5', $order->Total);
100
101
        $amounts = [];
102
        foreach ($order->Modifiers()->sort('Sort') as $modifier) {
103
            $amounts[] = (string)$modifier->Amount;
104
        }
105
106
        $this->assertEquals(['10', '104.5'], $amounts);
107
108
        OrderModifierTest_TestModifier::$value = 42;
109
110
        try {
111
            // Calculate will now fail!
112
            $order->calculate();
113
        } catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
114
        }
115
116
        // reload order from DB
117
        $order = Order::get()->byID($order->ID);
118
119
        // Order Total should not have changed
120
        $this->assertEquals('522.5', $order->Total);
121
122
        $amounts = [];
123
        foreach ($order->Modifiers()->sort('Sort') as $modifier) {
124
            $amounts[] = (string)$modifier->Amount;
125
        }
126
127
        $this->assertEquals(['10', '104.5'], $amounts);
128
    }
129
130
    public function createOrder()
131
    {
132
        $order = new Order();
133
        $order->write();
134
        $item1a = $this->mp3player->createItem(2);
135
        $item1a->write();
136
        $order->Items()->add($item1a);
137
        $item1b = $this->socks->createItem();
138
        $item1b->write();
139
        $order->Items()->add($item1b);
140
        return $order;
141
    }
142
}
143