Skip to content

Commit f0dedde

Browse files
author
Valeriy Nayda
committed
GraphQl-41: [Query] My Account > My Orders
1 parent 0b22554 commit f0dedde

File tree

4 files changed

+188
-194
lines changed

4 files changed

+188
-194
lines changed

app/code/Magento/SalesGraphQl/Model/Resolver/Orders.php

Lines changed: 47 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -7,70 +7,66 @@
77

88
namespace Magento\SalesGraphQl\Model\Resolver;
99

10-
use Magento\Authorization\Model\UserContextInterface;
1110
use Magento\Framework\GraphQl\Config\Element\Field;
1211
use Magento\Framework\GraphQl\Query\ResolverInterface;
1312
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1413
use Magento\Sales\Model\ResourceModel\Order\CollectionFactoryInterface;
15-
use Magento\CustomerGraphQl\Model\Customer\CheckCustomerAccountInterface;
14+
use Magento\CustomerGraphQl\Model\Customer\CheckCustomerAccount;
1615

1716
/**
18-
* Class Orders
17+
* Orders data reslover
1918
*/
2019
class Orders implements ResolverInterface
2120
{
22-
/**
23-
* @var CollectionFactoryInterface
24-
*/
25-
private $collectionFactory;
21+
/**
22+
* @var CollectionFactoryInterface
23+
*/
24+
private $collectionFactory;
2625

27-
/**
28-
* @var CheckCustomerAccountInterface
29-
*/
30-
private $checkCustomerAccount;
26+
/**
27+
* @var CheckCustomerAccount
28+
*/
29+
private $checkCustomerAccount;
3130

32-
/**
33-
* Orders constructor.
34-
* @param CollectionFactoryInterface $collectionFactory
35-
* @param CheckCustomerAccountInterface $checkCustomerAccount
36-
*/
37-
public function __construct(
38-
CollectionFactoryInterface $collectionFactory,
39-
CheckCustomerAccountInterface $checkCustomerAccount
40-
) {
41-
$this->collectionFactory = $collectionFactory;
42-
$this->checkCustomerAccount = $checkCustomerAccount;
31+
/**
32+
* @param CollectionFactoryInterface $collectionFactory
33+
* @param CheckCustomerAccount $checkCustomerAccount
34+
*/
35+
public function __construct(
36+
CollectionFactoryInterface $collectionFactory,
37+
CheckCustomerAccount $checkCustomerAccount
38+
) {
39+
$this->collectionFactory = $collectionFactory;
40+
$this->checkCustomerAccount = $checkCustomerAccount;
4341

44-
}
42+
}
4543

46-
/**
47-
* {@inheritdoc}
48-
*/
49-
public function resolve(
50-
Field $field,
51-
$context,
52-
ResolveInfo $info,
53-
array $value = null,
54-
array $args = null
55-
) {
56-
$customerId = $context->getUserId();
44+
/**
45+
* @inheritdoc
46+
*/
47+
public function resolve(
48+
Field $field,
49+
$context,
50+
ResolveInfo $info,
51+
array $value = null,
52+
array $args = null
53+
) {
54+
$customerId = $context->getUserId();
55+
$this->checkCustomerAccount->execute($customerId, $context->getUserType());
5756

58-
$this->checkCustomerAccount->execute($customerId, $context->getUserType());
57+
$items = [];
58+
$orders = $this->collectionFactory->create($customerId);
5959

60-
$items = [];
61-
$orders = $this->collectionFactory->create($customerId);
62-
63-
/** @var \Magento\Sales\Model\Order $order */
64-
foreach ($orders as $order) {
65-
$items[] = [
66-
'id' => $order->getId(),
67-
'increment_id' => $order->getIncrementId(),
68-
'created_at' => $order->getCreatedAt(),
69-
'grand_total' => $order->getGrandTotal(),
70-
'status' => $order->getStatus()
71-
];
72-
}
73-
74-
return ['items' => $items];
75-
}
60+
/** @var \Magento\Sales\Model\Order $order */
61+
foreach ($orders as $order) {
62+
$items[] = [
63+
'id' => $order->getId(),
64+
'increment_id' => $order->getIncrementId(),
65+
'created_at' => $order->getCreatedAt(),
66+
'grand_total' => $order->getGrandTotal(),
67+
'status' => $order->getStatus(),
68+
];
69+
}
70+
return ['items' => $items];
71+
}
7672
}

