CheckoutTest   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 206
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 85
c 0
b 0
f 0
dl 0
loc 206
rs 10
wmc 13

12 Methods

Rating   Name   Duplication   Size   Complexity  
A testSetPaymentMethod() 0 4 1
A testSetShippingMethod() 0 4 1
A testMembersOnly() 0 14 1
A testSetUpBillingAddress() 0 7 1
A setUp() 0 13 1
A testSetUpShippingAddress() 0 7 1
A testCanBecomeMember() 0 13 2
A testMustBecomeOrBeMember() 0 17 1
A testNoMemberships() 0 13 1
A testMemberWithoutPassword() 0 9 1
A testMemberAlreadyExists() 0 10 1
A testMemberMissingIdentifier() 0 9 1
1
<?php
2
3
namespace SilverShop\Tests\Checkout;
4
5
use SilverShop\Cart\ShoppingCart;
6
use SilverShop\Checkout\Checkout;
7
use SilverShop\Checkout\CheckoutConfig;
8
use SilverShop\Checkout\ShopMemberFactory;
9
use SilverShop\Model\Address;
10
use SilverShop\Model\Order;
11
use SilverShop\Tests\ShopTest;
12
use SilverStripe\Core\Config\Config;
13
use SilverStripe\Dev\SapphireTest;
14
use SilverStripe\ORM\ValidationException;
15
use SilverStripe\Security\Member;
16
17
class CheckoutTest extends SapphireTest
18
{
19
    protected static $fixture_file = [
20
        __DIR__ . '/../Fixtures/Pages.yml',
21
        __DIR__ . '/../Fixtures/Orders.yml',
22
        __DIR__ . '/../Fixtures/Addresses.yml',
23
        __DIR__ . '/../Fixtures/ShopMembers.yml',
24
    ];
25
26
    /**
27
     * @var ShoppingCart
28
     */
29
    protected $cart;
30
31
    /**
32
     * @var Address
33
     */
34
    protected $address1;
35
36
    /**
37
     * @var Address
38
     */
39
    protected $address2;
40
41
    /**
42
     * @var Checkout
43
     */
44
    protected $checkout;
45
46
    /**
47
     * @var ShopMemberFactory
48
     */
49
    protected $memberFactory;
50
51
    public function setUp(): void
52
    {
53
        parent::setUp();
54
        ShopTest::setConfiguration();
55
        $this->cart = $this->objFromFixture(Order::class, "cart1");
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->objFromFixture(Si...\Order::class, 'cart1') of type SilverStripe\ORM\DataObject is incompatible with the declared type SilverShop\Cart\ShoppingCart of property $cart.

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...
56
        $this->address1 = $this->objFromFixture(Address::class, "address1");
57
        $this->address2 = $this->objFromFixture(Address::class, "address2");
58
        $this->checkout = new Checkout($this->cart);
59
        $this->memberFactory = new ShopMemberFactory();
60
61
        Config::modify()
62
            ->set(CheckoutConfig::class, 'member_creation_enabled', true)
63
            ->set(CheckoutConfig::class, 'membership_required', false);
64
    }
65
66
    public function testSetUpShippingAddress()
67
    {
68
        $this->checkout->setShippingAddress($this->address1);
69
        $this->assertEquals(
70
            $this->address1->ID,
71
            $this->cart->ShippingAddressID,
0 ignored issues
show
Bug introduced by
The property ShippingAddressID does not seem to exist on SilverShop\Cart\ShoppingCart.
Loading history...
72
            "shipping address was successfully added"
73
        );
74
    }
75
76
    public function testSetUpBillingAddress()
77
    {
78
        $this->checkout->setBillingAddress($this->address2);
79
        $this->assertEquals(
80
            $this->address2->ID,
81
            $this->cart->BillingAddressID,
0 ignored issues
show
Bug introduced by
The property BillingAddressID does not seem to exist on SilverShop\Cart\ShoppingCart.
Loading history...
82
            "billing address was successfully added"
83
        );
84
    }
85
86
    public function testSetShippingMethod()
87
    {
88
        //$this->checkout->setShippingMethod(new ShippingMethod()); //see shippingframework submodule
89
        $this->markTestIncomplete('combine shipping framework with core, or remove reliance');
90
    }
91
92
    public function testSetPaymentMethod()
93
    {
94
        $this->assertTrue($this->checkout->setPaymentMethod("Dummy"), "Valid method set correctly");
95
        $this->assertEquals('Dummy', $this->checkout->getSelectedPaymentMethod(false));
96
    }
97
98
    /**
99
     * Tests the default membership configuration.
100
     * You can become a member, but it is not necessary
101
     */
102
    public function testCanBecomeMember()
103
    {
104
        //check can proceeed with/without order
105
        //check member exists
106
        $result = $this->memberFactory->create(
107
            [
108
                'FirstName' => 'Jane',
109
                'Surname'   => 'Smith',
110
                'Email'     => '[email protected]',
111
                'Password'  => 'janesmith2012',
112
            ]
113
        );
114
        $this->assertTrue(($result instanceof Member), $this->checkout->getMessage() || '');
115
    }
116
117
    public function testMustBecomeOrBeMember()
118
    {
119
        CheckoutConfig::config()->member_creation_enabled = true;
120
        CheckoutConfig::config()->membership_required = true;
121
122
        $member = $this->memberFactory->create(
123
            [
124
                'FirstName' => 'Susan',
125
                'Surname'   => 'Jackson',
126
                'Email'     => '[email protected]',
127
                'Password'  => 'jaleho3htgll',
128
            ]
129
        );
130
131
        $this->assertTrue($this->checkout->validateMember($member));
132
        //check can't proceed without being a member
133
        $this->assertFalse($this->checkout->validateMember(false));
134
    }
135
136
    public function testNoMemberships()
137
    {
138
        CheckoutConfig::config()->member_creation_enabled = false;
139
        CheckoutConfig::config()->membership_required = false;
140
141
        $this->expectException(ValidationException::class);
142
143
        $member = $this->memberFactory->create(
0 ignored issues
show
Unused Code introduced by
The assignment to $member is dead and can be removed.
Loading history...
144
            [
145
                'FirstName' => 'Susan',
146
                'Surname'   => 'Jackson',
147
                'Email'     => '[email protected]',
148
                'Password'  => 'jaleho3htgll',
149
            ]
150
        );
151
    }
152
153
    /**
154
     * @expectedException \SilverStripe\ORM\ValidationException
155
     * @expectedExceptionMessage Creating new memberships is not allowed
156
     */
157
    public function testMembersOnly()
158
    {
159
        CheckoutConfig::config()->member_creation_enabled = false;
160
        CheckoutConfig::config()->membership_required = true;
161
162
        $this->expectException(ValidationException::class);
163
        $this->expectExceptionMessage('Creating new memberships is not allowed');
164
165
        $result = $this->memberFactory->create(
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
166
            [
167
                'FirstName' => 'Some',
168
                'Surname'   => 'Body',
169
                'Email'     => '[email protected]',
170
                'Password'  => 'pass1234',
171
            ]
172
        );
173
    }
174
175
    /**
176
     * @expectedException \SilverStripe\ORM\ValidationException
177
     * @expectedExceptionMessage A password is required
178
     */
179
    public function testMemberWithoutPassword()
180
    {
181
        $this->expectException(ValidationException::class);
182
        $this->expectExceptionMessage('A password is required');
183
        $result = $this->memberFactory->create(
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
184
            [
185
                'FirstName' => 'Jim',
186
                'Surname'   => 'Smith',
187
                'Email'     => '[email protected]',
188
            ]
189
        );
190
    }
191
192
    /**
193
     * @expectedException \SilverStripe\ORM\ValidationException
194
     * @expectedExceptionMessage A member already exists with the Email [email protected]
195
     */
196
    public function testMemberAlreadyExists()
197
    {
198
        $this->expectException(ValidationException::class);
199
        $this->expectExceptionMessage('A member already exists with the Email [email protected]');
200
        $result = $this->memberFactory->create(
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
201
            [
202
                'FirstName' => 'Jeremy',
203
                'Surname'   => 'Peremy',
204
                'Email'     => '[email protected]',
205
                'Password'  => 'jeremyperemy',
206
            ]
207
        );
208
    }
209
210
    /**
211
     * @expectedException \SilverStripe\ORM\ValidationException
212
     * @expectedExceptionMessage Required field not found: Email
213
     */
214
    public function testMemberMissingIdentifier()
215
    {
216
        $this->expectException(ValidationException::class);
217
        $this->expectExceptionMessage('Required field not found: Email');
218
        $result = $this->memberFactory->create(
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
219
            [
220
                'FirstName' => 'John',
221
                'Surname'   => 'Doe',
222
                'Password'  => 'johndoe1234',
223
            ]
224
        );
225
    }
226
}
227