|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2025 Adobe. |
| 4 | + * All rights reserved. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Magento\Framework\Console; |
| 8 | + |
| 9 | +use Magento\Framework\App\State; |
| 10 | +use Magento\Framework\Console\Cli; |
| 11 | +use PHPUnit\Framework\TestCase; |
| 12 | + |
| 13 | +/** |
| 14 | + * Integration test for CLI --magento-init-params functionality |
| 15 | + * Tests that Magento\Framework\App\State::getMode() returns the correct mode |
| 16 | + */ |
| 17 | +class CliStateTest extends TestCase |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @var State |
| 21 | + */ |
| 22 | + private $state; |
| 23 | + |
| 24 | + protected function setUp(): void |
| 25 | + { |
| 26 | + parent::setUp(); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Test that State::getMode() when --magento-init-params sets MAGE_MODE |
| 31 | + * |
| 32 | + * @param string $mode |
| 33 | + * @return void |
| 34 | + * @dataProvider modeDataProvider |
| 35 | + */ |
| 36 | + public function testStateGetModeWithMagentoInitParams(string $mode) |
| 37 | + { |
| 38 | + // Set up test argv with --magento-init-params |
| 39 | + $testArgv = [ |
| 40 | + 'php', |
| 41 | + 'bin/magento', |
| 42 | + 'setup:upgrade', |
| 43 | + '--magento-init-params=MAGE_MODE=' . $mode, |
| 44 | + ]; |
| 45 | + |
| 46 | + // Store original argv |
| 47 | + $originalArgv = $_SERVER['argv'] ?? null; |
| 48 | + $_SERVER['argv'] = $testArgv; |
| 49 | + |
| 50 | + try { |
| 51 | + // Create a new Cli instance which will use our fixed initObjectManager method |
| 52 | + $cli = new Cli('Magento CLI', '2.4.0'); |
| 53 | + |
| 54 | + // Get the ObjectManager from the Cli instance using reflection |
| 55 | + $reflection = new \ReflectionClass($cli); |
| 56 | + $objectManagerProperty = $reflection->getProperty('objectManager'); |
| 57 | + $objectManagerProperty->setAccessible(true); |
| 58 | + $objectManager = $objectManagerProperty->getValue($cli); |
| 59 | + |
| 60 | + // Get the State object from the ObjectManager |
| 61 | + $state = $objectManager->get(State::class); |
| 62 | + |
| 63 | + // Assert that State::getMode() returns the correct mode |
| 64 | + $this->assertEquals($mode, $state->getMode(), |
| 65 | + 'State::getMode() should return "' . $mode . '" when MAGE_MODE=' . $mode . ' is set via --magento-init-params'); |
| 66 | + |
| 67 | + } finally { |
| 68 | + // Restore original argv |
| 69 | + if ($originalArgv !== null) { |
| 70 | + $_SERVER['argv'] = $originalArgv; |
| 71 | + } else { |
| 72 | + unset($_SERVER['argv']); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Returns magento mode for cli command |
| 79 | + * |
| 80 | + * @return string[] |
| 81 | + */ |
| 82 | + public static function modeDataProvider(): array |
| 83 | + { |
| 84 | + return [ |
| 85 | + ['production'], |
| 86 | + ['developer'], |
| 87 | + ['default'] |
| 88 | + ]; |
| 89 | + } |
| 90 | +} |
0 commit comments