app/code/Magento/SalesGraphQl/composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
"require": {
66
"php": "~7.1.3||~7.2.0",
77
"magento/framework": "*",
8+
"magento/module-authorization": "*",
89
"magento/module-customer": "*",
10+
"magento/module-customer-graph-ql": "*",
911
"magento/module-catalog": "*",
12+
"magento/module-sales": "*",
1013
"magento/module-store": "*"
1114
},
1215
"suggest": {

dev/tests/api-functional/testsuite/Magento/GraphQl/Sales/OrdersTest.php

Lines changed: 64 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -16,93 +16,88 @@
1616
*/
1717
class OrdersTest extends GraphQlAbstract
1818
{
19-
/**
20-
* @var CustomerTokenServiceInterface
21-
*/
22-
private $customerTokenService;
19+
/**
20+
* @var CustomerTokenServiceInterface
21+
*/
22+
private $customerTokenService;
2323

24-
/**
25-
* {@inheritdoc}
26-
*/
2724
protected function setUp()
2825
{
2926
parent::setUp();
30-
$objectManager = Bootstrap::getObjectManager();
31-
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
27+
$this->customerTokenService = Bootstrap::getObjectManager()->get(CustomerTokenServiceInterface::class);
3228
}
3329

34-
/**
35-
* @magentoApiDataFixture Magento/Customer/_files/customer.php
36-
* @magentoApiDataFixture Magento/Sales/_files/orders_with_customer.php
37-
*/
30+
/**
31+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
32+
* @magentoApiDataFixture Magento/Sales/_files/orders_with_customer.php
33+
*/
3834
public function testOrdersQuery()
3935
{
40-
$query =
41-
<<<QUERY
36+
$query =
37+
<<<QUERY
4238
query {
4339
customerOrders {
4440
items {
45-
id
46-
increment_id
47-
created_at
48-
grand_total
49-
status
50-
}
41+
id
42+
increment_id
43+
created_at
44+
grand_total
45+
status
46+
}
5147
}
5248
}
5349
QUERY;
5450

55-
$currentEmail = 'customer@example.com';
56-
$currentPassword = 'password';
57-
58-
$response = $this->graphQlQuery($query, [], '', $this->getCustomerAuthHeaders($currentEmail, $currentPassword));
51+
$currentEmail = 'customer@example.com';
52+
$currentPassword = 'password';
53+
$response = $this->graphQlQuery($query, [], '', $this->getCustomerAuthHeaders($currentEmail, $currentPassword));
5954

60-
$expectedData = [
61-
[
62-
'increment_id' => '100000002',
63-
'status' => 'processing',
64-
'grand_total' => 120.00
65-
],
66-
[
67-
'increment_id' => '100000003',
68-
'status' => 'processing',
69-
'grand_total' => 130.00
70-
],
71-
[
72-
'increment_id' => '100000004',
73-
'status' => 'closed',
74-
'grand_total' => 140.00
75-
],
76-
[
77-
'increment_id' => '100000005',
78-
'status' => 'complete',
79-
'grand_total' => 150.00
80-
],
81-
[
82-
'increment_id' => '100000006',
83-
'status' => 'complete',
84-
'grand_total' => 160.00
85-
]
86-
];
55+
$expectedData = [
56+
[
57+
'increment_id' => '100000002',
58+
'status' => 'processing',
59+
'grand_total' => 120.00
60+
],
61+
[
62+
'increment_id' => '100000003',
63+
'status' => 'processing',
64+
'grand_total' => 130.00
65+
],
66+
[
67+
'increment_id' => '100000004',
68+
'status' => 'closed',
69+
'grand_total' => 140.00
70+
],
71+
[
72+
'increment_id' => '100000005',
73+
'status' => 'complete',
74+
'grand_total' => 150.00
75+
],
76+
[
77+
'increment_id' => '100000006',
78+
'status' => 'complete',
79+
'grand_total' => 160.00
80+
]
81+
];
8782

88-
$actualData = $response['customerOrders']['items'];
83+
$actualData = $response['customerOrders']['items'];
8984

90-
foreach ($expectedData as $key => $data) {
91-
$this->assertEquals($data['increment_id'], $actualData[$key]['increment_id']);
92-
$this->assertEquals($data['grand_total'], $actualData[$key]['grand_total']);
93-
$this->assertEquals($data['status'], $actualData[$key]['status']);
94-
}
85+
foreach ($expectedData as $key => $data) {
86+
$this->assertEquals($data['increment_id'], $actualData[$key]['increment_id']);
87+
$this->assertEquals($data['grand_total'], $actualData[$key]['grand_total']);
88+
$this->assertEquals($data['status'], $actualData[$key]['status']);
89+
}
9590
}
9691

97-
/**
98-
* @param string $email
99-
* @param string $password
100-
* @return array
101-
* @throws \Magento\Framework\Exception\AuthenticationException
102-
*/
103-
private function getCustomerAuthHeaders(string $email, string $password): array
104-
{
105-
$customerToken = $this->customerTokenService->createCustomerAccessToken($email, $password);
106-
return ['Authorization' => 'Bearer ' . $customerToken];
107-
}
92+
/**
93+
* @param string $email
94+
* @param string $password
95+
* @return array
96+
* @throws \Magento\Framework\Exception\AuthenticationException
97+
*/
98+
private function getCustomerAuthHeaders(string $email, string $password): array
99+
{
100+
$customerToken = $this->customerTokenService->createCustomerAccessToken($email, $password);
101+
return ['Authorization' => 'Bearer ' . $customerToken];
102+
}
108103
}

0 commit comments

Comments
 (0)