Skip to content

Commit

Permalink
Merge pull request #211 from lemberg/feature/203-allow-mysql-all-hosts
Browse files Browse the repository at this point in the history
Allow connecting to the MySQL instance from any host
  • Loading branch information
T2L committed Aug 12, 2020
2 parents 05e6c36 + 6252952 commit b1d4604
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

Fixes:

- [GH-203](https://github.com/lemberg/draft-environment/issues/203) - Allow connecting to the MySQL instance from any host, i.e. allow direct connection from the host OS to the MySQL instance in the guest OS (no SSH tunnel anymore). Side effect - tests can be run from the host OS, which speeds them up to 3-5x.
- [GH-205](https://github.com/lemberg/draft-environment/issues/205) - Fix PHP fatal error upon package removal (i.e. when running `composer remove lemberg/draft-environment`
- [GH-208](https://github.com/lemberg/draft-environment/issues/208) - Fix broken provisioning by adding `/vagrant` mount

Expand Down
1 change: 1 addition & 0 deletions default.vm-settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ mysql_users:
name: drupal
password: drupal
priv: 'drupal.*:ALL'
host: '%'

# Replication settings (replication is only enabled if master/user have values).
mysql_server_id: '1'
Expand Down
32 changes: 32 additions & 0 deletions src/Config/Update/Step/AllowAllHostsMysql.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Lemberg\Draft\Environment\Config\Update\Step;

use Lemberg\Draft\Environment\Config\Update\UpdateStepInterface;

/**
* Allows connecting to the MySQL instance from any host.
*/
final class AllowAllHostsMysql extends AbstractUpdateStep implements UpdateStepInterface {

/**
* {@inheritdoc}
*/
public function getWeight(): int {
return 6;
}

/**
* {@inheritdoc}
*/
public function update(array &$config): void {
if (array_key_exists('mysql_users', $config)) {
foreach ($config['mysql_users'] as &$value) {
$value['host'] = $value['host'] ?? '%';
}
}
}

}
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(5, $decoded_composer_lock['packages-dev'][$key]['extra']['draft-environment']['last-update-weight']);
self::assertSame(6, $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(5, $this->configUpdateManager->getLastAvailableUpdateWeight());
self::assertSame(6, $this->configUpdateManager->getLastAvailableUpdateWeight());
}

}
139 changes: 139 additions & 0 deletions tests/Unit/Config/Update/Step/AllowAllHostsMysqlTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<?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\AllowAllHostsMysql;
use Lemberg\Draft\Environment\Utility\Filesystem;
use org\bovigo\vfs\vfsStream;
use PHPUnit\Framework\TestCase;

/**
* Tests adding id to the synced folders configuration.
*
* @covers \Lemberg\Draft\Environment\Config\Update\Step\AbstractUpdateStep
* @covers \Lemberg\Draft\Environment\Config\Update\Step\AllowAllHostsMysql
*/
final class AllowAllHostsMysqlTest 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 AllowAllHostsMysql($this->composer, $this->io, $this->configUpdateManager);
self::assertSame(6, $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 AllowAllHostsMysql($this->composer, $this->io, $this->configUpdateManager);

$step->update($config);
self::assertSame($config, $expectedConfig);
}

/**
* Data provider for the ::testUpdate().
*
* @return array<int,array<int,string|array<string,mixed>>>
*/
final public function updateDataProvider(): array {
return [
[
[
'mysql_users' => [
[
'name' => 'drupal',
'password' => 'drupal',
'priv' => 'drupal.*:ALL',
],
[
'name' => 'drupal2',
'password' => 'drupal2',
'priv' => 'drupal.*:ALL',
],
[
'name' => 'drupal3',
'password' => 'drupal3',
'priv' => 'drupal.*:ALL',
'host' => 'localhost',
],
],
],
[
'mysql_users' => [
[
'name' => 'drupal',
'password' => 'drupal',
'priv' => 'drupal.*:ALL',
'host' => '%',
],
[
'name' => 'drupal2',
'password' => 'drupal2',
'priv' => 'drupal.*:ALL',
'host' => '%',
],
[
'name' => 'drupal3',
'password' => 'drupal3',
'priv' => 'drupal.*:ALL',
'host' => 'localhost',
],
],
],
],
];
}

}

0 comments on commit b1d4604

Please sign in to comment.