|
| 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\System\Config\Backend\Rss; |
| 9 | + |
| 10 | +use Magento\Catalog\Api\Data\ProductAttributeInterface; |
| 11 | +use Magento\Catalog\Api\ProductAttributeRepositoryInterface; |
| 12 | +use Magento\Catalog\Model\System\Config\Backend\Rss\Category; |
| 13 | +use Magento\Framework\App\Cache\TypeListInterface; |
| 14 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 15 | +use Magento\Framework\Data\Collection\AbstractDb; |
| 16 | +use Magento\Framework\Event\ManagerInterface as EventManager; |
| 17 | +use Magento\Framework\Model\Context; |
| 18 | +use Magento\Framework\Model\ResourceModel\AbstractResource; |
| 19 | +use Magento\Framework\Registry; |
| 20 | +use PHPUnit\Framework\MockObject\MockObject; |
| 21 | +use PHPUnit\Framework\TestCase; |
| 22 | + |
| 23 | +class CategoryTest extends TestCase |
| 24 | +{ |
| 25 | + /** |
| 26 | + * @var ScopeConfigInterface|MockObject |
| 27 | + */ |
| 28 | + private $configMock; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var ProductAttributeRepositoryInterface|MockObject |
| 32 | + */ |
| 33 | + private $productAttributeRepositoryMock; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var Category |
| 37 | + */ |
| 38 | + private $model; |
| 39 | + |
| 40 | + protected function setUp(): void |
| 41 | + { |
| 42 | + $contextMock = $this->createMock(Context::class); |
| 43 | + $eventManagerMock = $this->createMock(EventManager::class); |
| 44 | + $contextMock->method('getEventDispatcher') |
| 45 | + ->willReturn($eventManagerMock); |
| 46 | + $registryMock = $this->createMock(Registry::class); |
| 47 | + $this->configMock = $this->createMock(ScopeConfigInterface::class); |
| 48 | + $cacheTypeListMock = $this->createMock(TypeListInterface::class); |
| 49 | + $resourceMock = $this->createMock(AbstractResource::class); |
| 50 | + $resourceCollectionMock = $this->createMock(AbstractDb::class); |
| 51 | + $this->productAttributeRepositoryMock = $this->createMock(ProductAttributeRepositoryInterface::class); |
| 52 | + $this->model = new Category( |
| 53 | + $contextMock, |
| 54 | + $registryMock, |
| 55 | + $this->configMock, |
| 56 | + $cacheTypeListMock, |
| 57 | + $resourceMock, |
| 58 | + $resourceCollectionMock, |
| 59 | + ['path' => 'rss/catalog/category'], |
| 60 | + $this->productAttributeRepositoryMock |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @dataProvider afterSaveDataProvider |
| 66 | + * @param string $oldValue |
| 67 | + * @param string $newValue |
| 68 | + * @param bool $isUsedForSort |
| 69 | + * @param bool $isUpdateNeeded |
| 70 | + */ |
| 71 | + public function testAfterSave(string $oldValue, string $newValue, bool $isUsedForSort, bool $isUpdateNeeded): void |
| 72 | + { |
| 73 | + $this->configMock->expects($this->atLeastOnce()) |
| 74 | + ->method('getValue') |
| 75 | + ->with('rss/catalog/category', 'default', null) |
| 76 | + ->willReturn($oldValue); |
| 77 | + |
| 78 | + $productAttributeMock = $this->createMock(ProductAttributeInterface::class); |
| 79 | + $productAttributeMock->method('getUsedForSortBy') |
| 80 | + ->willReturn($isUsedForSort); |
| 81 | + $this->productAttributeRepositoryMock->method('get') |
| 82 | + ->with('updated_at') |
| 83 | + ->willReturn($productAttributeMock); |
| 84 | + |
| 85 | + $productAttributeMock->expects($this->exactly((int) $isUpdateNeeded)) |
| 86 | + ->method('setUsedForSortBy') |
| 87 | + ->with(true) |
| 88 | + ->willReturnSelf(); |
| 89 | + $this->productAttributeRepositoryMock->expects($this->exactly((int) $isUpdateNeeded)) |
| 90 | + ->method('save') |
| 91 | + ->with($productAttributeMock) |
| 92 | + ->willReturn($productAttributeMock); |
| 93 | + |
| 94 | + $this->model->setValue($newValue); |
| 95 | + $this->model->afterSave(); |
| 96 | + } |
| 97 | + |
| 98 | + public function afterSaveDataProvider(): array |
| 99 | + { |
| 100 | + return [ |
| 101 | + ['0', '1', false, true], |
| 102 | + ['0', '0', false, false], |
| 103 | + ['1', '0', false, false], |
| 104 | + ['0', '1', true, false], |
| 105 | + ]; |
| 106 | + } |
| 107 | +} |
0 commit comments