Skip to content

Commit

Permalink
Bugfix/migrate domain command (#113)
Browse files Browse the repository at this point in the history
* Fix bug where an empty string was causing the docker command to not run successfully via `so wp`

* Fix docker/docker compose error output not being properly captured

* Properly match parent class for error handler

* Add DockerException.php

* Better error handling/messaging for `so migrate-domain` command

* Bump to 5.6.3
  • Loading branch information
defunctl authored Mar 22, 2022
1 parent def2d1e commit 312a912
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 44 deletions.
4 changes: 3 additions & 1 deletion app/Commands/Docker.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ public function handle( Runner $runner, ResultRecorder $recorder, PermissionMana
->tty( $tty )
->run( $command );

$recorder->add( $response->process()->getOutput() );
$output = $response->process()->getOutput() ?: $response->process()->getErrorOutput();

$recorder->add( trim( (string) $output ) );

return $response->ok() ? self::SUCCESS : self::FAILURE;
}
Expand Down
4 changes: 3 additions & 1 deletion app/Commands/DockerCompose.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ public function handle( Runner $runner, Network $network, ResultRecorder $record
] )
->run( $command );

$recorder->add( $response->process()->getOutput() );
$output = $response->process()->getOutput() ?: $response->process()->getErrorOutput();

$recorder->add( trim( (string) $output ) );

return $response->ok() ? self::SUCCESS : self::FAILURE;
}
Expand Down
31 changes: 21 additions & 10 deletions app/Commands/LocalDocker/MigrateDomain.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

namespace App\Commands\LocalDocker;

use Exception;
use App\Exceptions\DockerException;
use App\Exceptions\SystemExitException;
use App\Recorders\ResultRecorder;
use App\Services\Docker\Local\Config;
use App\Exceptions\SystemExitException;
use Exception;
use Illuminate\Support\Facades\Artisan;

