Completed
Push — master ( 61756c...9c4d64 )
by Antony
02:56
created

tests/SilvershopJsonResponseTest.php (29 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Functional tests of json responses for shopping cart of Silverstripe Shop
4
 *
5
 * @package shop
6
 * @subpackage tests
7
 */
8
class SilvershopJsonResponseTest extends FunctionalTest
9
{
10
11
    protected static $fixture_file = 'silvershop/tests/fixtures/shop.yml';
12
13
    public function setUpOnce()
14
    {
15
        if (!ShoppingCart_Controller::has_extension('SilvershopJsonResponse')) {
16
            ShoppingCart_Controller::add_extension('SilvershopJsonResponse');
17
        }
18
        if (!VariationForm::has_extension('SilvershopJsonResponse')) {
19
            VariationForm::add_extension('SilvershopJsonResponse');
20
        }
21
        parent::setUpOnce();
22
    }
23
24
    public function setUp()
25
    {
26
        parent::setUp();
27
        ShopTest::setConfiguration(); //reset config
28
        Order::config()->modifiers = array(
29
            "FlatTaxModifier"
30
        );
31
        SilvershopJsonResponse::config()->include_totals = true;
32
33
        $this->mp3player = $this->objFromFixture('Product', 'mp3player');
34
        $this->socks     = $this->objFromFixture('Product', 'socks');
35
36
        //publish some products
37
        $this->mp3player->publish('Stage', 'Live');
38
        $this->socks->publish('Stage', 'Live');
39
40
        $this->cart = ShoppingCart::singleton();
41
        $this->cart->clear();
42
    }
43
44
    public function testAddToCart()
45
    {
46
        // test ajax request (Product Category page)
47
        $response = $this->get(ShoppingCart_Controller::add_item_link($this->mp3player) . "?ajax=1");
48
        $this->assertEquals(200, $response->getStatusCode(), "Response status code is 200");
0 ignored issues
show
The method assertEquals() does not seem to exist on object<SilvershopJsonResponseTest>.

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...
49
        $this->assertEquals("application/json; charset=utf-8", $response->getHeader("Content-Type"), "Json response header");
0 ignored issues
show
The method assertEquals() does not seem to exist on object<SilvershopJsonResponseTest>.

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...
50
        $this->assertJson($response->getBody(), "Contains json in the body of the response");
0 ignored issues
show
The method assertJson() does not seem to exist on object<SilvershopJsonResponseTest>.

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...
51
52
        $this->assertContains("addLink", $response->getBody(), "response contains a link to add additional quantities of an item in the cart");
53
        $this->assertContains("removeLink", $response->getBody(), "response contains a link to reduce the quantity of an item in a cart");
54
        $this->assertContains("removeallLink", $response->getBody(), "response contains a link to remove all of an item from a cart");
55
        $this->assertContains("setquantityLink", $response->getBody(), "response contains a link to set the quantity of an item in a cart");
56
        $this->assertContains("unitPrice", $response->getBody(), "response contains the unit price of items in a cart");
57
        $this->assertContains("subTotal", $response->getBody(), "response contains a subTotal when include_totals is set to true");
58
59
        // See what's in the cart
60
        $items = ShoppingCart::curr()->Items();
61
        $this->assertNotNull($items);
62
        $this->assertEquals($items->Count(), 1, 'There is 1 item in the cart');
0 ignored issues
show
The method assertEquals() does not seem to exist on object<SilvershopJsonResponseTest>.

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
    }
64
65
    public function testRemoveFromCart()
66
    {
67
68
        // add items via url to setup
69
        $this->get(ShoppingCart_Controller::set_quantity_item_link($this->mp3player, array('quantity' => 5)));
70
        $this->get(ShoppingCart_Controller::add_item_link($this->socks));
71
        ShoppingCart::curr()->calculate();  // recalculate the shopping cart
72
73
        $cart = $this->get("shoppingcart/get" . "?ajax=1");
74
        $this->assertContains('"id":"10"', $cart->getBody(), "Contains the mp3 player");
75
        $this->assertContains('"id":"6"', $cart->getBody(), "Contains the socks");
76
77
        // remove the one of the mp3 players via url making the total 4
78
        $response = $this->get(ShoppingCart_Controller::remove_item_link($this->mp3player) . "?ajax=1");
79
        $this->assertEquals(200, $response->getStatusCode(), "Response status code is 200");
0 ignored issues
show
The method assertEquals() does not seem to exist on object<SilvershopJsonResponseTest>.

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...
80
        $this->assertEquals("application/json; charset=utf-8", $response->getHeader("Content-Type"), "Json response header");
0 ignored issues
show
The method assertEquals() does not seem to exist on object<SilvershopJsonResponseTest>.

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...
81
        $this->assertJson($response->getBody(), "Contains json in the body of the response");
0 ignored issues
show
The method assertJson() does not seem to exist on object<SilvershopJsonResponseTest>.

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...
82
        $this->assertContains('{"id":"10"', $response->getBody(), "Contains json in the body of the response and id of the mp3 player");
83
84
        // remove the one of the socks via url making the total NIL so it is fully removed
85
        $response = $this->get(ShoppingCart_Controller::remove_item_link($this->socks) . "?ajax=1");
86
        $this->assertEquals(200, $response->getStatusCode(), "Response status code is 200");
0 ignored issues
show
The method assertEquals() does not seem to exist on object<SilvershopJsonResponseTest>.

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...
87
        $this->assertEquals("application/json; charset=utf-8", $response->getHeader("Content-Type"), "Json response header");
0 ignored issues
show
The method assertEquals() does not seem to exist on object<SilvershopJsonResponseTest>.

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...
88
        $this->assertJson($response->getBody(), "Contains json in the body of the response");
0 ignored issues
show
The method assertJson() does not seem to exist on object<SilvershopJsonResponseTest>.

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...
89
        $this->assertFalse($this->cart->get($this->socks), "socks completely removed");
0 ignored issues
show
The method assertFalse() does not seem to exist on object<SilvershopJsonResponseTest>.

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...
90
        $this->assertContains('{"id":"6"', $response->getBody(), "id of Socks returned");
91
    }
92
93
    public function testRemoveAllFromCart()
94
    {
95
        // add items via url to setup
96
        $this->get(ShoppingCart_Controller::set_quantity_item_link($this->mp3player, array('quantity' => 5)));
97
        $this->get(ShoppingCart_Controller::add_item_link($this->socks));
98
99
        // remove items via url
100
        $response = $this->get(ShoppingCart_Controller::remove_all_item_link($this->mp3player) . "?ajax=1"); //remove all mp3 players
101
        $this->assertEquals(200, $response->getStatusCode(), "Response status code is 200");
0 ignored issues
show
The method assertEquals() does not seem to exist on object<SilvershopJsonResponseTest>.

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...
102
        $this->assertEquals("application/json; charset=utf-8", $response->getHeader("Content-Type"), "Json response header");
0 ignored issues
show
The method assertEquals() does not seem to exist on object<SilvershopJsonResponseTest>.

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...
103
        $this->assertJson($response->getBody(), "Contains json in the body of the response");
0 ignored issues
show
The method assertJson() does not seem to exist on object<SilvershopJsonResponseTest>.

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...
104
        $this->assertContains('{"id":"10"', $response->getBody(), "Contains json in the body of the response and id of item removed");
105
        $this->assertFalse($this->cart->get($this->mp3player), "Mp3 Players are not in the cart");
0 ignored issues
show
The method assertFalse() does not seem to exist on object<SilvershopJsonResponseTest>.

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...
106
    }
107
108
    public function testSetQuantityInCart()
109
    {
110
        // add items via url to setup
111
        $this->get(ShoppingCart_Controller::set_quantity_item_link($this->mp3player, array('quantity' => 5)));
112
        $this->get(ShoppingCart_Controller::add_item_link($this->socks));
113
114
        // set items via url
115
        $response = $this->get(ShoppingCart_Controller::set_quantity_item_link($this->mp3player, array('quantity' => 3, 'ajax' => 1))); //set quantity of mp3 players to 3
0 ignored issues
show
It seems like \ShoppingCart_Controller...ty' => 3, 'ajax' => 1)) targeting ShoppingCart_Controller::set_quantity_item_link() can also be of type false; however, FunctionalTest::get() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
116
        $this->assertEquals(200, $response->getStatusCode(), "Response status code is 200");
0 ignored issues
show
The method assertEquals() does not seem to exist on object<SilvershopJsonResponseTest>.

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...
117
        $this->assertEquals("application/json; charset=utf-8", $response->getHeader("Content-Type"), "Json response header");
0 ignored issues
show
The method assertEquals() does not seem to exist on object<SilvershopJsonResponseTest>.

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...
118
        $this->assertJson($response->getBody(), "Contains json in the body of the response");
0 ignored issues
show
The method assertJson() does not seem to exist on object<SilvershopJsonResponseTest>.

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...
119
120
        $this->assertContains('Quantity has been set', $response->getBody(), "Contains a confirmation message that the quantity has been set to a new value");
121
    }
122
123
    public function testClearAllItemsFromTheCart()
124
    {
125
        // add items via url to setup
126
        $this->get(ShoppingCart_Controller::set_quantity_item_link($this->mp3player, array('quantity' => 5)));
127
        $this->get(ShoppingCart_Controller::add_item_link($this->socks));
128
129
        // remove items via url
130
        $response = $this->get("shoppingcart/clear?ajax=1");
131
        $this->assertEquals(200, $response->getStatusCode(), "Response status code is 200");
0 ignored issues
show
The method assertEquals() does not seem to exist on object<SilvershopJsonResponseTest>.

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...
132
        $this->assertEquals("application/json; charset=utf-8", $response->getHeader("Content-Type"), "Json response header");
0 ignored issues
show
The method assertEquals() does not seem to exist on object<SilvershopJsonResponseTest>.

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...
133
        $this->assertJson($response->getBody(), "Contains json in the body of the response");
0 ignored issues
show
The method assertJson() does not seem to exist on object<SilvershopJsonResponseTest>.

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...
134
135
        $this->assertContains('Cart was successfully cleared', $response->getBody(), "Contains a message that the cart has been cleared");
136
    }
137
138
    public function testVariations()
139
    {
140
        $this->loadFixture('silvershop/tests/fixtures/variations.yml');
141
        $ballRoot = $this->objFromFixture('Product', 'ball');
142
        $ballRoot->publish('Stage', 'Live');
143
        $ball1 = $this->objFromFixture('ProductVariation', 'redlarge');
144
        $ball2 = $this->objFromFixture('ProductVariation', 'redsmall');
145
146
        // Add the two variation items
147
        $response = $this->get(ShoppingCart_Controller::add_item_link($ball1) . "?ajax=1");
148
        $this->assertEquals(200, $response->getStatusCode(), "Response status code is 200");
0 ignored issues
show
The method assertEquals() does not seem to exist on object<SilvershopJsonResponseTest>.

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...
149
        $this->assertEquals("application/json; charset=utf-8", $response->getHeader("Content-Type"), "Json response header");
150
        $this->assertJson($response->getBody(), "Contains json in the body of the response");
0 ignored issues
show
The method assertJson() does not seem to exist on object<SilvershopJsonResponseTest>.

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...
151
        $this->assertContains("Size:Large, Color:Red", $response->getBody(), "Contains json in the body of the cart id");
152
153
        $response = $this->get(ShoppingCart_Controller::add_item_link($ball2) . "?ajax=1");
154
        $items = ShoppingCart::curr()->Items();
155
        $this->assertNotNull($items);
0 ignored issues
show
The method assertNotNull() does not seem to exist on object<SilvershopJsonResponseTest>.

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...
156
        $this->assertEquals($items->Count(), 2, 'There are 2 items in the cart');
0 ignored issues
show
The method assertEquals() does not seem to exist on object<SilvershopJsonResponseTest>.

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...
157
158
        // Remove one and see what happens
159
        $response = $this->get(ShoppingCart_Controller::remove_all_item_link($ball1) . "?ajax=1");
160
        $this->assertEquals($items->Count(), 1, 'There is 1 item in the cart');
0 ignored issues
show
The method assertEquals() does not seem to exist on object<SilvershopJsonResponseTest>.

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...
161
        $this->assertFalse($this->cart->get($ball1), "first item not in cart");
0 ignored issues
show
The method assertFalse() does not seem to exist on object<SilvershopJsonResponseTest>.

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...
162
        $this->assertNotNull($this->cart->get($ball1), "second item is in cart");
0 ignored issues
show
The method assertNotNull() does not seem to exist on object<SilvershopJsonResponseTest>.

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...
163
    }
164
165
    public function testGet()
166
    {
167
        // add items via url to setup
168
        $this->get(ShoppingCart_Controller::set_quantity_item_link($this->mp3player, array('quantity' => 5)));
169
        $this->get(ShoppingCart_Controller::add_item_link($this->socks));
170
        ShoppingCart::curr()->calculate();  // recalculate the shopping cart
171
172
        $response = $this->get("shoppingcart/get" . "?ajax=1");
173
174
        $this->assertContains('tableTitle', $response->getBody(), "Json contains tableTitle");
175
        $this->assertContains('tableValue', $response->getBody(), "Json contains tableValue");
176
        $this->assertContains('Tax : GST 15%', $response->getBody(), "Contains GST modifier in the response");
177
        $this->assertContains('"subTotal":1008', $response->getBody(), "Contains SubTotal of 1008");
178
        $this->assertContains('"grandTotal":1159.2', $response->getBody(), "Contains a GrandTotal of 1159.2; the GST amount (the Flat Tax Modifier) is the difference");
179
    }
180
}
181