|
| 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\Model\ResourceModel\Review\Summary; |
| 9 | + |
| 10 | +use Magento\Framework\Data\Collection\Db\FetchStrategy\Query; |
| 11 | +use Magento\Framework\Data\Collection\EntityFactory; |
| 12 | +use Magento\Framework\DB\Adapter\AdapterInterface; |
| 13 | +use Magento\Framework\DB\Adapter\Pdo\Mysql; |
| 14 | +use Magento\Framework\DB\Select; |
| 15 | +use Magento\Framework\DB\Select\SelectRenderer; |
| 16 | +use Magento\Framework\Model\ResourceModel\Db\AbstractDb; |
| 17 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 18 | +use Magento\Review\Model\ResourceModel\Review\Summary\Collection; |
| 19 | +use PHPUnit\Framework\MockObject\MockObject; |
| 20 | +use Psr\Log\LoggerInterface; |
| 21 | + |
| 22 | +/** |
| 23 | + * Tests some functionality of the Review Summary collection |
| 24 | + */ |
| 25 | +class CollectionTest extends \PHPUnit\Framework\TestCase |
| 26 | +{ |
| 27 | + /** |
| 28 | + * @var Collection |
| 29 | + */ |
| 30 | + protected Collection $collection; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var Query |
| 34 | + */ |
| 35 | + protected Query $fetchStrategyMock; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var EntityFactory |
| 39 | + */ |
| 40 | + protected EntityFactory $entityFactoryMock; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var LoggerInterface |
| 44 | + */ |
| 45 | + protected LoggerInterface $loggerMock; |
| 46 | + |
| 47 | + /** |
| 48 | + * @var AbstractDb |
| 49 | + */ |
| 50 | + protected AbstractDb $resourceMock; |
| 51 | + |
| 52 | + /** |
| 53 | + * @var AdapterInterface |
| 54 | + */ |
| 55 | + protected AdapterInterface $connectionMock; |
| 56 | + |
| 57 | + /** |
| 58 | + * @var Select |
| 59 | + */ |
| 60 | + protected Select $selectMock; |
| 61 | + |
| 62 | + protected function setUp(): void |
| 63 | + { |
| 64 | + $this->fetchStrategyMock = $this->createPartialMock( |
| 65 | + Query::class, |
| 66 | + ['fetchAll'] |
| 67 | + ); |
| 68 | + $this->entityFactoryMock = $this->createPartialMock( |
| 69 | + EntityFactory::class, |
| 70 | + ['create'] |
| 71 | + ); |
| 72 | + $this->loggerMock = $this->getMockForAbstractClass(LoggerInterface::class); |
| 73 | + $this->resourceMock = $this->getMockBuilder(AbstractDb::class) |
| 74 | + ->setMethods(['getConnection', 'getMainTable', 'getTable']) |
| 75 | + ->disableOriginalConstructor() |
| 76 | + ->getMockForAbstractClass(); |
| 77 | + $this->connectionMock = $this->createPartialMock( |
| 78 | + Mysql::class, |
| 79 | + ['select', 'query'] |
| 80 | + ); |
| 81 | + $selectRenderer = $this->getMockBuilder(SelectRenderer::class) |
| 82 | + ->disableOriginalConstructor() |
| 83 | + ->getMock(); |
| 84 | + $this->selectMock = $this->getMockBuilder(Select::class) |
| 85 | + ->setMethods(['where']) |
| 86 | + ->setConstructorArgs(['adapter' => $this->connectionMock, 'selectRenderer' => $selectRenderer]) |
| 87 | + ->getMock(); |
| 88 | + $this->connectionMock->expects($this->once()) |
| 89 | + ->method('select') |
| 90 | + ->willReturn($this->selectMock); |
| 91 | + $this->resourceMock->expects($this->once()) |
| 92 | + ->method('getConnection') |
| 93 | + ->willReturn($this->connectionMock); |
| 94 | + $this->resourceMock->expects($this->once()) |
| 95 | + ->method('getMainTable') |
| 96 | + ->willReturn('main_table_name'); |
| 97 | + |
| 98 | + $this->resourceMock->expects($this->once()) |
| 99 | + ->method('getTable') |
| 100 | + ->willReturnArgument(0); |
| 101 | + $objectManager = new ObjectManager($this); |
| 102 | + $this->collection = $objectManager->getObject( |
| 103 | + Collection::class, |
| 104 | + [ |
| 105 | + 'entityFactory' => $this->entityFactoryMock, |
| 106 | + 'logger' => $this->loggerMock, |
| 107 | + 'fetchStrategy' => $this->fetchStrategyMock, |
| 108 | + 'resource' => $this->resourceMock |
| 109 | + ] |
| 110 | + ); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * @param array|int $storeId |
| 115 | + * @param string $expectedQuery |
| 116 | + * @dataProvider storeIdDataProvider |
| 117 | + */ |
| 118 | + public function testAddStoreFilter(array|int $storeId, string $expectedQuery) |
| 119 | + { |
| 120 | + $this->selectMock->expects($this->once())->method('where')->with($expectedQuery, $storeId); |
| 121 | + $this->collection->addStoreFilter($storeId); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * @return array |
| 126 | + */ |
| 127 | + public function storeIdDataProvider(): array |
| 128 | + { |
| 129 | + return [ |
| 130 | + [1, 'store_id = ?'], |
| 131 | + [[1,2], 'store_id IN (?)'] |
| 132 | + ]; |
| 133 | + } |
| 134 | +} |
0 commit comments