Completed
Push — master ( 3c661d...2a57f9 )
by Will
26s queued 12s
created

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