Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ReviewTest.php
Go to the documentation of this file.
1 <?php
8 
12 
16 class ReviewTest extends \PHPUnit\Framework\TestCase
17 {
19  protected $review;
20 
23 
25  protected $contextMock;
26 
28  protected $registryMock;
29 
32 
34  protected $statusFactoryMock;
35 
37  protected $reviewSummaryMock;
38 
40  protected $summaryModMock;
41 
43  protected $summaryMock;
44 
46  protected $storeManagerMock;
47 
49  protected $urlInterfaceMock;
50 
52  protected $resource;
53 
55  protected $reviewId = 8;
56 
57  protected function setUp()
58  {
59  $this->contextMock = $this->createMock(\Magento\Framework\Model\Context::class);
60  $this->registryMock = $this->createMock(\Magento\Framework\Registry::class);
61  $this->productFactoryMock = $this->createPartialMock(
62  \Magento\Review\Model\ResourceModel\Review\Product\CollectionFactory::class,
63  ['create']
64  );
65  $this->statusFactoryMock = $this->createPartialMock(
66  \Magento\Review\Model\ResourceModel\Review\Status\CollectionFactory::class,
67  ['create']
68  );
69  $this->reviewSummaryMock = $this->createMock(
70  \Magento\Review\Model\ResourceModel\Review\Summary\CollectionFactory::class
71  );
72  $this->summaryModMock = $this->createPartialMock(
73  \Magento\Review\Model\Review\SummaryFactory::class,
74  ['create']
75  );
76  $this->summaryMock = $this->createMock(\Magento\Review\Model\Review\Summary::class);
77  $this->storeManagerMock = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
78  $this->urlInterfaceMock = $this->createMock(\Magento\Framework\UrlInterface::class);
79  $this->resource = $this->createMock(\Magento\Review\Model\ResourceModel\Review::class);
80 
81  $this->objectManagerHelper = new ObjectManagerHelper($this);
82  $this->review = $this->objectManagerHelper->getObject(
83  \Magento\Review\Model\Review::class,
84  [
85  'context' => $this->contextMock,
86  'registry' => $this->registryMock,
87  'productFactory' => $this->productFactoryMock,
88  'statusFactory' => $this->statusFactoryMock,
89  'summaryFactory' => $this->reviewSummaryMock,
90  'summaryModFactory' => $this->summaryModMock,
91  'reviewSummary' => $this->summaryMock,
92  'storeManager' => $this->storeManagerMock,
93  'urlModel' => $this->urlInterfaceMock,
94  'resource' => $this->resource,
95  'data' => ['review_id' => $this->reviewId, 'status_id' => 1, 'stores' => [2, 3, 4]]
96  ]
97  );
98  }
99 
100  public function testGetProductCollection()
101  {
102  $collection = $this->createMock(\Magento\Review\Model\ResourceModel\Review\Product\Collection::class);
103  $this->productFactoryMock->expects($this->once())
104  ->method('create')
105  ->will($this->returnValue($collection));
106  $this->assertSame($collection, $this->review->getProductCollection());
107  }
108 
109  public function testGetStatusCollection()
110  {
111  $collection = $this->createMock(\Magento\Review\Model\ResourceModel\Review\Status\Collection::class);
112  $this->statusFactoryMock->expects($this->once())
113  ->method('create')
114  ->will($this->returnValue($collection));
115  $this->assertSame($collection, $this->review->getStatusCollection());
116  }
117 
118  public function testGetTotalReviews()
119  {
120  $primaryKey = 'review_id';
121  $approvedOnly = false;
122  $storeId = 0;
123  $result = 5;
124  $this->resource->expects($this->once())->method('getTotalReviews')
125  ->with($this->equalTo($primaryKey), $this->equalTo($approvedOnly), $this->equalTo($storeId))
126  ->will($this->returnValue($result));
127  $this->assertSame($result, $this->review->getTotalReviews($primaryKey, $approvedOnly, $storeId));
128  }
129 
130  public function testAggregate()
131  {
132  $this->resource->expects($this->once())->method('aggregate')
133  ->with($this->equalTo($this->review))
134  ->will($this->returnValue($this->review));
135  $this->assertSame($this->review, $this->review->aggregate());
136  }
137 
138  public function testGetEntitySummary()
139  {
140  $productId = 6;
141  $storeId = 4;
142  $testSummaryData = ['test' => 'value'];
143  $summary = new \Magento\Framework\DataObject();
144  $summary->setData($testSummaryData);
145 
146  $product = $this->createPartialMock(
147  \Magento\Catalog\Model\Product::class,
148  ['getId', 'setRatingSummary', '__wakeup']
149  );
150  $product->expects($this->once())->method('getId')->will($this->returnValue($productId));
151  $product->expects($this->once())->method('setRatingSummary')->with($summary)->will($this->returnSelf());
152 
153  $summaryData = $this->createPartialMock(
154  \Magento\Review\Model\Review\Summary::class,
155  ['load', 'getData', 'setStoreId', '__wakeup']
156  );
157  $summaryData->expects($this->once())->method('setStoreId')
158  ->with($this->equalTo($storeId))
159  ->will($this->returnSelf());
160  $summaryData->expects($this->once())->method('load')
161  ->with($this->equalTo($productId))
162  ->will($this->returnSelf());
163  $summaryData->expects($this->once())->method('getData')->will($this->returnValue($testSummaryData));
164  $this->summaryModMock->expects($this->once())->method('create')->will($this->returnValue($summaryData));
165  $this->assertNull($this->review->getEntitySummary($product, $storeId));
166  }
167 
168  public function testGetPendingStatus()
169  {
170  $this->assertSame(Review::STATUS_PENDING, $this->review->getPendingStatus());
171  }
172 
173  public function testGetReviewUrl()
174  {
175  $result = 'http://some.url';
176  $this->urlInterfaceMock->expects($this->once())->method('getUrl')
177  ->with($this->equalTo('review/product/view'), $this->equalTo(['id' => $this->reviewId]))
178  ->will($this->returnValue($result));
179  $this->assertSame($result, $this->review->getReviewUrl());
180  }
181 
189  {
190  if ($storeId) {
191  $this->urlInterfaceMock->expects($this->once())->method('setScope')
192  ->with($this->equalTo($storeId))
193  ->will($this->returnSelf());
194  }
195 
196  $this->urlInterfaceMock->expects($this->once())->method('getUrl')
197  ->with($this->equalTo('catalog/product/view'), $this->equalTo(['id' => $productId]))
198  ->will($this->returnValue($result));
199  $this->assertSame($result, $this->review->getProductUrl($productId, $storeId));
200  }
201 
205  public function getProductUrlDataProvider()
206  {
207  return [
208  'store id specified' => [3, 5, 'http://some.url'],
209  'store id is not specified' => [3, null, 'http://some.url/2/'],
210  ];
211  }
212 
213  public function testIsApproved()
214  {
215  $this->assertTrue($this->review->isApproved());
216  }
217 
224  {
225  $store = $this->createMock(\Magento\Store\Model\Store::class);
226  if ($storeId) {
227  $store->expects($this->once())->method('getId')->will($this->returnValue($storeId));
228  $this->storeManagerMock->expects($this->once())
229  ->method('getStore')
230  ->with($this->equalTo($store))
231  ->will($this->returnValue($store));
232  }
233  $this->assertSame($result, $this->review->isAvailableOnStore($store));
234  }
235 
240  {
241  return [
242  'store id is set and not in list' => [1, false],
243  'store id is set' => [3, true],
244  'store id is not set' => [null, false],
245  ];
246  }
247 
248  public function testGetEntityIdByCode()
249  {
250  $entityCode = 'test';
251  $result = 22;
252  $this->resource->expects($this->once())->method('getEntityIdByCode')
253  ->with($this->equalTo($entityCode))
254  ->will($this->returnValue($result));
255  $this->assertSame($result, $this->review->getEntityIdByCode($entityCode));
256  }
257 
258  public function testGetIdentities()
259  {
260  $this->review->setStatusId(Review::STATUS_PENDING);
261  $this->assertEmpty($this->review->getIdentities());
262 
263  $productId = 1;
264  $this->review->setEntityPkValue($productId);
265  $this->review->setStatusId(Review::STATUS_PENDING);
266  $this->assertEquals([Product::CACHE_TAG . '_' . $productId], $this->review->getIdentities());
267 
268  $this->review->setEntityPkValue($productId);
269  $this->review->setStatusId(Review::STATUS_APPROVED);
270  $this->assertEquals([Product::CACHE_TAG . '_' . $productId], $this->review->getIdentities());
271 
272  $this->review->setEntityPkValue($productId);
273  $this->review->setStatusId(Review::STATUS_NOT_APPROVED);
274  $this->assertEquals([Product::CACHE_TAG . '_' . $productId], $this->review->getIdentities());
275  }
276 }
testGetProductUrl($productId, $storeId, $result)
Definition: ReviewTest.php:188