Completed
Push — master ( e39500...4392d1 )
by Robbie
13s
created

testDMSDocumentRequestForm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
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
    public function setUp()
18
    {
19
        parent::setUp();
20
21
        DMSCheckoutController::add_extension('StubDMSDocumentCheckoutPageExtension');
22
        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...
23
24
        $this->controller = DMSCheckoutController::create();
25
        $this->cart = $this->controller->getCart();
26
    }
27
28
    /**
29
     * Tests DMSDocumentRequest form has a FieldList
30
     */
31
    public function testDMSDocumentRequestForm()
32
    {
33
        $form = $this->controller->DMSDocumentRequestForm();
34
        $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...
35
    }
36
37
    /**
38
     * Tests if the DMSDocumentRequestForm is extensible
39
     */
40
    public function testDMSDocumentRequestFormIsExtensible()
41
    {
42
        $controller = $this->controller;
43
        $form = $controller->DMSDocumentRequestForm();
44
        $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...
45
            $form->Fields()->fieldByName('NewTextField'),
46
            'DMSDocumentRequestForm() is extensible as it included the field from the extension'
47
        );
48
    }
49
50
    /**
51
     * Tests if a Cart is received
52
     */
53
    public function testGetCart()
54
    {
55
        $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...
56
    }
57
58
    /**
59
     * Tests whether the cart items are updated from the controller
60
     */
61
    public function testUpdateCartItems()
62
    {
63
        $doc = $this->objFromFixture('DMSDocument', 'doc1');
64
        /** @var DMSRequestItem $item */
65
        $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...
66
        $this->cart->addItem($item);
67
        $updatedQuantities = array(
68
            'ItemQuantity' => array($doc->ID => 5),
69
        );
70
        $this->controller->updateCartItems($updatedQuantities);
71
        $this->assertEquals(6, $this->cart->getItem($item->getItemId())->getQuantity());
72
    }
73
74
    /**
75
     * Tests whether the recipient details are updated from the controller
76
     */
77
    public function testUpdateCartReceiverInfo()
78
    {
79
        $newInfo = array(
80
            'ReceiverName'            => 'Joe Soap',
81
            'ReceiverPhone'           => '111',
82
            'ReceiverEmail'           => '[email protected]',
83
            'DeliveryAddressLine1'    => 'A1',
84
            'DeliveryAddressLine2'    => 'A2',
85
            'DeliveryAddressCountry'  => 'NZ',
86
            'DeliveryAddressPostCode' => '6011',
87
        );
88
        $this->controller->updateCartReceiverInfo($newInfo);
89
        $this->assertEquals($newInfo, $this->cart->getReceiverInfo());
90
    }
91
92
    /**
93
     * Tests whether emails are sent. Emails are mocked so not actually sent.
94
     */
95
    public function testSend()
96
    {
97
        // Set admin email
98
        Config::inst()->update('Email', 'admin_email', 'admin');
99
        $data = array(
100
            'ReceiverName'            => 'Joe Soap',
101
            'ReceiverPhone'           => '111',
102
            'ReceiverEmail'           => '[email protected]',
103
            'DeliveryAddressLine1'    => 'A1',
104
            'DeliveryAddressLine2'    => 'A2',
105
            'DeliveryAddressCountry'  => 'NZ',
106
            'DeliveryAddressPostCode' => '6011'
107
        );
108
        $this->cart->setReceiverInfo($data);
109
        $result = $this->controller->send();
110
        $this->assertTrue(is_array($result));
111
        $this->assertEquals('[email protected]', $result['to']);
112
        $this->assertEquals('admin', $result['from']);
113
    }
114
115
    /**
116
     * Tests whether email sending is extensible.
117
     */
118
    public function testSendIsExtensible()
119
    {
120
        $result = $this->controller->send();
121
        $this->assertEquals('Subject is changed', $result['subject']);
122
    }
123
124
    /**
125
     * Test to see whether the cart is empty after a request is sent.
126
     */
127
    public function testDoRequestSend()
128
    {
129
        // Form for use later
130
        $form = $this->controller->DMSDocumentRequestForm();
131
        $doc = $this->objFromFixture('DMSDocument', 'doc1');
132
        // Add some an item to the cart to assert later that its empty
133
        $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...
134
        $this->controller->getCart()->addItem($item);
135
        $data = array(
136
            'ReceiverName'            => 'Joe Soap',
137
            'ReceiverPhone'           => '111',
138
            'ReceiverEmail'           => '[email protected]',
139
            'DeliveryAddressLine1'    => 'A1',
140
            'DeliveryAddressLine2'    => 'A2',
141
            'DeliveryAddressCountry'  => 'NZ',
142
            'DeliveryAddressPostCode' => '6011',
143
            'ItemQuantity'            => array($doc->ID => 5),
144
        );
145
        $request = new SS_HTTPRequest('POST', 'mock/url');
146
        // Assert cart is empty
147
        $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...
148
        $result = $this->controller->doRequestSend($data, $form, $request);
149
        $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...
150
        $this->assertTrue($this->controller->getCart()->isCartEmpty());
151
    }
152
153
    /**
154
     * Test the checkout success page shows a pretty message
155
     */
156
    public function testCompletePage()
157
    {
158
        $result = (string) $this->get('checkout/complete')->getBody();
159
        $this->assertContains('Thanks!', $result);
160
        $this->assertContains('You will receive a confirmation email', $result);
161
    }
162
163
    /**
164
     * Ensure the link is "friendly", not a class name
165
     */
166
    public function testLink()
167
    {
168
        $this->assertSame('checkout', $this->controller->Link());
169
        $this->assertSame('checkout/complete', $this->controller->Link('complete'));
170
    }
171
172
    /**
173
     * Test that the items in my cart are listed on the checkout page, and that some form fields exist
174
     */
175
    public function testIndexCheckoutForm()
176
    {
177
        $backend = DMSSessionBackend::singleton();
178
        $document = $this->objFromFixture('DMSDocument', 'limited_supply');
179
        $requestItem = DMSRequestItem::create($document);
180
        $backend->addItem($requestItem);
181
182
        $response = $this->get('checkout');
183
        $this->assertEquals(200, $response->getStatusCode());
184
185
        $body = (string) $response->getBody();
186
        $this->assertContains('Checkout', $body);
187
        $this->assertContains('Your request in summary', $body);
188
        $this->assertContains('Doc3', $body);
189
        $this->assertContains('Receiver Name', $body);
190
    }
191
}
192