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

tests/php/Cart/ShoppingCartTest.php (1 issue)

1
<?php
2
3
namespace SilverShop\Tests\Cart;
4
5
use SilverShop\Cart\ShoppingCart;
6
use SilverShop\Model\Order;
7
use SilverShop\Model\Variation\OrderItem;
8
use SilverShop\Model\Variation\Variation;
9
use SilverShop\Page\Product;
10
use SilverShop\Tests\ShopTest;
11
use SilverStripe\Core\Config\Config;
12
use SilverStripe\Dev\SapphireTest;
13
14
class ShoppingCartTest extends SapphireTest
15
{
16
    protected static $fixture_file  = __DIR__ . '/../Fixtures/shop.yml';
17
18
    public static $disable_theme  = true;
19
20
    protected static $use_draft_site = false;
21
22
    /**
23
     * @var Product
24
     */
25
    protected $product;
26
27
    /**
28
     * @var ShoppingCart
29
     */
30
    protected $cart;
31
32
    public function setUp()
33
    {
34
        parent::setUp();
35
        ShopTest::setConfiguration(); //reset config
36
        Config::modify()->set(Order::class, 'extensions', [ShoppingCartTest_TestShoppingCartHooksExtension::class]);
37
38
        ShoppingCart::singleton()->clear();
39
        ShoppingCartTest_TestShoppingCartHooksExtension::reset();
40
41
        $this->cart = ShoppingCart::singleton();
42
        $this->product = $this->objFromFixture(Product::class, 'mp3player');
43
        $this->product->publishSingle();
44
    }
45
46
    public function testAddToCart()
47
    {
48
        $this->assertTrue((boolean)$this->cart->add($this->product), "add one item");
49
        $this->assertEquals(
50
            ['onStartOrder', 'beforeAdd', 'afterAdd'],
51
            ShoppingCartTest_TestShoppingCartHooksExtension::$stack
52
        );
53
54
        $this->assertTrue((boolean)$this->cart->add($this->product), "add another item");
55
        $this->assertEquals(
56
            ['onStartOrder', 'beforeAdd', 'afterAdd', 'beforeAdd', 'afterAdd'],
57
            ShoppingCartTest_TestShoppingCartHooksExtension::$stack
58
        );
59
60
        $item = $this->cart->get($this->product);
61
        $this->assertEquals($item->Quantity, 2, "quantity is 2");
62
    }
63
64
    public function testRemoveFromCart()
65
    {
66
        $this->assertTrue((boolean)$this->cart->add($this->product), "add item");
67
        $this->assertEquals(
68
            ['onStartOrder', 'beforeAdd', 'afterAdd'],
69
            ShoppingCartTest_TestShoppingCartHooksExtension::$stack
70
        );
71
72
        $this->assertTrue($this->cart->remove($this->product), "item was removed");
73
        $this->assertEquals(
74
            ['onStartOrder', 'beforeAdd', 'afterAdd', 'beforeRemove', 'afterRemove'],
75
            ShoppingCartTest_TestShoppingCartHooksExtension::$stack
76
        );
77
        $item = $this->cart->get($this->product);
78
        $this->assertFalse((bool)$item, "item not in cart");
79
        $this->assertFalse($this->cart->remove($this->product), "try remove non-existent item");
80
    }
81
82
    public function testSetQuantity()
83
    {
84
        $this->assertTrue((boolean)$this->cart->setQuantity($this->product, 25), "quantity set");
85
86
        $this->assertEquals(
87
            ['onStartOrder', 'beforeSetQuantity', 'afterSetQuantity'],
88
            ShoppingCartTest_TestShoppingCartHooksExtension::$stack
89
        );
90
91
        $item = $this->cart->get($this->product);
92
        $this->assertEquals($item->Quantity, 25, "quantity is 25");
93
    }
94
95
    public function testClear()
96
    {
97
        //$this->assertFalse($this->cart->current(),"there is no cart initally");
98
        $this->assertTrue((boolean)$this->cart->add($this->product), "add one item");
99
        $this->assertTrue((boolean)$this->cart->add($this->product), "add another item");
100
        $this->assertInstanceOf(Order::class, $this->cart->current(), "there's a cart");
101
        $this->assertTrue($this->cart->clear(), "clear the cart");
102
        $this->assertFalse((bool)$this->cart->current(), "there is no cart");
103
    }
104
105
    public function testCartSingleton()
106
    {
107
        $this->assertTrue((boolean)$this->cart->add($this->product), "add one item");
108
        $order = $this->cart->current();
109
110
        $this->assertEquals($order->ID, ShoppingCart::curr()->ID, "if singleton order ids will match");
111
    }
112
113
    public function testErrorInCartHooks()
114
    {
115
        Config::modify()->set(Order::class, 'extensions', [ShoppingCartTest_TestShoppingCartErroringHooksExtension::class]);
116
117
        ShoppingCart::singleton()->clear();
118
        $cart = ShoppingCart::singleton();
119
120
        $this->assertTrue((boolean)$this->cart->add($this->product, 1), "add one item");
121
        $item = $cart->get($this->product);
122
        $this->assertFalse(
123
            (boolean)$this->cart->add($this->product, 1),
124
            "Cannot add more than one item, extension will error"
125
        );
126
        $this->assertEquals($item->Quantity, 1, "quantity is 1");
127
128
        $this->assertTrue((boolean)$cart->setQuantity($this->product, 10), "quantity set");
129
        $item = $cart->get($this->product);
130
        $this->assertEquals($item->Quantity, 10, "quantity is 10");
131
132
        $this->assertFalse((boolean)$cart->setQuantity($this->product, 11), "Cannot set quantity to more than 10 items");
133
        $item = $cart->get($this->product);
134
        $this->assertEquals($item->Quantity, 10, "quantity is 10");
135
    }
136
137
    public function testProductVariations()
138
    {
139
        $this->loadFixture(__DIR__ . '/../Fixtures/variations.yml');
0 ignored issues
show
Deprecated Code introduced by
The function SilverStripe\Dev\SapphireTest::loadFixture() has been deprecated: 4.0.0:5.0.0 ( Ignorable by Annotation )

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

139
        /** @scrutinizer ignore-deprecated */ $this->loadFixture(__DIR__ . '/../Fixtures/variations.yml');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
140
        $ball1 = $this->objFromFixture(Variation::class, 'redlarge');
141
        $ball2 = $this->objFromFixture(Variation::class, 'redsmall');
142
143
        $this->assertTrue((boolean)$this->cart->add($ball1), "add one item");
144
145
        $this->assertEquals(
146
            ['onStartOrder', 'beforeAdd', 'afterAdd'],
147
            ShoppingCartTest_TestShoppingCartHooksExtension::$stack
148
        );
149
150
        $this->assertTrue((boolean)$this->cart->add($ball2), "add another item");
151
        $this->assertTrue($this->cart->remove($ball1), "remove first item");
152
153
        $this->assertEquals(
154
            ['onStartOrder', 'beforeAdd', 'afterAdd', 'beforeAdd', 'afterAdd', 'beforeRemove', 'afterRemove'],
155
            ShoppingCartTest_TestShoppingCartHooksExtension::$stack
156
        );
157
158
        $this->assertFalse((bool)$this->cart->get($ball1), "first item not in cart");
159
        $this->assertNotNull($this->cart->get($ball2), "second item is in cart");
160
161
        $ball = $this->objFromFixture(Product::class, 'ball');
162
        $redlarge = $this->objFromFixture(Variation::class, 'redlarge');
163
        // setting price of variation to zero, so it can't be added to cart.
164
        $redlarge->Price = 0;
165
        $redlarge->write();
166
167
        $ball->BasePrice = 0;
168
        $ball->write();
169
170
        $item = $this->cart->add($ball);
171
        $this->assertNotNull($item, "Product with variations can be added to cart");
172
        $this->assertInstanceOf(OrderItem::class, $item, 'A variation should be added to cart.');
173
        $this->assertEquals(20, $item->Buyable()->Price, 'The buyable variation was added');
174
    }
175
}
176