|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2025 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\MediaGalleryRenditions\Test\Unit\Model; |
| 9 | + |
| 10 | +use Magento\Framework\App\Config\Initial; |
| 11 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 12 | +use Magento\Framework\App\ResourceConnection; |
| 13 | +use Magento\Framework\DB\Adapter\AdapterInterface; |
| 14 | +use Magento\Framework\DB\Select; |
| 15 | +use Magento\MediaGalleryRenditions\Model\Config; |
| 16 | +use PHPUnit\Framework\MockObject\MockObject; |
| 17 | +use PHPUnit\Framework\TestCase; |
| 18 | + |
| 19 | +class ConfigTest extends TestCase |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @var ScopeConfigInterface|MockObject |
| 23 | + */ |
| 24 | + private $scopeConfigMock; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var Initial|MockObject |
| 28 | + */ |
| 29 | + private $initialConfigMock; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var ResourceConnection|MockObject |
| 33 | + */ |
| 34 | + private $resourceConnectionMock; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var Config |
| 38 | + */ |
| 39 | + private $config; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var AdapterInterface|MockObject |
| 43 | + */ |
| 44 | + private $connectionMock; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var Select|MockObject |
| 48 | + */ |
| 49 | + private $selectMock; |
| 50 | + |
| 51 | + protected function setUp(): void |
| 52 | + { |
| 53 | + $this->scopeConfigMock = $this->createMock(ScopeConfigInterface::class); |
| 54 | + $this->initialConfigMock = $this->createMock(Initial::class); |
| 55 | + $this->resourceConnectionMock = $this->createMock(ResourceConnection::class); |
| 56 | + $this->connectionMock = $this->getMockBuilder(AdapterInterface::class) |
| 57 | + ->addMethods(['fetchColumn']) |
| 58 | + ->getMockForAbstractClass(); |
| 59 | + $this->selectMock = $this->createMock(Select::class); |
| 60 | + $this->resourceConnectionMock->method('getConnection') |
| 61 | + ->willReturn($this->connectionMock); |
| 62 | + $this->resourceConnectionMock->method('getTableName') |
| 63 | + ->with('core_config_data') |
| 64 | + ->willReturn('core_config_data'); |
| 65 | + $this->config = new Config( |
| 66 | + $this->scopeConfigMock, |
| 67 | + $this->initialConfigMock, |
| 68 | + $this->resourceConnectionMock |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Test getWidth() with successful database retrieval |
| 74 | + */ |
| 75 | + public function testGetWidthSuccess(): void |
| 76 | + { |
| 77 | + $expectedWidth = 800; |
| 78 | + $this->connectionMock->method('select') |
| 79 | + ->willReturn($this->selectMock); |
| 80 | + $this->selectMock->method('from') |
| 81 | + ->willReturnSelf(); |
| 82 | + $this->selectMock->method('where') |
| 83 | + ->willReturnSelf(); |
| 84 | + $this->connectionMock->method('query') |
| 85 | + ->with($this->selectMock) |
| 86 | + ->willReturnSelf(); |
| 87 | + $this->connectionMock->method('fetchColumn') |
| 88 | + ->willReturn((string)$expectedWidth); |
| 89 | + $result = $this->config->getWidth(); |
| 90 | + $this->assertEquals($expectedWidth, $result); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Test getWidth() with empty database result falling back to initial config |
| 95 | + */ |
| 96 | + public function testGetWidthFallback(): void |
| 97 | + { |
| 98 | + $expectedWidth = 600; |
| 99 | + $this->connectionMock->method('select') |
| 100 | + ->willReturn($this->selectMock); |
| 101 | + $this->selectMock->method('from') |
| 102 | + ->willReturnSelf(); |
| 103 | + $this->selectMock->method('where') |
| 104 | + ->willReturnSelf(); |
| 105 | + $this->connectionMock->method('query') |
| 106 | + ->with($this->selectMock) |
| 107 | + ->willReturnSelf(); |
| 108 | + $this->connectionMock->method('fetchColumn') |
| 109 | + ->willReturn(false); |
| 110 | + $this->initialConfigMock->method('getData') |
| 111 | + ->with('default') |
| 112 | + ->willReturn([ |
| 113 | + 'system' => [ |
| 114 | + 'media_gallery_renditions' => [ |
| 115 | + 'width' => $expectedWidth |
| 116 | + ] |
| 117 | + ] |
| 118 | + ]); |
| 119 | + $result = $this->config->getWidth(); |
| 120 | + $this->assertEquals($expectedWidth, $result); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Test getHeight() with successful database retrieval |
| 125 | + */ |
| 126 | + public function testGetHeightSuccess(): void |
| 127 | + { |
| 128 | + $expectedHeight = 600; |
| 129 | + $this->connectionMock->method('select') |
| 130 | + ->willReturn($this->selectMock); |
| 131 | + $this->selectMock->method('from') |
| 132 | + ->willReturnSelf(); |
| 133 | + $this->selectMock->method('where') |
| 134 | + ->willReturnSelf(); |
| 135 | + $this->connectionMock->method('query') |
| 136 | + ->with($this->selectMock) |
| 137 | + ->willReturnSelf(); |
| 138 | + $this->connectionMock->method('fetchColumn') |
| 139 | + ->willReturn((string)$expectedHeight); |
| 140 | + $result = $this->config->getHeight(); |
| 141 | + $this->assertEquals($expectedHeight, $result); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Test getHeight() with empty database result falling back to initial config |
| 146 | + */ |
| 147 | + public function testGetHeightFallback(): void |
| 148 | + { |
| 149 | + $expectedHeight = 400; |
| 150 | + $this->connectionMock->method('select') |
| 151 | + ->willReturn($this->selectMock); |
| 152 | + $this->selectMock->method('from') |
| 153 | + ->willReturnSelf(); |
| 154 | + $this->selectMock->method('where') |
| 155 | + ->willReturnSelf(); |
| 156 | + $this->connectionMock->method('query') |
| 157 | + ->with($this->selectMock) |
| 158 | + ->willReturnSelf(); |
| 159 | + $this->connectionMock->method('fetchColumn') |
| 160 | + ->willReturn(null); |
| 161 | + $this->initialConfigMock->method('getData') |
| 162 | + ->with('default') |
| 163 | + ->willReturn([ |
| 164 | + 'system' => [ |
| 165 | + 'media_gallery_renditions' => [ |
| 166 | + 'height' => $expectedHeight |
| 167 | + ] |
| 168 | + ] |
| 169 | + ]); |
| 170 | + $result = $this->config->getHeight(); |
| 171 | + $this->assertEquals($expectedHeight, $result); |
| 172 | + } |
| 173 | +} |
0 commit comments