|
| 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\Catalog\Test\Unit\Model\ResourceModel\Product; |
| 9 | + |
| 10 | +use Magento\Catalog\Model\ResourceModel\Product\Image; |
| 11 | +use Magento\Framework\DB\Adapter\AdapterInterface; |
| 12 | +use Magento\Framework\DB\Query\Generator; |
| 13 | +use Magento\Framework\DB\Select; |
| 14 | +use Magento\Framework\App\ResourceConnection; |
| 15 | +use Magento\Catalog\Model\ResourceModel\Product\Gallery; |
| 16 | + |
| 17 | +class ImageTest extends \PHPUnit\Framework\TestCase |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @var AdapterInterface | \PHPUnit_Framework_MockObject_MockObject |
| 21 | + */ |
| 22 | + protected $connectionMock; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var Generator | \PHPUnit_Framework_MockObject_MockObject |
| 26 | + */ |
| 27 | + protected $generatorMock; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var ResourceConnection | \PHPUnit_Framework_MockObject_MockObject |
| 31 | + */ |
| 32 | + protected $resourceMock; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var Image |
| 36 | + */ |
| 37 | + protected $imageModel; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var int |
| 41 | + */ |
| 42 | + protected $imagesCount = 50; |
| 43 | + |
| 44 | + /** |
| 45 | + * @var int |
| 46 | + */ |
| 47 | + protected $batchSize = 10; |
| 48 | + |
| 49 | + protected function setUp(): void |
| 50 | + { |
| 51 | + $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); |
| 52 | + |
| 53 | + $this->connectionMock = $this->createMock(AdapterInterface::class); |
| 54 | + |
| 55 | + $this->resourceMock = $this->createMock(ResourceConnection::class); |
| 56 | + $this->resourceMock->method('getConnection')->willReturn($this->connectionMock); |
| 57 | + $this->resourceMock->method('getTableName')->willReturnArgument(0); |
| 58 | + |
| 59 | + $this->generatorMock = $this->createMock(Generator::class); |
| 60 | + |
| 61 | + $this->imageModel = $objectManager->getObject( |
| 62 | + Image::class, |
| 63 | + [ |
| 64 | + 'generator' => $this->generatorMock, |
| 65 | + 'resourceConnection' => $this->resourceMock, |
| 66 | + 'batchSize' => $this->batchSize |
| 67 | + ] |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * @return \PHPUnit_Framework_MockObject_MockObject |
| 73 | + */ |
| 74 | + protected function getVisibleImagesSelectMock(): \PHPUnit_Framework_MockObject_MockObject |
| 75 | + { |
| 76 | + $selectMock = $this->getMockBuilder(Select::class) |
| 77 | + ->disableOriginalConstructor() |
| 78 | + ->getMock(); |
| 79 | + $selectMock->expects($this->once()) |
| 80 | + ->method('distinct') |
| 81 | + ->willReturnSelf(); |
| 82 | + $selectMock->expects($this->once()) |
| 83 | + ->method('from') |
| 84 | + ->with( |
| 85 | + ['images' => Gallery::GALLERY_TABLE], |
| 86 | + 'value as filepath' |
| 87 | + )->willReturnSelf(); |
| 88 | + $selectMock->expects($this->once()) |
| 89 | + ->method('where') |
| 90 | + ->with('disabled = 0') |
| 91 | + ->willReturnSelf(); |
| 92 | + |
| 93 | + return $selectMock; |
| 94 | + } |
| 95 | + |
| 96 | + public function testGetCountAllProductImages(): void |
| 97 | + { |
| 98 | + $selectMock = $this->getVisibleImagesSelectMock(); |
| 99 | + $selectMock->expects($this->exactly(2)) |
| 100 | + ->method('reset') |
| 101 | + ->withConsecutive( |
| 102 | + ['columns'], |
| 103 | + ['distinct'] |
| 104 | + )->willReturnSelf(); |
| 105 | + $selectMock->expects($this->once()) |
| 106 | + ->method('columns') |
| 107 | + ->with(new \Zend_Db_Expr('count(distinct value)')) |
| 108 | + ->willReturnSelf(); |
| 109 | + |
| 110 | + $this->connectionMock->expects($this->once()) |
| 111 | + ->method('select') |
| 112 | + ->willReturn($selectMock); |
| 113 | + $this->connectionMock->expects($this->once()) |
| 114 | + ->method('fetchOne') |
| 115 | + ->with($selectMock) |
| 116 | + ->willReturn($this->imagesCount); |
| 117 | + |
| 118 | + $this->assertSame($this->imagesCount, $this->imageModel->getCountAllProductImages()); |
| 119 | + } |
| 120 | + |
| 121 | + public function testGetAllProductImages(): void |
| 122 | + { |
| 123 | + $getBatchIteratorMock = function ($selectMock, $imagesCount, $batchSize): array { |
| 124 | + $result = []; |
| 125 | + $count = $imagesCount / $batchSize; |
| 126 | + while ($count) { |
| 127 | + $count--; |
| 128 | + $result[$count] = $selectMock; |
| 129 | + } |
| 130 | + |
| 131 | + return $result; |
| 132 | + }; |
| 133 | + |
| 134 | + $getAllProductImagesSelectFetchResults = function ($batchSize): array { |
| 135 | + $result = []; |
| 136 | + $count = $batchSize; |
| 137 | + while ($count) { |
| 138 | + $count--; |
| 139 | + $result[$count] = $count; |
| 140 | + } |
| 141 | + |
| 142 | + return $result; |
| 143 | + }; |
| 144 | + |
| 145 | + $this->connectionMock->expects($this->once()) |
| 146 | + ->method('select') |
| 147 | + ->willReturn($this->getVisibleImagesSelectMock()); |
| 148 | + |
| 149 | + $fetchResult = $getAllProductImagesSelectFetchResults($this->batchSize); |
| 150 | + $this->connectionMock->expects($this->exactly($this->imagesCount / $this->batchSize)) |
| 151 | + ->method('fetchAll') |
| 152 | + ->willReturn($fetchResult); |
| 153 | + |
| 154 | + /** @var Select | \PHPUnit_Framework_MockObject_MockObject $selectMock */ |
| 155 | + $selectMock = $this->getMockBuilder(Select::class) |
| 156 | + ->disableOriginalConstructor() |
| 157 | + ->getMock(); |
| 158 | + |
| 159 | + $batchIteratorMock = $getBatchIteratorMock($selectMock, $this->imagesCount, $this->batchSize); |
| 160 | + $this->generatorMock->expects($this->once()) |
| 161 | + ->method('generate') |
| 162 | + ->with( |
| 163 | + 'value_id', |
| 164 | + $selectMock, |
| 165 | + $this->batchSize, |
| 166 | + \Magento\Framework\DB\Query\BatchIteratorInterface::NON_UNIQUE_FIELD_ITERATOR |
| 167 | + )->willReturn($batchIteratorMock); |
| 168 | + |
| 169 | + $this->assertCount($this->imagesCount, $this->imageModel->getAllProductImages()); |
| 170 | + } |
| 171 | +} |
0 commit comments