|
| 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\ConfigurableProduct\Test\Unit\Ui\DataProvider; |
| 9 | + |
| 10 | +use Magento\Catalog\Model\Product\Type; |
| 11 | +use Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection; |
| 12 | +use Magento\ConfigurableProduct\Model\ConfigurableAttributeHandler; |
| 13 | +use Magento\ConfigurableProduct\Model\Product\Type\Configurable; |
| 14 | +use Magento\ConfigurableProduct\Ui\DataProvider\Attributes; |
| 15 | +use Magento\Framework\DataObject; |
| 16 | +use Magento\Framework\DB\Select; |
| 17 | +use PHPUnit\Framework\TestCase; |
| 18 | + |
| 19 | +class AttributesTest extends TestCase |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @var Collection |
| 23 | + */ |
| 24 | + private Collection $collectionMock; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var Select |
| 28 | + */ |
| 29 | + private Select $selectMock; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var Attributes |
| 33 | + */ |
| 34 | + private Attributes $attributes; |
| 35 | + |
| 36 | + /** |
| 37 | + * @return void |
| 38 | + */ |
| 39 | + protected function setUp(): void |
| 40 | + { |
| 41 | + $this->collectionMock = $this->getMockBuilder(Collection::class) |
| 42 | + ->disableOriginalConstructor() |
| 43 | + ->getMock(); |
| 44 | + $this->selectMock = $this->getMockBuilder(Select::class) |
| 45 | + ->disableOriginalConstructor() |
| 46 | + ->onlyMethods(['where']) |
| 47 | + ->getMock(); |
| 48 | + $collectionAttributeHandlerMock = $this->getMockBuilder(ConfigurableAttributeHandler::class) |
| 49 | + ->disableOriginalConstructor() |
| 50 | + ->onlyMethods(['getApplicableAttributes']) |
| 51 | + ->getMock(); |
| 52 | + $collectionAttributeHandlerMock->expects($this->once()) |
| 53 | + ->method('getApplicableAttributes') |
| 54 | + ->willReturn($this->collectionMock); |
| 55 | + $this->attributes = new Attributes( |
| 56 | + 'myName', |
| 57 | + 'myPrimaryFieldName', |
| 58 | + 'myRequestFieldName', |
| 59 | + $collectionAttributeHandlerMock |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * @return void |
| 65 | + */ |
| 66 | + public function testGetData() |
| 67 | + { |
| 68 | + $expectedResult = [ |
| 69 | + 'totalRecords' => 1, |
| 70 | + 'items' => [ |
| 71 | + 0 => ['attribute' => 'color'] |
| 72 | + ] |
| 73 | + ]; |
| 74 | + $this->collectionMock->expects($this->once()) |
| 75 | + ->method('getSelect') |
| 76 | + ->willReturn($this->selectMock); |
| 77 | + $this->selectMock->expects($this->once()) |
| 78 | + ->method('where') |
| 79 | + ->with('(`apply_to` IS NULL) OR |
| 80 | + ( |
| 81 | + FIND_IN_SET(' . |
| 82 | + sprintf("'%s'", Type::TYPE_SIMPLE) . ', |
| 83 | + `apply_to` |
| 84 | + ) AND |
| 85 | + FIND_IN_SET(' . |
| 86 | + sprintf("'%s'", Type::TYPE_VIRTUAL) . ', |
| 87 | + `apply_to` |
| 88 | + ) AND |
| 89 | + FIND_IN_SET(' . |
| 90 | + sprintf("'%s'", Configurable::TYPE_CODE) . ', |
| 91 | + `apply_to` |
| 92 | + ) |
| 93 | + )') |
| 94 | + ->willReturnSelf(); |
| 95 | + $this->collectionMock->expects($this->once()) |
| 96 | + ->method('getItems') |
| 97 | + ->willReturn([new DataObject(['attribute' => 'color'])]); |
| 98 | + $this->collectionMock->expects($this->once()) |
| 99 | + ->method('getSize') |
| 100 | + ->willReturn(1); |
| 101 | + $this->assertEquals($expectedResult, $this->attributes->getData()); |
| 102 | + } |
| 103 | +} |
0 commit comments