Completed
Pull Request — 2.0 (#461)
by Roman
27:38
created

SteppedCheckoutTest::setUpOnce()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
class SteppedCheckoutTest extends FunctionalTest
4
{
5
    protected static $fixture_file = array(
6
        'silvershop/tests/fixtures/Pages.yml',
7
        'silvershop/tests/fixtures/shop.yml',
8
    );
9
    protected static $use_draft_site = true; //so we don't need to publish
10
    protected $autoFollowRedirection = false;
11
    /** @var CheckoutPage_Controller */
12
    protected $checkout;
13
    /** @var Product */
14
    protected $socks;
15
16
    public function setUpOnce()
17
    {
18
        parent::setUpOnce();
19
        // clear session
20
        ShoppingCart::singleton()->clear();
21
    }
22
23
    public function setUp()
24
    {
25
        parent::setUp();
26
        ShopTest::setConfiguration();
27
        //set up steps
28
        SteppedCheckout::setupSteps(); //use default steps
29
30
        $this->socks = $this->objFromFixture("Product", "socks");
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->objFromFixture('Product', 'socks') can also be of type object<DataObject>. However, the property $socks is declared as type object<Product>. 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...
31
        $this->socks->publish('Stage', 'Live');
32
33
        $checkoutpage = $this->objFromFixture("CheckoutPage", "checkout");
34
        $checkoutpage->publish('Stage', 'Live');
35
        $this->checkout = new CheckoutPage_Controller();
36
        $this->checkout->handleRequest(new SS_HTTPRequest("GET", "checkout"), DataModel::inst());
37
38
        $this->cart = $this->objFromFixture("Order", "cart");
0 ignored issues
show
Bug introduced by
The property cart does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
39
        ShoppingCart::singleton()->setCurrent($this->cart);
0 ignored issues
show
Documentation introduced by
$this->cart is of type object<DataObject>|null, but the function expects a object<Order>.

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...
40
    }
41
42
    public function testTemplateFunctionsForFirstStep()
43
    {
44
        //put us at the first step index == membership
45
        $indexRequest = new SS_HTTPRequest('GET', "");
46
        $this->checkout = new CheckoutPage_Controller(); // from 3.3 on it's necessary to have a clean controller here
47
        $this->checkout->handleRequest($indexRequest, DataModel::inst());
48
        $this->assertTrue($this->checkout->StepExists('membership'));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<SteppedCheckoutTest>.

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...
49
        $this->assertFalse($this->checkout->IsPastStep('membership'));
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<SteppedCheckoutTest>.

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
        $this->assertTrue($this->checkout->IsCurrentStep('membership'));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<SteppedCheckoutTest>.

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...
51
        $this->assertFalse($this->checkout->IsFutureStep('membership'));
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<SteppedCheckoutTest>.

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...
52
53
        $this->checkout->NextStepLink();
54
55
        $this->assertTrue($this->checkout->StepExists('contactdetails'));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<SteppedCheckoutTest>.

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
        $this->assertFalse($this->checkout->IsPastStep('contactdetails'));
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<SteppedCheckoutTest>.

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...
57
        $this->assertFalse($this->checkout->IsCurrentStep('contactdetails'));
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<SteppedCheckoutTest>.

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...
58
        $this->assertTrue($this->checkout->IsFutureStep('contactdetails'));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<SteppedCheckoutTest>.

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
    public function testTemplateFunctionsForOtherSteps()
62
    {
63
        $summaryRequest = new SS_HTTPRequest('GET', "summary");
64
        $this->checkout = new CheckoutPage_Controller();
65
        $this->checkout->handleRequest($summaryRequest, DataModel::inst()); //change to summary step
66
        $this->assertTrue($this->checkout->StepExists('summary'));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<SteppedCheckoutTest>.

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...
67
        $this->assertFalse($this->checkout->IsPastStep('summary'));
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<SteppedCheckoutTest>.

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...
68
        $this->assertTrue($this->checkout->IsCurrentStep('summary'));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<SteppedCheckoutTest>.

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...
69
        $this->assertFalse($this->checkout->IsFutureStep('summary'));
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<SteppedCheckoutTest>.

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...
70
71
        $this->assertFalse($this->checkout->StepExists('nosuchstep'));
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<SteppedCheckoutTest>.

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...
72
    }
73
74
    public function testMembershipStep()
75
    {
76
        //this should still work if there is no cart
77
        ShoppingCart::singleton()->clear();
78
79
        $this->checkout->index();
80
        $this->checkout->membership();
81
        $this->post('/checkout/guestcontinue', array()); //redirect to next step
82
        $this->checkout->createaccount(new SS_HTTPRequest('GET', "/checkout/createaccount"));
83
84
        $form = $this->checkout->MembershipForm();
85
        $data = array();
86
        $form->loadDataFrom($data);
87
88
        $data = array(
89
            'FirstName'              => 'Michael',
90
            'Surname'                => 'Black',
91
            'Email'                  => '[email protected]',
92
            'Password'               => array(
93
                '_Password'        => 'pass1234',
94
                '_ConfirmPassword' => 'pass1234',
95
            ),
96
            'action_docreateaccount' => 'Create New Account',
97
        );
98
        $response = $this->post('/checkout/CreateAccountForm', $data); //redirect to next step
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
99
100
        $member = ShopMember::get_by_identifier("[email protected]");
101
        $this->assertTrue((boolean)$member, "Check new account was created");
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<SteppedCheckoutTest>.

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...
102
        $this->assertEquals('Michael', $member->FirstName);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SteppedCheckoutTest>.

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...
103
        $this->assertEquals('Black', $member->Surname);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SteppedCheckoutTest>.

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...
104
    }
105
106
    public function testContactDetails()
107
    {
108
        $this->objFromFixture("Member", "joebloggs")->logIn();
109
        $this->checkout->contactdetails();
110
        $data = array(
111
            'FirstName'                => 'Pauline',
112
            'Surname'                  => 'Richardson',
113
            'Email'                    => '[email protected]',
114
            'action_setcontactdetails' => 1,
115
        );
116
        $response = $this->post('/checkout/ContactDetailsForm', $data);
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
117
118
        $this->markTestIncomplete('check order has been updated');
0 ignored issues
show
Bug introduced by
The method markTestIncomplete() does not seem to exist on object<SteppedCheckoutTest>.

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...
119
    }
120
121
    public function testShippingAddress()
122
    {
123
        $this->objFromFixture("Member", "joebloggs")->logIn();
124
        $this->checkout->shippingaddress();
125
        $data = array(
126
            'Address'           => '2b Baba place',
127
            'AddressLine2'      => 'Level 2',
128
            'City'              => 'Newton',
129
            'State'             => 'Wellington',
130
            'Country'           => 'NZ',
131
            'action_setaddress' => 1,
132
        );
133
        $response = $this->post('/checkout/AddressForm', $data);
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
134
135
        $this->markTestIncomplete('assertions!');
0 ignored issues
show
Bug introduced by
The method markTestIncomplete() does not seem to exist on object<SteppedCheckoutTest>.

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...
136
    }
137
138
    public function testBillingAddress()
139
    {
140
        $this->objFromFixture("Member", "joebloggs")->logIn();
141
        $this->checkout->billingaddress();
142
        $data = array(
143
            'Address'                  => '3 Art Cresent',
144
            'AddressLine2'             => '',
145
            'City'                     => 'Walkworth',
146
            'State'                    => 'New Caliphoneya',
147
            'Country'                  => 'ZA',
148
            'action_setbillingaddress' => 1,
149
        );
150
        $response = $this->post('/checkout/AddressForm', $data);
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
151
152
        $this->markTestIncomplete('assertions!');
0 ignored issues
show
Bug introduced by
The method markTestIncomplete() does not seem to exist on object<SteppedCheckoutTest>.

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
    }
154
155
    public function testPaymentMethod()
156
    {
157
        $data = array(
158
            'PaymentMethod'           => 'Dummy',
159
            'action_setpaymentmethod' => 1,
160
        );
161
        $response = $this->post('/checkout/PaymentMethodForm', $data);
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
162
        $this->assertEquals('Dummy', Checkout::get($this->cart)->getSelectedPaymentMethod());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SteppedCheckoutTest>.

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...
163
    }
164
165
    public function testSummary()
166
    {
167
        $this->checkout->summary();
168
        $form = $this->checkout->ConfirmationForm();
169
        $data = array(
170
            'Notes'                  => 'Leave it around the back',
171
            'ReadTermsAndConditions' => 1,
172
        );
173
        $member = $this->objFromFixture("Member", "joebloggs");
174
        $member->logIn(); //log in member before processing
175
176
        Checkout::get($this->cart)->setPaymentMethod("Dummy"); //a selected payment method is required
177
        $form->loadDataFrom($data);
178
        $this->assertTrue($form->validate(), "Checkout data is valid");
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<SteppedCheckoutTest>.

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...
179
        $response = $this->post('/checkout/ConfirmationForm', $data);
180
        $this->assertEquals('Cart', $this->cart->Status, "Order is still in cart");
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SteppedCheckoutTest>.

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...
181
182
        $order = Order::get()->byID($this->cart->ID);
183
184
        $this->assertEquals("Leave it around the back", $order->Notes);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SteppedCheckoutTest>.

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...
185
186
        //redirect to make payment
187
        $this->assertEquals(302, $response->getStatusCode());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SteppedCheckoutTest>.

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...
188
        $this->assertEquals(
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SteppedCheckoutTest>.

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...
189
            Director::baseURL() . "checkout/payment",
190
            $response->getHeader('Location')
191
        );
192
    }
193
}
194