Completed
Pull Request — 2.0 (#461)
by Roman
27:38
created

CheckoutFormTest::setUpOnce()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
class CheckoutFormTest extends SapphireTest
4
{
5
    public static $fixture_file = 'silvershop/tests/fixtures/shop.yml';
6
7
    public function setUpOnce()
8
    {
9
        parent::setUpOnce();
10
        // clear session
11
        ShoppingCart::singleton()->clear();
12
    }
13
14
    public function setUp()
15
    {
16
        parent::setUp();
17
        ShopTest::setConfiguration();
18
        $this->mp3player = $this->objFromFixture('Product', 'mp3player');
0 ignored issues
show
Bug introduced by
The property mp3player 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...
19
        $this->mp3player->publish('Stage', 'Live');
20
        $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...
21
        $this->socks->publish('Stage', 'Live');
22
        $this->beachball = $this->objFromFixture('Product', 'beachball');
0 ignored issues
show
Bug introduced by
The property beachball 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...
23
        $this->beachball->publish('Stage', 'Live');
24
25
        $this->checkoutcontroller = new CheckoutPage_Controller();
0 ignored issues
show
Bug introduced by
The property checkoutcontroller 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...
26
27
        ShoppingCart::singleton()->add($this->socks); //start cart
0 ignored issues
show
Documentation introduced by
$this->socks is of type object<DataObject>|null, but the function expects a object<Buyable>.

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...
28
    }
29
30
    public function testCheckoutForm()
31
    {
32
        $order = ShoppingCart::curr();
33
        $config = new SinglePageCheckoutComponentConfig($order);
34
        $form = new CheckoutForm($this->checkoutcontroller, "OrderForm", $config);
35
        $data = array(
36
            "CustomerDetailsCheckoutComponent_FirstName"    => "Jane",
37
            "CustomerDetailsCheckoutComponent_Surname"      => "Smith",
38
            "CustomerDetailsCheckoutComponent_Email"        => "[email protected]",
39
            "ShippingAddressCheckoutComponent_Country"      => "NZ",
40
            "ShippingAddressCheckoutComponent_Address"      => "1234 Green Lane",
41
            "ShippingAddressCheckoutComponent_AddressLine2" => "Building 2",
42
            "ShippingAddressCheckoutComponent_City"         => "Bleasdfweorville",
43
            "ShippingAddressCheckoutComponent_State"        => "Trumpo",
44
            "ShippingAddressCheckoutComponent_PostalCode"   => "4123",
45
            "ShippingAddressCheckoutComponent_Phone"        => "032092277",
46
            "BillingAddressCheckoutComponent_Country"       => "NZ",
47
            "BillingAddressCheckoutComponent_Address"       => "1234 Green Lane",
48
            "BillingAddressCheckoutComponent_AddressLine2"  => "Building 2",
49
            "BillingAddressCheckoutComponent_City"          => "Bleasdfweorville",
50
            "BillingAddressCheckoutComponent_State"         => "Trumpo",
51
            "BillingAddressCheckoutComponent_PostalCode"    => "4123",
52
            "BillingAddressCheckoutComponent_Phone"         => "032092277",
53
            "PaymentCheckoutComponent_PaymentMethod"        => "Dummy",
54
            "NotesCheckoutComponent_Notes"                  => "Leave it around the back",
55
            "TermsCheckoutComponent_ReadTermsAndConditions" => "1",
56
        );
57
        $form->loadDataFrom($data, true);
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a integer.

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...
58
        $valid = $form->validate();
59
        $errors = $form->getValidator()->getErrors();
60
        $this->assertTrue($valid, print_r($errors, true));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<CheckoutFormTest>.

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...
61
        $form->checkoutSubmit($data, $form);
62
        $this->assertEquals("Jane", $order->FirstName);
0 ignored issues
show
Documentation introduced by
The property FirstName does not exist on object<Order>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Bug introduced by
The method assertEquals() does not seem to exist on object<CheckoutFormTest>.

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...
63
        $shipping = $order->ShippingAddress();
0 ignored issues
show
Bug introduced by
The method ShippingAddress() does not exist on Order. Did you maybe mean getShippingAddress()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
64
        $this->assertEquals("NZ", $shipping->Country);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<CheckoutFormTest>.

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...
65
        $this->assertEquals("Cart", $order->Status);
0 ignored issues
show
Bug introduced by
The property Status does not seem to exist. Did you mean placed_status?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
Bug introduced by
The method assertEquals() does not seem to exist on object<CheckoutFormTest>.

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...
66
67
        $this->markTestIncomplete('test invalid data');
0 ignored issues
show
Bug introduced by
The method markTestIncomplete() does not seem to exist on object<CheckoutFormTest>.

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...
68
        $this->markTestIncomplete('test components individually');
0 ignored issues
show
Bug introduced by
The method markTestIncomplete() does not seem to exist on object<CheckoutFormTest>.

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...
69
    }
70
71
    public function testCheckoutFormForSingleCountrySiteWithReadonlyFieldsForCountry()
72
    {
73
74
        // Set as a single country site
75
        $this->loadFixture("silvershop/tests/fixtures/singlecountry.yml");
76
        $singlecountry = SiteConfig::current_site_config();
77
        $this->assertEquals(
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<CheckoutFormTest>.

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
            "NZ",
79
            $singlecountry->getSingleCountry(),
80
            "Confirm that website is setup as a single country site"
81
        );
82
83
        $order = ShoppingCart::curr();
84
        $config = new SinglePageCheckoutComponentConfig($order);
85
        $form = new CheckoutForm($this->checkoutcontroller, "OrderForm", $config);
86
        // no country fields due to readonly field
87
        $dataCountryAbsent = array(
88
            "CustomerDetailsCheckoutComponent_FirstName"    => "Jane",
89
            "CustomerDetailsCheckoutComponent_Surname"      => "Smith",
90
            "CustomerDetailsCheckoutComponent_Email"        => "[email protected]",
91
            "ShippingAddressCheckoutComponent_Address"      => "1234 Green Lane",
92
            "ShippingAddressCheckoutComponent_AddressLine2" => "Building 2",
93
            "ShippingAddressCheckoutComponent_City"         => "Bleasdfweorville",
94
            "ShippingAddressCheckoutComponent_State"        => "Trumpo",
95
            "ShippingAddressCheckoutComponent_PostalCode"   => "4123",
96
            "ShippingAddressCheckoutComponent_Phone"        => "032092277",
97
            "BillingAddressCheckoutComponent_Address"       => "1234 Green Lane",
98
            "BillingAddressCheckoutComponent_AddressLine2"  => "Building 2",
99
            "BillingAddressCheckoutComponent_City"          => "Bleasdfweorville",
100
            "BillingAddressCheckoutComponent_State"         => "Trumpo",
101
            "BillingAddressCheckoutComponent_PostalCode"    => "4123",
102
            "BillingAddressCheckoutComponent_Phone"         => "032092277",
103
            "PaymentCheckoutComponent_PaymentMethod"        => "Dummy",
104
            "NotesCheckoutComponent_Notes"                  => "Leave it around the back",
105
            "TermsCheckoutComponent_ReadTermsAndConditions" => "1",
106
        );
107
        $form->loadDataFrom($dataCountryAbsent, true);
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a integer.

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...
108
        $valid = $form->validate();
109
        $errors = $form->getValidator()->getErrors();
110
        $this->assertTrue($valid, print_r($errors, true));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<CheckoutFormTest>.

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...
111
        $this->assertTrue(
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<CheckoutFormTest>.

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...
112
            $form->Fields()->fieldByName("ShippingAddressCheckoutComponent_Country_readonly")->isReadonly(),
113
            "Shipping Address Country field is readonly"
114
        );
115
        $this->assertTrue(
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<CheckoutFormTest>.

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...
116
            $form->Fields()->fieldByName("BillingAddressCheckoutComponent_Country_readonly")->isReadonly(),
117
            "Billing Address Country field is readonly"
118
        );
119
        $form->checkoutSubmit($dataCountryAbsent, $form);
120
121
        $shipping = $order->ShippingAddress();
0 ignored issues
show
Bug introduced by
The method ShippingAddress() does not exist on Order. Did you maybe mean getShippingAddress()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
122
        $this->assertEquals("NZ", $shipping->Country);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<CheckoutFormTest>.

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...
123
124
        $billing = $order->BillingAddress();
0 ignored issues
show
Bug introduced by
The method BillingAddress() does not exist on Order. Did you maybe mean getBillingAddress()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
125
        $this->assertEquals("NZ", $billing->Country);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<CheckoutFormTest>.

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...
126
    }
127
}
128
129
130