|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Review\Test\Unit\Model; |
| 9 | + |
| 10 | +use Magento\Catalog\Model\Product; |
| 11 | +use Magento\Framework\DB\Select; |
| 12 | +use Magento\Review\Model\AppendSummaryDataToObjectByEntityCode; |
| 13 | +use Magento\Review\Model\ResourceModel\Review\Summary as ResourceSummary; |
| 14 | +use Magento\Review\Model\ResourceModel\Review\Summary\Collection as SummaryCollection; |
| 15 | +use Magento\Review\Model\ResourceModel\Review\Summary\CollectionFactory as SummaryCollectionFactory; |
| 16 | +use Magento\Review\Model\Review\Summary; |
| 17 | +use PHPUnit\Framework\MockObject\MockObject; |
| 18 | +use PHPUnit\Framework\TestCase; |
| 19 | + |
| 20 | +/** |
| 21 | + * Unit tests for \Magento\Review\Model\AppendSummaryDataToObjectByEntityCode class |
| 22 | + */ |
| 23 | +class AppendSummaryDataToObjectByEntityCodeTest extends TestCase |
| 24 | +{ |
| 25 | + /** |
| 26 | + * @var SummaryCollectionFactory|MockObject |
| 27 | + */ |
| 28 | + private $summaryCollectionFactoryMock; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var Product|MockObject |
| 32 | + */ |
| 33 | + private $productMock; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var Summary|MockObject |
| 37 | + */ |
| 38 | + private $summaryMock; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var SummaryCollection|MockObject |
| 42 | + */ |
| 43 | + private $summaryCollectionMock; |
| 44 | + |
| 45 | + /** |
| 46 | + * @var Select|MockObject |
| 47 | + */ |
| 48 | + private $selectMock; |
| 49 | + |
| 50 | + /** |
| 51 | + * @var ResourceSummary|MockObject |
| 52 | + */ |
| 53 | + private $resourceSummaryMock; |
| 54 | + |
| 55 | + /** |
| 56 | + * @var AppendSummaryDataToObjectByEntityCode |
| 57 | + */ |
| 58 | + private $model; |
| 59 | + |
| 60 | + /** |
| 61 | + * @inheriDoc |
| 62 | + */ |
| 63 | + protected function setUp(): void |
| 64 | + { |
| 65 | + $this->summaryCollectionFactoryMock = $this->getMockBuilder(SummaryCollectionFactory::class) |
| 66 | + ->disableOriginalConstructor() |
| 67 | + ->onlyMethods(['create']) |
| 68 | + ->getMock(); |
| 69 | + |
| 70 | + $this->productMock = $this->getMockBuilder(Product::class) |
| 71 | + ->disableOriginalConstructor() |
| 72 | + ->onlyMethods(['getId', 'addData']) |
| 73 | + ->getMock(); |
| 74 | + |
| 75 | + $this->summaryMock = $this->getMockBuilder(Summary::class) |
| 76 | + ->disableOriginalConstructor() |
| 77 | + ->onlyMethods(['getData']) |
| 78 | + ->getMock(); |
| 79 | + |
| 80 | + $this->summaryCollectionMock = $this->getMockBuilder(SummaryCollection::class) |
| 81 | + ->disableOriginalConstructor() |
| 82 | + ->onlyMethods( |
| 83 | + [ |
| 84 | + 'addStoreFilter', |
| 85 | + 'getSelect', |
| 86 | + 'getResource', |
| 87 | + 'getFirstItem', |
| 88 | + ] |
| 89 | + ) |
| 90 | + ->getMock(); |
| 91 | + |
| 92 | + $this->selectMock = $this->getMockBuilder(Select::class) |
| 93 | + ->disableOriginalConstructor() |
| 94 | + ->onlyMethods(['joinLeft', 'where']) |
| 95 | + ->getMock(); |
| 96 | + |
| 97 | + $this->resourceSummaryMock = $this->getMockBuilder(ResourceSummary::class) |
| 98 | + ->disableOriginalConstructor() |
| 99 | + ->onlyMethods(['getTable']) |
| 100 | + ->getMock(); |
| 101 | + |
| 102 | + $this->model = new AppendSummaryDataToObjectByEntityCode( |
| 103 | + $this->summaryCollectionFactoryMock |
| 104 | + ); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * @return void |
| 109 | + */ |
| 110 | + public function testExecute(): void |
| 111 | + { |
| 112 | + $productId = 6; |
| 113 | + $storeId = 4; |
| 114 | + $entityCode = 'product'; |
| 115 | + $summaryData = [ |
| 116 | + 'reviews_count' => 2, |
| 117 | + 'rating_summary' => 80, |
| 118 | + ]; |
| 119 | + |
| 120 | + $this->productMock->expects($this->once()) |
| 121 | + ->method('getId') |
| 122 | + ->willReturn($productId); |
| 123 | + |
| 124 | + $this->productMock->expects($this->once()) |
| 125 | + ->method('addData') |
| 126 | + ->with($summaryData) |
| 127 | + ->willReturnSelf(); |
| 128 | + |
| 129 | + $this->summaryMock->expects($this->exactly(2)) |
| 130 | + ->method('getData') |
| 131 | + ->willReturnMap( |
| 132 | + [ |
| 133 | + ['reviews_count', null, $summaryData['reviews_count']], |
| 134 | + ['rating_summary', null, $summaryData['rating_summary']], |
| 135 | + ] |
| 136 | + ); |
| 137 | + |
| 138 | + $this->summaryCollectionMock->expects($this->once()) |
| 139 | + ->method('addStoreFilter') |
| 140 | + ->with($storeId) |
| 141 | + ->willReturnSelf(); |
| 142 | + $this->summaryCollectionMock->expects($this->once()) |
| 143 | + ->method('getSelect') |
| 144 | + ->willReturn($this->selectMock); |
| 145 | + $this->summaryCollectionMock->expects($this->once()) |
| 146 | + ->method('getResource') |
| 147 | + ->willReturn($this->resourceSummaryMock); |
| 148 | + |
| 149 | + $this->resourceSummaryMock->expects($this->once()) |
| 150 | + ->method('getTable') |
| 151 | + ->willReturn('table_name'); |
| 152 | + |
| 153 | + $this->summaryCollectionMock->expects($this->once()) |
| 154 | + ->method('getFirstItem') |
| 155 | + ->willReturn($this->summaryMock); |
| 156 | + |
| 157 | + $this->selectMock->expects($this->once()) |
| 158 | + ->method('joinLeft') |
| 159 | + ->willReturnSelf(); |
| 160 | + $this->selectMock->expects($this->exactly(2)) |
| 161 | + ->method('where') |
| 162 | + ->willReturnSelf(); |
| 163 | + |
| 164 | + $this->summaryCollectionFactoryMock->expects($this->once()) |
| 165 | + ->method('create') |
| 166 | + ->willReturn($this->summaryCollectionMock); |
| 167 | + |
| 168 | + $this->model->execute($this->productMock, $storeId, $entityCode); |
| 169 | + } |
| 170 | +} |
0 commit comments