WishlistTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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\Wishlist;
  8. use Magento\Integration\Api\CustomerTokenServiceInterface;
  9. use Magento\TestFramework\Helper\Bootstrap;
  10. use Magento\TestFramework\TestCase\GraphQlAbstract;
  11. use Magento\Wishlist\Model\Item;
  12. use Magento\Wishlist\Model\ResourceModel\Wishlist as WishlistResourceModel;
  13. use Magento\Wishlist\Model\WishlistFactory;
  14. class WishlistTest extends GraphQlAbstract
  15. {
  16. /**
  17. * @var CustomerTokenServiceInterface
  18. */
  19. private $customerTokenService;
  20. /**
  21. * @var WishlistFactory
  22. */
  23. private $wishlistFactory;
  24. /**
  25. * @var WishlistResourceModel
  26. */
  27. private $wishlistResource;
  28. protected function setUp()
  29. {
  30. $this->customerTokenService = Bootstrap::getObjectManager()->get(CustomerTokenServiceInterface::class);
  31. $this->wishlistFactory = Bootstrap::getObjectManager()->get(WishlistFactory::class);
  32. $this->wishlistResource = Bootstrap::getObjectManager()->get(WishlistResourceModel::class);
  33. }
  34. /**
  35. * @magentoApiDataFixture Magento/Wishlist/_files/wishlist.php
  36. */
  37. public function testGetCustomerWishlist(): void
  38. {
  39. /** @var \Magento\Wishlist\Model\Wishlist $wishlist */
  40. $wishlist = $this->wishlistFactory->create();
  41. $this->wishlistResource->load($wishlist, 1, 'customer_id');
  42. /** @var Item $wishlistItem */
  43. $wishlistItem = $wishlist->getItemCollection()->getFirstItem();
  44. $wishlistItemProduct = $wishlistItem->getProduct();
  45. $query =
  46. <<<QUERY
  47. {
  48. wishlist {
  49. items_count
  50. name
  51. sharing_code
  52. updated_at
  53. items {
  54. id
  55. qty
  56. description
  57. added_at
  58. product {
  59. sku
  60. name
  61. }
  62. }
  63. }
  64. }
  65. QUERY;
  66. $response = $this->graphQlQuery(
  67. $query,
  68. [],
  69. '',
  70. $this->getCustomerAuthHeaders('customer@example.com', 'password')
  71. );
  72. $this->assertEquals($wishlist->getItemsCount(), $response['wishlist']['items_count']);
  73. $this->assertEquals($wishlist->getName(), $response['wishlist']['name']);
  74. $this->assertEquals($wishlist->getSharingCode(), $response['wishlist']['sharing_code']);
  75. $this->assertEquals($wishlist->getUpdatedAt(), $response['wishlist']['updated_at']);
  76. $this->assertEquals($wishlistItem->getId(), $response['wishlist']['items'][0]['id']);
  77. $this->assertEquals($wishlistItem->getData('qty'), $response['wishlist']['items'][0]['qty']);
  78. $this->assertEquals($wishlistItem->getDescription(), $response['wishlist']['items'][0]['description']);
  79. $this->assertEquals($wishlistItem->getAddedAt(), $response['wishlist']['items'][0]['added_at']);
  80. $this->assertEquals($wishlistItemProduct->getSku(), $response['wishlist']['items'][0]['product']['sku']);
  81. $this->assertEquals($wishlistItemProduct->getName(), $response['wishlist']['items'][0]['product']['name']);
  82. }
  83. /**
  84. * @param string $email
  85. * @param string $password
  86. * @return array
  87. * @throws \Magento\Framework\Exception\AuthenticationException
  88. */
  89. private function getCustomerAuthHeaders(string $email, string $password): array
  90. {
  91. $customerToken = $this->customerTokenService->createCustomerAccessToken($email, $password);
  92. return ['Authorization' => 'Bearer ' . $customerToken];
  93. }
  94. }