Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DefaultItemTest.php
Go to the documentation of this file.
1 <?php
7 
9 
10 class DefaultItemTest extends \PHPUnit\Framework\TestCase
11 {
15  private $model;
16 
20  private $imageHelper;
21 
25  private $configurationPool;
26 
30  private $itemResolver;
31 
32  protected function setUp()
33  {
34  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
35  $this->imageHelper = $this->getMockBuilder(\Magento\Catalog\Helper\Image::class)
36  ->disableOriginalConstructor()
37  ->getMock();
38  $this->configurationPool = $this->getMockBuilder(\Magento\Catalog\Helper\Product\ConfigurationPool::class)
39  ->setMethods([])
40  ->disableOriginalConstructor()
41  ->getMock();
42  $checkoutHelper = $this->getMockBuilder(\Magento\Checkout\Helper\Data::class)
43  ->setMethods(['formatPrice'])->disableOriginalConstructor()->getMock();
44  $checkoutHelper->expects($this->any())->method('formatPrice')->willReturn(5);
45  $this->itemResolver = $this->createMock(ItemResolverInterface::class);
46  $this->model = $objectManager->getObject(
47  \Magento\Checkout\CustomerData\DefaultItem::class,
48  [
49  'imageHelper' => $this->imageHelper,
50  'configurationPool' => $this->configurationPool,
51  'checkoutHelper' => $checkoutHelper,
52  'itemResolver' => $this->itemResolver,
53  ]
54  );
55  }
56 
57  public function testGetItemData()
58  {
59  $urlModel = $this->getMockBuilder(\Magento\Catalog\Model\Product\Url::class)
60  ->disableOriginalConstructor()
61  ->getMock();
62  $product = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
63  ->setMethods(['getUrlModel', 'isVisibleInSiteVisibility', 'getSku'])
64  ->disableOriginalConstructor()
65  ->getMock();
66  $product->expects($this->any())->method('getUrlModel')->willReturn($urlModel);
67  $product->expects($this->any())->method('isVisibleInSiteVisibility')->willReturn(true);
68  $product->expects($this->any())->method('getSku')->willReturn('simple');
70  $item = $this->getMockBuilder(\Magento\Quote\Model\Quote\Item::class)
71  ->setMethods(['getProductType', 'getProduct', 'getCalculationPrice'])
72  ->disableOriginalConstructor()
73  ->getMock();
74  $item->expects($this->any())->method('getProduct')->willReturn($product);
75  $item->expects($this->any())->method('getProductType')->willReturn('simple');
76  $item->expects($this->any())->method('getCalculationPrice')->willReturn(5);
77 
78  $this->imageHelper->expects($this->any())->method('init')->with($product)->willReturnSelf();
79  $this->imageHelper->expects($this->any())->method('getUrl')->willReturn('url');
80  $this->imageHelper->expects($this->any())->method('getLabel')->willReturn('label');
81  $this->imageHelper->expects($this->any())->method('getWidth')->willReturn(100);
82  $this->imageHelper->expects($this->any())->method('getHeight')->willReturn(100);
83  $this->configurationPool->expects($this->any())->method('getByProductType')->willReturn($product);
84 
85  $this->itemResolver->expects($this->any())
86  ->method('getFinalProduct')
87  ->with($item)
88  ->will($this->returnValue($product));
89 
90  $itemData = $this->model->getItemData($item);
91  $this->assertArrayHasKey('options', $itemData);
92  $this->assertArrayHasKey('qty', $itemData);
93  $this->assertArrayHasKey('item_id', $itemData);
94  $this->assertArrayHasKey('configure_url', $itemData);
95  $this->assertArrayHasKey('is_visible_in_site_visibility', $itemData);
96  $this->assertArrayHasKey('product_type', $itemData);
97  $this->assertArrayHasKey('product_name', $itemData);
98  $this->assertArrayHasKey('product_sku', $itemData);
99  $this->assertArrayHasKey('product_url', $itemData);
100  $this->assertArrayHasKey('product_has_url', $itemData);
101  $this->assertArrayHasKey('product_price', $itemData);
102  $this->assertArrayHasKey('product_price_value', $itemData);
103  $this->assertArrayHasKey('product_image', $itemData);
104  $this->assertArrayHasKey('canApplyMsrp', $itemData);
105  }
106 }
$objectManager
Definition: bootstrap.php:17