|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Magento\Catalog\Test\Unit\Model\ResourceModel; |
| 8 | + |
| 9 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 10 | + |
| 11 | +/** |
| 12 | + * Test for Magento\Catalog\Model\ResourceModel\Config |
| 13 | + */ |
| 14 | +class ConfigTest extends \PHPUnit\Framework\TestCase |
| 15 | +{ |
| 16 | + /** |
| 17 | + * @var \Magento\Catalog\Model\ResourceModel\Config |
| 18 | + */ |
| 19 | + private $model; |
| 20 | + |
| 21 | + /** |
| 22 | + * @var \PHPUnit_Framework_MockObject_MockObject |
| 23 | + */ |
| 24 | + private $resource; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var \PHPUnit_Framework_MockObject_MockObject |
| 28 | + */ |
| 29 | + private $storeManager; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var \PHPUnit_Framework_MockObject_MockObject |
| 33 | + */ |
| 34 | + private $eavConfig; |
| 35 | + |
| 36 | + protected function setUp() |
| 37 | + { |
| 38 | + $objectManager = new ObjectManager($this); |
| 39 | + |
| 40 | + $this->resource = $this->createMock(\Magento\Framework\App\ResourceConnection::class); |
| 41 | + $this->storeManager = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class); |
| 42 | + $this->eavConfig = $this->createMock(\Magento\Eav\Model\Config::class); |
| 43 | + |
| 44 | + $this->model = $objectManager->getObject( |
| 45 | + \Magento\Catalog\Model\ResourceModel\Config::class, |
| 46 | + [ |
| 47 | + 'resource' => $this->resource, |
| 48 | + 'storeManager' => $this->storeManager, |
| 49 | + 'eavConfig' => $this->eavConfig, |
| 50 | + ] |
| 51 | + ); |
| 52 | + |
| 53 | + parent::setUp(); |
| 54 | + } |
| 55 | + |
| 56 | + public function testGetAttributesUsedForSortBy() |
| 57 | + { |
| 58 | + $expression = 'someExpression'; |
| 59 | + $storeId = 1; |
| 60 | + $entityTypeId = 4; |
| 61 | + |
| 62 | + $connectionMock = $this->createMock(\Magento\Framework\DB\Adapter\AdapterInterface::class); |
| 63 | + $selectMock = $this->createMock(\Magento\Framework\DB\Select::class); |
| 64 | + $storeMock = $this->createMock(\Magento\Store\Api\Data\StoreInterface::class); |
| 65 | + $entityTypeMock = $this->createMock(\Magento\Eav\Model\Entity\Type::class); |
| 66 | + |
| 67 | + $this->resource->expects($this->atLeastOnce())->method('getConnection')->willReturn($connectionMock); |
| 68 | + |
| 69 | + $connectionMock->expects($this->once())->method('getCheckSql') |
| 70 | + ->with('al.value IS NULL', 'main_table.frontend_label', 'al.value') |
| 71 | + ->willReturn($expression); |
| 72 | + $connectionMock->expects($this->atLeastOnce())->method('select')->willReturn($selectMock); |
| 73 | + |
| 74 | + $this->resource->expects($this->exactly(3))->method('getTableName')->withConsecutive( |
| 75 | + ['eav_attribute'], |
| 76 | + ['catalog_eav_attribute'], |
| 77 | + ['eav_attribute_label'] |
| 78 | + )->willReturnOnConsecutiveCalls('eav_attribute', 'catalog_eav_attribute', 'eav_attribute_label'); |
| 79 | + |
| 80 | + $this->storeManager->expects($this->once())->method('getStore')->willReturn($storeMock); |
| 81 | + $storeMock->expects($this->once())->method('getId')->willReturn($storeId); |
| 82 | + |
| 83 | + $this->eavConfig->expects($this->once())->method('getEntityType')->willReturn($entityTypeMock); |
| 84 | + $entityTypeMock->expects($this->once())->method('getId')->willReturn($entityTypeId); |
| 85 | + |
| 86 | + $selectMock->expects($this->once())->method('from') |
| 87 | + ->with(['main_table' => 'eav_attribute'])->willReturn($selectMock); |
| 88 | + $selectMock->expects($this->once())->method('join')->with( |
| 89 | + ['additional_table' => 'catalog_eav_attribute'], |
| 90 | + 'main_table.attribute_id = additional_table.attribute_id' |
| 91 | + )->willReturn($selectMock); |
| 92 | + $selectMock->expects($this->once())->method('joinLeft') |
| 93 | + ->with( |
| 94 | + ['al' => 'eav_attribute_label'], |
| 95 | + 'al.attribute_id = main_table.attribute_id AND al.store_id = ' . $storeId, |
| 96 | + ['store_label' => $expression] |
| 97 | + )->willReturn($selectMock); |
| 98 | + $selectMock->expects($this->exactly(2))->method('where')->withConsecutive( |
| 99 | + ['main_table.entity_type_id = ?', $entityTypeId], |
| 100 | + ['additional_table.used_for_sort_by = ?', 1] |
| 101 | + )->willReturn($selectMock); |
| 102 | + |
| 103 | + $connectionMock->expects($this->once())->method('fetchAll')->with($selectMock); |
| 104 | + |
| 105 | + $this->model->getAttributesUsedForSortBy(); |
| 106 | + } |
| 107 | +} |
0 commit comments