Completed
Push — master ( 1f0f27...563dd9 )
by Franco
11s
created

tests/DMSDocumentCartTest.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * Class DMSDocumentCartTest contains all the tests for {@link DMSDocumentCart}
5
 */
6
class DMSDocumentCartTest extends SapphireTest
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 View Code Duplication
    public function testAddItem()
22
    {
23
        $doc = $this->objFromFixture('DMSDocument', 'doc1');
24
        /** @var DMSRequestItem $item */
25
        $item = DMSRequestItem::create();
26
        $item->setDocument($doc)->setQuantity(2);
27
        $this->cart->addItem($item);
28
        $this->assertFalse($this->cart->isCartEmpty());
29
    }
30
31 View Code Duplication
    public function testEmptyCart()
32
    {
33
        $doc = $this->objFromFixture('DMSDocument', 'doc1');
34
        /** @var DMSRequestItem $item */
35
        $item = DMSRequestItem::create();
36
        $item->setDocument($doc)->setQuantity(18);
37
        $this->cart->addItem($item);
38
        $this->assertFalse($this->cart->isCartEmpty());
39
40
        $this->cart->emptyCart();
41
        $this->assertTrue($this->cart->isCartEmpty());
42
    }
43
44
    public function testGetBackend()
45
    {
46
        /** @var DMSDocumentCart $cart */
47
        $this->assertEquals('DMSSessionBackend', get_class($this->cart->getBackend()));
48
    }
49
50
    public function testGetBackURL()
51
    {
52
        $url = 'TestURL';
53
        $this->cart->setBackUrl($url);
54
        $this->assertEquals($url, $this->cart->getBackUrl());
55
    }
56
57 View Code Duplication
    public function testGetItem()
58
    {
59
        $doc = $this->objFromFixture('DMSDocument', 'doc1');
60
        /** @var DMSRequestItem $item */
61
        $item = DMSRequestItem::create();
62
        $item->setDocument($doc)->setQuantity(2);
63
        $this->cart->addItem($item);
64
65
        $this->assertEquals($doc, $this->cart->getItem($doc->ID)->getDocument());
66
    }
67
68 View Code Duplication
    public function testGetItems()
69
    {
70
        $doc = $this->objFromFixture('DMSDocument', 'doc1');
71
        /** @var DMSRequestItem $item */
72
        $item = DMSRequestItem::create();
73
        $item->setDocument($doc)->setQuantity(2);
74
        $this->cart->addItem($item);
75
76
        $this->assertCount(1, $this->cart->getItems());
77
    }
78
79
    public function testGetReceiverInfo()
80
    {
81
        $info = array('Name' => 'Jane');
82
        $this->cart->setReceiverInfo($info);
83
        $this->assertEquals($info, $this->cart->getReceiverInfo());
84
    }
85
86
    public function testIsCartEmpty()
87
    {
88
        $this->assertTrue($this->cart->isCartEmpty());
89
        $doc = $this->objFromFixture('DMSDocument', 'doc1');
90
        /** @var DMSRequestItem $item */
91
        $item = DMSRequestItem::create()->setDocument($doc)->setQuantity(2);
92
        $this->cart->addItem($item);
93
        $this->assertFalse($this->cart->isCartEmpty());
94
    }
95
96 View Code Duplication
    public function testRemoveItem()
97
    {
98
        $this->assertTrue($this->cart->isCartEmpty());
99
        $doc = $this->objFromFixture('DMSDocument', 'doc1');
100
        /** @var DMSRequestItem $item */
101
        $item = DMSRequestItem::create();
102
        $item->setDocument($doc)->setQuantity(2);
103
        $this->cart->addItem($item);
104
        $this->assertFalse($this->cart->isCartEmpty());
105
106
        $this->cart->removeItem($item);
107
        $this->assertTrue($this->cart->isCartEmpty());
108
    }
109
110 View Code Duplication
    public function testRemoveItemByID()
111
    {
112
        $this->assertTrue($this->cart->isCartEmpty());
113
        $doc = $this->objFromFixture('DMSDocument', 'doc1');
114
        /** @var DMSRequestItem $item */
115
        $item = DMSRequestItem::create();
116
        $item->setDocument($doc)->setQuantity(2);
117
        $this->cart->addItem($item);
118
        $this->assertFalse($this->cart->isCartEmpty());
119
120
        $this->cart->removeItemByID($doc->ID);
121
        $this->assertTrue($this->cart->isCartEmpty());
122
    }
123
124
    public function testUpdateItemQuantity()
125
    {
126
        $doc = $this->objFromFixture('DMSDocument', 'doc1');
127
128
        // Test Update
129
        /** @var DMSRequestItem $item */
130
        $item = DMSRequestItem::create();
131
        $item->setDocument($doc)->setQuantity(2);
132
        $this->cart->addItem($item);
133
        $this->assertEquals(2, $this->cart->getItem($doc->ID)->getQuantity());
134
135
        $this->cart->updateItemQuantity($item->getItemID(), 17);
136
        $this->assertEquals(19, $this->cart->getItem($doc->ID)->getQuantity());
137
138
        // Test Add
139
        $this->cart->updateItemQuantity($doc->ID, 16);
140
        $this->assertEquals(35, $this->cart->getItem($doc->ID)->getQuantity());
141
142
        // Test Deduct
143
        $this->cart->updateItemQuantity($doc->ID, -16);
144
        $this->assertEquals(19, $this->cart->getItem($doc->ID)->getQuantity());
145
146
        // Test deduct all items that it removes it
147
        $this->cart->updateItemQuantity($doc->ID, -19);
148
        $this->assertTrue($this->cart->isCartEmpty());
149
    }
150
151
    public function testSaveSubmission()
152
    {
153
        $page = $this->objFromFixture('DMSDocumentCartCheckoutPage', 'page1');
154
        /** @var DMSDocumentCartCheckoutPage_Controller $controller */
155
        $controller = ModelAsController::controller_for($page);
0 ignored issues
show
$page is of type object<DataObject>|null, but the function expects a object<SiteTree>.

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...
156
        // Form for use later
157
        $form = $controller->DMSDocumentRequestForm();
158
        $doc = $this->objFromFixture('DMSDocument', 'doc1');
159
        // Add some an item to the cart to assert later that its empty
160
        $item = DMSRequestItem::create()->setDocument($doc)->setQuantity(15);
0 ignored issues
show
$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...
161
        $controller->getCart()->addItem($item);
162
163
        $submissionID = $controller->getCart()->saveSubmission($form);
164
        $submission = DMSDocumentCartSubmission::get()->byID($submissionID);
165
        $this->assertEquals(15, $submission->Items()->first()->Quantity);
166
    }
167
168
169 View Code Duplication
    public function testIsInCart()
170
    {
171
        $doc = $this->objFromFixture('DMSDocument', 'doc1');
172
        /** @var DMSRequestItem $item */
173
        $item = DMSRequestItem::create()->setDocument($doc)->setQuantity(2);
174
        $this->assertFalse($this->cart->isInCart($item->getItemId()));
175
        $this->cart->addItem($item);
176
        $this->assertTrue($this->cart->isInCart($item->getItemId()));
177
    }
178
}
179