ShippingEstimateFormTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 7
dl 0
loc 105
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 19 2
B testGetEstimates() 0 26 1
A testShippingEstimateWithReadonlyFieldForCountry() 0 23 1
A useTheme() 0 15 1
1
<?php
2
3
class ShippingEstimateFormTest extends FunctionalTest{
4
5
    static $fixture_file = array(
6
        "silvershop-shipping/tests/fixtures/TableShippingMethod.yml",
7
        "silvershop/tests/fixtures/shop.yml",
8
        "silvershop/tests/fixtures/Pages.yml"
9
    );
10
    protected static $use_draft_site = true;
11
12
    function setUp() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
13
        if(method_exists('SapphireTest', 'useTestTheme')){
14
            $this->useTestTheme($themeBaseDir = dirname(__FILE__), 'testtheme', function(){});
15
        } else {
16
            $this->useTheme('testtheme');
17
        }
18
        parent::setUp();
19
        ShopTest::setConfiguration();
20
        // add product to the cart
21
        $this->socks = $this->objFromFixture('Product', 'socks');
0 ignored issues
show
Bug introduced by
The property socks does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
22
        $this->socks->publish('Stage','Live');
23
24
        $this->cartpage = $this->objFromFixture("CartPage", "cart");
0 ignored issues
show
Bug introduced by
The property cartpage does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
25
        $this->cartpage->publish('Stage','Live');
26
        ShoppingCart::singleton()->setCurrent($this->objFromFixture("Order", "cart")); //set the current cart
0 ignored issues
show
Documentation introduced by
$this->objFromFixture('Order', 'cart') is of type object<DataObject>|null, but the function expects a object<Order>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
27
28
        // Open cart page
29
        $page = $this->get('/cart');
0 ignored issues
show
Unused Code introduced by
$page is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
30
    }
31
32
    function testGetEstimates() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
33
34
        //good data for Shipping Estimate Form
35
        $data = array(
36
            'Country' => 'NZ',
37
            'State' => 'Auckland',
38
            'City' => 'Auckland',
39
            'PostalCode' => 1010
40
        );
41
        $page1 = $this->post('/cart/ShippingEstimateForm', $data);
42
        $this->assertEquals(200, $page1->getStatusCode(), "a page should load");
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<ShippingEstimateFormTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
43
        $this->assertContains("Quantity-based shipping", $page1->getBody(), "ShippingEstimates presented in a table");
44
45
46
        //un-escaped data for Shipping Estimate Form
47
        $data = array(
48
            'Country' => 'NZ',
49
            'State' => 'Hawke\'s Bay',
50
            'City' => 'SELECT * FROM \" \' WHERE AND EVIL',
51
            'PostalCode' => 1234
52
        );
53
        $page2 = $this->post('/cart/ShippingEstimateForm', $data);
54
        $this->assertEquals(200, $page2->getStatusCode(), "a page should load");
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<ShippingEstimateFormTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
55
        $this->assertContains("Quantity-based shipping", $page2->getBody(), "ShippingEstimates can be successfully presented with un-escaped data in the form");
56
57
    }
58
59
    function testShippingEstimateWithReadonlyFieldForCountry() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
60
        // setup a single-country site
61
        $siteconfig = DataObject::get_one('SiteConfig');
62
        $siteconfig->AllowedCountries = "NZ";
63
        $siteconfig->write();
64
65
        // Open cart page where Country field is readonly
66
        $page = $this->get('/cart');
67
        $this->assertContains("Country_readonly", $page->getBody(), "The Country field is readonly");
68
        $this->assertNotContains("<option value=\"NZ\">New Zealand</option>", $page->getBody(), "Dropdown field is not shown");
69
70
        // The Shipping Estimate Form can post with a Country readonly field
71
        $data = array(
72
            'State' => 'Waikato',
73
            'City' => 'Hamilton',
74
            'PostalCode' => 3210
75
        );
76
        $page3 = $this->post('/cart/ShippingEstimateForm', $data);
77
        $this->assertEquals(200, $page3->getStatusCode(), "a page should load");
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<ShippingEstimateFormTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
78
        $this->assertContains("Quantity-based shipping", $page3->getBody(), "ShippingEstimates can be successfully presented with a Country readonly field");
79
80
81
    }
82
83
84
    /**
85
    * Test against a SS Shop Shipping theme
86
    *
87
    * Template CartPage.ss contains <% include ShippingEstimator %>
88
    * Function adapted from SSViewerTest.php in SSv3.1
89
    * @param $theme string - theme name
90
    */
91
    protected function useTheme($theme) {
92
        global $project;
93
94
        $themeBaseDir = dirname(__FILE__);
95
        $manifest = new SS_TemplateManifest($themeBaseDir, $project, true, true);
96
97
        SS_TemplateLoader::instance()->pushManifest($manifest);
98
99
        $origTheme = Config::inst()->get('SSViewer', 'theme');
100
        Config::inst()->update('SSViewer', 'theme', $theme);
101
102
        // Remove all the test themes we created
103
        SS_TemplateLoader::instance()->popManifest();
104
        Config::inst()->update('SSViewer', 'theme', $origTheme);
105
    }
106
107
}
108