PostageFormTest::testObjectChanged()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 12
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 23
rs 9.8666
1
<?php
2
3
namespace SilverCommerce\Postage\Tests;
4
5
use SilverStripe\Control\Director;
6
use SilverStripe\Control\Controller;
7
use SilverStripe\Core\Config\Config;
8
use SilverStripe\Dev\FunctionalTest;
9
use SilverStripe\Forms\DropdownField;
10
use SilverStripe\Forms\ReadonlyField;
11
use SilverStripe\Forms\OptionsetField;
12
use SilverCommerce\GeoZones\Model\Zone;
13
use SilverStripe\SiteConfig\SiteConfig;
14
use SilverCommerce\GeoZones\Model\Region;
15
use SilverCommerce\Postage\Forms\PostageForm;
16
use SilverCommerce\GeoZones\Forms\RegionSelectionField;
17
use SilverCommerce\Postage\Tests\Model\ExtendableObject;
18
use SilverCommerce\Postage\Tests\Control\PostageController;
19
use SilverCommerce\Postage\Helpers\Parcel;
20
use SilverCommerce\Postage\Helpers\PostageOption;
21
22
class PostageFormTest extends FunctionalTest
23
{
24
    protected static $fixture_file = 'PostageFormTest.yml';
25
26
    protected static $extra_dataobjects = [
27
        ExtendableObject::class
28
    ];
29
30
    public function setUp()
31
    {
32
        parent::setUp();
33
        Config::inst()->set(Region::class, "create_on_build", false);
0 ignored issues
show
Bug introduced by
The method set() does not exist on SilverStripe\Config\Coll...nfigCollectionInterface. It seems like you code against a sub-type of SilverStripe\Config\Coll...nfigCollectionInterface such as SilverStripe\Config\Coll...nfigCollectionInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

33
        Config::inst()->/** @scrutinizer ignore-call */ set(Region::class, "create_on_build", false);
Loading history...
34
        Director::config()->set(
35
            "rules",
36
            [
37
                'postagetest//$Action/$ID/$OtherID' => PostageController::class
38
            ]
39
        );
40
    }
41
42
    /**
43
     * Test that the form generates dropdowns when no country provided
44
     */
45
    public function testDropdowns()
46
    {
47
        $object = $this->objFromFixture(ExtendableObject::class, "uk");
48
49
        $form = PostageForm::create(
50
            Controller::curr(),
51
            "PostageForm",
52
            $object,
53
            $object->SubTotal,
54
            $object->TotalWeight,
55
            $object->TotalItems
56
        );
57
58
        $this->assertInstanceOf(
59
            DropdownField::class,
60
            $form->Fields()->dataFieldByName("Country")
61
        );
62
63
        $this->assertInstanceOf(
64
            RegionSelectionField::class,
65
            $form->Fields()->dataFieldByName("Region")
66
        );
67
    }
68
69
    /**
70
     * Test that the form generates a correct list of postage
71
     */
72
    public function testFixed()
73
    {
74
        $object = $this->objFromFixture(ExtendableObject::class, "uk");
75
76
        $form = PostageForm::create(
77
            Controller::curr(),
78
            "PostageForm",
79
            $object,
80
            $object->SubTotal,
81
            $object->TotalWeight,
82
            $object->TotalItems,
83
            $object->DeliveryCountry,
84
            $object->DeliveryCounty
85
        );
86
87
        $this->assertInstanceOf(
88
            OptionsetField::class,
89
            $form->Fields()->dataFieldByName("PostageKey")
90
        );
91
    }
92
93
94
    /**
95
     * Test that the form generates a no postage error
96
     */
97
    public function testNoPostage()
98
    {
99
        $object = $this->objFromFixture(ExtendableObject::class, "us");
100
101
        $form = PostageForm::create(
102
            Controller::curr(),
103
            "PostageForm",
104
            $object,
105
            $object->SubTotal,
106
            $object->TotalWeight,
107
            $object->TotalItems,
108
            $object->DeliveryCountry,
109
            $object->DeliveryCounty
110
        );
111
112
        $field = $form->Fields()->dataFieldByName("NoPostage");
113
114
        $this->assertInstanceOf(ReadonlyField::class, $field);
115
        $this->assertEquals(
116
            "Unfortunatley we cannot post to this location",
117
            $field->Value()
118
        );
119
    }
120
121
    /**
122
     * Test that the form updated the object with the new postage
123
     *
124
     */
125
    public function testObjectChanged()
126
    {
127
        $object = $this->objFromFixture(ExtendableObject::class, "uk");
128
        $id = $object->ID;
129
        $page = $this->get('postagetest/index/' . $id);
130
        $postage_key = $object->getPostage()->getKey();
131
132
        // Page should load..
133
        $this->assertEquals(200, $page->getStatusCode());
134
135
        // Submit the postage form
136
        $result = $this->submitForm(
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
137
            "PostageForm_PostageForm",
138
            null,
139
            ["PostageKey" => $postage_key]
140
        );
141
142
        // Reload the page and check result
143
        $page = $this->get('postagetest/index/' . $id);
0 ignored issues
show
Unused Code introduced by
The assignment to $page is dead and can be removed.
Loading history...
144
145
        // Check the rendered key
146
        $this->assertExactMatchBySelector("p.postage-key", [
147
            $postage_key
148
        ]);
149
    }
150
}
151