Skip to content

Commit 7359390

Browse files
committed
feat: add new domain to the domain option when doing a domain change
1 parent 676694f commit 7359390

3 files changed

Lines changed: 125 additions & 3 deletions

File tree

src/Command/Environment/ChangeEnvironmentDomainCommand.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\Console\Input\InputInterface;
1919
use Ymir\Cli\Command\AbstractInvocationCommand;
2020
use Ymir\Cli\Console\OutputInterface;
21+
use Ymir\Cli\ProjectConfiguration\DomainConfigurationChange;
2122
use Ymir\Cli\Support\Arr;
2223

2324
class ChangeEnvironmentDomainCommand extends AbstractInvocationCommand
@@ -53,7 +54,7 @@ protected function perform(InputInterface $input, OutputInterface $output)
5354
if (empty($currentDomain) && $output->confirm(sprintf('Do you want to use the "<comment>%s</comment>" vanity domain as the current environment domain to replace?', $vanityDomain))) {
5455
$currentDomain = $vanityDomain;
5556
} elseif (empty($currentDomain)) {
56-
$currentDomain = (string) $output->ask('What is the current environment domain that you want to replace?');
57+
$currentDomain = strtolower((string) $output->ask('What is the current environment domain that you want to replace?'));
5758
}
5859

5960
$domainOption = Arr::get($this->projectConfiguration->getEnvironment($environment), 'domain');
@@ -62,11 +63,11 @@ protected function perform(InputInterface $input, OutputInterface $output)
6263
if (is_string($domainOption)) {
6364
$newDomain = $domainOption;
6465
} elseif (is_array($domainOption)) {
65-
$newDomain = (string) $output->choice(sprintf('Which mapped domain do you want to use as the new "<comment>%s</comment>" environment domain?', $environment), $domainOption);
66+
$newDomain = strtolower((string) $output->choice(sprintf('Which mapped domain do you want to use as the new "<comment>%s</comment>" environment domain?', $environment), $domainOption));
6667
} elseif (empty($domainOption) && $currentDomain !== $vanityDomain && $output->confirm(sprintf('Do you want to use the "<comment>%s</comment>" vanity domain as the new "<comment>%s</comment>" environment domain?', $vanityDomain, $environment))) {
6768
$newDomain = $vanityDomain;
6869
} elseif (empty($domainOption)) {
69-
$newDomain = (string) $output->ask(sprintf('What is the new domain that you want to use as the new "<comment>%s</comment>" environment domain?', $environment));
70+
$newDomain = strtolower((string) $output->ask(sprintf('What is the new domain that you want to use as the new "<comment>%s</comment>" environment domain?', $environment)));
7071
}
7172

7273
$currentDomain = parse_url($currentDomain, PHP_URL_HOST) ?? $currentDomain;
@@ -84,6 +85,10 @@ protected function perform(InputInterface $input, OutputInterface $output)
8485
return;
8586
}
8687

88+
if ($newDomain !== $vanityDomain && !Arr::has((array) $domainOption, $newDomain) && $output->confirm(sprintf('Do you want to add the "<comment>%s</comment>" domain to your "<comment>%s</comment>" environment configuration?', $newDomain, $environment))) {
89+
$this->projectConfiguration->applyChangesToEnvironment($environment, new DomainConfigurationChange($newDomain));
90+
}
91+
8792
$output->info(sprintf('Changing "<comment>%s</comment>" environment domain from "<comment>%s</comment>" to "<comment>%s</comment>"', $environment, $currentDomain, $newDomain));
8893

8994
$result = $this->invokeWpCliCommand(sprintf('wp search-replace %s %s --all-tables', $currentDomain, $newDomain), $environment);
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of Ymir command-line tool.
7+
*
8+
* (c) Carl Alexander <support@ymirapp.com>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Ymir\Cli\ProjectConfiguration;
15+
16+
class DomainConfigurationChange implements ConfigurationChangeInterface
17+
{
18+
/**
19+
* The domain to add to the configuration.
20+
*
21+
* @var string
22+
*/
23+
private $domain;
24+
25+
/**
26+
* Constructor.
27+
*/
28+
public function __construct(string $domain)
29+
{
30+
$this->domain = $domain;
31+
}
32+
33+
/**
34+
* {@inheritdoc}
35+
*/
36+
public function apply(array $options, string $projectType): array
37+
{
38+
if (empty($options['domain'])) {
39+
$options['domain'] = $this->domain;
40+
} elseif (is_array($options['domain']) || (is_string($options['domain']) && strtolower($options['domain']) !== strtolower($this->domain))) {
41+
$options['domain'] = collect($this->domain)->merge($options['domain'])->unique()->values()->all();
42+
}
43+
44+
return $options;
45+
}
46+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of Ymir command-line tool.
7+
*
8+
* (c) Carl Alexander <support@ymirapp.com>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Ymir\Cli\Tests\Unit\ProjectConfiguration;
15+
16+
use Ymir\Cli\ProjectConfiguration\DomainConfigurationChange;
17+
use Ymir\Cli\Tests\Unit\TestCase;
18+
19+
/**
20+
* @covers \Ymir\Cli\ProjectConfiguration\DomainConfigurationChange
21+
*/
22+
class DomainConfigurationChangeTest extends TestCase
23+
{
24+
public function testApplyDoesNothingIfNewDomainIsAlreadyOnTopInDomainOption()
25+
{
26+
$existingDomain = $this->faker->domainName;
27+
$newDomain = $this->faker->domainName;
28+
29+
$this->assertSame([
30+
'domain' => [$newDomain, $existingDomain],
31+
], (new DomainConfigurationChange($newDomain))->apply(['domain' => [$newDomain, $existingDomain]], 'wordpress'));
32+
}
33+
34+
public function testApplyDoesNothingIfNewDomainSameAsExistingStringDomainOption()
35+
{
36+
$domain = $this->faker->domainName;
37+
38+
$this->assertSame([
39+
'domain' => $domain,
40+
], (new DomainConfigurationChange($domain))->apply(['domain' => $domain], 'wordpress'));
41+
}
42+
43+
public function testApplyWithMovesNewDomainToTopIfExistsInDomainOption()
44+
{
45+
$existingDomain = $this->faker->domainName;
46+
$newDomain = $this->faker->domainName;
47+
48+
$this->assertSame([
49+
'domain' => [$newDomain, $existingDomain],
50+
], (new DomainConfigurationChange($newDomain))->apply(['domain' => [$existingDomain, $newDomain]], 'wordpress'));
51+
}
52+
53+
public function testApplyWithNoDomainOption()
54+
{
55+
$domain = $this->faker->domainName;
56+
57+
$this->assertSame([
58+
'domain' => $domain,
59+
], (new DomainConfigurationChange($domain))->apply([], 'wordpress'));
60+
}
61+
62+
public function testApplyWithPrependsNewDomainToExistingDomainOption()
63+
{
64+
$existingDomain = $this->faker->domainName;
65+
$newDomain = $this->faker->domainName;
66+
67+
$this->assertSame([
68+
'domain' => [$newDomain, $existingDomain],
69+
], (new DomainConfigurationChange($newDomain))->apply(['domain' => $existingDomain], 'wordpress'));
70+
}
71+
}

0 commit comments

Comments
 (0)