|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Magento\RemoteStorage\Test\Unit\Setup; |
| 8 | + |
| 9 | +use Magento\Framework\App\DeploymentConfig; |
| 10 | +use Magento\Framework\Exception\LocalizedException; |
| 11 | +use Magento\RemoteStorage\Driver\DriverFactoryPool; |
| 12 | +use Magento\RemoteStorage\Driver\RemoteDriverInterface; |
| 13 | +use Magento\RemoteStorage\Setup\ConfigOptionsList; |
| 14 | +use Magento\RemoteStorage\Driver\DriverFactoryInterface; |
| 15 | +use PHPUnit\Framework\MockObject\MockObject; |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | +use Psr\Log\LoggerInterface; |
| 18 | + |
| 19 | +class ConfigOptionsListTest extends TestCase |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @var DriverFactoryPool|MockObject |
| 23 | + */ |
| 24 | + private $driverFactoryPoolMock; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var LoggerInterface|MockObject |
| 28 | + */ |
| 29 | + private $loggerMock; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var ConfigOptionsList |
| 33 | + */ |
| 34 | + private $configOptionsList; |
| 35 | + |
| 36 | + /** |
| 37 | + * @return void |
| 38 | + * @throws \Magento\Framework\Exception\FileSystemException |
| 39 | + */ |
| 40 | + protected function setUp(): void |
| 41 | + { |
| 42 | + $this->driverFactoryPoolMock = $this->getMockBuilder(DriverFactoryPool::class) |
| 43 | + ->disableOriginalConstructor() |
| 44 | + ->getMock(); |
| 45 | + |
| 46 | + $this->loggerMock = $this->getMockBuilder(LoggerInterface::class) |
| 47 | + ->disableOriginalConstructor() |
| 48 | + ->getMock(); |
| 49 | + |
| 50 | + $this->configOptionsList = new ConfigOptionsList( |
| 51 | + $this->driverFactoryPoolMock, |
| 52 | + $this->loggerMock |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @param array $input |
| 58 | + * @param bool $isDeploymentConfigExists |
| 59 | + * @param array $expectedOutput |
| 60 | + * @dataProvider validateDataProvider |
| 61 | + */ |
| 62 | + public function testValidate(array $input, bool $isDeploymentConfigExists, array $expectedOutput) |
| 63 | + { |
| 64 | + $deploymentConfigMock = $this->getMockBuilder(DeploymentConfig::class) |
| 65 | + ->disableOriginalConstructor() |
| 66 | + ->getMock(); |
| 67 | + |
| 68 | + $deploymentConfigMock |
| 69 | + ->expects(static::once()) |
| 70 | + ->method('getConfigData') |
| 71 | + ->willReturn($isDeploymentConfigExists); |
| 72 | + |
| 73 | + $isConnectionToBeTested = $isDeploymentConfigExists && isset( |
| 74 | + $input['remote-storage-region'], |
| 75 | + $input['remote-storage-bucket'] |
| 76 | + ); |
| 77 | + |
| 78 | + if ($isConnectionToBeTested) { |
| 79 | + $driverFactoryMock = $this->getMockBuilder(DriverFactoryInterface::class) |
| 80 | + ->disableOriginalConstructor() |
| 81 | + ->getMock(); |
| 82 | + |
| 83 | + $this->driverFactoryPoolMock |
| 84 | + ->expects(static::once()) |
| 85 | + ->method('get') |
| 86 | + ->with($input['remote-storage-driver']) |
| 87 | + ->willReturn($driverFactoryMock); |
| 88 | + |
| 89 | + $remoteDriverMock = $this->getMockBuilder(RemoteDriverInterface::class) |
| 90 | + ->disableOriginalConstructor() |
| 91 | + ->getMock(); |
| 92 | + |
| 93 | + $driverFactoryMock |
| 94 | + ->expects(static::once()) |
| 95 | + ->method('createConfigured') |
| 96 | + ->willReturn($remoteDriverMock); |
| 97 | + |
| 98 | + $testMethodExpectation = $remoteDriverMock->expects(static::once())->method('test'); |
| 99 | + |
| 100 | + $isExceptionExpectedToBeCaught = (bool) count($expectedOutput); |
| 101 | + |
| 102 | + if ($isExceptionExpectedToBeCaught) { |
| 103 | + $adapterErrorMessage = str_replace('Adapter error: ', '', $expectedOutput[0]); |
| 104 | + $exception = new LocalizedException(__($adapterErrorMessage)); |
| 105 | + $testMethodExpectation->willThrowException($exception); |
| 106 | + $this->loggerMock->expects(static::once())->method('critical')->with($exception->getMessage()); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + $this->assertEquals( |
| 111 | + $expectedOutput, |
| 112 | + $this->configOptionsList->validate($input, $deploymentConfigMock) |
| 113 | + ); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * @return array |
| 118 | + */ |
| 119 | + public function validateDataProvider() |
| 120 | + { |
| 121 | + return [ |
| 122 | + 'Local File Storage Before Deployment Config Exists' => [ |
| 123 | + [], false, [], |
| 124 | + ], |
| 125 | + 'Local File Storage After Deployment Config Exists' => [ |
| 126 | + [], true, [], |
| 127 | + ], |
| 128 | + 'Remote Storage Before Deployment Config Exists' => [ |
| 129 | + [ |
| 130 | + 'remote-storage-driver' => 'aws-s3', |
| 131 | + 'remote-storage-region' => 'us-east-1', |
| 132 | + 'remote-storage-bucket' => 'bucket1', |
| 133 | + ], |
| 134 | + false, |
| 135 | + [], |
| 136 | + ], |
| 137 | + 'Remote Storage Missing Region' => [ |
| 138 | + [ |
| 139 | + 'remote-storage-driver' => 'aws-s3', |
| 140 | + 'remote-storage-bucket' => 'bucket1', |
| 141 | + ], |
| 142 | + true, |
| 143 | + [ |
| 144 | + 'Region is required', |
| 145 | + ], |
| 146 | + ], |
| 147 | + 'Remote Storage Missing Bucket' => [ |
| 148 | + [ |
| 149 | + 'remote-storage-driver' => 'aws-s3', |
| 150 | + 'remote-storage-region' => 'us-east-1', |
| 151 | + ], |
| 152 | + true, |
| 153 | + [ |
| 154 | + 'Bucket is required', |
| 155 | + ], |
| 156 | + ], |
| 157 | + 'Remote Storage Missing Region and Bucket' => [ |
| 158 | + [ |
| 159 | + 'remote-storage-driver' => 'aws-s3', |
| 160 | + ], |
| 161 | + true, |
| 162 | + [ |
| 163 | + 'Region is required', |
| 164 | + 'Bucket is required', |
| 165 | + ], |
| 166 | + ], |
| 167 | + 'Valid Remote Storage Config with Successful Test Connection' => [ |
| 168 | + [ |
| 169 | + 'remote-storage-driver' => 'aws-s3', |
| 170 | + 'remote-storage-region' => 'us-east-1', |
| 171 | + 'remote-storage-bucket' => 'bucket1', |
| 172 | + 'remote-storage-prefix' => '', |
| 173 | + ], |
| 174 | + true, |
| 175 | + [], |
| 176 | + ], |
| 177 | + 'Valid Remote Storage With Unsuccessful Test Connection' => [ |
| 178 | + [ |
| 179 | + 'remote-storage-driver' => 'aws-s3', |
| 180 | + 'remote-storage-region' => 'us-east-1', |
| 181 | + 'remote-storage-bucket' => 'bucket1', |
| 182 | + 'remote-storage-prefix' => '', |
| 183 | + ], |
| 184 | + true, |
| 185 | + [ |
| 186 | + 'Adapter error: [Message from LocalizedException]', |
| 187 | + ] |
| 188 | + ], |
| 189 | + ]; |
| 190 | + } |
| 191 | +} |
0 commit comments