Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ProductRenderListTest.php
Go to the documentation of this file.
1 <?php
7 
15 
19 class ProductRenderListTest extends \PHPUnit\Framework\TestCase
20 {
22  private $model;
23 
25  private $collectionFactoryMock;
26 
28  private $collectionProcessorMock;
29 
31  private $productRenderCollectorCompositeMock;
32 
34  private $productRenderSearchResultsFactoryMock;
35 
37  private $productRenderFactoryMock;
38 
40  private $configMock;
41 
43  private $productVisibility;
44 
46  private $collectionModifier;
47 
48  public function setUp()
49  {
50  $this->collectionFactoryMock = $this
51  ->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory::class)
52  ->setMethods(['create'])
53  ->disableOriginalConstructor()
54  ->getMock();
55  $this->collectionProcessorMock = $this
56  ->getMockBuilder(\Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface::class)
57  ->disableOriginalConstructor()
58  ->getMockForAbstractClass();
59  $this->productRenderCollectorCompositeMock = $this
60  ->getMockBuilder(\Magento\Catalog\Ui\DataProvider\Product\ProductRenderCollectorComposite::class)
61  ->disableOriginalConstructor()
62  ->getMock();
63  $this->productRenderSearchResultsFactoryMock = $this
64  ->getMockBuilder(\Magento\Catalog\Model\ProductRenderSearchResultsFactory::class)
65  ->setMethods(['create'])
66  ->disableOriginalConstructor()
67  ->getMock();
68  $this->productRenderFactoryMock = $this
69  ->getMockBuilder(\Magento\Catalog\Model\ProductRenderFactory::class)
70  ->disableOriginalConstructor()
71  ->setMethods(['create'])
72  ->getMock();
73  $this->configMock = $this->getMockBuilder(\Magento\Catalog\Model\Config::class)
74  ->disableOriginalConstructor()
75  ->getMock();
76  $this->configMock->expects($this->once())
77  ->method('getProductAttributes')
78  ->willReturn([]);
79  $this->productVisibility = $this->getMockBuilder(Visibility::class)
80  ->disableOriginalConstructor()
81  ->getMock();
82  $this->collectionModifier = $this->getMockBuilder(CollectionModifier::class)
83  ->disableOriginalConstructor()
84  ->getMock();
85 
86  $this->model = new \Magento\Catalog\Model\ProductRenderList(
87  $this->collectionFactoryMock,
88  $this->collectionProcessorMock,
89  $this->productRenderCollectorCompositeMock,
90  $this->productRenderSearchResultsFactoryMock,
91  $this->productRenderFactoryMock,
92  $this->configMock,
93  $this->collectionModifier,
94  ['msrp_price']
95  );
96  }
97 
98  public function testGetList()
99  {
100  $storeId = 1;
101  $currencyCode = 'USD';
102 
103  $product = $this->getMockBuilder(ProductInterface::class)
104  ->disableOriginalConstructor()
105  ->getMock();
106  $iterator = new \IteratorIterator(new \ArrayIterator([$product]));
107  $productRender = $this->getMockBuilder(ProductRenderInterface::class)
108  ->disableOriginalConstructor()
109  ->getMock();
110  $searchResult = $this->getMockBuilder(SearchResultInterface::class)
111  ->disableOriginalConstructor()
112  ->getMock();
113  $searchCriteria = $this->getMockBuilder(SearchCriteria::class)
114  ->disableOriginalConstructor()
115  ->getMock();
116  $productCollection = $this->getMockBuilder(Collection::class)
117  ->disableOriginalConstructor()
118  ->getMock();
119  $this->collectionFactoryMock->expects($this->once())
120  ->method('create')
121  ->willReturn($productCollection);
122  $productCollection->expects($this->once())
123  ->method('addAttributeToSelect')
124  ->with(['msrp_price'])
125  ->willReturnSelf();
126  $productCollection->expects($this->once())
127  ->method('setStoreId')
128  ->with($storeId)
129  ->willReturnSelf();
130  $productCollection->expects($this->once())
131  ->method('addMinimalPrice')
132  ->willReturnSelf();
133  $productCollection->expects($this->once())
134  ->method('addFinalPrice')
135  ->willReturnSelf();
136  $productCollection->expects($this->once())
137  ->method('addTaxPercents')
138  ->willReturnSelf();
139  $this->collectionProcessorMock->expects($this->once())
140  ->method('process')
142  $productCollection->expects($this->once())
143  ->method('getIterator')
144  ->willReturn($iterator);
145  $this->collectionModifier->expects($this->once())
146  ->method('apply')
147  ->with($productCollection);
148  $this->collectionProcessorMock->expects($this->once())
149  ->method('process')
151  $product->expects($this->once())
152  ->method('getId')
153  ->willReturn(1);
154  $this->productRenderFactoryMock->expects($this->once())
155  ->method('create')
156  ->willReturn($productRender);
157  $productRender->expects($this->once())
158  ->method('setStoreId')
159  ->with(1);
160  $productRender->expects($this->once())
161  ->method('setCurrencyCode')
162  ->with($currencyCode);
163  $this->productRenderCollectorCompositeMock->expects($this->once())
164  ->method('collect')
165  ->with($product, $productRender);
166  $this->productRenderSearchResultsFactoryMock->expects($this->once())
167  ->method('create')
168  ->willReturn($searchResult);
169  $searchResult->expects($this->once())
170  ->method('setItems')
171  ->with([
172  1 => $productRender
173  ]);
174  $searchResult->expects($this->once())
175  ->method('setTotalCount')
176  ->with(1);
177  $searchResult->expects($this->once())
178  ->method('setSearchCriteria')
179  ->with($searchCriteria);
180 
181  $this->assertEquals($searchResult, $this->model->getList($searchCriteria, $storeId, $currencyCode));
182  }
183 }
$searchCriteria