Completed
Push — master ( 3c661d...2a57f9 )
by Will
26s queued 12s
created

tests/php/Checkout/CheckoutTest.php (2 issues)

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 = array(
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()
52
    {
53
        parent::setUp();
54
        ShopTest::setConfiguration();
55
        $this->cart = $this->objFromFixture(Order::class, "cart1");
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
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
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
            array(
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
            array(
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(
144
            array(
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
        $result = $this->memberFactory->create(
162
            array(
163
                'FirstName' => 'Some',
164
                'Surname'   => 'Body',
165
                'Email'     => '[email protected]',
166
                'Password'  => 'pass1234',
167
            )
168
        );
169
170
        $this->fail("Exception was expected here");
171
    }
172
173
    /**
174
     * @expectedException \SilverStripe\ORM\ValidationException
175
     * @expectedExceptionMessage A password is required
176
     */
177
    public function testMemberWithoutPassword()
178
    {
179
        $result = $this->memberFactory->create(
180
            array(
181
                'FirstName' => 'Jim',
182
                'Surname'   => 'Smith',
183
                'Email'     => '[email protected]',
184
            )
185
        );
186
        $this->fail("Exception was expected here");
187
    }
188
189
    /**
190
     * @expectedException \SilverStripe\ORM\ValidationException
191
     * @expectedExceptionMessage A member already exists with the Email [email protected]
192
     */
193
    public function testMemberAlreadyExists()
194
    {
195
        $result = $this->memberFactory->create(
196
            array(
197
                'FirstName' => 'Jeremy',
198
                'Surname'   => 'Peremy',
199
                'Email'     => '[email protected]',
200
                'Password'  => 'jeremyperemy',
201
            )
202
        );
203
        $this->fail("Exception was expected here");
204
    }
205
206
    /**
207
     * @expectedException \SilverStripe\ORM\ValidationException
208
     * @expectedExceptionMessage Required field not found: Email
209
     */
210
    public function testMemberMissingIdentifier()
211
    {
212
        $result = $this->memberFactory->create(
213
            array(
214
                'FirstName' => 'John',
215
                'Surname'   => 'Doe',
216
                'Password'  => 'johndoe1234',
217
            )
218
        );
219
        $this->fail("Exception was expected here");
220
    }
221
}
222