OrdersTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\GraphQl\Sales;
  8. use Magento\Integration\Api\CustomerTokenServiceInterface;
  9. use Magento\TestFramework\TestCase\GraphQlAbstract;
  10. use Magento\TestFramework\Helper\Bootstrap;
  11. /**
  12. * Class OrdersTest
  13. */
  14. class OrdersTest extends GraphQlAbstract
  15. {
  16. /**
  17. * @var CustomerTokenServiceInterface
  18. */
  19. private $customerTokenService;
  20. protected function setUp()
  21. {
  22. parent::setUp();
  23. $this->customerTokenService = Bootstrap::getObjectManager()->get(CustomerTokenServiceInterface::class);
  24. }
  25. /**
  26. * @magentoApiDataFixture Magento/Customer/_files/customer.php
  27. * @magentoApiDataFixture Magento/Sales/_files/orders_with_customer.php
  28. */
  29. public function testOrdersQuery()
  30. {
  31. $query =
  32. <<<QUERY
  33. query {
  34. customerOrders {
  35. items {
  36. id
  37. increment_id
  38. created_at
  39. grand_total
  40. status
  41. }
  42. }
  43. }
  44. QUERY;
  45. $currentEmail = 'customer@example.com';
  46. $currentPassword = 'password';
  47. $response = $this->graphQlQuery($query, [], '', $this->getCustomerAuthHeaders($currentEmail, $currentPassword));
  48. $expectedData = [
  49. [
  50. 'increment_id' => '100000002',
  51. 'status' => 'processing',
  52. 'grand_total' => 120.00
  53. ],
  54. [
  55. 'increment_id' => '100000003',
  56. 'status' => 'processing',
  57. 'grand_total' => 130.00
  58. ],
  59. [
  60. 'increment_id' => '100000004',
  61. 'status' => 'closed',
  62. 'grand_total' => 140.00
  63. ],
  64. [
  65. 'increment_id' => '100000005',
  66. 'status' => 'complete',
  67. 'grand_total' => 150.00
  68. ],
  69. [
  70. 'increment_id' => '100000006',
  71. 'status' => 'complete',
  72. 'grand_total' => 160.00
  73. ]
  74. ];
  75. $actualData = $response['customerOrders']['items'];
  76. foreach ($expectedData as $key => $data) {
  77. $this->assertEquals(
  78. $data['increment_id'],
  79. $actualData[$key]['increment_id'],
  80. "increment_id is different than the expected for order - " . $data['increment_id']
  81. );
  82. $this->assertEquals(
  83. $data['grand_total'],
  84. $actualData[$key]['grand_total'],
  85. "grand_total is different than the expected for order - " . $data['increment_id']
  86. );
  87. $this->assertEquals(
  88. $data['status'],
  89. $actualData[$key]['status'],
  90. "status is different than the expected for order - " . $data['increment_id']
  91. );
  92. }
  93. }
  94. /**
  95. * @param string $email
  96. * @param string $password
  97. * @return array
  98. * @throws \Magento\Framework\Exception\AuthenticationException
  99. */
  100. private function getCustomerAuthHeaders(string $email, string $password): array
  101. {
  102. $customerToken = $this->customerTokenService->createCustomerAccessToken($email, $password);
  103. return ['Authorization' => 'Bearer ' . $customerToken];
  104. }
  105. }