/**
Expand Down Expand Up @@ -36,12 +37,12 @@ class MigrateDomain extends BaseLocalDocker {
* @param \App\Recorders\ResultRecorder $recorder
*
* @throws \App\Exceptions\SystemExitException
* @throws \Exception
* @return void
* @throws \Exception|\App\Exceptions\DockerException
*
* @return int
*/
public function handle( Config $config, ResultRecorder $recorder ): void {
Artisan::call( Wp::class, [
public function handle( Config $config, ResultRecorder $recorder ): int {
$result = Artisan::call( Wp::class, [
'args' => [
'db',
'prefix',
Expand All @@ -50,9 +51,13 @@ public function handle( Config $config, ResultRecorder $recorder ): void {
'--quiet' => true,
] );

$dbPrefix = trim( $recorder->first() );
if ( $result === self::FAILURE ) {
throw new DockerException( $recorder->first() );
}

Artisan::call( Wp::class, [
$dbPrefix = $recorder->first();

$result = Artisan::call( Wp::class, [
'args' => [
'db',
'query',
Expand All @@ -63,11 +68,15 @@ public function handle( Config $config, ResultRecorder $recorder ): void {
'--quiet' => true,
] );

$domain = trim( $recorder->offsetGet( 1 ) );
if ( $result === self::FAILURE ) {
throw new DockerException( $recorder->offsetGet( 1 ) );
}

$domain = $recorder->offsetGet( 1 );
$sourceDomain = parse_url( $domain, PHP_URL_HOST );

if ( empty( $sourceDomain ) ) {
throw new Exception( sprintf( 'Invalid siteurl found in options table: %s', $domain ) );
throw new Exception( sprintf( 'Invalid siteurl found in options table: "%s". Verify your project is using the same table_prefix as the imported database.', $domain ) );
}

$targetDomain = $config->getProjectDomain();
Expand Down Expand Up @@ -111,6 +120,8 @@ public function handle( Config $config, ResultRecorder $recorder ): void {
] );

$this->info( 'Done.' );

return self::SUCCESS;
}

}
10 changes: 5 additions & 5 deletions app/Commands/LocalDocker/Wp.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ class Wp extends BaseLocalDocker {
* @param \App\Services\XdebugValidator $xdebugValidator
* @param \App\Services\Docker\Container $container
*
* @return int|null
* @return int
*/
public function handle( Config $config, XdebugValidator $xdebugValidator, Container $container ): ?int {
$params = [
public function handle( Config $config, XdebugValidator $xdebugValidator, Container $container ): int {
$params = array_filter( [
'exec',
! $this->option( 'notty' ) ? '--tty' : '',
'-w',
$config->getWorkdir(),
];
] );

if ( $this->option( 'xdebug' ) ) {

Expand Down Expand Up @@ -79,7 +79,7 @@ public function handle( Config $config, XdebugValidator $xdebugValidator, Contai

$params = array_merge( $params, $env, [ $containerId ], $exec, $this->argument( 'args' ) );

return Artisan::call( Docker::class, $params );
return (int) Artisan::call( Docker::class, $params );
}

}
12 changes: 12 additions & 0 deletions app/Exceptions/DockerException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php declare( strict_types=1 );

namespace App\Exceptions;

use Exception;

/**
* Thrown when docker or docker compose returns an error.
*/
class DockerException extends Exception {

}
14 changes: 7 additions & 7 deletions app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
<?php declare( strict_types=1 );

namespace App\Exceptions;

use Throwable;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Throwable;

/**
* Class Handler
Expand All @@ -26,14 +26,14 @@ class Handler extends ExceptionHandler {
/**
* Report or log an exception.
*
* @param \Throwable $exception
* @param \Throwable $e
*
* @return void
* @throws \Exception|\Throwable
*
* @throws \Exception
* @return void
*/
public function report( Throwable $exception ) {
parent::report( $exception );
public function report( Throwable $e ) {
parent::report( $e );
}

}
2 changes: 1 addition & 1 deletion config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
*/

//'version' => app('git.version'),
'version' => '5.6.2',
'version' => '5.6.3',

/*
|--------------------------------------------------------------------------
Expand Down
92 changes: 75 additions & 17 deletions tests/Feature/Commands/LocalDocker/MigrateDomainTest.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
<?php declare(strict_types=1);
<?php declare( strict_types=1 );

namespace Tests\Feature\Commands\LocalDocker;

use Exception;
use App\Commands\LocalDocker\MigrateDomain;
use App\Commands\LocalDocker\Wp;
use App\Exceptions\DockerException;
use App\Exceptions\SystemExitException;
use App\Recorders\ResultRecorder;
use Exception;
use Illuminate\Support\Facades\Artisan;
use LaravelZero\Framework\Commands\Command;

class MigrateDomainTest extends LocalDockerCommand {
final class MigrateDomainTest extends LocalDockerCommand {

private $wpCommand;
private $recorder;
Expand Down Expand Up @@ -50,7 +52,7 @@ public function test_it_calls_migrate_domain_command() {

$this->wpCommand->shouldReceive( 'call' )
->with( Wp::class, [
'args' => [
'args' => [
'db',
'query',
"UPDATE tribe_options SET option_value = REPLACE( option_value, 'test.com', 'test.tribe' ) WHERE option_name = 'siteurl'",
Expand All @@ -61,7 +63,7 @@ public function test_it_calls_migrate_domain_command() {

$this->wpCommand->shouldReceive( 'call' )
->with( Wp::class, [
'args' => [
'args' => [
'search-replace',
'test.com',
'test.tribe',
Expand All @@ -72,7 +74,7 @@ public function test_it_calls_migrate_domain_command() {

$this->wpCommand->shouldReceive( 'call' )
->with( Wp::class, [
'args' => [
'args' => [
'cache',
'flush',
],
Expand All @@ -90,7 +92,7 @@ public function test_it_calls_migrate_domain_command() {
$this->assertStringContainsString( 'Done.', $tester->getDisplay() );
}

public function test_it_throws_exeception_on_invalid_site_url() {
public function test_it_throws_an_exception_on_invalid_site_url() {
$this->expectException( Exception::class );
$this->expectExceptionMessage( 'Invalid siteurl found in options table:' );

Expand Down Expand Up @@ -124,14 +126,12 @@ public function test_it_throws_exeception_on_invalid_site_url() {

$command = $this->app->make( MigrateDomain::class );

$tester = $this->runCommand( $command, [], [
$this->runCommand( $command, [], [
'Ready to search and replace "https://test.com" to "https://test.tribe" (This cannot be undone)?' => 'yes',
] );

$this->assertSame( 1, $tester->getStatusCode() );
}

public function test_it_throws_exception_on_matching_domains() {
public function test_it_throws_an_exception_on_matching_domains() {
$this->expectException( Exception::class );
$this->expectExceptionMessage( 'Error: Source and target domains match:' );

Expand Down Expand Up @@ -166,14 +166,12 @@ public function test_it_throws_exception_on_matching_domains() {

$command = $this->app->make( MigrateDomain::class );

$tester = $this->runCommand( $command, [], [
$this->runCommand( $command, [], [
'Ready to search and replace "https://test.com" to "https://test.tribe" (This cannot be undone)?' => 'yes',
] );

$this->assertSame( 1, $tester->getStatusCode() );
}

public function test_it_throws_exception_on_no_confirmation() {
public function test_it_throws_an_exception_on_no_confirmation() {
$this->expectException( SystemExitException::class );
$this->expectExceptionMessage( 'Cancelling' );

Expand Down Expand Up @@ -208,11 +206,71 @@ public function test_it_throws_exception_on_no_confirmation() {

$command = $this->app->make( MigrateDomain::class );

$tester = $this->runCommand( $command, [], [
$this->runCommand( $command, [], [
'Ready to search and replace "https://test.com" to "https://test.tribe" (This cannot be undone)?' => 'no',
] );
}

public function test_it_throws_an_exception_on_db_prefix_docker_error() {
$this->expectException( DockerException::class );

$this->wpCommand->shouldReceive( 'call' )
->with( Wp::class, [
'args' => [
'db',
'prefix',
],
'--notty' => true,
'--quiet' => true,
] )->andReturn( Command::FAILURE );

$this->recorder->shouldReceive( 'first' )->andReturn( 'Error: No such container:' );

Artisan::swap( $this->wpCommand );

$command = $this->app->make( MigrateDomain::class );

$this->runCommand( $command, [], [
'Ready to search and replace "https://test.com" to "https://test.tribe" (This cannot be undone)?' => 'yes',
] );
}

public function test_it_throws_an_exception_on_domain_site_url() {
$this->expectException( DockerException::class );

$this->assertSame( 1, $tester->getStatusCode() );
$this->wpCommand->shouldReceive( 'call' )
->with( Wp::class, [
'args' => [
'db',
'prefix',
],
'--notty' => true,
'--quiet' => true,
] );

$this->recorder->shouldReceive( 'first' )->andReturn( 'tribe_' );

$this->wpCommand->shouldReceive( 'call' )
->with( Wp::class, [
'args' => [
'db',
'query',
"SELECT option_value FROM tribe_options WHERE option_name = 'siteurl'",
'--skip-column-names',
],
'--notty' => true,
'--quiet' => true,
] )->andReturn( Command::FAILURE );

$this->recorder->shouldReceive( 'offsetGet' )->with( 1 )->andReturn( 'Error: No such container:' );

Artisan::swap( $this->wpCommand );

$command = $this->app->make( MigrateDomain::class );

$this->runCommand( $command, [], [
'Ready to search and replace "https://test.com" to "https://test.tribe" (This cannot be undone)?' => 'yes',
] );
}

}
2 changes: 0 additions & 2 deletions tests/Feature/Commands/LocalDocker/WpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ public function test_it_calls_local_wp_command_with_options() {

$this->docker->shouldReceive( 'call' )->with( Docker::class, [
'exec',
'',
'-w',
'/application/www',
'--env',
Expand Down Expand Up @@ -110,7 +109,6 @@ public function test_it_warns_the_user_if_xdebug_is_not_correctly_configured() {

$this->docker->shouldReceive( 'call' )->with( Docker::class, [
'exec',
'',
'-w',
'/application/www',
'--env',
Expand Down

0 comments on commit 312a912

Please sign in to comment.