1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace yii2mod\cart\tests; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii2mod\cart\storage\StorageInterface; |
7
|
|
|
use yii2mod\cart\tests\data\Product; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class CartTest |
11
|
|
|
* |
12
|
|
|
* @package yii2mod\cart\tests |
13
|
|
|
*/ |
14
|
|
|
class CartTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
public function testEmptyCart() |
17
|
|
|
{ |
18
|
|
|
$this->assertEmpty(Yii::$app->cart->getItems()); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function testAddItem() |
22
|
|
|
{ |
23
|
|
|
$cart = Yii::$app->cart; |
24
|
|
|
$product = Product::findOne(1); |
25
|
|
|
$cart->add($product); |
26
|
|
|
|
27
|
|
|
$this->assertContains($product, $cart->getItems()); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
View Code Duplication |
public function testRemoveItem() |
|
|
|
|
31
|
|
|
{ |
32
|
|
|
$cart = Yii::$app->cart; |
33
|
|
|
$product = Product::findOne(1); |
34
|
|
|
$cart->add($product); |
35
|
|
|
|
36
|
|
|
$this->assertContains($product, $cart->getItems()); |
37
|
|
|
|
38
|
|
|
$cart->remove($product->getUniqueId()); |
39
|
|
|
$this->assertEmpty($cart->getItems()); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
View Code Duplication |
public function testClearCart() |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
$cart = Yii::$app->cart; |
45
|
|
|
$product = Product::findOne(1); |
46
|
|
|
$cart->add($product); |
47
|
|
|
|
48
|
|
|
$this->assertEquals(1, $cart->getCount()); |
49
|
|
|
|
50
|
|
|
$cart->clear(); |
51
|
|
|
$this->assertEmpty($cart->getItems()); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testGetAttributeTotalValue() |
55
|
|
|
{ |
56
|
|
|
$cart = Yii::$app->cart; |
57
|
|
|
$product = Product::findOne(1); |
58
|
|
|
$cart->add($product); |
59
|
|
|
|
60
|
|
|
$this->assertEquals(100, $cart->getAttributeTotal('price')); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testGetItems() |
64
|
|
|
{ |
65
|
|
|
$cart = Yii::$app->cart; |
66
|
|
|
$product = Product::findOne(1); |
67
|
|
|
$cart->add($product); |
68
|
|
|
|
69
|
|
|
$items = $cart->getItems(); |
70
|
|
|
|
71
|
|
|
$this->assertCount(1, $items); |
72
|
|
|
$this->assertEquals('Amazon Kindle', $items[1]['name']); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function testGetStorage() |
76
|
|
|
{ |
77
|
|
|
$this->assertInstanceOf(StorageInterface::class, Yii::$app->cart->getStorage()); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.