testEmailIsSentUponStepCheckoutCompletion()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 93
Code Lines 65

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 65
c 4
b 0
f 0
dl 0
loc 93
rs 8.7636
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\ORM\DataObject;
8
use SilverStripe\Security\Member;
9
use SilverStripe\View\SSViewer;
10
use SilverShop\Tests\ShopTest;
11
use SilverShop\Extension\SteppedCheckoutExtension;
12
use SilverShop\Page\Product;
13
use SilverShop\Page\CheckoutPage;
14
use SilverShop\Page\AccountPage;
15
use SilverShop\Cart\ShoppingCart;
16
use SilverShop\Cart\ShoppingCartController;
17
18
/**
19
 * Test that the Manual payment method can be used on a stepped checkout and
20
 * that an email is sent upon completing the form
21
 */
22
class SteppedCheckoutPageTest extends FunctionalTest
23
{
24
    protected static $fixture_file = [
25
        'vendor/silvershop/core/tests/php/Fixtures/Pages.yml',
26
        'vendor/silvershop/core/tests/php/Fixtures/shop.yml',
27
        'vendor/silvershop/core/tests/php/Fixtures/Orders.yml',
28
        'orders.yml'
29
    ];
30
31
    /**
32
     * @var bool
33
     */
34
    protected static $disable_themes  = true;
35
36
    protected DataObject $laptop;
37
38
    protected function setUp(): void
39
    {
40
        parent::setUp();
41
        ShopTest::setConfiguration();
42
        ShoppingCart::singleton()->clear();
43
        SteppedCheckoutExtension::setupSteps(); //use default steps
44
45
        $siteconfig = DataObject::get_one(SiteConfig::class);
46
        $siteconfig->BankAccountPaymentMethodMessage = "You will be notified of the bank account details";
47
        $siteconfig->BankAccountNumber = "XX-3456-7891011-XX";
48
        $siteconfig->BankAccountDetails = "TestBank, Business Branch";
49
        $siteconfig->BankAccountInvoiceMessage = "Hey bo, just pop the dosh in the account";
50
        $siteconfig->write();
51
52
        SSViewer::config()->get('source_file_comments = true');
53
54
        // establish products
55
        $this->laptop = $this->objFromFixture(Product::class, "laptop");
56
        $this->laptop->publishSingle();
57
58
        // publish pages
59
        $checkoutpage = $this->objFromFixture(CheckoutPage::class, "checkout");
60
        $checkoutpage->publishSingle();
61
62
        $accountpage = $this->objFromFixture(AccountPage::class, "accountpage");
63
        $accountpage->publishSingle();
64
65
        // Login member
66
        $member = $this->objFromFixture(Member::class, "joebloggs");
67
        $this->logInAs($member);
68
69
        //add item to cart via url
70
        $this->get((string) 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

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