Skip to content

Commit

Permalink
Merge pull request #239 from lemberg/chore/231-xdebug-3
Browse files Browse the repository at this point in the history
Migrate to Xdebug 3
  • Loading branch information
T2L committed Feb 3, 2021
2 parents 351e22a + 862c43c commit 3879c1d
Show file tree
Hide file tree
Showing 6 changed files with 270 additions and 4 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## Draft Environment 3.x.x (Unreleased)

Updates:

- [GH-231](https://github.com/lemberg/draft-environment/issues/231) - Migrate to Xdebug 3


## Draft Environment 3.3.0, 2021-01-29

Updates:
Expand Down
4 changes: 2 additions & 2 deletions default.vm-settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,8 @@ 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.mode: debug
xdebug.discover_client_host: true
xdebug.remote_log: /var/log/draft/php_xdebug_remote.log

# Web server daemon to restart (defaults to Apache 2).
Expand Down
64 changes: 64 additions & 0 deletions src/Config/Update/Step/Xdebug2To3.php
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));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ final protected function assertComposerLockContainsPackageExtra(): void {
$key = array_search(App::PACKAGE_NAME, array_column($decoded_composer_lock['packages-dev'], 'name'), TRUE);

self::assertTrue($decoded_composer_lock['packages-dev'][$key]['extra']['draft-environment']['already-installed']);
self::assertSame(7, $decoded_composer_lock['packages-dev'][$key]['extra']['draft-environment']['last-update-weight']);
self::assertSame(8, $decoded_composer_lock['packages-dev'][$key]['extra']['draft-environment']['last-update-weight']);
}

}
2 changes: 1 addition & 1 deletion tests/Unit/Config/Manager/UpdateManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public function testGetAndSetLastAppliedUpdateWeight(): void {
* Tests the last available update weight getter.
*/
public function testGetLastAvailableUpdateWeight(): void {
self::assertSame(7, $this->configUpdateManager->getLastAvailableUpdateWeight());
self::assertSame(8, $this->configUpdateManager->getLastAvailableUpdateWeight());
}

}
195 changes: 195 additions & 0 deletions tests/Unit/Config/Update/Step/Xdebug2To3Test.php
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',
],
],
],
],
];
}

}

0 comments on commit 3879c1d

Please sign in to comment.