ItemTest.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Wishlist\Model;
  7. /**
  8. * Item test class.
  9. */
  10. class ItemTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Magento\Framework\App\ObjectManager
  14. */
  15. private $objectManager;
  16. /**
  17. * @var \Magento\Wishlist\Model\Item
  18. */
  19. private $model;
  20. /**
  21. * {@inheritDoc}
  22. */
  23. public function setUp()
  24. {
  25. $this->objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  26. $this->model = $this->objectManager->get(\Magento\Wishlist\Model\Item::class);
  27. }
  28. /**
  29. * @magentoDataFixture Magento/Catalog/_files/product_simple.php
  30. * @magentoAppIsolation enabled
  31. * @magentoDbIsolation enabled
  32. */
  33. public function testBuyRequest()
  34. {
  35. /** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */
  36. $productRepository = $this->objectManager->get(\Magento\Catalog\Api\ProductRepositoryInterface::class);
  37. $product = $productRepository->getById(1);
  38. /** @var \Magento\Wishlist\Model\Item\Option $option */
  39. $option = $this->objectManager->create(
  40. \Magento\Wishlist\Model\Item\Option::class,
  41. ['data' => ['code' => 'info_buyRequest', 'value' => '{"qty":23}']]
  42. );
  43. $option->setProduct($product);
  44. $this->model->addOption($option);
  45. // Assert getBuyRequest method
  46. $buyRequest = $this->model->getBuyRequest();
  47. $this->assertEquals($buyRequest->getOriginalQty(), 23);
  48. // Assert mergeBuyRequest method
  49. $this->model->mergeBuyRequest(['qty' => 11, 'additional_data' => 'some value']);
  50. $buyRequest = $this->model->getBuyRequest();
  51. $this->assertEquals(
  52. ['additional_data' => 'some value', 'qty' => 0, 'original_qty' => 11],
  53. $buyRequest->getData()
  54. );
  55. }
  56. public function testSetBuyRequest()
  57. {
  58. $buyRequest = $this->objectManager->create(
  59. \Magento\Framework\DataObject::class,
  60. ['data' => ['field_1' => 'some data', 'field_2' => 234]]
  61. );
  62. $this->model->setBuyRequest($buyRequest);
  63. $this->assertEquals(
  64. '{"field_1":"some data","field_2":234,"id":null}',
  65. $this->model->getData('buy_request')
  66. );
  67. }
  68. }