Completed
Push — master ( 4cbc21...c64f37 )
by Antony
02:07
created

testEmailIsSentUponStepCheckoutCompletion()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 95
Code Lines 65

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 65
dl 0
loc 95
rs 8.7636
c 0
b 0
f 0
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace AntonyThorpe\SilverShopBankDeposit\Tests;
4
5
use SilverStripe\Dev\FunctionalTest;
6
use SilverStripe\SiteConfig\SiteConfig;
7
use SilverStripe\Core\Config\Config;
8
use SilverStripe\ORM\DataObject;
9
use SilverStripe\Security\Member;
10
use SilverStripe\View\SSViewer;
11
use SilverShop\Tests\ShopTest;
12
use SilverShop\Extension\SteppedCheckoutExtension;
13
use SilverShop\Page\Product;
14
use SilverShop\Page\CheckoutPage;
15
use SilverShop\Page\AccountPage;
16
use SilverShop\Model\Order;
17
use SilverShop\Cart\ShoppingCart;
18
use SilverShop\Cart\ShoppingCartController;
19
20
/**
21
 * Test that the Manual payment method can be used on a stepped checkout and
22
 * that an email is sent upon completing the form
23
 */
24
class SteppedCheckoutPageTest extends FunctionalTest
25
{
26
    protected static $fixture_file = array(
27
        'vendor/silvershop/core/tests/php/Fixtures/Pages.yml',
28
        'vendor/silvershop/core/tests/php/Fixtures/shop.yml',
29
        'vendor/silvershop/core/tests/php/Fixtures/Orders.yml',
30
        'Orders.yml'
31
    );
32
33
    protected static $disable_theme  = true;
34
35
    /**
36
     * @var SilverStripe\ORM\DataObject
0 ignored issues
show
Bug introduced by
The type AntonyThorpe\SilverShopB...erStripe\ORM\DataObject was not found. Did you mean SilverStripe\ORM\DataObject? If so, make sure to prefix the type with \.
Loading history...
37
     */
38
    protected $laptop;
39
40
    public function setUp()
41
    {
42
        parent::setUp();
43
        ShopTest::setConfiguration();
44
        ShoppingCart::singleton()->clear();
45
        SteppedCheckoutExtension::setupSteps(); //use default steps
46
47
        $siteconfig = DataObject::get_one(SiteConfig::class);
48
        $siteconfig->BankAccountPaymentMethodMessage = "You will be notified of the bank account details";
49
        $siteconfig->BankAccountNumber = "XX-3456-7891011-XX";
50
        $siteconfig->BankAccountDetails = "TestBank, Business Branch";
51
        $siteconfig->BankAccountInvoiceMessage = "Hey bo, just pop the dosh in the account";
52
        $siteconfig->write();
53
54
        SSViewer::config()->source_file_comments = true;
55
56
        // establish products
57
        $this->laptop = $this->objFromFixture(Product::class, "laptop");
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->objFromFixture(Si...oduct::class, 'laptop') of type SilverStripe\ORM\DataObject is incompatible with the declared type AntonyThorpe\SilverShopB...erStripe\ORM\DataObject of property $laptop.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
58
        $this->laptop->publishSingle();
59
60
        // publish pages
61
        $checkoutpage = $this->objFromFixture(CheckoutPage::class, "checkout");
62
        $checkoutpage->publishSingle();
63
64
        $accountpage = $this->objFromFixture(AccountPage::class, "accountpage");
65
        $accountpage->publishSingle();
66
67
        // Login member
68
        $member = $this->objFromFixture(Member::class, "joebloggs");
69
        $this->logInAs($member);
70
71
        //add item to cart via url
72
        $this->get(ShoppingCartController::add_item_link($this->laptop));
0 ignored issues
show
Bug introduced by
$this->laptop of type SilverStripe\ORM\DataObject is incompatible with the type SilverShop\Model\Buyable expected by parameter $buyable of SilverShop\Cart\Shopping...roller::add_item_link(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

72
        $this->get(ShoppingCartController::add_item_link(/** @scrutinizer ignore-type */ $this->laptop));
Loading history...
Bug introduced by
It seems like SilverShop\Cart\Shopping...tem_link($this->laptop) can also be of type false; however, parameter $url of SilverStripe\Dev\FunctionalTest::get() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

72
        $this->get(/** @scrutinizer ignore-type */ ShoppingCartController::add_item_link($this->laptop));
Loading history...
73
    }
74
75
    public function testEmailIsSentUponStepCheckoutCompletion()
76
    {
77
        $self = $this;
78
        $this->useTestTheme(
79
            dirname(__FILE__),
80
            'testtheme',
81
            function () use ($self) {
82
                $page = $self->get("checkout/");
83
                $self->assertEquals(
84
                    200,
85
                    $page->getStatusCode(),
86
                    "contact details page should load"
87
                );
88
89
                // contact form
90
                $page = $self->submitForm("CheckoutForm_ContactDetailsForm", "action_checkoutSubmit", array(
91
                    'CustomerDetailsCheckoutComponent_FirstName' => 'Joe',
92
                    'CustomerDetailsCheckoutComponent_Surname' => 'Bloggs',
93
                    'CustomerDetailsCheckoutComponent_Email' => '[email protected]'
94
                ));
95
                $self->assertEquals(
96
                    200,
97
                    $page->getStatusCode(),
98
                    "enter contact details page should load"
99
                );
100
101
                // Shipping Address form
102
                $page = $self->submitForm("CheckoutForm_ShippingAddressForm", "action_setshippingaddress", array(
103
                    'ShippingAddressCheckoutComponent_Country' => 'AU',
104
                    'ShippingAddressCheckoutComponent_Address' => '201-203 BROADWAY AVE',
105
                    'ShippingAddressCheckoutComponent_AddressLine2' => 'U 235',
106
                    'ShippingAddressCheckoutComponent_City' => 'WEST BEACH',
107
                    'ShippingAddressCheckoutComponent_State' => 'South Australia',
108
                    'ShippingAddressCheckoutComponent_PostalCode' => '5024',
109
                    'ShippingAddressCheckoutComponent_Phone' => '',
110
                    'SeperateBilling' => '0'
111
112
                ));
113
                $self->assertEquals(
114
                    200,
115
                    $page->getStatusCode(),
116
                    "payment methods page should load"
117
                );
118
119
                $self->assertContains(
120
                    "CheckoutForm_PaymentMethodForm_PaymentMethod_Manual",
121
                    $page->getBody(),
122
                    "Manual payment method available"
123
                );
124
125
                $self->assertContains(
126
                    "You will be notified of the bank account details",
127
                    $page->getBody(),
128
                    "Bank Account Message presented during the payment method section"
129
                );
130
131
                // Payment Method can be manual
132
                $page = $self->submitForm("CheckoutForm_PaymentMethodForm", "action_setpaymentmethod", array(
133
                    'PaymentMethod' => 'Manual',
134
                ));
135
                $self->assertEquals(
136
                    200,
137
                    $page->getStatusCode(),
138
                    "Payment Method set.  The summary page should load."
139
                );
140
141
                // Summary
142
                $page = $self->submitForm("PaymentForm_ConfirmationForm", "action_checkoutSubmit", array(
143
                    'PaymentForm_ConfirmationForm_Notes' => 'Test',
144
                ));
145
                $self->assertEquals(
146
                    200,
147
                    $page->getStatusCode(),
148
                    "enter summary page should load"
149
                );
150
                $self->assertContains(
151
                    '<h2>My Account</h2>',
152
                    $page->getBody(),
153
                    "Account Page should load"
154
                );
155
156
                $self->assertContains(
157
                    'XX-3456-7891011-XX',
158
                    $page->getBody(),
159
                    "Account Page contains bank deposit instructions"
160
                );
161
162
                $self->assertContains(
163
                    'Hey bo, just pop the dosh in the account',
164
                    $page->getBody(),
165
                    "Bank Account Invoice Message delivered"
166
                );
167
168
                $self->assertEmailSent(
169
                    '[email protected]'
170
                );
171
            }
172
        );
173
    }
174
}
175