WishlistTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Wishlist\Model;
  7. use Magento\Catalog\Api\ProductRepositoryInterface;
  8. use Magento\Catalog\Model\Product\Attribute\Source\Status as ProductStatus;
  9. use Magento\Framework\App\ObjectManager;
  10. use Magento\Framework\DataObject;
  11. class WishlistTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var ObjectManager
  15. */
  16. private $objectManager;
  17. /**
  18. * @var Wishlist
  19. */
  20. private $wishlist;
  21. /**
  22. * {@inheritDoc}
  23. */
  24. protected function setUp()
  25. {
  26. $this->objectManager = ObjectManager::getInstance();
  27. $this->wishlist = $this->objectManager->get(Wishlist::class);
  28. }
  29. /**
  30. * @magentoDataFixture Magento/Catalog/_files/product_simple.php
  31. * @magentoDataFixture Magento/Customer/_files/customer.php
  32. * @magentoAppIsolation enabled
  33. * @magentoDbIsolation enabled
  34. */
  35. public function testAddNewItem()
  36. {
  37. $productSku = 'simple';
  38. $customerId = 1;
  39. /** @var ProductRepositoryInterface $productRepository */
  40. $productRepository = $this->objectManager->create(ProductRepositoryInterface::class);
  41. $product = $productRepository->get($productSku);
  42. $this->wishlist->loadByCustomerId($customerId, true);
  43. $this->wishlist->addNewItem(
  44. $product,
  45. '{"qty":2}'
  46. );
  47. $this->wishlist->addNewItem(
  48. $product,
  49. ['qty' => 3]
  50. );
  51. $this->wishlist->addNewItem(
  52. $product,
  53. new DataObject(['qty' => 4])
  54. );
  55. $this->wishlist->addNewItem($product);
  56. /** @var Item $wishlistItem */
  57. $wishlistItem = $this->wishlist->getItemCollection()->getFirstItem();
  58. $this->assertInstanceOf(Item::class, $wishlistItem);
  59. $this->assertEquals($wishlistItem->getQty(), 10);
  60. }
  61. /**
  62. * @magentoDataFixture Magento/Catalog/_files/product_simple.php
  63. * @magentoDataFixture Magento/Customer/_files/customer.php
  64. * @magentoAppIsolation enabled
  65. * @magentoDbIsolation enabled
  66. * @expectedException \InvalidArgumentException
  67. * @expectedExceptionMessage Invalid wishlist item configuration.
  68. */
  69. public function testAddNewItemInvalidWishlistItemConfiguration()
  70. {
  71. $productSku = 'simple';
  72. $customerId = 1;
  73. /** @var ProductRepositoryInterface $productRepository */
  74. $productRepository = $this->objectManager->create(ProductRepositoryInterface::class);
  75. $product = $productRepository->get($productSku);
  76. $this->wishlist->loadByCustomerId($customerId, true);
  77. $this->wishlist->addNewItem(
  78. $product,
  79. '{"qty":2'
  80. );
  81. $this->wishlist->addNewItem($product);
  82. }
  83. /**
  84. * @magentoDbIsolation disabled
  85. * @magentoDataFixture Magento/Wishlist/_files/wishlist.php
  86. */
  87. public function testGetItemCollection()
  88. {
  89. $productSku = 'simple';
  90. $customerId = 1;
  91. $this->wishlist->loadByCustomerId($customerId, true);
  92. $itemCollection = $this->wishlist->getItemCollection();
  93. /** @var \Magento\Wishlist\Model\Item $item */
  94. $item = $itemCollection->getFirstItem();
  95. $this->assertEquals($productSku, $item->getProduct()->getSku());
  96. }
  97. /**
  98. * @magentoDbIsolation disabled
  99. * @magentoDataFixture Magento/Wishlist/_files/wishlist.php
  100. */
  101. public function testGetItemCollectionWithDisabledProduct()
  102. {
  103. $productSku = 'simple';
  104. $customerId = 1;
  105. $productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
  106. $product = $productRepository->get($productSku);
  107. $product->setStatus(ProductStatus::STATUS_DISABLED);
  108. $productRepository->save($product);
  109. $this->wishlist->loadByCustomerId($customerId, true);
  110. $itemCollection = $this->wishlist->getItemCollection();
  111. $this->assertEmpty($itemCollection->getItems());
  112. }
  113. }