|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2025 Adobe. |
| 4 | + * All rights reserved. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Framework\Console; |
| 9 | + |
| 10 | +use Magento\Framework\App\Filesystem\DirectoryList; |
| 11 | +use Magento\Framework\App\ObjectManager; |
| 12 | +use Magento\Framework\App\State; |
| 13 | +use PHPUnit\Framework\TestCase; |
| 14 | + |
| 15 | +/** |
| 16 | + * Integration test for CLI --magento-init-params functionality |
| 17 | + */ |
| 18 | +class CliStateTest extends TestCase |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @var mixed|null |
| 22 | + */ |
| 23 | + private $originalArgv; |
| 24 | + |
| 25 | + /** |
| 26 | + * @inheritDoc |
| 27 | + */ |
| 28 | + protected function setUp(): void |
| 29 | + { |
| 30 | + parent::setUp(); |
| 31 | + |
| 32 | + // Store original argv |
| 33 | + $this->originalArgv = $_SERVER['argv'] ?? null; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * @inheritDoc |
| 38 | + */ |
| 39 | + protected function tearDown(): void |
| 40 | + { |
| 41 | + // Restore original argv |
| 42 | + if ($this->originalArgv !== null) { |
| 43 | + $_SERVER['argv'] = $this->originalArgv; |
| 44 | + $this->originalArgv = null; |
| 45 | + } else { |
| 46 | + unset($_SERVER['argv']); |
| 47 | + } |
| 48 | + |
| 49 | + parent::tearDown(); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Test that State::getMode() when --magento-init-params sets MAGE_MODE |
| 54 | + * |
| 55 | + * @param string $mode |
| 56 | + * @return void |
| 57 | + * @dataProvider modeDataProvider |
| 58 | + */ |
| 59 | + public function testStateGetModeWithMagentoInitParams(string $mode) |
| 60 | + { |
| 61 | + // Set up test argv with --magento-init-params |
| 62 | + $testArgv = [ |
| 63 | + 'php', |
| 64 | + 'bin/magento', |
| 65 | + 'setup:upgrade', |
| 66 | + '--magento-init-params=MAGE_MODE=' . $mode, |
| 67 | + ]; |
| 68 | + $_SERVER['argv'] = $testArgv; |
| 69 | + |
| 70 | + // Get the State object from the ObjectManager |
| 71 | + $state = $this->getObjectManager()->get(State::class); |
| 72 | + |
| 73 | + // Assert that State::getMode() returns the correct mode |
| 74 | + $this->assertEquals( |
| 75 | + $mode, |
| 76 | + $state->getMode(), |
| 77 | + 'State::getMode() should return "' . $mode . '" when MAGE_MODE set via --magento-init-params' |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Test that multiple --magento-init-params are processed correctly |
| 83 | + * |
| 84 | + * @return void |
| 85 | + */ |
| 86 | + public function testMultipleMagentoInitParams() |
| 87 | + { |
| 88 | + $mode = 'developer'; |
| 89 | + $cachePath = '/var/tmp/cache'; |
| 90 | + $varPath = '/var/tmp/var'; |
| 91 | + |
| 92 | + // Set up test argv with multiple --magento-init-params |
| 93 | + $testArgv = [ |
| 94 | + 'php', |
| 95 | + 'bin/magento', |
| 96 | + 'setup:upgrade', |
| 97 | + '--magento-init-params=MAGE_MODE=' .$mode . |
| 98 | + '&MAGE_DIRS[cache][path]=' . $cachePath . '&MAGE_DIRS[var][path]=' . $varPath, |
| 99 | + ]; |
| 100 | + $_SERVER['argv'] = $testArgv; |
| 101 | + |
| 102 | + // Get the ObjectManager |
| 103 | + $objectManager = $this->getObjectManager(); |
| 104 | + |
| 105 | + // Get the State object from the ObjectManager |
| 106 | + $state = $objectManager->get(State::class); |
| 107 | + |
| 108 | + // Assert that State::getMode() returns the correct mode |
| 109 | + $this->assertEquals( |
| 110 | + $mode, |
| 111 | + $state->getMode(), |
| 112 | + 'State::getMode() should return "' . $mode . '" when MAGE_MODE set via --magento-init-params' |
| 113 | + ); |
| 114 | + |
| 115 | + // Get the DirectoryList to verify filesystem paths were set |
| 116 | + $directoryList = $objectManager->get(DirectoryList::class); |
| 117 | + |
| 118 | + // Assert that custom filesystem paths were applied |
| 119 | + $this->assertEquals( |
| 120 | + $cachePath, |
| 121 | + $directoryList->getPath(DirectoryList::CACHE), |
| 122 | + 'Custom cache directory path should be set via --magento-init-params' |
| 123 | + ); |
| 124 | + |
| 125 | + $this->assertEquals( |
| 126 | + $varPath, |
| 127 | + $directoryList->getPath(DirectoryList::VAR_DIR), |
| 128 | + 'Custom var directory path should be set via --magento-init-params' |
| 129 | + ); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Returns magento mode for cli command |
| 134 | + * |
| 135 | + * @return string[] |
| 136 | + */ |
| 137 | + public static function modeDataProvider(): array |
| 138 | + { |
| 139 | + return [ |
| 140 | + ['production'], |
| 141 | + ['developer'], |
| 142 | + ['default'] |
| 143 | + ]; |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Get the ObjectManager from the Cli instance using reflection |
| 148 | + * |
| 149 | + * @return ObjectManager |
| 150 | + */ |
| 151 | + private function getObjectManager() |
| 152 | + { |
| 153 | + // Create a new Cli instance |
| 154 | + $cli = new Cli('Magento CLI'); |
| 155 | + |
| 156 | + // Get the ObjectManager from the Cli instance using reflection |
| 157 | + $reflection = new \ReflectionClass($cli); |
| 158 | + $objectManagerProperty = $reflection->getProperty('objectManager'); |
| 159 | + $objectManagerProperty->setAccessible(true); |
| 160 | + return $objectManagerProperty->getValue($cli); |
| 161 | + } |
| 162 | +} |
0 commit comments