|
| 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 $collection; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var \Magento\Review\Model\ResourceModel\Review\CollectionFactory|\PHPUnit_Framework_MockObject_MockObject |
| 38 | + */ |
| 39 | + private $collectionFactory; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var \Magento\Framework\Registry|\PHPUnit_Framework_MockObject_MockObject |
| 43 | + */ |
| 44 | + private $registry; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject |
| 48 | + */ |
| 49 | + private $product; |
| 50 | + |
| 51 | + /** |
| 52 | + * @var \Magento\Store\Model\StoreManager|\PHPUnit_Framework_MockObject_MockObject |
| 53 | + */ |
| 54 | + private $storeManager; |
| 55 | + |
| 56 | + /** |
| 57 | + * @var \Magento\Store\Model\Store|\PHPUnit_Framework_MockObject_MockObject |
| 58 | + */ |
| 59 | + private $store; |
| 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 | + 'storeManager' => $this->storeManager, |
| 70 | + 'registry' => $this->registry, |
| 71 | + 'collectionFactory' => $this->collectionFactory, |
| 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->collection = $this->getMockBuilder(Collection::class) |
| 89 | + ->disableOriginalConstructor() |
| 90 | + ->setMethods(['addStoreFilter', 'addStatusFilter', 'addEntityFilter', 'getSize', '__wakeup']) |
| 91 | + ->getMock(); |
| 92 | + |
| 93 | + $this->collection->expects(static::any()) |
| 94 | + ->method('addStoreFilter') |
| 95 | + ->willReturnSelf(); |
| 96 | + |
| 97 | + $this->collection->expects(static::any()) |
| 98 | + ->method('addStatusFilter') |
| 99 | + ->with(Review::STATUS_APPROVED) |
| 100 | + ->willReturnSelf(); |
| 101 | + |
| 102 | + $this->collection->expects(static::any()) |
| 103 | + ->method('addEntityFilter') |
| 104 | + ->willReturnSelf(); |
| 105 | + |
| 106 | + $this->collectionFactory = $this->getMockBuilder(CollectionFactory::class) |
| 107 | + ->disableOriginalConstructor() |
| 108 | + ->setMethods(['create', '__wakeup']) |
| 109 | + ->getMock(); |
| 110 | + |
| 111 | + $this->collectionFactory->expects(static::once()) |
| 112 | + ->method('create') |
| 113 | + ->willReturn($this->collection); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Create mock for registry object |
| 118 | + */ |
| 119 | + private function initRegistryMock() |
| 120 | + { |
| 121 | + $this->initProductMock(); |
| 122 | + $this->registry = $this->getMockBuilder(Registry::class) |
| 123 | + ->disableOriginalConstructor() |
| 124 | + ->setMethods(['registry']) |
| 125 | + ->getMock(); |
| 126 | + |
| 127 | + $this->registry->expects(static::once()) |
| 128 | + ->method('registry') |
| 129 | + ->with('product') |
| 130 | + ->willReturn($this->product); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Create mock object for catalog product |
| 135 | + */ |
| 136 | + private function initProductMock() |
| 137 | + { |
| 138 | + $this->product = $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->store = $this->getMockBuilder(Store::class) |
| 150 | + ->disableOriginalConstructor() |
| 151 | + ->setMethods(['getId', '__wakeup']) |
| 152 | + ->getMock(); |
| 153 | + |
| 154 | + $this->storeManager = $this->getMockBuilder(StoreManager::class) |
| 155 | + ->disableOriginalConstructor() |
| 156 | + ->setMethods(['getStore', '__wakeup']) |
| 157 | + ->getMock(); |
| 158 | + |
| 159 | + $this->storeManager->expects(static::any()) |
| 160 | + ->method('getStore') |
| 161 | + ->willReturn($this->store); |
| 162 | + } |
| 163 | +} |
0 commit comments