|
| 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\Wishlist\Test\Unit\Model\Product; |
| 9 | + |
| 10 | +use Magento\Catalog\Model\Product; |
| 11 | +use Magento\Catalog\Model\ResourceModel\Product\Collection; |
| 12 | +use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory; |
| 13 | +use Magento\Framework\DB\Adapter\AdapterInterface; |
| 14 | +use Magento\Wishlist\Model\Product\AttributeValueProvider; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | +use PHPUnit_Framework_MockObject_MockObject; |
| 17 | + |
| 18 | +/** |
| 19 | + * AttributeValueProviderTest |
| 20 | + */ |
| 21 | +class AttributeValueProviderTest extends TestCase |
| 22 | +{ |
| 23 | + /** |
| 24 | + * @var AttributeValueProvider|PHPUnit_Framework_MockObject_MockObject |
| 25 | + */ |
| 26 | + private $attributeValueProvider; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var CollectionFactory|PHPUnit_Framework_MockObject_MockObject |
| 30 | + */ |
| 31 | + private $productCollectionFactoryMock; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var Product|PHPUnit_Framework_MockObject_MockObject |
| 35 | + */ |
| 36 | + private $productMock; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var AdapterInterface|PHPUnit_Framework_MockObject_MockObject |
| 40 | + */ |
| 41 | + private $connectionMock; |
| 42 | + |
| 43 | + /** |
| 44 | + * Set Up |
| 45 | + * |
| 46 | + * @return void |
| 47 | + */ |
| 48 | + protected function setUp() |
| 49 | + { |
| 50 | + $this->productCollectionFactoryMock = $this->createPartialMock( |
| 51 | + CollectionFactory::class, |
| 52 | + ['create'] |
| 53 | + ); |
| 54 | + $this->attributeValueProvider = new AttributeValueProvider( |
| 55 | + $this->productCollectionFactoryMock |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Get attribute text when the flat table is disabled |
| 61 | + * |
| 62 | + * @param int $productId |
| 63 | + * @param string $attributeCode |
| 64 | + * @param string $attributeText |
| 65 | + * @return void |
| 66 | + * @dataProvider attributeDataProvider |
| 67 | + */ |
| 68 | + public function testGetAttributeTextWhenFlatIsDisabled(int $productId, string $attributeCode, string $attributeText) |
| 69 | + { |
| 70 | + $this->productMock = $this->getMockBuilder(Product::class) |
| 71 | + ->disableOriginalConstructor() |
| 72 | + ->setMethods(['getData']) |
| 73 | + ->getMock(); |
| 74 | + |
| 75 | + $this->productMock->expects($this->any()) |
| 76 | + ->method('getData') |
| 77 | + ->with($attributeCode) |
| 78 | + ->willReturn($attributeText); |
| 79 | + |
| 80 | + $productCollection = $this->getMockBuilder(Collection::class) |
| 81 | + ->disableOriginalConstructor() |
| 82 | + ->setMethods([ |
| 83 | + 'addIdFilter', 'addStoreFilter', 'addAttributeToSelect', 'isEnabledFlat', 'getFirstItem' |
| 84 | + ])->getMock(); |
| 85 | + |
| 86 | + $productCollection->expects($this->any()) |
| 87 | + ->method('addIdFilter') |
| 88 | + ->willReturnSelf(); |
| 89 | + $productCollection->expects($this->any()) |
| 90 | + ->method('addStoreFilter') |
| 91 | + ->willReturnSelf(); |
| 92 | + $productCollection->expects($this->any()) |
| 93 | + ->method('addAttributeToSelect') |
| 94 | + ->willReturnSelf(); |
| 95 | + $productCollection->expects($this->any()) |
| 96 | + ->method('isEnabledFlat') |
| 97 | + ->willReturn(false); |
| 98 | + $productCollection->expects($this->any()) |
| 99 | + ->method('getFirstItem') |
| 100 | + ->willReturn($this->productMock); |
| 101 | + |
| 102 | + $this->productCollectionFactoryMock->expects($this->atLeastOnce()) |
| 103 | + ->method('create') |
| 104 | + ->willReturn($productCollection); |
| 105 | + |
| 106 | + $actual = $this->attributeValueProvider->getRawAttributeValue($productId, $attributeCode); |
| 107 | + |
| 108 | + $this->assertEquals($attributeText, $actual); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Get attribute text when the flat table is enabled |
| 113 | + * |
| 114 | + * @dataProvider attributeDataProvider |
| 115 | + * @param int $productId |
| 116 | + * @param string $attributeCode |
| 117 | + * @param string $attributeText |
| 118 | + * @return void |
| 119 | + */ |
| 120 | + public function testGetAttributeTextWhenFlatIsEnabled(int $productId, string $attributeCode, string $attributeText) |
| 121 | + { |
| 122 | + $this->connectionMock = $this->getMockBuilder(AdapterInterface::class)->getMockForAbstractClass(); |
| 123 | + $this->connectionMock->expects($this->any()) |
| 124 | + ->method('fetchRow') |
| 125 | + ->willReturn([ |
| 126 | + $attributeCode => $attributeText |
| 127 | + ]); |
| 128 | + $this->productMock = $this->getMockBuilder(Product::class) |
| 129 | + ->disableOriginalConstructor() |
| 130 | + ->setMethods(['getData']) |
| 131 | + ->getMock(); |
| 132 | + $this->productMock->expects($this->any()) |
| 133 | + ->method('getData') |
| 134 | + ->with($attributeCode) |
| 135 | + ->willReturn($attributeText); |
| 136 | + |
| 137 | + $productCollection = $this->getMockBuilder(Collection::class) |
| 138 | + ->disableOriginalConstructor() |
| 139 | + ->setMethods([ |
| 140 | + 'addIdFilter', 'addStoreFilter', 'addAttributeToSelect', 'isEnabledFlat', 'getConnection' |
| 141 | + ])->getMock(); |
| 142 | + |
| 143 | + $productCollection->expects($this->any()) |
| 144 | + ->method('addIdFilter') |
| 145 | + ->willReturnSelf(); |
| 146 | + $productCollection->expects($this->any()) |
| 147 | + ->method('addStoreFilter') |
| 148 | + ->willReturnSelf(); |
| 149 | + $productCollection->expects($this->any()) |
| 150 | + ->method('addAttributeToSelect') |
| 151 | + ->willReturnSelf(); |
| 152 | + $productCollection->expects($this->any()) |
| 153 | + ->method('isEnabledFlat') |
| 154 | + ->willReturn(true); |
| 155 | + $productCollection->expects($this->any()) |
| 156 | + ->method('getConnection') |
| 157 | + ->willReturn($this->connectionMock); |
| 158 | + |
| 159 | + $this->productCollectionFactoryMock->expects($this->atLeastOnce()) |
| 160 | + ->method('create') |
| 161 | + ->willReturn($productCollection); |
| 162 | + |
| 163 | + $actual = $this->attributeValueProvider->getRawAttributeValue($productId, $attributeCode); |
| 164 | + |
| 165 | + $this->assertEquals($attributeText, $actual); |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * @return array |
| 170 | + */ |
| 171 | + public function attributeDataProvider(): array |
| 172 | + { |
| 173 | + return [ |
| 174 | + [1, 'attribute_code', 'Attribute Text'] |
| 175 | + ]; |
| 176 | + } |
| 177 | +} |
0 commit comments