|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\MagentoCloud\Test\Unit\Config\Environment; |
| 7 | + |
| 8 | +use Magento\MagentoCloud\Config\Environment; |
| 9 | +use Magento\MagentoCloud\Config\Environment\Reader; |
| 10 | +use Magento\MagentoCloud\Filesystem\ConfigFileList; |
| 11 | +use Magento\MagentoCloud\Filesystem\Driver\File; |
| 12 | +use Magento\MagentoCloud\Filesystem\SystemList; |
| 13 | +use phpmock\phpunit\PHPMock; |
| 14 | +use PHPUnit\Framework\MockObject\MockObject; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | + |
| 17 | +/** |
| 18 | + * @inheritdoc |
| 19 | + */ |
| 20 | +class ReaderTest extends TestCase |
| 21 | +{ |
| 22 | + use PHPMock; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var SystemList|MockObject |
| 26 | + */ |
| 27 | + private $systemListMock; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var Environment|MockObject |
| 31 | + */ |
| 32 | + private $environmentMock; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var ConfigFileList|MockObject |
| 36 | + */ |
| 37 | + private $configFileListMock; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var File|MockObject |
| 41 | + */ |
| 42 | + private $fileMock; |
| 43 | + |
| 44 | + /** |
| 45 | + * @var Reader |
| 46 | + */ |
| 47 | + private $reader; |
| 48 | + |
| 49 | + /** |
| 50 | + * @inheritdoc |
| 51 | + */ |
| 52 | + protected function setUp() |
| 53 | + { |
| 54 | + /** |
| 55 | + * This lines are required for proper running of Magento\MagentoCloud\Test\Unit\Filesystem\Driver\FileTest |
| 56 | + */ |
| 57 | + self::defineFunctionMock('Magento\MagentoCloud\Filesystem\Driver', 'file_get_contents'); |
| 58 | + self::defineFunctionMock('Magento\MagentoCloud\Filesystem\Driver', 'file_exists'); |
| 59 | + |
| 60 | + $this->systemListMock = $this->createMock(SystemList::class); |
| 61 | + $this->environmentMock = $this->createMock(Environment::class); |
| 62 | + $this->configFileListMock = $this->createMock(ConfigFileList::class); |
| 63 | + $this->fileMock = $this->createPartialMock(File::class, []); |
| 64 | + |
| 65 | + $this->reader = new Reader( |
| 66 | + $this->systemListMock, |
| 67 | + $this->environmentMock, |
| 68 | + $this->configFileListMock, |
| 69 | + $this->fileMock |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + public function testRead() |
| 74 | + { |
| 75 | + $baseDir = __DIR__ . '/_file/'; |
| 76 | + |
| 77 | + $this->configFileListMock->expects($this->once()) |
| 78 | + ->method('getEnvConfig') |
| 79 | + ->willReturn($baseDir . '/.magento.env.yaml'); |
| 80 | + $this->systemListMock->expects($this->once()) |
| 81 | + ->method('getMagentoRoot') |
| 82 | + ->willReturn($baseDir); |
| 83 | + $this->environmentMock->expects($this->once()) |
| 84 | + ->method('getBranchName') |
| 85 | + ->willReturn('test-branch'); |
| 86 | + |
| 87 | + $this->reader->read(); |
| 88 | + $this->assertEquals( |
| 89 | + [ |
| 90 | + 'stage' => [ |
| 91 | + 'global' => ['SCD_ON_DEMAND' => false, 'UPDATE_URLS' => false], |
| 92 | + 'deploy' => ['DATABASE_CONFIGURATION' => ['host' => 'localhost'], 'SCD_THREADS' => 3], |
| 93 | + 'build' => ['SCD_THREADS' => 2], |
| 94 | + ], |
| 95 | + 'log' => [ |
| 96 | + 'gelf' => [ |
| 97 | + 'min_level' => 'info', |
| 98 | + 'use_default_formatter' => true, |
| 99 | + 'additional' => ['project' => 'project'], |
| 100 | + ], |
| 101 | + 'syslog' => ['ident' => 'ident-branch', 'facility' => 7], |
| 102 | + ], |
| 103 | + ], |
| 104 | + $this->reader->read() |
| 105 | + ); |
| 106 | + } |
| 107 | + |
| 108 | + public function testReadBranchConfigNotExists() |
| 109 | + { |
| 110 | + $baseDir = __DIR__ . '/_file/'; |
| 111 | + |
| 112 | + $this->configFileListMock->expects($this->once()) |
| 113 | + ->method('getEnvConfig') |
| 114 | + ->willReturn($baseDir . '/.magento.env.yaml'); |
| 115 | + $this->systemListMock->expects($this->once()) |
| 116 | + ->method('getMagentoRoot') |
| 117 | + ->willReturn($baseDir); |
| 118 | + $this->environmentMock->expects($this->once()) |
| 119 | + ->method('getBranchName') |
| 120 | + ->willReturn('not-exist'); |
| 121 | + |
| 122 | + $this->assertEquals( |
| 123 | + [ |
| 124 | + 'stage' => [ |
| 125 | + 'global' => ['SCD_ON_DEMAND' => true, 'UPDATE_URLS' => false], |
| 126 | + 'deploy' => [ |
| 127 | + 'DATABASE_CONFIGURATION' => [ |
| 128 | + 'host' => '127.0.0.1', |
| 129 | + 'port' => '3306', |
| 130 | + 'schema' => 'test_schema', |
| 131 | + ], |
| 132 | + 'SCD_THREADS' => 5, |
| 133 | + ], |
| 134 | + ], |
| 135 | + 'log' => [ |
| 136 | + 'gelf' => [ |
| 137 | + 'min_level' => 'info', |
| 138 | + 'use_default_formatter' => true, |
| 139 | + 'additional' => ['project' => 'project', 'app_id' => 'app'], |
| 140 | + ], |
| 141 | + ], |
| 142 | + ], |
| 143 | + $this->reader->read() |
| 144 | + ); |
| 145 | + } |
| 146 | + |
| 147 | + public function testReadBranchConfigWithEmptySectionAndStage() |
| 148 | + { |
| 149 | + $baseDir = __DIR__ . '/_file/'; |
| 150 | + |
| 151 | + $this->configFileListMock->expects($this->once()) |
| 152 | + ->method('getEnvConfig') |
| 153 | + ->willReturn($baseDir . '/.magento.env.yaml'); |
| 154 | + $this->systemListMock->expects($this->once()) |
| 155 | + ->method('getMagentoRoot') |
| 156 | + ->willReturn($baseDir); |
| 157 | + $this->environmentMock->expects($this->once()) |
| 158 | + ->method('getBranchName') |
| 159 | + ->willReturn('test-branch-emty'); |
| 160 | + |
| 161 | + $this->assertEquals( |
| 162 | + [ |
| 163 | + 'stage' => [ |
| 164 | + 'global' => ['SCD_ON_DEMAND' => true, 'UPDATE_URLS' => false], |
| 165 | + 'deploy' => [ |
| 166 | + 'DATABASE_CONFIGURATION' => [ |
| 167 | + 'host' => '127.0.0.1', |
| 168 | + 'port' => '3306', |
| 169 | + 'schema' => 'test_schema', |
| 170 | + ], |
| 171 | + 'SCD_THREADS' => 5, |
| 172 | + ], |
| 173 | + ], |
| 174 | + 'log' => [ |
| 175 | + 'gelf' => [ |
| 176 | + 'min_level' => 'info', |
| 177 | + 'use_default_formatter' => true, |
| 178 | + 'additional' => ['project' => 'project', 'app_id' => 'app'], |
| 179 | + ], |
| 180 | + ], |
| 181 | + ], |
| 182 | + $this->reader->read() |
| 183 | + ); |
| 184 | + } |
| 185 | + |
| 186 | + public function testReadMainConfigWithEmptySectionAndStage() |
| 187 | + { |
| 188 | + $baseDir = __DIR__ . '/_file/'; |
| 189 | + |
| 190 | + $this->configFileListMock->expects($this->once()) |
| 191 | + ->method('getEnvConfig') |
| 192 | + ->willReturn($baseDir . '/.magento-with-empty-sections.env.yaml'); |
| 193 | + $this->systemListMock->expects($this->once()) |
| 194 | + ->method('getMagentoRoot') |
| 195 | + ->willReturn($baseDir); |
| 196 | + $this->environmentMock->expects($this->once()) |
| 197 | + ->method('getBranchName') |
| 198 | + ->willReturn('test-branch'); |
| 199 | + |
| 200 | + $this->reader->read(); |
| 201 | + $this->assertEquals( |
| 202 | + [ |
| 203 | + 'stage' => [ |
| 204 | + 'global' => ['SCD_ON_DEMAND' => false], |
| 205 | + 'deploy' => ['DATABASE_CONFIGURATION' => ['host' => 'localhost'], 'SCD_THREADS' => 3], |
| 206 | + 'build' => ['SCD_THREADS' => 2], |
| 207 | + ], |
| 208 | + 'log' => [ |
| 209 | + 'gelf' => [ |
| 210 | + 'min_level' => 'info', |
| 211 | + 'use_default_formatter' => true, |
| 212 | + 'additional' => ['project' => 'project'], |
| 213 | + ], |
| 214 | + 'syslog' => ['ident' => 'ident-branch', 'facility' => 7], |
| 215 | + ], |
| 216 | + ], |
| 217 | + $this->reader->read() |
| 218 | + ); |
| 219 | + } |
| 220 | +} |
0 commit comments