Completed
Branch master (886cf7)
by Jan
07:19
created

OrderingProcessTest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 227
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 10
dl 0
loc 227
rs 10
c 0
b 0
f 0
ccs 0
cts 118
cp 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 8 1
A testOrderingProcess() 0 33 1
A createProduct() 0 27 1
A addProductToCart() 0 16 1
A changeProductQuantity() 0 13 1
A removeProductFromCart() 0 12 1
A createCustomer() 0 41 1
A loginUser() 0 14 1
A changePaymentMethod() 0 12 1
A payOrder() 0 13 1
A getOrderIdByResponse() 0 10 1
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Storefront\Test;
4
5
use Doctrine\DBAL\Connection;
6
use Shopware\Core\Checkout\Customer\CustomerDefinition;
7
use Shopware\Core\Defaults;
8
use Shopware\Core\Framework\Context;
9
use Shopware\Core\Framework\ORM\Read\ReadCriteria;
10
use Shopware\Core\Framework\ORM\RepositoryInterface;
11
use Shopware\Core\Framework\ORM\Write\EntityWriter;
12
use Shopware\Core\Framework\ORM\Write\EntityWriterInterface;
13
use Shopware\Core\Framework\ORM\Write\WriteContext;
14
use Shopware\Core\Framework\Struct\Uuid;
15
use Shopware\Core\Framework\Test\Api\ApiTestCase;
16
use Shopware\Core\PlatformRequest;
17
use Symfony\Component\HttpFoundation\Response;
18
19
class OrderingProcessTest extends ApiTestCase
20
{
21
    /**
22
     * @var Connection
23
     */
24
    private $connection;
25
26
    /**
27
     * @var RepositoryInterface
28
     */
29
    private $orderRepository;
30
31
    /**
32
     * @var EntityWriterInterface
33
     */
34
    private $entityWriter;
35
36
    public function setUp()
37
    {
38
        parent::setUp();
39
40
        $this->connection = $this->getContainer()->get(Connection::class);
41
        $this->orderRepository = $this->getContainer()->get('order.repository');
42
        $this->entityWriter = $this->getContainer()->get(EntityWriter::class);
43
    }
44
45
    public function testOrderingProcess(): void
46
    {
47
        $this->markTestSkipped('Storefront not fully implemented yet.');
48
49
        $email = Uuid::uuid4()->toString() . '@shopware.com';
50
        $customerId = $this->createCustomer($email, 'test1234');
51
        $this->assertNotEmpty($customerId, 'Customer was not created.');
52
53
        $this->loginUser($email, 'test1234');
54
55
        $product1 = $this->createProduct('Shopware stickers', 10, 11.9, 19);
56
        $product2 = $this->createProduct('Shopware t-shirt', 20, 23.8, 19);
57
        $product3 = $this->createProduct('Shopware cup', 5, 5.95, 19);
58
59
        $this->addProductToCart($product1, 1);
60
        $this->addProductToCart($product2, 5);
61
        $this->addProductToCart($product3, 10);
62
63
        $this->changeProductQuantity($product3, 3);
64
65
        $this->removeProductFromCart($product2);
66
67
        $this->changePaymentMethod(Defaults::PAYMENT_METHOD_PAID_IN_ADVANCE);
68
69
        $orderId = $this->payOrder();
70
        self::assertTrue(Uuid::isValid($orderId));
71
72
        $order = $this->orderRepository->read(new ReadCriteria([$orderId]), Context::createDefaultContext(Defaults::TENANT_ID))->get($orderId);
73
74
        self::assertEquals(Defaults::PAYMENT_METHOD_PAID_IN_ADVANCE, $order->getPaymentMethodId());
75
        self::assertEquals(25, $order->getAmountTotal());
76
        self::assertEquals($customerId, $order->getCustomer()->getId());
77
    }
78
79
    private function createProduct(
80
        string $name,
81
        float $grossPrice,
82
        float $netPrice,
83
        float $taxRate
84
    ): string {
85
        $id = Uuid::uuid4()->getHex();
86
87
        $data = [
88
            'id' => $id,
89
            'name' => $name,
90
            'tax' => ['name' => 'test', 'rate' => $taxRate],
91
            'manufacturer' => ['name' => 'test'],
92
            'price' => ['gross' => $grossPrice, 'net' => $netPrice],
93
        ];
94
95
        $this->apiClient->request('POST', '/api/v' . PlatformRequest::API_VERSION . '/product', [], [], [], json_encode($data));
96
        $response = $this->apiClient->getResponse();
97
98
        /* @var Response $response */
99
        self::assertSame(Response::HTTP_NO_CONTENT, $response->getStatusCode(), $response->getContent());
100
101
        self::assertNotEmpty($response->headers->get('Location'));
102
        self::assertStringEndsWith('api/v' . PlatformRequest::API_VERSION . '/product/' . $id, $response->headers->get('Location'));
103
104
        return $id;
105
    }
106
107
    private function addProductToCart(string $id, int $quantity)
108
    {
109
        $data = [
110
            'identifier' => $id,
111
            'quantity' => $quantity,
112
        ];
113
114
        $this->storefrontApiClient->request('POST', '/cart/addProduct', $data);
115
        $response = $this->storefrontApiClient->getResponse();
116
117
        $this->assertEquals(200, $response->getStatusCode(), print_r($response->getContent(), true));
118
119
        $content = json_decode($response->getContent(), true);
120
121
        self::assertEquals(true, $content['success']);
122
    }
123
124
    private function changeProductQuantity(string $id, int $quantity)
125
    {
126
        $data = [
127
            'identifier' => $id,
128
            'quantity' => $quantity,
129
        ];
130
131
        $this->storefrontApiClient->request('POST', '/cart/setLineItemQuantity', $data);
132
        $response = $this->storefrontApiClient->getResponse();
133
        $content = json_decode($response->getContent(), true);
134
135
        self::assertEquals(true, $content['success']);
136
    }
137
138
    private function removeProductFromCart(string $id)
139
    {
140
        $data = [
141
            'identifier' => $id,
142
        ];
143
144
        $this->storefrontApiClient->request('POST', '/cart/removeLineItem', $data);
145
        $response = $this->storefrontApiClient->getResponse();
146
        $content = json_decode($response->getContent(), true);
147
148
        self::assertEquals(true, $content['success']);
149
    }
150
151
    private function createCustomer($email, $password): string
152
    {
153
        $customerId = Uuid::uuid4()->getHex();
154
        $addressId = Uuid::uuid4()->getHex();
155
156
        $customer = [
157
            'id' => $customerId,
158
            'number' => '1337',
159
            'salutation' => 'Herr',
160
            'firstName' => 'Max',
161
            'lastName' => 'Mustermann',
162
            'email' => $email,
163
            'password' => $password,
164
            'defaultPaymentMethodId' => Defaults::PAYMENT_METHOD_INVOICE,
165
            'groupId' => Defaults::FALLBACK_CUSTOMER_GROUP,
166
            'touchpointId' => Defaults::TOUCHPOINT,
167
            'defaultBillingAddressId' => $addressId,
168
            'defaultShippingAddressId' => $addressId,
169
            'addresses' => [
170
                [
171
                    'id' => $addressId,
172
                    'customerId' => $customerId,
173
                    'countryId' => 'ffe61e1c-9915-4f95-9701-4a310ab5482d',
174
                    'salutation' => 'Herr',
175
                    'firstName' => 'Max',
176
                    'lastName' => 'Mustermann',
177
                    'street' => 'Ebbinghoff 10',
178
                    'zipcode' => '48624',
179
                    'city' => 'Schöppingen',
180
                ],
181
            ],
182
        ];
183
184
        $this->entityWriter->upsert(
185
            CustomerDefinition::class,
186
            [$customer],
187
            WriteContext::createFromContext(Context::createDefaultContext(Defaults::TENANT_ID))
188
        );
189
190
        return $customerId;
191
    }
192
193
    private function loginUser(string $email, string $password)
194
    {
195
        $data = [
196
            'email' => $email,
197
            'password' => $password,
198
        ];
199
200
        $this->storefrontApiClient->request('POST', '/account/login', $data);
201
202
        /** @var Response $response */
203
        $response = $this->storefrontApiClient->getResponse();
204
205
        $this->assertStringEndsWith('/account', (string) $response->headers->get('Location'), $response->getContent());
206
    }
207
208
    private function changePaymentMethod(string $paymentMethodId)
209
    {
210
        $data = [
211
            'paymentMethodId' => $paymentMethodId,
212
        ];
213
214
        $this->storefrontApiClient->request('POST', '/checkout/saveShippingPayment', $data);
215
216
        /** @var Response $response */
217
        $response = $this->storefrontApiClient->getResponse();
218
        $this->assertStringEndsWith('/checkout/confirm', $response->headers->get('Location'));
219
    }
220
221
    private function payOrder(): string
222
    {
223
        $data = [
224
            'sAGB' => 'on',
225
        ];
226
227
        $this->storefrontApiClient->request('POST', '/checkout/pay', $data);
228
229
        /** @var Response $response */
230
        $response = $this->storefrontApiClient->getResponse();
231
232
        return $this->getOrderIdByResponse($response);
233
    }
234
235
    private function getOrderIdByResponse(Response $response): string
236
    {
237
        $this->assertTrue($response->headers->has('location'), print_r($response->getContent(), true));
238
        $location = $response->headers->get('location');
239
        $query = parse_url($location, PHP_URL_QUERY);
240
        $parsedQuery = [];
241
        parse_str($query, $parsedQuery);
242
243
        return $parsedQuery['order'];
244
    }
245
}
246