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

tests/php/Model/AddressTest.php (2 issues)

1
<?php
2
3
namespace SilverShop\Tests\Model;
4
5
use SilverShop\Model\Address;
6
use SilverStripe\Dev\SapphireTest;
7
use Exception;
8
9
class AddressTest extends SapphireTest
10
{
11
    public function testForm()
12
    {
13
        $address = Address::create()->update(
14
            [
15
                'Country' => 'NZ',
16
                'State' => 'Wellington',
17
                'City' => 'TeAro',
18
                'PostalCode' => '1333',
19
                'Address' => '23 Blah Street',
20
                'AddressLine2' => 'Fitzgerald Building, Foor 3',
21
                'Company' => 'Ink inc',
22
                'FirstName' => 'Jerald',
23
                'Surname' => 'Smith',
24
                'Phone' => '12346678',
25
            ]
26
        );
27
28
        $fields = $address->getFrontEndFields();
0 ignored issues
show
The assignment to $fields is dead and can be removed.
Loading history...
29
        $requiremetns = $address->getRequiredFields();
0 ignored issues
show
The assignment to $requiremetns is dead and can be removed.
Loading history...
30
        $this->assertEquals(
31
            "Ink inc|Jerald Smith|23 Blah Street|Fitzgerald Building, Foor 3|TeAro|Wellington|1333|NZ",
32
            $address->toString("|")
33
        );
34
    }
35
36
    public function testRequiredFields()
37
    {
38
        // create address instance that lacks some required fields (Address)
39
        $address = Address::create()->update(
40
            [
41
                'Country' => 'NZ',
42
                'State' => 'Wellington',
43
                'City' => 'TeAro',
44
            ]
45
        );
46
47
        $writeFailed = false;
48
        try {
49
            $address->write();
50
        } catch (Exception $ex) {
51
            $writeFailed = true;
52
        }
53
54
        $this->assertTrue($writeFailed, "Address should not be writable, since it doesn't contain all required fields");
55
56
        // Create an Address that satisfies the baseline required fields, but not the ones that were added via subclass.
57
        $address = ExtendedTestAddress::create()->update(
58
            [
59
                'Country' => 'NZ',
60
                'State' => 'Wellington',
61
                'City' => 'TeAro',
62
                'Address' => '23 Blah Street',
63
            ]
64
        );
65
66
        $writeFailed = false;
67
        try {
68
            $address->write();
69
        } catch (Exception $ex) {
70
            $writeFailed = true;
71
        }
72
73
        $this->assertTrue(
74
            $writeFailed,
75
            "Address should not be writable, since it doesn't contain required fields added via subclass"
76
        );
77
    }
78
}
79