testEmailIsSentUponStepCheckoutCompletionByGuest()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 100
Code Lines 70

Duplication

Lines 0
Ratio 0 %

Importance

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

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