AddressBookCheckoutComponentTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 56
c 1
b 0
f 0
dl 0
loc 131
rs 10
wmc 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 16 1
A testUseExistingAddress() 0 7 1
A testShouldNotCreateBlankAddresses() 0 11 1
A testCreateNewAddress() 0 4 1
A testIncompleteNewAddress() 0 7 1
A testShouldRejectExistingIfNotLoggedIn() 0 7 1
A testShouldRejectExistingIfNotOwnedByMember() 0 11 1
1
<?php
2
3
namespace SilverShop\Tests\Checkout\Component;
4
5
use SilverShop\Checkout\CheckoutComponentConfig;
6
use SilverShop\Checkout\CheckoutConfig;
7
use SilverShop\Checkout\Component\AddressBookBilling;
8
use SilverShop\Model\Address;
9
use SilverShop\Model\Order;
10
use SilverShop\Tests\ShopTest;
11
use SilverStripe\Security\Member;
12
use SilverStripe\ORM\ValidationException;
13
use SilverStripe\Dev\SapphireTest;
14
use SilverStripe\Security\Security;
15
16
class AddressBookCheckoutComponentTest extends SapphireTest
17
{
18
    // Component namespace
19
    const COMPONENT_NS = 'SilverShop-Checkout-Component-AddressBookBilling';
20
21
    protected static $fixture_file = [
22
        '../../Fixtures/Orders.yml',
23
        '../../Fixtures/ShopMembers.yml',
24
    ];
25
26
    /**
27
     * @var Order $cart
28
     */
29
    protected $cart;
30
31
    /**
32
     * @var Member $member
33
     */
34
    protected $member;
35
36
    /**
37
     * @var Address $address1
38
     */
39
    protected $address1;
40
41
    /**
42
     * @var Address $address2
43
     */
44
    protected $address2;
45
46
    /**
47
     * @var CheckoutComponentConfig $config
48
     */
49
    protected $config;
50
51
    protected $fixtureNewAddress = [
52
        self::COMPONENT_NS . '_BillingAddressID' => 'newaddress',
53
        self::COMPONENT_NS . '_Country'          => 'US',
54
        self::COMPONENT_NS . '_Address'          => '123 Test St',
55
        self::COMPONENT_NS . '_AddressLine2'     => 'Apt 4',
56
        self::COMPONENT_NS . '_City'             => 'Siloam Springs',
57
        self::COMPONENT_NS . '_State'            => 'AR',
58
        self::COMPONENT_NS . '_PostalCode'       => '72761',
59
        self::COMPONENT_NS . '_Phone'            => '11231231234',
60
    ];
61
62
    public function setUp(): void
63
    {
64
        ShopTest::setConfiguration();
65
        CheckoutConfig::config()->membership_required = false;
66
        parent::setUp();
67
68
        $this->member = $this->objFromFixture(Member::class, "jeremyperemy");
69
        $this->cart = $this->objFromFixture(Order::class, "cart1");
70
        $this->address1 = $this->objFromFixture(Address::class, "address1");
71
        $this->address2 = $this->objFromFixture(Address::class, "address2");
72
        $this->config = new CheckoutComponentConfig($this->cart, true);
73
74
        $this->config->addComponent(new AddressBookBilling());
75
76
        $this->address1->MemberID = $this->member->ID;
77
        $this->address1->write();
78
    }
79
80
    public function testCreateNewAddress()
81
    {
82
        $this->assertTrue(
83
            $this->config->validateData($this->fixtureNewAddress)
84
        );
85
    }
86
87
    public function testIncompleteNewAddress()
88
    {
89
        $this->expectException(ValidationException::class);
90
        $data = $this->fixtureNewAddress;
91
        $data[self::COMPONENT_NS . '_Country'] = '';
92
93
        $this->config->validateData($data);
94
    }
95
96
    public function testUseExistingAddress()
97
    {
98
        Security::setCurrentUser($this->member);
99
        $this->assertTrue(
100
            $this->config->validateData(
101
                [
102
                    self::COMPONENT_NS . '_BillingAddressID' => $this->address1->ID,
103
                ]
104
            )
105
        );
106
    }
107
108
    public function testShouldRejectExistingIfNotLoggedIn()
109
    {
110
        $this->expectException(ValidationException::class);
111
        $this->assertTrue(
112
            $this->config->validateData(
113
                [
114
                    self::COMPONENT_NS . '_BillingAddressID' => $this->address1->ID,
115
                ]
116
            )
117
        );
118
    }
119
120
    public function testShouldRejectExistingIfNotOwnedByMember()
121
    {
122
        $this->expectException(ValidationException::class);
123
        Security::setCurrentUser($this->member);
124
        $this->address1->MemberID = 0;
0 ignored issues
show
Bug Best Practice introduced by
The property MemberID does not exist on SilverShop\Model\Address. Since you implemented __set, consider adding a @property annotation.
Loading history...
125
        $this->address1->write();
126
127
        $this->assertTrue(
128
            $this->config->validateData(
129
                [
130
                    self::COMPONENT_NS . '_BillingAddressID' => $this->address1->ID,
131
                ]
132
            )
133
        );
134
    }
135
136
    public function testShouldNotCreateBlankAddresses()
137
    {
138
        $beforeCount = Address::get()->count();
139
        $this->config->setData(
140
            [
141
                self::COMPONENT_NS . '_BillingAddressID' => $this->address1->ID,
142
            ]
143
        );
144
145
        $this->assertEquals($this->cart->BillingAddressID, $this->address1->ID);
146
        $this->assertEquals($beforeCount, Address::get()->count());
147
    }
148
}
149