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 | public static $fixture_file = __DIR__ . '/../Fixtures/shop.yml'; |
||||
17 | public static $disable_theme = true; |
||||
18 | protected static $use_draft_site = false; |
||||
19 | |||||
20 | /** |
||||
21 | * @var Product |
||||
22 | */ |
||||
23 | protected $product; |
||||
24 | |||||
25 | /** |
||||
26 | * @var ShoppingCart |
||||
27 | */ |
||||
28 | protected $cart; |
||||
29 | |||||
30 | public function setUp() |
||||
31 | { |
||||
32 | parent::setUp(); |
||||
33 | ShopTest::setConfiguration(); //reset config |
||||
34 | Config::modify()->set(Order::class, 'extensions', [ShoppingCartTest_TestShoppingCartHooksExtension::class]); |
||||
35 | |||||
36 | ShoppingCart::singleton()->clear(); |
||||
37 | ShoppingCartTest_TestShoppingCartHooksExtension::reset(); |
||||
38 | $this->cart = ShoppingCart::singleton(); |
||||
39 | $this->product = $this->objFromFixture(Product::class, 'mp3player'); |
||||
40 | $this->product->publishSingle(); |
||||
41 | } |
||||
42 | |||||
43 | public function testAddToCart() |
||||
44 | { |
||||
45 | $this->assertTrue((boolean)$this->cart->add($this->product), "add one item"); |
||||
46 | $this->assertEquals( |
||||
47 | ['onStartOrder', 'beforeAdd', 'afterAdd'], |
||||
48 | ShoppingCartTest_TestShoppingCartHooksExtension::$stack |
||||
49 | ); |
||||
50 | |||||
51 | $this->assertTrue((boolean)$this->cart->add($this->product), "add another item"); |
||||
52 | $this->assertEquals( |
||||
53 | ['onStartOrder', 'beforeAdd', 'afterAdd', 'beforeAdd', 'afterAdd'], |
||||
54 | ShoppingCartTest_TestShoppingCartHooksExtension::$stack |
||||
55 | ); |
||||
56 | |||||
57 | $item = $this->cart->get($this->product); |
||||
58 | $this->assertEquals($item->Quantity, 2, "quantity is 2"); |
||||
59 | } |
||||
60 | |||||
61 | public function testRemoveFromCart() |
||||
62 | { |
||||
63 | $this->assertTrue((boolean)$this->cart->add($this->product), "add item"); |
||||
64 | $this->assertEquals( |
||||
65 | ['onStartOrder', 'beforeAdd', 'afterAdd'], |
||||
66 | ShoppingCartTest_TestShoppingCartHooksExtension::$stack |
||||
67 | ); |
||||
68 | |||||
69 | $this->assertTrue($this->cart->remove($this->product), "item was removed"); |
||||
70 | $this->assertEquals( |
||||
71 | ['onStartOrder', 'beforeAdd', 'afterAdd', 'beforeRemove', 'afterRemove'], |
||||
72 | ShoppingCartTest_TestShoppingCartHooksExtension::$stack |
||||
73 | ); |
||||
74 | $item = $this->cart->get($this->product); |
||||
75 | $this->assertFalse((bool)$item, "item not in cart"); |
||||
76 | $this->assertFalse($this->cart->remove($this->product), "try remove non-existent item"); |
||||
77 | } |
||||
78 | |||||
79 | public function testSetQuantity() |
||||
80 | { |
||||
81 | $this->assertTrue((boolean)$this->cart->setQuantity($this->product, 25), "quantity set"); |
||||
82 | |||||
83 | $this->assertEquals( |
||||
84 | ['onStartOrder', 'beforeSetQuantity', 'afterSetQuantity'], |
||||
85 | ShoppingCartTest_TestShoppingCartHooksExtension::$stack |
||||
86 | ); |
||||
87 | |||||
88 | $item = $this->cart->get($this->product); |
||||
89 | $this->assertEquals($item->Quantity, 25, "quantity is 25"); |
||||
90 | } |
||||
91 | |||||
92 | public function testClear() |
||||
93 | { |
||||
94 | //$this->assertFalse($this->cart->current(),"there is no cart initally"); |
||||
95 | $this->assertTrue((boolean)$this->cart->add($this->product), "add one item"); |
||||
96 | $this->assertTrue((boolean)$this->cart->add($this->product), "add another item"); |
||||
97 | $this->assertInstanceOf(Order::class, $this->cart->current(), "there's a cart"); |
||||
98 | $this->assertTrue($this->cart->clear(), "clear the cart"); |
||||
99 | $this->assertFalse((bool)$this->cart->current(), "there is no cart"); |
||||
100 | } |
||||
101 | |||||
102 | public function testProductVariations() |
||||
103 | { |
||||
104 | $this->loadFixture(__DIR__ . '/../Fixtures/variations.yml'); |
||||
105 | $ball1 = $this->objFromFixture(Variation::class, 'redlarge'); |
||||
106 | $ball2 = $this->objFromFixture(Variation::class, 'redsmall'); |
||||
107 | |||||
108 | $this->assertTrue((boolean)$this->cart->add($ball1), "add one item"); |
||||
109 | |||||
110 | $this->assertEquals( |
||||
111 | ['onStartOrder', 'beforeAdd', 'afterAdd'], |
||||
112 | ShoppingCartTest_TestShoppingCartHooksExtension::$stack |
||||
113 | ); |
||||
114 | |||||
115 | $this->assertTrue((boolean)$this->cart->add($ball2), "add another item"); |
||||
116 | $this->assertTrue($this->cart->remove($ball1), "remove first item"); |
||||
117 | |||||
118 | $this->assertEquals( |
||||
119 | ['onStartOrder', 'beforeAdd', 'afterAdd', 'beforeAdd', 'afterAdd', 'beforeRemove', 'afterRemove'], |
||||
120 | ShoppingCartTest_TestShoppingCartHooksExtension::$stack |
||||
121 | ); |
||||
122 | |||||
123 | $this->assertFalse((bool)$this->cart->get($ball1), "first item not in cart"); |
||||
124 | $this->assertNotNull($this->cart->get($ball2), "second item is in cart"); |
||||
125 | } |
||||
126 | |||||
127 | public function testCartSingleton() |
||||
128 | { |
||||
129 | $this->assertTrue((boolean)$this->cart->add($this->product), "add one item"); |
||||
130 | $order = $this->cart->current(); |
||||
131 | |||||
132 | $this->assertEquals($order->ID, ShoppingCart::curr()->ID, "if singleton order ids will match"); |
||||
133 | } |
||||
134 | |||||
135 | public function testAddProductWithVariations() |
||||
136 | { |
||||
137 | $this->loadFixture(__DIR__ . '/../Fixtures/variations.yml'); |
||||
0 ignored issues
–
show
|
|||||
138 | $ball = $this->objFromFixture(Product::class, 'ball'); |
||||
139 | $redlarge = $this->objFromFixture(Variation::class, 'redlarge'); |
||||
140 | // setting price of variation to zero, so it can't be added to cart. |
||||
141 | $redlarge->Price = 0; |
||||
142 | $redlarge->write(); |
||||
143 | |||||
144 | $ball->BasePrice = 0; |
||||
145 | $ball->write(); |
||||
146 | |||||
147 | $item = $this->cart->add($ball); |
||||
0 ignored issues
–
show
$ball of type SilverStripe\ORM\DataObject is incompatible with the type SilverShop\Model\Buyable expected by parameter $buyable of SilverShop\Cart\ShoppingCart::add() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
148 | $this->assertNotNull($item, "Product with variations can be added to cart"); |
||||
149 | $this->assertInstanceOf(OrderItem::class, $item, 'A variation should be added to cart.'); |
||||
150 | $this->assertEquals(20, $item->Buyable()->Price, 'The buyable variation was added'); |
||||
151 | } |
||||
152 | |||||
153 | public function testErrorInCartHooks() |
||||
154 | { |
||||
155 | Config::modify()->set(Order::class, 'extensions', [ShoppingCartTest_TestShoppingCartErroringHooksExtension::class]); |
||||
156 | |||||
157 | ShoppingCart::singleton()->clear(); |
||||
158 | $cart = ShoppingCart::singleton(); |
||||
159 | |||||
160 | $this->assertTrue((boolean)$this->cart->add($this->product, 1), "add one item"); |
||||
161 | $item = $cart->get($this->product); |
||||
162 | $this->assertFalse( |
||||
163 | (boolean)$this->cart->add($this->product, 1), |
||||
164 | "Cannot add more than one item, extension will error" |
||||
165 | ); |
||||
166 | $this->assertEquals($item->Quantity, 1, "quantity is 1"); |
||||
167 | |||||
168 | $this->assertTrue((boolean)$cart->setQuantity($this->product, 10), "quantity set"); |
||||
169 | $item = $cart->get($this->product); |
||||
170 | $this->assertEquals($item->Quantity, 10, "quantity is 10"); |
||||
171 | |||||
172 | $this->assertFalse((boolean)$cart->setQuantity($this->product, 11), "Cannot set quantity to more than 10 items"); |
||||
173 | $item = $cart->get($this->product); |
||||
174 | $this->assertEquals($item->Quantity, 10, "quantity is 10"); |
||||
175 | } |
||||
176 | } |
||||
177 |
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.