DefaultItemTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Checkout\Test\Unit\CustomerData;
  7. use Magento\Catalog\Model\Product\Configuration\Item\ItemResolverInterface;
  8. class DefaultItemTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \Magento\Checkout\CustomerData\DefaultItem
  12. */
  13. private $model;
  14. /**
  15. * @var \Magento\Catalog\Helper\Image
  16. */
  17. private $imageHelper;
  18. /**
  19. * @var \Magento\Catalog\Helper\Product\ConfigurationPool
  20. */
  21. private $configurationPool;
  22. /**
  23. * @var ItemResolverInterface|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. private $itemResolver;
  26. protected function setUp()
  27. {
  28. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  29. $this->imageHelper = $this->getMockBuilder(\Magento\Catalog\Helper\Image::class)
  30. ->disableOriginalConstructor()
  31. ->getMock();
  32. $this->configurationPool = $this->getMockBuilder(\Magento\Catalog\Helper\Product\ConfigurationPool::class)
  33. ->setMethods([])
  34. ->disableOriginalConstructor()
  35. ->getMock();
  36. $checkoutHelper = $this->getMockBuilder(\Magento\Checkout\Helper\Data::class)
  37. ->setMethods(['formatPrice'])->disableOriginalConstructor()->getMock();
  38. $checkoutHelper->expects($this->any())->method('formatPrice')->willReturn(5);
  39. $this->itemResolver = $this->createMock(ItemResolverInterface::class);
  40. $this->model = $objectManager->getObject(
  41. \Magento\Checkout\CustomerData\DefaultItem::class,
  42. [
  43. 'imageHelper' => $this->imageHelper,
  44. 'configurationPool' => $this->configurationPool,
  45. 'checkoutHelper' => $checkoutHelper,
  46. 'itemResolver' => $this->itemResolver,
  47. ]
  48. );
  49. }
  50. public function testGetItemData()
  51. {
  52. $urlModel = $this->getMockBuilder(\Magento\Catalog\Model\Product\Url::class)
  53. ->disableOriginalConstructor()
  54. ->getMock();
  55. $product = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
  56. ->setMethods(['getUrlModel', 'isVisibleInSiteVisibility', 'getSku'])
  57. ->disableOriginalConstructor()
  58. ->getMock();
  59. $product->expects($this->any())->method('getUrlModel')->willReturn($urlModel);
  60. $product->expects($this->any())->method('isVisibleInSiteVisibility')->willReturn(true);
  61. $product->expects($this->any())->method('getSku')->willReturn('simple');
  62. /** @var \Magento\Quote\Model\Quote\Item $item */
  63. $item = $this->getMockBuilder(\Magento\Quote\Model\Quote\Item::class)
  64. ->setMethods(['getProductType', 'getProduct', 'getCalculationPrice'])
  65. ->disableOriginalConstructor()
  66. ->getMock();
  67. $item->expects($this->any())->method('getProduct')->willReturn($product);
  68. $item->expects($this->any())->method('getProductType')->willReturn('simple');
  69. $item->expects($this->any())->method('getCalculationPrice')->willReturn(5);
  70. $this->imageHelper->expects($this->any())->method('init')->with($product)->willReturnSelf();
  71. $this->imageHelper->expects($this->any())->method('getUrl')->willReturn('url');
  72. $this->imageHelper->expects($this->any())->method('getLabel')->willReturn('label');
  73. $this->imageHelper->expects($this->any())->method('getWidth')->willReturn(100);
  74. $this->imageHelper->expects($this->any())->method('getHeight')->willReturn(100);
  75. $this->configurationPool->expects($this->any())->method('getByProductType')->willReturn($product);
  76. $this->itemResolver->expects($this->any())
  77. ->method('getFinalProduct')
  78. ->with($item)
  79. ->will($this->returnValue($product));
  80. $itemData = $this->model->getItemData($item);
  81. $this->assertArrayHasKey('options', $itemData);
  82. $this->assertArrayHasKey('qty', $itemData);
  83. $this->assertArrayHasKey('item_id', $itemData);
  84. $this->assertArrayHasKey('configure_url', $itemData);
  85. $this->assertArrayHasKey('is_visible_in_site_visibility', $itemData);
  86. $this->assertArrayHasKey('product_type', $itemData);
  87. $this->assertArrayHasKey('product_name', $itemData);
  88. $this->assertArrayHasKey('product_sku', $itemData);
  89. $this->assertArrayHasKey('product_url', $itemData);
  90. $this->assertArrayHasKey('product_has_url', $itemData);
  91. $this->assertArrayHasKey('product_price', $itemData);
  92. $this->assertArrayHasKey('product_price_value', $itemData);
  93. $this->assertArrayHasKey('product_image', $itemData);
  94. $this->assertArrayHasKey('canApplyMsrp', $itemData);
  95. }
  96. }