1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Cart module for Yii2 |
5
|
|
|
* |
6
|
|
|
* @link https://github.com/hiqdev/yii2-cart |
7
|
|
|
* @package yii2-cart |
8
|
|
|
* @license BSD-3-Clause |
9
|
|
|
* @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/) |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace hiqdev\yii2\cart\tests\unit; |
13
|
|
|
|
14
|
|
|
use hiqdev\yii2\cart\Module; |
15
|
|
|
use hiqdev\yii2\cart\ShoppingCart; |
16
|
|
|
use Yii; |
17
|
|
|
use yii\helpers\Url; |
18
|
|
|
use yii\web\Application; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Module test suite. |
22
|
|
|
*/ |
23
|
|
|
class ModuleTest extends \PHPUnit\Framework\TestCase |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var Module |
27
|
|
|
*/ |
28
|
|
|
protected $object; |
29
|
|
|
|
30
|
|
|
protected function setUp() |
31
|
|
|
{ |
32
|
|
|
$application = new Application([ |
|
|
|
|
33
|
|
|
'id' => 'mock', |
34
|
|
|
'basePath' => dirname(dirname(__DIR__)), |
35
|
|
|
'modules' => [ |
36
|
|
|
'cart' => [ |
37
|
|
|
'class' => Module::class, |
38
|
|
|
], |
39
|
|
|
], |
40
|
|
|
'components' => [ |
41
|
|
|
'urlManager' => [ |
42
|
|
|
'enablePrettyUrl' => true, |
43
|
|
|
'showScriptName' => false, |
44
|
|
|
], |
45
|
|
|
], |
46
|
|
|
]); |
47
|
|
|
$this->object = Yii::$app->getModule('cart'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
protected function tearDown() |
51
|
|
|
{ |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testGetInstance() |
55
|
|
|
{ |
56
|
|
|
$module = Module::getInstance(); |
57
|
|
|
$this->assertInstanceOf(Module::class, $module); |
58
|
|
|
$this->assertSame($this->object, $module); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function testGetCart() |
62
|
|
|
{ |
63
|
|
|
$this->assertInstanceOf(ShoppingCart::class, $this->object->getCart()); |
64
|
|
|
$this->assertSame(Yii::$app->getModule('cart')->get('cart'), $this->object->cart); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function testCreateUrl() |
68
|
|
|
{ |
69
|
|
|
$this->assertStringEndsWith(Url::to('/cart/cart/index'), $this->object->createUrl()); |
70
|
|
|
$this->assertStringEndsWith(Url::to('/cart/cart/something'), $this->object->createUrl('something')); |
71
|
|
|
$this->assertStringEndsWith(Url::to(['/cart/cart/order', 'a' => 2]), $this->object->createUrl(['order', 'a' => 2])); |
72
|
|
|
$this->assertStringEndsWith(Url::to(['/cart/test/order', 'a' => 2]), $this->object->createUrl(['test/order', 'a' => 2])); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
protected $testString = 'a and b'; |
76
|
|
|
|
77
|
|
View Code Duplication |
public function testOrderButton() |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
$this->object->setOrderButton($this->testString); |
80
|
|
|
$this->assertSame($this->testString, $this->object->getOrderButton()); |
81
|
|
|
$this->object->setOrderButton(function () { return $this->testString; }); |
82
|
|
|
$this->assertSame($this->testString, $this->object->getOrderButton()); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
View Code Duplication |
public function testPaymentMethods() |
|
|
|
|
86
|
|
|
{ |
87
|
|
|
$this->object->setPaymentMethods($this->testString); |
|
|
|
|
88
|
|
|
$this->assertSame($this->testString, $this->object->getPaymentMethods()); |
89
|
|
|
$this->object->setPaymentMethods(function () { return $this->testString; }); |
|
|
|
|
90
|
|
|
$this->assertSame($this->testString, $this->object->getPaymentMethods()); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.