Completed
Pull Request — master (#33)
by Franco
02:07
created

testUpdateCartReceiverInfo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
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 recipient details are updated from the controller
60
     */
61
    public function testUpdateCartReceiverInfo()
62
    {
63
        $newInfo = array(
64
            'ReceiverName'            => 'Joe Soap',
65
            'ReceiverPhone'           => '111',
66
            'ReceiverEmail'           => '[email protected]',
67
            'DeliveryAddressLine1'    => 'A1',
68
            'DeliveryAddressLine2'    => 'A2',
69
            'DeliveryAddressCountry'  => 'NZ',
70
            'DeliveryAddressPostCode' => '6011',
71
        );
72
        $this->controller->updateCartReceiverInfo($newInfo);
73
        $this->assertEquals($newInfo, $this->cart->getReceiverInfo());
74
    }
75
76
    /**
77
     * Tests whether emails are sent. Emails are mocked so not actually sent.
78
     */
79
    public function testSend()
80
    {
81
        // Set admin email
82
        Config::inst()->update('Email', 'admin_email', 'admin');
83
        $data = array(
84
            'ReceiverName'            => 'Joe Soap',
85
            'ReceiverPhone'           => '111',
86
            'ReceiverEmail'           => '[email protected]',
87
            'DeliveryAddressLine1'    => 'A1',
88
            'DeliveryAddressLine2'    => 'A2',
89
            'DeliveryAddressCountry'  => 'NZ',
90
            'DeliveryAddressPostCode' => '6011'
91
        );
92
        $this->cart->setReceiverInfo($data);
93
        $result = $this->controller->send();
94
        $this->assertTrue(is_array($result));
95
        $this->assertEquals('[email protected]', $result['to']);
96
        $this->assertEquals('admin', $result['from']);
97
    }
98
99
    /**
100
     * Tests whether email sending is extensible.
101
     */
102
    public function testSendIsExtensible()
103
    {
104
        $result = $this->controller->send();
105
        $this->assertEquals('Subject is changed', $result['subject']);
106
    }
107
108
    /**
109
     * Test to see whether the cart is empty after a request is sent.
110
     */
111
    public function testDoRequestSend()
112
    {
113
        // Form for use later
114
        $form = $this->controller->DMSDocumentRequestForm();
115
        $doc = $this->objFromFixture('DMSDocument', 'doc1');
116
        // Add some an item to the cart to assert later that its empty
117
        $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...
118
        $this->controller->getCart()->addItem($item);
119
        $data = array(
120
            'ReceiverName'            => 'Joe Soap',
121
            'ReceiverPhone'           => '111',
122
            'ReceiverEmail'           => '[email protected]',
123
            'DeliveryAddressLine1'    => 'A1',
124
            'DeliveryAddressLine2'    => 'A2',
125
            'DeliveryAddressCountry'  => 'NZ',
126
            'DeliveryAddressPostCode' => '6011',
127
            'ItemQuantity'            => array($doc->ID => 5),
128
        );
129
        $request = new SS_HTTPRequest('POST', 'mock/url');
130
        // Assert cart is empty
131
        $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...
132
        $result = $this->controller->doRequestSend($data, $form, $request);
133
        $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...
134
        $this->assertTrue($this->controller->getCart()->isCartEmpty());
135
    }
136
137
    /**
138
     * Test the checkout success page shows a pretty message
139
     */
140
    public function testCompletePage()
141
    {
142
        $result = (string) $this->get('checkout/complete')->getBody();
143
        $this->assertContains('Thanks!', $result);
144
        $this->assertContains('You will receive a confirmation email', $result);
145
    }
146
147
    /**
148
     * Ensure the link is "friendly", not a class name
149
     */
150
    public function testLink()
151
    {
152
        $this->assertSame('checkout', $this->controller->Link());
153
        $this->assertSame('checkout/complete', $this->controller->Link('complete'));
154
    }
155
156
    /**
157
     * Test that the items in my cart are listed on the checkout page, and that some form fields exist
158
     */
159
    public function testIndexCheckoutForm()
160
    {
161
        $backend = DMSSessionBackend::singleton();
162
        $document = $this->objFromFixture('DMSDocument', 'limited_supply');
163
        $requestItem = DMSRequestItem::create($document);
164
        $backend->addItem($requestItem);
165
166
        $response = $this->get('checkout');
167
        $this->assertEquals(200, $response->getStatusCode());
168
169
        $body = (string) $response->getBody();
170
        $this->assertContains('Checkout', $body);
171
        $this->assertContains('Your request in summary', $body);
172
        $this->assertContains('Doc3', $body);
173
        $this->assertContains('Receiver Name', $body);
174
    }
175
}
176