Skip to content

Commit 0001ee3

Browse files
author
Ievgen Sentiabov
committed
MAGETWO-44113: Creating Ratings in the Frontend (not available after Magento installation)
- Changed cache tag - Added unit tests
1 parent f68d2bd commit 0001ee3

File tree

5 files changed

+194
-6
lines changed

5 files changed

+194
-6
lines changed

app/code/Magento/Review/Block/Product/Review.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
*/
66
namespace Magento\Review\Block\Product;
77

8+
use Magento\Framework\DataObject\IdentityInterface;
9+
use Magento\Framework\View\Element\Template;
810

911
/**
1012
* Product Review Tab
1113
*
1214
* @author Magento Core Team <core@magentocommerce.com>
1315
*/
14-
class Review extends \Magento\Framework\View\Element\Template
16+
class Review extends Template implements IdentityInterface
1517
{
1618
/**
1719
* Core registry
@@ -98,4 +100,14 @@ public function getCollectionSize()
98100

99101
return $collection->getSize();
100102
}
103+
104+
/**
105+
* Return unique ID(s) for each object in system
106+
*
107+
* @return array
108+
*/
109+
public function getIdentities()
110+
{
111+
return [\Magento\Review\Model\Review::CACHE_TAG];
112+
}
101113
}

app/code/Magento/Review/Model/Rating.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
namespace Magento\Review\Model;
77

88
use Magento\Framework\DataObject\IdentityInterface;
9-
use Magento\Store\Model\Store;
109

1110
/**
1211
* Rating model
@@ -172,7 +171,7 @@ public function getEntityIdByCode($entityCode)
172171
*/
173172
public function getIdentities()
174173
{
175-
// skip cache for all store
176-
return [Store::CACHE_TAG];
174+
// clear cache for all reviews
175+
return [Review::CACHE_TAG];
177176
}
178177
}

app/code/Magento/Review/Model/Review.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ class Review extends \Magento\Framework\Model\AbstractModel implements IdentityI
3131
*/
3232
protected $_eventPrefix = 'review';
3333

