Completed
Push — master ( 8319fc...4c173a )
by Robbie
02:01
created

testDMSDocumentRequestFormIsExtensible()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
class DMSDocumentCartCheckoutPageTest 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 = 'DMSDocumentCartTest.yml';
6
    /**
7
     * @var DMSDocumentCartCheckoutPage_Controller
8
     */
9
    protected $controller;
10
11
    /**
12
     * @var DMSDocumentCart
13
     */
14
    protected $cart;
15
16
    /**
17
     * @var DMSDocumentCartCheckoutPage
18
     */
19
    protected $page;
20
21
    public function setUp()
22
    {
23
        parent::setUp();
24
        $this->page = $this->objFromFixture('DMSDocumentCartCheckoutPage', 'page1');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->objFromFixture('D...CheckoutPage', 'page1') can also be of type object<DataObject>. However, the property $page is declared as type object<DMSDocumentCartCheckoutPage>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
25
        DMSDocumentCartCheckoutPage_Controller::add_extension('StubDMSDocumentCheckoutPageExtension');
26
        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...
27
        $this->controller = ModelAsController::controller_for($this->page);
0 ignored issues
show
Documentation introduced by
$this->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...
28
        $this->cart = $this->controller->getCart();
29
    }
30
31
    /**
32
     * Tests DMSDocumentRequest form has a FieldList
33
     */
34
    public function testDMSDocumentRequestForm()
35
    {
36
        $form = $this->controller->DMSDocumentRequestForm();
37
        $this->assertInstanceOf('FieldList', $form->Fields());
0 ignored issues
show
Bug introduced by
The method assertInstanceOf() does not seem to exist on object<DMSDocumentCartCheckoutPageTest>.

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

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

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...
59
    }
60
61
    /**
62
     * Tests whether the cart items are updated from the controller
63
     */
64
    public function testUpdateCartItems()
65
    {
66
        $doc = $this->objFromFixture('DMSDocument', 'doc1');
67
        /** @var DMSRequestItem $item */
68
        $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...
69
        $this->cart->addItem($item);
70
        $updatedQuantities = array(
71
            'ItemQuantity' => array($doc->ID => 5),
72
        );
73
        $this->controller->updateCartItems($updatedQuantities);
74
        $this->assertEquals(6, $this->cart->getItem($item->getItemId())->getQuantity());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<DMSDocumentCartCheckoutPageTest>.

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...
75
    }
76
77
    /**
78
     * Tests whether the recipient details are updated from the controller
79
     */
80
    public function testUpdateCartReceiverInfo()
81
    {
82
        $newInfo = array(
83
            'ReceiverName'            => 'Joe Soap',
84
            'ReceiverPhone'           => '111',
85
            'ReceiverEmail'           => '[email protected]',
86
            'DeliveryAddressLine1'    => 'A1',
87
            'DeliveryAddressLine2'    => 'A2',
88
            'DeliveryAddressCountry'  => 'NZ',
89
            'DeliveryAddressPostCode' => '6011',
90
        );
91
        $this->controller->updateCartReceiverInfo($newInfo);
92
        $this->assertEquals($newInfo, $this->cart->getReceiverInfo());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<DMSDocumentCartCheckoutPageTest>.

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...
93
    }
94
95
    /**
96
     * Tests whether emails are sent. Emails are mocked so not actually sent.
97
     */
98
    public function testSend()
99
    {
100
        // Set admin email
101
        Config::inst()->update('Email', 'admin_email', 'admin');
102
        $data = array(
103
            'ReceiverName'            => 'Joe Soap',
104
            'ReceiverPhone'           => '111',
105
            'ReceiverEmail'           => '[email protected]',
106
            'DeliveryAddressLine1'    => 'A1',
107
            'DeliveryAddressLine2'    => 'A2',
108
            'DeliveryAddressCountry'  => 'NZ',
109
            'DeliveryAddressPostCode' => '6011'
110
        );
111
        $this->cart->setReceiverInfo($data);
112
        $result = $this->controller->send();
113
        $this->assertTrue(is_array($result));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<DMSDocumentCartCheckoutPageTest>.

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...
114
        $this->assertEquals('[email protected]', $result['to']);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<DMSDocumentCartCheckoutPageTest>.

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...
115
        $this->assertEquals('admin', $result['from']);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<DMSDocumentCartCheckoutPageTest>.

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...
116
    }
117
118
    /**
119
     * Tests whether email sending is extensible.
120
     */
121
    public function testSendIsExtensible()
122
    {
123
        $result = $this->controller->send();
124
        $this->assertEquals('Subject is changed', $result['subject']);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<DMSDocumentCartCheckoutPageTest>.

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...
125
    }
126
127
    /**
128
     * Test to see whether the cart is empty after a request is sent.
129
     */
130
    public function testDoRequestSend()
131
    {
132
        // Form for use later
133
        $form = $this->controller->DMSDocumentRequestForm();
134
        $doc = $this->objFromFixture('DMSDocument', 'doc1');
135
        // Add some an item to the cart to assert later that its empty
136
        $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...
137
        $this->controller->getCart()->addItem($item);
138
        $data = array(
139
            'ReceiverName'            => 'Joe Soap',
140
            'ReceiverPhone'           => '111',
141
            'ReceiverEmail'           => '[email protected]',
142
            'DeliveryAddressLine1'    => 'A1',
143
            'DeliveryAddressLine2'    => 'A2',
144
            'DeliveryAddressCountry'  => 'NZ',
145
            'DeliveryAddressPostCode' => '6011',
146
            'ItemQuantity'            => array($doc->ID => 5),
147
        );
148
        $request = new SS_HTTPRequest('POST', 'mock/url');
149
        // Assert cart is empty
150
        $this->assertFalse($this->controller->getCart()->isCartEmpty());
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<DMSDocumentCartCheckoutPageTest>.

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

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
        $this->assertTrue($this->controller->getCart()->isCartEmpty());
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<DMSDocumentCartCheckoutPageTest>.

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...
154
    }
155
}
156