Completed
Push — master ( 660d11...e1d670 )
by Franco
01:58
created

DMSDocumentCartTest::testGetCartSummaryCacheKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
/**
4
 * Class DMSDocumentCartTest contains all the tests for {@link DMSDocumentCart}
5
 */
6
class DMSDocumentCartTest extends SapphireTest
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
7
{
8
    protected static $fixture_file = 'DMSDocumentCartTest.yml';
9
10
    /**
11
     * @var DMSDocumentCart
12
     */
13
    protected $cart;
14
15
    public function setUp()
16
    {
17
        parent::setUp();
18
        $this->cart = singleton('DMSDocumentCart');
19
    }
20
21
    /**
22
     * @expectedException DMSDocumentCartException
23
     * @expectedExceptionMessage Backend must implement DMSCartBackendInterface!
24
     */
25
    public function testConstructorThrowsExceptionWhenProvidedBackendDoesNotImplementInterface()
26
    {
27
        DMSDocumentCart::create(new DMSDocument);
28
    }
29
30 View Code Duplication
    public function testAddItem()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
31
    {
32
        $doc = $this->objFromFixture('DMSDocument', 'doc1');
33
34
        $item = DMSRequestItem::create();
35
        $item->setDocument($doc)->setQuantity(2);
0 ignored issues
show
Documentation introduced by
$doc is of type object<DataObject>|null, but the function expects a object<DMSDocument>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
36
        $this->cart->addItem($item);
37
        $this->assertFalse($this->cart->isCartEmpty());
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<DMSDocumentCartTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
38
    }
39
40 View Code Duplication
    public function testEmptyCart()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
41
    {
42
        $doc = $this->objFromFixture('DMSDocument', 'doc1');
43
44
        $item = DMSRequestItem::create();
45
        $item->setDocument($doc)->setQuantity(18);
0 ignored issues
show
Documentation introduced by
$doc is of type object<DataObject>|null, but the function expects a object<DMSDocument>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
46
        $this->cart->addItem($item);
47
        $this->assertFalse($this->cart->isCartEmpty());
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<DMSDocumentCartTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
48
49
        $this->cart->emptyCart();
50
        $this->assertTrue($this->cart->isCartEmpty());
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<DMSDocumentCartTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
51
    }
52
53
    public function testGetBackend()
54
    {
55
        $this->assertInstanceOf('DMSSessionBackend', $this->cart->getBackend());
0 ignored issues
show
Bug introduced by
The method assertInstanceOf() does not seem to exist on object<DMSDocumentCartTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
56
    }
57
58
    public function testGetBackURL()
59
    {
60
        $url = 'TestURL';
61
        $this->cart->setBackUrl($url);
62
        $this->assertEquals($url, $this->cart->getBackUrl());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<DMSDocumentCartTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
63
    }
64
65 View Code Duplication
    public function testGetItem()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
66
    {
67
        $doc = $this->objFromFixture('DMSDocument', 'doc1');
68
69
        $item = DMSRequestItem::create();
70
        $item->setDocument($doc)->setQuantity(2);
0 ignored issues
show
Documentation introduced by
$doc is of type object<DataObject>|null, but the function expects a object<DMSDocument>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
71
        $this->cart->addItem($item);
72
73
        $this->assertEquals($doc, $this->cart->getItem($doc->ID)->getDocument());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<DMSDocumentCartTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
74
    }
75
76
    public function testGetItems()
77
    {
78
        $item = DMSRequestItem::create();
79
        $item->setDocument($this->objFromFixture('DMSDocument', 'doc1'))->setQuantity(2);
0 ignored issues
show
Documentation introduced by
$this->objFromFixture('DMSDocument', 'doc1') is of type object<DataObject>|null, but the function expects a object<DMSDocument>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
80
        $this->cart->addItem($item);
81
        $this->assertCount(1, $this->cart->getItems());
0 ignored issues
show
Bug introduced by
The method assertCount() does not seem to exist on object<DMSDocumentCartTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
82
83
        $item2 = DMSRequestItem::create();
84
        $item2->setDocument($this->objFromFixture('DMSDocument', 'limited_supply'))->setQuantity(2);
0 ignored issues
show
Documentation introduced by
$this->objFromFixture('D...ent', 'limited_supply') is of type object<DataObject>|null, but the function expects a object<DMSDocument>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
85
        $this->cart->addItem($item2);
86
        $this->assertCount(2, $this->cart->getItems());
0 ignored issues
show
Bug introduced by
The method assertCount() does not seem to exist on object<DMSDocumentCartTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
87
88
        // Edge case handling for when a document is deleted in the background
89
        $item2->getDocument()->delete();
90
        $this->assertCount(1, $this->cart->getItems());
0 ignored issues
show
Bug introduced by
The method assertCount() does not seem to exist on object<DMSDocumentCartTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
91
    }
92
93
    public function testGetReceiverInfo()
94
    {
95
        $info = array('Name' => 'Jane');
96
        $this->cart->setReceiverInfo($info);
97
        $this->assertEquals($info, $this->cart->getReceiverInfo());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<DMSDocumentCartTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
98
    }
99
100
    public function testIsCartEmpty()
101
    {
102
        $this->assertTrue($this->cart->isCartEmpty());
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<DMSDocumentCartTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
103
        $doc = $this->objFromFixture('DMSDocument', 'doc1');
104
        $item = DMSRequestItem::create()->setDocument($doc)->setQuantity(2);
0 ignored issues
show
Documentation introduced by
$doc is of type object<DataObject>|null, but the function expects a object<DMSDocument>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
105
        $this->cart->addItem($item);
106
        $this->assertFalse($this->cart->isCartEmpty());
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<DMSDocumentCartTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
107
    }
108
109 View Code Duplication
    public function testRemoveItem()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
110
    {
111
        $this->assertTrue($this->cart->isCartEmpty());
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<DMSDocumentCartTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
112
        $doc = $this->objFromFixture('DMSDocument', 'doc1');
113
114
        $item = DMSRequestItem::create();
115
        $item->setDocument($doc)->setQuantity(2);
0 ignored issues
show
Documentation introduced by
$doc is of type object<DataObject>|null, but the function expects a object<DMSDocument>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
116
        $this->cart->addItem($item);
117
        $this->assertFalse($this->cart->isCartEmpty());
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<DMSDocumentCartTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
118
119
        $this->cart->removeItem($item);
120
        $this->assertTrue($this->cart->isCartEmpty());
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<DMSDocumentCartTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
121
    }
122
123 View Code Duplication
    public function testRemoveItemByID()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
124
    {
125
        $this->assertTrue($this->cart->isCartEmpty());
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<DMSDocumentCartTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
126
        $doc = $this->objFromFixture('DMSDocument', 'doc1');
127
128
        $item = DMSRequestItem::create();
129
        $item->setDocument($doc)->setQuantity(2);
0 ignored issues
show
Documentation introduced by
$doc is of type object<DataObject>|null, but the function expects a object<DMSDocument>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
130
        $this->cart->addItem($item);
131
        $this->assertFalse($this->cart->isCartEmpty());
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<DMSDocumentCartTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
132
133
        $this->cart->removeItemByID($doc->ID);
134
        $this->assertTrue($this->cart->isCartEmpty());
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<DMSDocumentCartTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
135
    }
136
137
    public function testUpdateItemQuantity()
138
    {
139
        $doc = $this->objFromFixture('DMSDocument', 'doc1');
140
141
        // Test Update
142
        $item = DMSRequestItem::create();
143
        $item->setDocument($doc)->setQuantity(2);
0 ignored issues
show
Documentation introduced by
$doc is of type object<DataObject>|null, but the function expects a object<DMSDocument>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
144
        $this->cart->addItem($item);
145
        $this->assertEquals(2, $this->cart->getItem($doc->ID)->getQuantity());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<DMSDocumentCartTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
146
147
        $this->cart->updateItemQuantity($item->getItemID(), 17);
148
        $this->assertEquals(19, $this->cart->getItem($doc->ID)->getQuantity());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<DMSDocumentCartTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
149
150
        // Test Add
151
        $this->cart->updateItemQuantity($doc->ID, 16);
152
        $this->assertEquals(35, $this->cart->getItem($doc->ID)->getQuantity());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<DMSDocumentCartTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
153
154
        // Test Deduct
155
        $this->cart->updateItemQuantity($doc->ID, -16);
156
        $this->assertEquals(19, $this->cart->getItem($doc->ID)->getQuantity());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<DMSDocumentCartTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
157
158
        // Test deduct all items that it removes it
159
        $this->cart->updateItemQuantity($doc->ID, -19);
160
        $this->assertTrue($this->cart->isCartEmpty());
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<DMSDocumentCartTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
161
    }
162
163
    public function testSaveSubmission()
164
    {
165
        $controller = DMSCheckoutController::create();
166
167
        // Form for use later
168
        $form = $controller->DMSDocumentRequestForm();
169
        $doc = $this->objFromFixture('DMSDocument', 'doc1');
170
171
        // Add some an item to the cart to assert later that its empty
172
        $item = DMSRequestItem::create($doc)->setQuantity(15);
173
        $controller->getCart()->addItem($item);
174
175
        $submissionID = $controller->getCart()->saveSubmission($form);
176
        $submission = DMSDocumentCartSubmission::get()->byID($submissionID);
177
        $this->assertEquals(15, $submission->Items()->first()->Quantity);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<DMSDocumentCartTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
178
    }
179
180
181 View Code Duplication
    public function testIsInCart()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
182
    {
183
        $doc = $this->objFromFixture('DMSDocument', 'doc1');
184
        $item = DMSRequestItem::create()->setDocument($doc)->setQuantity(2);
0 ignored issues
show
Documentation introduced by
$doc is of type object<DataObject>|null, but the function expects a object<DMSDocument>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
185
        $this->assertFalse($this->cart->isInCart($item->getItemId()));
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<DMSDocumentCartTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
186
187
        $this->cart->addItem($item);
188
        $this->assertTrue($this->cart->isInCart($item->getItemId()));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<DMSDocumentCartTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
189
    }
190
191
    /**
192
     * Ensure that the cart contents are hashed an used for the cart summary cache key
193
     */
194
    public function testGetCartSummaryCacheKey()
195
    {
196
        $item = DMSRequestItem::create($this->objFromFixture('DMSDocument', 'doc1'))->setQuantity(2);
197
        $item2 = DMSRequestItem::create($this->objFromFixture('DMSDocument', 'limited_supply'))->setQuantity(2);
198
199
        $this->cart->addItem($item)->addItem($item2);
200
201
        $hash = md5(serialize($this->cart->getItems()));
202
        $this->assertContains($hash, $this->cart->getCartSummaryCacheKey());
203
    }
204
}
205