Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ProductsRenderInfoSectionTest.php
Go to the documentation of this file.
1 <?php
7 
15 
19 class ProductsRenderInfoSectionTest extends \PHPUnit\Framework\TestCase
20 {
22  protected $model;
23 
26 
28  protected $storeManagerMock;
29 
32 
34  protected $filterBuilderMock;
35 
38 
40  protected $synchronizerMock;
41 
43  protected $hydratorMock;
44 
45  protected function setUp()
46  {
47  $this->storeManagerMock = $this->getMockBuilder(\Magento\Store\Model\StoreManager::class)
48  ->disableOriginalConstructor()
49  ->getMock();
50  $this->searchCriteriaBuilderMock = $this
51  ->getMockBuilder(\Magento\Framework\Api\SearchCriteriaBuilder::class)
52  ->disableOriginalConstructor()
53  ->getMock();
54  $this->filterBuilderMock = $this->getMockBuilder(\Magento\Framework\Api\FilterBuilder::class)
55  ->disableOriginalConstructor()
56  ->getMock();
57  $this->productRenderRepositoryMock = $this
58  ->getMockBuilder(\Magento\Catalog\Model\ProductRenderList::class)
59  ->disableOriginalConstructor()
60  ->getMock();
61  $this->synchronizerMock = $this
62  ->getMockBuilder(\Magento\Catalog\Model\Product\ProductFrontendAction\Synchronizer::class)
63  ->disableOriginalConstructor()
64  ->getMock();
65  $this->hydratorMock = $this->getMockBuilder(\Magento\Framework\EntityManager\Hydrator::class)
66  ->disableOriginalConstructor()
67  ->getMock();
68 
69  $this->objectManagerHelper = new ObjectManagerHelper($this);
70  $this->model = $this->objectManagerHelper->getObject(
71  \Magento\Catalog\CustomerData\ProductsRenderInfoSection::class,
72  [
73  'storeManager' => $this->storeManagerMock,
74  'searchCriteriaBuilder' => $this->searchCriteriaBuilderMock,
75  'filterBuilder' => $this->filterBuilderMock,
76  'productRenderList' => $this->productRenderRepositoryMock,
77  'actionsSynchronizer' => $this->synchronizerMock,
78  'hydrator' => $this->hydratorMock
79  ]
80  );
81  }
82 
83  private function prepareProductIds()
84  {
85  $actionFirst = $this->createMock(ProductFrontendActionInterface::class);
86  $actionSecond = $this->createMock(ProductFrontendActionInterface::class);
87  $actions = [$actionFirst, $actionSecond];
88  $this->synchronizerMock->expects($this->once())
89  ->method('getAllActions')
90  ->willReturn($actions);
91  $actionFirst->expects($this->any())
92  ->method('getProductId')
93  ->willReturn(1);
94  $actionSecond->expects($this->any())
95  ->method('getProductId')
96  ->willReturn(2);
97  }
98 
99  public function testGetSectionData()
100  {
101  $productRender = $this->createMock(ProductRenderInterface::class);
102  $searchResult = $this->createMock(ProductRenderSearchResultsInterface::class);
103 
104  $store = $this->getMockBuilder(Store::class)
105  ->disableOriginalConstructor()
106  ->getMock();
107  $store->expects($this->once())
108  ->method('getId')
109  ->willReturn(3);
110  $store->expects($this->once())
111  ->method('getCurrentCurrencyCode')
112  ->willReturn('UAH');
113  $this->storeManagerMock->expects($this->once())
114  ->method('getStore')
115  ->willReturn($store);
116  $filterMock = $this->createMock(Filter::class);
117  $this->filterBuilderMock
118  ->expects($this->once())
119  ->method('setField')
120  ->with('entity_id')
121  ->willReturnSelf();
122  $this->prepareProductIds();
123  $this->filterBuilderMock
124  ->expects($this->once())
125  ->method('setValue')
126  ->with([1, 2])
127  ->willReturnSelf();
128  $this->filterBuilderMock
129  ->expects($this->once())
130  ->method('setConditionType')
131  ->with('in')
132  ->willReturnSelf();
133  $this->filterBuilderMock
134  ->expects($this->once())
135  ->method('create')
136  ->willReturn($filterMock);
137  $searchCriteria = $this->createMock(SearchCriteria::class);
138  $this->searchCriteriaBuilderMock
139  ->expects($this->once())
140  ->method('addFilters')
141  ->with([$filterMock])
142  ->willReturnSelf();
143  $this->searchCriteriaBuilderMock->expects($this->once())
144  ->method('create')
145  ->willReturn($searchCriteria);
146  $this->productRenderRepositoryMock->expects($this->once())
147  ->method('getList')
148  ->with($searchCriteria, 3, 'UAH')
149  ->willReturn($searchResult);
150  $searchResult->expects($this->any())
151  ->method('getItems')
152  ->willReturn([$productRender]);
153  $this->hydratorMock->expects($this->once())
154  ->method('extract')
155  ->with($productRender)
156  ->willReturn(
157  [
158  'name' => 'One',
159  'price_info' => [
160  'final_price' => 12
161  ]
162  ]
163  );
164 
165  $productRender->expects($this->once())
166  ->method('getId')
167  ->willReturn(1);
168 
169  $this->assertEquals(
170  [
171  1 => [
172  'name' => 'One',
173  'price_info' => [
174  'final_price' => 12
175  ]
176  ]
177  ],
178  $this->model->getSectionData()
179  );
180  }
181 }
$searchCriteria