34+
/**
35+
* Cache tag
36+
*/
37+
const CACHE_TAG = 'review_block';
38+
3439
/**
3540
* Product entity review code
3641
*/
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Review\Test\Unit\Block\Product;
8+
9+
use Magento\Framework\Registry;
10+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
11+
use Magento\Framework\View\Element\Template\Context;
12+
use Magento\Catalog\Model\Product;
13+
use Magento\Review\Block\Product\Review as ReviewBlock;
14+
use Magento\Review\Model\ResourceModel\Review\Collection;
15+
use Magento\Review\Model\ResourceModel\Review\CollectionFactory;
16+
use Magento\Review\Model\Review;
17+
use Magento\Store\Model\Store;
18+
use Magento\Store\Model\StoreManager;
19+
20+
/**
21+
* Class ReviewTest
22+
* @package Magento\Review\Test\Unit\Block\Product
23+
*/
24+
class ReviewTest extends \PHPUnit_Framework_TestCase
25+
{
26+
/**
27+
* @var \Magento\Review\Block\Product\Review
28+
*/
29+
private $block;
30+
31+
/**
32+
* @var \Magento\Review\Model\ResourceModel\Review\Collection|\PHPUnit_Framework_MockObject_MockObject
33+
*/
34+
private $collectionMock;
35+
36+
/**
37+
* @var \Magento\Review\Model\ResourceModel\Review\CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
38+
*/
39+
private $collectionFactoryMock;
40+
41+
/**
42+
* @var \Magento\Framework\Registry|\PHPUnit_Framework_MockObject_MockObject
43+
*/
44+
private $registryMock;
45+
46+
/**
47+
* @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject
48+
*/
49+
private $productMock;
50+
51+
/**
52+
* @var \Magento\Framework\View\Element\Template\Context|\PHPUnit_Framework_MockObject_MockObject
53+
*/
54+
private $contextMock;
55+
56+
/**
57+
* @va rMagento\Store\Model\Store|\PHPUnit_Framework_MockObject_MockObject
58+
*/
59+
private $storeMock;
60+
61+
protected function setUp()
62+
{
63+
$this->initContextMock();
64+
$this->initRegistryMock();
65+
$this->initCollectionMocks();
66+
67+
$helper = new ObjectManager($this);
68+
$this->block = $helper->getObject(ReviewBlock::class, [
69+
'context' => $this->contextMock,
70+
'registry' => $this->registryMock,
71+
'collectionFactory' => $this->collectionFactoryMock,
72+
]);
73+
}
74+
75+
/**
76+
* @covers \Magento\Review\Block\Product\Review::getIdentities()
77+
*/
78+
public function testGetIdentities()
79+
{
80+
static::assertEquals([Review::CACHE_TAG], $this->block->getIdentities());
81+
}
82+
83+
/**
84+
* Create mocks for collection and its factory
85+
*/
86+
private function initCollectionMocks()
87+
{
88+
$this->collectionMock = $this->getMockBuilder(Collection::class)
89+
->disableOriginalConstructor()
90+
->setMethods(['addStoreFilter', 'addStatusFilter', 'addEntityFilter', 'getSize', '__wakeup'])
91+
->getMock();
92+
93+
$this->collectionMock->expects(static::any())
94+
->method('addStoreFilter')
95+
->willReturnSelf();
96+
97+
$this->collectionMock->expects(static::any())
98+
->method('addStatusFilter')
99+
->with(Review::STATUS_APPROVED)
100+
->willReturnSelf();
101+
102+
$this->collectionMock->expects(static::any())
103+
->method('addEntityFilter')
104+
->willReturnSelf();
105+
106+
$this->collectionFactoryMock = $this->getMockBuilder(CollectionFactory::class)
107+
->disableOriginalConstructor()
108+
->setMethods(['create', '__wakeup'])
109+
->getMock();
110+
111+
$this->collectionFactoryMock->expects(static::once())
112+
->method('create')
113+
->willReturn($this->collectionMock);
114+
}
115+
116+
/**
117+
* Create mock for registry object
118+
*/
119+
private function initRegistryMock()
120+
{
121+
$this->initProductMock();
122+
$this->registryMock = $this->getMockBuilder(Registry::class)
123+
->disableOriginalConstructor()
124+
->setMethods(['registry'])
125+
->getMock();
126+
127+
$this->registryMock->expects(static::once())
128+
->method('registry')
129+
->with('product')
130+
->willReturn($this->productMock);
131+
}
132+
133+
/**
134+
* Create mock object for catalog product
135+
*/
136+
private function initProductMock()
137+
{
138+
$this->productMock = $this->getMockBuilder(Product::class)
139+
->disableOriginalConstructor()
140+
->setMethods(['getId'])
141+
->getMock();
142+
}
143+
144+
/**
145+
* Create mock object for context
146+
*/
147+
private function initContextMock()
148+
{
149+
$this->storeMock = $this->getMockBuilder(Store::class)
150+
->disableOriginalConstructor()
151+
->setMethods(['getId', '__wakeup'])
152+
->getMock();
153+
154+
$storeManager = $this->getMockBuilder(StoreManager::class)
155+
->disableOriginalConstructor()
156+
->setMethods(['getStore', '__wakeup'])
157+
->getMock();
158+
159+
$storeManager->expects(static::once())
160+
->method('getStore')
161+
->willReturn($this->storeMock);
162+
163+
$this->contextMock = $this->getMockBuilder(Context::class)
164+
->disableOriginalConstructor()
165+
->setMethods(['getStoreManager'])
166+
->getMock();
167+
168+
$this->contextMock->expects(static::once())
169+
->method('getStoreManager')
170+
->willReturn($storeManager);
171+
}
172+
}

app/code/Magento/Review/Test/Unit/Model/RatingTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
namespace Magento\Review\Test\Unit\Model;
77

88
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
9-
use Magento\Store\Model\Store;
9+
use Magento\Review\Model\Review;
1010
use Magento\Review\Model\Rating;
1111

1212
class RatingTest extends \PHPUnit_Framework_TestCase
@@ -31,6 +31,6 @@ protected function setUp()
3131
*/
3232
public function testGetIdentities()
3333
{
34-
static::assertEquals([Store::CACHE_TAG], $this->rating->getIdentities());
34+
static::assertEquals([Review::CACHE_TAG], $this->rating->getIdentities());
3535
}
3636
}

0 commit comments

Comments
 (0)