FlatTaxModifierTest   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 41
c 1
b 0
f 0
dl 0
loc 74
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testInclusiveTax() 0 15 1
A setUp() 0 21 1
A testExclusiveTax() 0 15 1
1
<?php
2
3
namespace SilverShop\Tests\Model\Modifiers;
4
5
use SilverShop\Cart\ShoppingCart;
6
use SilverShop\Model\Modifiers\OrderModifier;
7
use SilverShop\Model\Modifiers\Tax\FlatTax;
8
use SilverShop\Model\Order;
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
15
/**
16
 * @package    shop
17
 * @subpackage tests
18
 */
19
class FlatTaxModifierTest extends FunctionalTest
20
{
21
    protected static $fixture_file = __DIR__ . '/../../Fixtures/shop.yml';
22
    protected static $disable_theme = true;
23
24
    protected static $extra_dataobjects = [
25
        CustomProduct_OrderItem::class
26
    ];
27
28
    /**
29
     * @var Product
30
     */
31
    protected $mp3player;
32
33
    /**
34
     * @var ShoppingCart
35
     */
36
    protected $cart;
37
38
    public function setUp(): void
39
    {
40
        parent::setUp();
41
        ShoppingCart::singleton()->clear();
42
        ShopTest::setConfiguration();
43
44
        Config::modify()
45
            ->set(
46
                Order::class,
47
                'modifiers',
48
                [
49
                    FlatTax::class
50
                ]
51
            )
52
            ->set(FlatTax::class, 'name', 'GST')
53
            ->set(FlatTax::class, 'rate', 0.15);
54
55
        $this->logInWithPermission('ADMIN');
56
        $this->cart = ShoppingCart::singleton();
57
        $this->mp3player = $this->objFromFixture(Product::class, 'mp3player');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->objFromFixture(Si...ct::class, 'mp3player') of type SilverStripe\ORM\DataObject is incompatible with the declared type SilverShop\Page\Product of property $mp3player.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
58
        $this->mp3player->publishSingle();
59
    }
60
61
    public function testInclusiveTax()
62
    {
63
        Config::modify()->set(FlatTax::class, 'exclusive', false);
64
        $this->cart->clear();
65
        $this->cart->add($this->mp3player);
66
        $order = $this->cart->current();
67
        $order->calculate();
68
        /**
69
         * @var OrderModifier $modifier
70
         */
71
        $modifier = $order->Modifiers()
72
            ->filter('ClassName', FlatTax::class)
73
            ->first();
74
        $this->assertEquals(26.09, $modifier->Amount); //remember that 15% tax inclusive is different to exclusive
75
        $this->assertEquals(200, $order->GrandTotal());
76
    }
77
78
    public function testExclusiveTax()
79
    {
80
        Config::modify()->set(FlatTax::class, 'exclusive', true);
81
        $this->cart->clear();
82
        $this->cart->add($this->mp3player);
83
        $order = $this->cart->current();
84
        $order->calculate();
85
        /**
86
         * @var OrderModifier $modifier
87
         */
88
        $modifier = $order->Modifiers()
89
            ->filter('ClassName', FlatTax::class)
90
            ->first();
91
        $this->assertEquals(30, $modifier->Amount);
92
        $this->assertEquals(230, $order->GrandTotal());
93
    }
94
}
95