Completed
Pull Request — master (#24)
by Robbie
02:08
created

DMSCheckoutControllerTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 194
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 10
dl 0
loc 194
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 10 1
A testDMSDocumentRequestForm() 0 5 1
A testDMSDocumentRequestFormIsExtensible() 0 9 1
A testGetCart() 0 4 1
A testUpdateCartItems() 0 12 1
A testUpdateCartReceiverInfo() 0 14 1
A testSend() 0 19 1
A testSendIsExtensible() 0 5 1
B testDoRequestSend() 0 25 1
A testCompletePage() 0 6 1
A testLink() 0 5 1
A testIndexCheckoutForm() 0 16 1
1
<?php
2
3
class DMSCheckoutControllerTest extends FunctionalTest
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...
4
{
5
    protected static $fixture_file = 'dms-cart/tests/DMSDocumentCartTest.yml';
6
7
    /**
8
     * @var DMSCheckoutController
9
     */
10
    protected $controller;
11
12
    /**
13
     * @var DMSDocumentCart
14
     */
15
    protected $cart;
16
17
    /**
18
     * @var DMSDocumentCartCheckoutPage
19
     */
20
    protected $page;
21
22
    public function setUp()
23
    {
24
        parent::setUp();
25
26
        DMSCheckoutController::add_extension('StubDMSDocumentCheckoutPageExtension');
27
        Injector::inst()->registerService(new StubEmail(), 'Email');
0 ignored issues
show
Documentation introduced by
new \StubEmail() is of type object<StubEmail>, but the function expects a object<stdClass>.

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...
28
29
        $this->controller = DMSCheckoutController::create();
30
        $this->cart = $this->controller->getCart();
31
    }
32
33
    /**
34
     * Tests DMSDocumentRequest form has a FieldList
35
     */
36
    public function testDMSDocumentRequestForm()
37
    {
38
        $form = $this->controller->DMSDocumentRequestForm();
39
        $this->assertInstanceOf('FieldList', $form->Fields());
0 ignored issues
show
Bug introduced by
The method assertInstanceOf() does not seem to exist on object<DMSCheckoutControllerTest>.

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...
40
    }
41
42
    /**
43
     * Tests if the DMSDocumentRequestForm is extensible
44
     */
45
    public function testDMSDocumentRequestFormIsExtensible()
46
    {
47
        $controller = $this->controller;
48
        $form = $controller->DMSDocumentRequestForm();
49
        $this->assertNotNull(
0 ignored issues
show
Bug introduced by
The method assertNotNull() does not seem to exist on object<DMSCheckoutControllerTest>.

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...
50
            $form->Fields()->fieldByName('NewTextField'),
51
            'DMSDocumentRequestForm() is extensible as it included the field from the extension'
52
        );
53
    }
54
55
    /**
56
     * Tests if a Cart is received
57
     */
58
    public function testGetCart()
59
    {
60
        $this->assertInstanceOf('DMSDocumentCart', $this->controller->getCart());
0 ignored issues
show
Bug introduced by
The method assertInstanceOf() does not seem to exist on object<DMSCheckoutControllerTest>.

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...
61
    }
62
63
    /**
64
     * Tests whether the cart items are updated from the controller
65
     */
66
    public function testUpdateCartItems()
67
    {
68
        $doc = $this->objFromFixture('DMSDocument', 'doc1');
69
        /** @var DMSRequestItem $item */
70
        $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...
71
        $this->cart->addItem($item);
72
        $updatedQuantities = array(
73
            'ItemQuantity' => array($doc->ID => 5),
74
        );
75
        $this->controller->updateCartItems($updatedQuantities);
76
        $this->assertEquals(6, $this->cart->getItem($item->getItemId())->getQuantity());
77
    }
78
79
    /**
80
     * Tests whether the recipient details are updated from the controller
81
     */
82
    public function testUpdateCartReceiverInfo()
83
    {
84
        $newInfo = array(
85
            'ReceiverName'            => 'Joe Soap',
86
            'ReceiverPhone'           => '111',
87
            'ReceiverEmail'           => '[email protected]',
88
            'DeliveryAddressLine1'    => 'A1',
89
            'DeliveryAddressLine2'    => 'A2',
90
            'DeliveryAddressCountry'  => 'NZ',
91
            'DeliveryAddressPostCode' => '6011',
92
        );
93
        $this->controller->updateCartReceiverInfo($newInfo);
94
        $this->assertEquals($newInfo, $this->cart->getReceiverInfo());
95
    }
96
97
    /**
98
     * Tests whether emails are sent. Emails are mocked so not actually sent.
99
     */
100
    public function testSend()
101
    {
102
        // Set admin email
103
        Config::inst()->update('Email', 'admin_email', 'admin');
104
        $data = array(
105
            'ReceiverName'            => 'Joe Soap',
106
            'ReceiverPhone'           => '111',
107
            'ReceiverEmail'           => '[email protected]',
108
            'DeliveryAddressLine1'    => 'A1',
109
            'DeliveryAddressLine2'    => 'A2',
110
            'DeliveryAddressCountry'  => 'NZ',
111
            'DeliveryAddressPostCode' => '6011'
112
        );
113
        $this->cart->setReceiverInfo($data);
114
        $result = $this->controller->send();
115
        $this->assertTrue(is_array($result));
116
        $this->assertEquals('[email protected]', $result['to']);
117
        $this->assertEquals('admin', $result['from']);
118
    }
119
120
    /**
121
     * Tests whether email sending is extensible.
122
     */
123
    public function testSendIsExtensible()
124
    {
125
        $result = $this->controller->send();
126
        $this->assertEquals('Subject is changed', $result['subject']);
127
    }
128
129
    /**
130
     * Test to see whether the cart is empty after a request is sent.
131
     */
132
    public function testDoRequestSend()
133
    {
134
        // Form for use later
135
        $form = $this->controller->DMSDocumentRequestForm();
136
        $doc = $this->objFromFixture('DMSDocument', 'doc1');
137
        // Add some an item to the cart to assert later that its empty
138
        $item = DMSRequestItem::create()->setDocument($doc)->setQuantity(15);
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...
139
        $this->controller->getCart()->addItem($item);
140
        $data = array(
141
            'ReceiverName'            => 'Joe Soap',
142
            'ReceiverPhone'           => '111',
143
            'ReceiverEmail'           => '[email protected]',
144
            'DeliveryAddressLine1'    => 'A1',
145
            'DeliveryAddressLine2'    => 'A2',
146
            'DeliveryAddressCountry'  => 'NZ',
147
            'DeliveryAddressPostCode' => '6011',
148
            'ItemQuantity'            => array($doc->ID => 5),
149
        );
150
        $request = new SS_HTTPRequest('POST', 'mock/url');
151
        // Assert cart is empty
152
        $this->assertFalse($this->controller->getCart()->isCartEmpty());
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<DMSCheckoutControllerTest>.

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
        $result = $this->controller->doRequestSend($data, $form, $request);
154
        $this->assertInstanceOf('SS_HTTPResponse', $result);
0 ignored issues
show
Bug introduced by
The method assertInstanceOf() does not seem to exist on object<DMSCheckoutControllerTest>.

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...
155
        $this->assertTrue($this->controller->getCart()->isCartEmpty());
156
    }
157
158
    /**
159
     * Test the checkout success page shows a pretty message
160
     */
161
    public function testCompletePage()
162
    {
163
        $result = (string) $this->get('checkout/complete')->getBody();
164
        $this->assertContains('Thanks!', $result);
165
        $this->assertContains('You will receive a confirmation email', $result);
166
    }
167
168
    /**
169
     * Ensure the link is "friendly", not a class name
170
     */
171
    public function testLink()
172
    {
173
        $this->assertSame('checkout', $this->controller->Link());
174
        $this->assertSame('checkout/complete', $this->controller->Link('complete'));
175
    }
176
177
    /**
178
     * Test that the items in my cart are listed on the checkout page, and that some form fields exist
179
     */
180
    public function testIndexCheckoutForm()
181
    {
182
        $backend = DMSSessionBackend::singleton();
183
        $document = $this->objFromFixture('DMSDocument', 'limited_supply');
184
        $requestItem = DMSRequestItem::create($document);
185
        $backend->addItem($requestItem);
186
187
        $response = $this->get('checkout');
188
        $this->assertEquals(200, $response->getStatusCode());
189
190
        $body = (string) $response->getBody();
191
        $this->assertContains('Checkout', $body);
192
        $this->assertContains('Your request in summary', $body);
193
        $this->assertContains('Doc3', $body);
194
        $this->assertContains('Receiver Name', $body);
195
    }
196
}
197