This repository has been archived by the owner on Oct 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#231 Add update step for Xdebug 2 to 3 migration
- Loading branch information
Showing
4 changed files
with
261 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Lemberg\Draft\Environment\Config\Update\Step; | ||
|
||
use Lemberg\Draft\Environment\Config\Update\UpdateStepInterface; | ||
|
||
/** | ||
* Updates PHP configuration to support Xdebug 3. | ||
* | ||
* @link https://github.com/lemberg/draft-environment/issues/231 | ||
*/ | ||
final class Xdebug2To3 extends AbstractUpdateStep implements UpdateStepInterface { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getWeight(): int { | ||
return 8; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function update(array &$config): void { | ||
if (array_key_exists('php_extensions_configuration', $config)) { | ||
if (array_key_exists('xdebug', $config['php_extensions_configuration'])) { | ||
$xdebug_config = &$config['php_extensions_configuration']['xdebug']; | ||
if (array_key_exists('xdebug.remote_enable', $xdebug_config)) { | ||
$this->replaceArrayKey($xdebug_config, 'xdebug.remote_enable', 'xdebug.mode'); | ||
$xdebug_config['xdebug.mode'] = $xdebug_config['xdebug.mode'] === 'On' ? 'debug' : 'off'; | ||
} | ||
if (array_key_exists('xdebug.remote_connect_back', $xdebug_config)) { | ||
$this->replaceArrayKey($xdebug_config, 'xdebug.remote_connect_back', 'xdebug.discover_client_host'); | ||
$xdebug_config['xdebug.discover_client_host'] = $xdebug_config['xdebug.discover_client_host'] === 'On' ? TRUE : FALSE; | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Replaces key in the array in order to maintain the elements order. | ||
* | ||
* @param array<string,string> $array | ||
* Input array. | ||
* @param string $search | ||
* Key to be replaced. | ||
* @param string $replacement | ||
* New key. | ||
* | ||
* @throws \UnexpectedValueException | ||
* Thrown when a searched key does not exist in the array. | ||
*/ | ||
private function replaceArrayKey(array &$array, string $search, string $replacement): void { | ||
$keys = array_keys($array); | ||
if (FALSE === ($index = array_search($search, $keys, TRUE))) { | ||
throw new \UnexpectedValueException(sprintf('Key "%s" does not exist in the config array', $search)); | ||
} | ||
$keys[$index] = $replacement; | ||
$array = array_combine($keys, array_values($array)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Lemberg\Tests\Unit\Draft\Environment\Config\Update\Step; | ||
|
||
use Composer\Composer; | ||
use Composer\Config as ComposerConfig; | ||
use Composer\IO\IOInterface; | ||
use Lemberg\Draft\Environment\Config\Config; | ||
use Lemberg\Draft\Environment\Config\Manager\UpdateManager; | ||
use Lemberg\Draft\Environment\Config\Update\Step\Xdebug2To3; | ||
use Lemberg\Draft\Environment\Utility\Filesystem; | ||
use org\bovigo\vfs\vfsStream; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* Tests updating PHP configuration. | ||
* | ||
* @link https://github.com/lemberg/draft-environment/issues/204 | ||
* | ||
* @covers \Lemberg\Draft\Environment\Config\Update\Step\AbstractUpdateStep | ||
* @covers \Lemberg\Draft\Environment\Config\Update\Step\Xdebug2To3 | ||
*/ | ||
final class Xdebug2To3Test extends TestCase { | ||
|
||
/** | ||
* @var \Composer\Composer | ||
*/ | ||
private $composer; | ||
|
||
/** | ||
* @var \Composer\IO\IOInterface | ||
*/ | ||
private $io; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $root; | ||
|
||
/** | ||
* @var \Lemberg\Draft\Environment\Config\Manager\UpdateManagerInterface | ||
*/ | ||
private $configUpdateManager; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function setUp(): void { | ||
$this->composer = new Composer(); | ||
$this->composer->setConfig(new ComposerConfig()); | ||
$this->io = $this->createMock(IOInterface::class); | ||
|
||
// Mock source and target configuration directories. | ||
$this->root = vfsStream::setup()->url(); | ||
$fs = new Filesystem(); | ||
$fs->mkdir(["$this->root/source", "$this->root/target"]); | ||
|
||
$configObject = new Config("$this->root/source", "$this->root/target"); | ||
$this->configUpdateManager = new UpdateManager($this->composer, $this->io, $configObject); | ||
} | ||
|
||
/** | ||
* Tests step weight getter. | ||
*/ | ||
final public function testGetWeight(): void { | ||
$step = new Xdebug2To3($this->composer, $this->io, $this->configUpdateManager); | ||
self::assertSame(8, $step->getWeight()); | ||
} | ||
|
||
/** | ||
* Tests update step execution. | ||
* | ||
* @param array<string,mixed> $config | ||
* @param array<string,mixed> $expectedConfig | ||
* | ||
* @dataProvider updateDataProvider | ||
*/ | ||
final public function testUpdate(array $config, array $expectedConfig): void { | ||
$step = new Xdebug2To3($this->composer, $this->io, $this->configUpdateManager); | ||
|
||
$step->update($config); | ||
self::assertSame($expectedConfig, $config); | ||
} | ||
|
||
/** | ||
* Data provider for the ::testUpdate(). | ||
* | ||
* @return array<int,array<int,string|array<string,mixed>>> | ||
*/ | ||
final public function updateDataProvider(): array { | ||
return [ | ||
[ | ||
[], | ||
[], | ||
], | ||
[ | ||
[ | ||
'php_extensions_configuration' => [ | ||
'opcache' => [ | ||
'opcache.error_log' => '/var/log/draft/php_opcache_error.log', | ||
], | ||
], | ||
], | ||
[ | ||
'php_extensions_configuration' => [ | ||
'opcache' => [ | ||
'opcache.error_log' => '/var/log/draft/php_opcache_error.log', | ||
], | ||
], | ||
], | ||
], | ||
[ | ||
[ | ||
'php_extensions_configuration' => [ | ||
'opcache' => [ | ||
'opcache.error_log' => '/var/log/draft/php_opcache_error.log', | ||
], | ||
'xdebug' => [ | ||
'xdebug.mode' => 'debug', | ||
'xdebug.discover_client_host' => TRUE, | ||
'xdebug.remote_log' => '/var/log/draft/php_xdebug_remote.log', | ||
], | ||
], | ||
], | ||
[ | ||
'php_extensions_configuration' => [ | ||
'opcache' => [ | ||
'opcache.error_log' => '/var/log/draft/php_opcache_error.log', | ||
], | ||
'xdebug' => [ | ||
'xdebug.mode' => 'debug', | ||
'xdebug.discover_client_host' => TRUE, | ||
'xdebug.remote_log' => '/var/log/draft/php_xdebug_remote.log', | ||
], | ||
], | ||
], | ||
], | ||
[ | ||
[ | ||
'php_extensions_configuration' => [ | ||
'opcache' => [ | ||
'opcache.error_log' => '/var/log/draft/php_opcache_error.log', | ||
], | ||
'xdebug' => [ | ||
'xdebug.remote_enable' => 'Off', | ||
'xdebug.remote_connect_back' => 'Off', | ||
'xdebug.remote_log' => '/var/log/draft/php_xdebug_remote.log', | ||
], | ||
], | ||
], | ||
[ | ||
'php_extensions_configuration' => [ | ||
'opcache' => [ | ||
'opcache.error_log' => '/var/log/draft/php_opcache_error.log', | ||
], | ||
'xdebug' => [ | ||
'xdebug.mode' => 'off', | ||
'xdebug.discover_client_host' => FALSE, | ||
'xdebug.remote_log' => '/var/log/draft/php_xdebug_remote.log', | ||
], | ||
], | ||
], | ||
], | ||
[ | ||
[ | ||
'php_extensions_configuration' => [ | ||
'opcache' => [ | ||
'opcache.error_log' => '/var/log/draft/php_opcache_error.log', | ||
], | ||
'xdebug' => [ | ||
'xdebug.remote_enable' => 'On', | ||
'xdebug.remote_connect_back' => 'On', | ||
'xdebug.remote_log' => '/var/log/draft/php_xdebug_remote.log', | ||
], | ||
], | ||
], | ||
[ | ||
'php_extensions_configuration' => [ | ||
'opcache' => [ | ||
'opcache.error_log' => '/var/log/draft/php_opcache_error.log', | ||
], | ||
'xdebug' => [ | ||
'xdebug.mode' => 'debug', | ||
'xdebug.discover_client_host' => TRUE, | ||
'xdebug.remote_log' => '/var/log/draft/php_xdebug_remote.log', | ||
], | ||
], | ||
], | ||
], | ||
]; | ||
} | ||
|
||
} |