QuoteItemTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Bundle\Test\Unit\Model\Plugin;
  7. use Magento\Quote\Model\Quote\Item\ToOrderItem;
  8. use Magento\Sales\Api\Data\OrderItemInterface;
  9. use Magento\Quote\Model\Quote\Item\AbstractItem;
  10. use Magento\Catalog\Model\Product;
  11. class QuoteItemTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var \PHPUnit_Framework_MockObject_MockObject|Product
  15. */
  16. private $productMock;
  17. /** @var \Magento\Bundle\Model\Plugin\QuoteItem */
  18. protected $model;
  19. /** @var \PHPUnit_Framework_MockObject_MockObject|AbstractItem */
  20. protected $quoteItemMock;
  21. /** @var \PHPUnit_Framework_MockObject_MockObject|OrderItemInterface */
  22. protected $orderItemMock;
  23. /**
  24. * @var \PHPUnit_Framework_MockObject_MockObject|ToOrderItem
  25. */
  26. protected $subjectMock;
  27. protected function setUp()
  28. {
  29. $this->orderItemMock = $this->getMockForAbstractClass(
  30. OrderItemInterface::class,
  31. [],
  32. '',
  33. false,
  34. false,
  35. true,
  36. ['getProductOptions', 'setProductOptions']
  37. );
  38. $this->quoteItemMock = $this->getMockForAbstractClass(
  39. AbstractItem::class,
  40. [],
  41. '',
  42. false,
  43. false,
  44. true,
  45. ['getProduct']
  46. );
  47. $this->subjectMock = $this->createMock(ToOrderItem::class);
  48. $this->productMock = $this->createMock(Product::class);
  49. $this->model = new \Magento\Bundle\Model\Plugin\QuoteItem();
  50. }
  51. public function testAroundItemToOrderItemPositive()
  52. {
  53. $attributeValue = 'test_value';
  54. $productOptions = [
  55. 'option_1' => 'value_1',
  56. 'option_2' => 'value_2'
  57. ];
  58. $expectedOptions = $productOptions + ['bundle_selection_attributes' => $attributeValue];
  59. $bundleAttribute = $this->createMock(\Magento\Catalog\Model\Product\Configuration\Item\Option::class);
  60. $bundleAttribute->expects($this->once())
  61. ->method('getValue')
  62. ->willReturn($attributeValue);
  63. $this->productMock->expects($this->once())
  64. ->method('getCustomOption')
  65. ->with('bundle_selection_attributes')
  66. ->willReturn($bundleAttribute);
  67. $this->quoteItemMock->expects($this->once())->method('getProduct')->willReturn($this->productMock);
  68. $this->orderItemMock->expects($this->once())->method('getProductOptions')->willReturn($productOptions);
  69. $this->orderItemMock->expects($this->once())->method('setProductOptions')->with($expectedOptions);
  70. $orderItem = $this->model->afterConvert($this->subjectMock, $this->orderItemMock, $this->quoteItemMock);
  71. $this->assertSame($this->orderItemMock, $orderItem);
  72. }
  73. public function testAroundItemToOrderItemNegative()
  74. {
  75. $this->productMock->expects($this->once())
  76. ->method('getCustomOption')
  77. ->with('bundle_selection_attributes')->willReturn(false);
  78. $this->quoteItemMock->expects($this->once())->method('getProduct')
  79. ->willReturn($this->productMock);
  80. $this->orderItemMock->expects($this->never())->method('setProductOptions');
  81. $orderItem = $this->model->afterConvert($this->subjectMock, $this->orderItemMock, $this->quoteItemMock);
  82. $this->assertSame($this->orderItemMock, $orderItem);
  83. }
  84. }