Completed
Push — 1.9 ( 5c3e2e...1529fd )
by
unknown
60:20
created

CartControllerTest::getCustomer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace OroCRM\Bundle\MagentoBundle\Tests\Functional\Controller\Api\Rest;
4
5
use Doctrine\ORM\EntityManager;
6
7
use Oro\Bundle\TestFrameworkBundle\Test\WebTestCase;
8
use Oro\Bundle\UserBundle\Entity\User;
9
10
use OroCRM\Bundle\ChannelBundle\Entity\Channel;
11
use OroCRM\Bundle\MagentoBundle\Entity\Cart;
12
use OroCRM\Bundle\MagentoBundle\Entity\Customer;
13
14
/**
15
 * @outputBuffering enabled
16
 * @dbIsolation
17
 */
18
class CartControllerTest extends WebTestCase
19
{
20
    public function setUp()
21
    {
22
        $this->initClient(['debug' => false], $this->generateWsseAuthHeader());
23
24
        $this->loadFixtures(
25
            [
26
                'OroCRM\Bundle\MagentoBundle\Tests\Functional\Fixture\LoadMagentoChannel'
27
            ]
28
        );
29
    }
30
31
    public function testCget()
32
    {
33
        $this->client->request('GET', $this->getUrl('oro_api_get_carts'));
34
        $orders = $this->getJsonResponseContent($this->client->getResponse(), 200);
35
        $this->assertGreaterThan(0, count($orders));
36
    }
37
38
    public function testPost()
39
    {
40
        $user = $this->getUser();
41
42
        $request = [
43
            'subTotal'          => 100,
44
            'grandTotal'        => 100,
45
            'taxAmount'         => 10,
46
            'cartItems'         => [
47
                [
48
                    'sku'            => 'some sku',
49
                    'name'           => 'some name',
50
                    'qty'            => 10,
51
                    'price'          => 100,
52
                    'discountAmount' => 10,
53
                    'taxPercent'     => 5,
54
                    'weight'         => 1,
55
                    'productId'      => 100500,
56
                    'parentItemId'   => 100499,
57
                    'freeShipping'   => 'nope',
58
                    'giftMessage'    => 'some gift',
59
                    'taxClassId'     => 'some tax',
60
                    'description'    => '',
61
                    'isVirtual'      => true,
62
                    'customPrice'    => 100,
63
                    'priceInclTax'   => 100,
64
                    'rowTotal'       => 100,
65
                    'taxAmount'      => 10,
66
                    'productType'    => 'some type'
67
                ]
68
            ],
69
            'customer'          => $this->getCustomer()->getId(),
70
            'store'             => $this->getStore()->getId(),
71
            'itemsQty'          => 100,
72
            'baseCurrencyCode'  => 'some text',
73
            'storeCurrencyCode' => 'some text 2',
74
            'quoteCurrencyCode' => 'some text 3',
75
            'storeToBaseRate'   => 10,
76
            'storeToQuoteRate'  => 10,
77
            'email'             => '[email protected]',
78
            'giftMessage'       => 'some message',
79
            'isGuest'           => true,
80
            'shippingAddress'   => [
81
                'label'        => 'new1',
82
                'street'       => 'street',
83
                'city'         => 'new city',
84
                'postalCode'   => '10000',
85
                'country'      => 'US',
86
                'region'       => 'US-AL',
87
                'firstName'    => 'first',
88
                'lastName'     => 'last',
89
                'nameSuffix'   => 'suffix',
90
                'phone'        => '123123123'
91
            ],
92
            'billingAddress'    => [
93
                'label'        => 'new2',
94
                'street'       => 'street',
95
                'city'         => 'new city',
96
                'postalCode'   => '10000',
97
                'country'      => 'US',
98
                'region'       => 'US-AL',
99
                'firstName'    => 'first',
100
                'lastName'     => 'last',
101
                'nameSuffix'   => 'suffix',
102
                'phone'        => '123123123'
103
            ],
104
            'paymentDetails'    => 'some details',
105
            'status'            => 'open',
106
            'notes'             => 'some text 4',
107
            'statusMessage'     => 'some text 5',
108
            'owner'             => $user->getId(),
109
            'dataChannel'       => $this->getChannel()->getId(),
110
            'channel'           => $this->getChannel()->getDataSource()->getId()
111
        ];
112
113
        $this->client->request('POST', $this->getUrl('oro_api_post_cart'), $request);
114
115
        $result = $this->getJsonResponseContent($this->client->getResponse(), 201);
116
117
        $this->assertArrayHasKey('id', $result);
118
        $this->assertNotEmpty($result['id']);
119
120
        $this->client->request('GET', $this->getUrl('oro_api_get_cart', ['id' => $result['id']]));
121
122
        /** @var Cart $cart */
123
        $cart = $this->getJsonResponseContent($this->client->getResponse(), 200);
124
125
        $this->assertCount(1, $cart['cartItems']);
126
        $this->assertNotEmpty($cart['billingAddress']);
127
        $this->assertInternalType('array', $cart['billingAddress']);
128
        $this->assertNotEmpty($cart['shippingAddress']);
129
        $this->assertInternalType('array', $cart['shippingAddress']);
130
        $this->assertEquals(1, $cart['itemsCount']);
131
132
        return $cart;
133
    }
134
135
    /**
136
     * @param array $cart
137
     *
138
     * @return int $id
139
     *
140
     * @depends testPost
141
     */
142
    public function testPut($cart)
143
    {
144
        $cart['giftMessage']     .= '_Updated';
145
        $cart['baseCurrencyCode'] .= '_Updated';
146
147
        $id = $cart['id'];
148
        unset(
149
            $cart['id'],
150
            $cart['createdAt'],
151
            $cart['updatedAt'],
152
            $cart['importedAt'],
153
            $cart['syncedAt'],
154
            $cart['originId'],
155
            $cart['billingAddress'],
156
            $cart['shippingAddress'],
157
            $cart['cartItems'],
158
            $cart['itemsCount'],
159
            $cart['firstName'],
160
            $cart['lastName'],
161
            $cart['opportunity'],
162
            $cart['organization']
163
        );
164
        $this->client->request(
165
            'PUT',
166
            $this->getUrl('oro_api_put_cart', ['id' => $id]),
167
            $cart
168
        );
169
170
        $result = $this->client->getResponse();
171
        $this->assertEmptyResponseStatusCodeEquals($result, 204);
172
173
        $this->client->request('GET', $this->getUrl('oro_api_get_cart', ['id' => $id]));
174
175
        $result = $this->getJsonResponseContent($this->client->getResponse(), 200);
176
177
        $this->assertCount(1, $result['cartItems']);
178
        $this->assertNotEmpty($result['billingAddress']);
179
        $this->assertInternalType('array', $result['billingAddress']);
180
        $this->assertNotEmpty($result['shippingAddress']);
181
        $this->assertInternalType('array', $result['shippingAddress']);
182
        $this->assertEquals(1, $result['itemsCount']);
183
184
        $this->assertEquals($cart['giftMessage'], $result['giftMessage'], 'Customer was not updated');
185
        $this->assertEquals($cart['baseCurrencyCode'], $result['baseCurrencyCode'], 'Customer was not updated');
186
187
        return $id;
188
    }
189
190
    /**
191
     * @param int $id
192
     *
193
     * @depends testPut
194
     */
195
    public function testDelete($id)
196
    {
197
        $this->client->request(
198
            'DELETE',
199
            $this->getUrl('oro_api_delete_cart', ['id' => $id])
200
        );
201
202
        $result = $this->client->getResponse();
203
204
        $this->assertEmptyResponseStatusCodeEquals($result, 204);
205
206
        $this->client->request('GET', $this->getUrl('oro_api_get_cart', ['id' => $id]));
207
208
        $this->getJsonResponseContent($this->client->getResponse(), 404);
209
    }
210
211
    /**
212
     * @return EntityManager
213
     */
214
    protected function getEntityManager()
215
    {
216
        return $this->getContainer()->get('doctrine')->getManager();
217
    }
218
219
    /**
220
     * @return User
221
     */
222
    protected function getUser()
223
    {
224
        return $this->getEntityManager()->getRepository('OroUserBundle:User')->findOneByUsername(self::USER_NAME);
225
    }
226
227
    /**
228
     * Get loaded channel
229
     *
230
     * @return Channel
231
     */
232
    protected function getChannel()
233
    {
234
        return $this->getReference('default_channel');
235
    }
236
237
    /**
238
     * @return Customer
239
     */
240
    protected function getCustomer()
241
    {
242
        return $this->getReference('customer');
243
    }
244
245
    /**
246
     * return Store
247
     */
248
    protected function getStore()
249
    {
250
        return $this->getReference('store');
251
    }
252
}
253