ItemPoolTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. class ItemPoolTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \PHPUnit_Framework_MockObject_MockObject
  11. */
  12. protected $objectManagerMock;
  13. /**
  14. * @var string
  15. */
  16. protected $defaultItemId = 'default_item_id';
  17. /**
  18. * @var string[]
  19. */
  20. protected $itemMap = [];
  21. /**
  22. * @var \Magento\Checkout\CustomerData\ItemPool
  23. */
  24. protected $model;
  25. protected function setUp()
  26. {
  27. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  28. $this->objectManagerMock = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
  29. $this->model = $objectManager->getObject(
  30. \Magento\Checkout\CustomerData\ItemPool::class,
  31. [
  32. 'objectManager' => $this->objectManagerMock,
  33. 'defaultItemId' => $this->defaultItemId,
  34. 'itemMap' => $this->itemMap,
  35. ]
  36. );
  37. }
  38. public function testGetItemDataIfItemNotExistInMap()
  39. {
  40. $itemData = ['key' => 'value'];
  41. $productType = 'product_type';
  42. $quoteItemMock = $this->createMock(\Magento\Quote\Model\Quote\Item::class);
  43. $quoteItemMock->expects($this->once())->method('getProductType')->willReturn($productType);
  44. $itemMock = $this->createMock(\Magento\Checkout\CustomerData\ItemInterface::class);
  45. $itemMock->expects($this->once())->method('getItemData')->with($quoteItemMock)->willReturn($itemData);
  46. $this->objectManagerMock->expects($this->once())
  47. ->method('get')
  48. ->with($this->defaultItemId)
  49. ->willReturn($itemMock);
  50. $this->assertEquals($itemData, $this->model->getItemData($quoteItemMock));
  51. }
  52. public function testGetItemDataIfItemExistInMap()
  53. {
  54. $itemData = ['key' => 'value'];
  55. $productType = 'product_type';
  56. $this->itemMap[$productType] = 'product_id';
  57. $quoteItemMock = $this->createMock(\Magento\Quote\Model\Quote\Item::class);
  58. $quoteItemMock->expects($this->once())->method('getProductType')->willReturn($productType);
  59. $itemMock = $this->createMock(\Magento\Checkout\CustomerData\ItemInterface::class);
  60. $itemMock->expects($this->once())->method('getItemData')->with($quoteItemMock)->willReturn($itemData);
  61. $this->objectManagerMock->expects($this->once())
  62. ->method('get')
  63. ->with($this->itemMap[$productType])
  64. ->willReturn($itemMock);
  65. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  66. $this->model = $objectManager->getObject(
  67. \Magento\Checkout\CustomerData\ItemPool::class,
  68. [
  69. 'objectManager' => $this->objectManagerMock,
  70. 'defaultItemId' => $this->defaultItemId,
  71. 'itemMap' => $this->itemMap,
  72. ]
  73. );
  74. $this->assertEquals($itemData, $this->model->getItemData($quoteItemMock));
  75. }
  76. /**
  77. * @expectedException \Magento\Framework\Exception\LocalizedException
  78. * #@expectedExceptionMessage product_type doesn't extend \Magento\Checkout\CustomerData\ItemInterface
  79. */
  80. public function testGetItemDataIfItemNotValid()
  81. {
  82. $itemData = ['key' => 'value'];
  83. $productType = 'product_type';
  84. $quoteItemMock = $this->createMock(\Magento\Quote\Model\Quote\Item::class);
  85. $quoteItemMock->expects($this->once())->method('getProductType')->willReturn($productType);
  86. $this->objectManagerMock->expects($this->once())
  87. ->method('get')
  88. ->with($this->defaultItemId)
  89. ->willReturn($this->createMock(\Magento\Quote\Model\Quote\Item::class));
  90. $this->assertEquals($itemData, $this->model->getItemData($quoteItemMock));
  91. }
  92. }