Skip to content

Commit

Permalink
Add consistency in failure actions hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
gmazzap committed Aug 29, 2024
1 parent aebd505 commit 127523b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
15 changes: 10 additions & 5 deletions src/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,19 @@ class Package
public const ACTION_FAILED_BOOT = 'failed-boot';

/**
* Action fired when a package is connected successfully.
* Action fired when adding a module failed.
*/
public const ACTION_PACKAGE_CONNECTED = 'package-connected';
public const ACTION_FAILED_ADD_MODULE = 'failed-add-module';

/**
* Action fired when a package connection failed.
*/
public const ACTION_FAILED_CONNECTION = 'failed-connection';
public const ACTION_FAILED_CONNECT = 'failed-connect';

/**
* Action fired when a package is connected successfully.
*/
public const ACTION_PACKAGE_CONNECTED = 'package-connected';

/**
* Module states can be used to get information about your module.
Expand Down Expand Up @@ -254,7 +259,7 @@ public function addModule(Module $module): Package
$status = $added ? self::MODULE_ADDED : self::MODULE_NOT_ADDED;
$this->moduleProgress($module->id(), $status);
} catch (\Throwable $throwable) {
$this->handleFailure($throwable, self::ACTION_FAILED_BUILD);
$this->handleFailure($throwable, self::ACTION_FAILED_ADD_MODULE);
}

return $this;
Expand Down Expand Up @@ -741,7 +746,7 @@ private function handleConnectionFailure(string $packageName, string $reason, bo
$message = "Failed connecting package {$packageName} because {$reason}.";

do_action(
$this->hookName(self::ACTION_FAILED_CONNECTION),
$this->hookName(self::ACTION_FAILED_CONNECT),
$packageName,
new \WP_Error('failed_connection', $message, $errorData)
);
Expand Down
27 changes: 18 additions & 9 deletions tests/unit/PackageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1195,7 +1195,7 @@ public function testConnectBuiltPackageFromBuildPackageFailsDebugOff(): void
$package1 = $this->stubSimplePackage('1');
$package2 = $this->stubSimplePackage('2');

Monkey\Actions\expectDone($package2->hookName(Package::ACTION_FAILED_CONNECTION))
Monkey\Actions\expectDone($package2->hookName(Package::ACTION_FAILED_CONNECT))
->once()
->with($package1->name(), \Mockery::type(\WP_Error::class));

Expand All @@ -1213,7 +1213,7 @@ public function testConnectBuiltPackageFromBuildPackageFailsDebugOn(): void
$package1 = $this->stubSimplePackage('1');
$package2 = $this->stubSimplePackage('2', true);

Monkey\Actions\expectDone($package2->hookName(Package::ACTION_FAILED_CONNECTION))
Monkey\Actions\expectDone($package2->hookName(Package::ACTION_FAILED_CONNECT))
->once()
->with($package1->name(), \Mockery::type(\WP_Error::class));

Expand All @@ -1232,7 +1232,7 @@ public function testConnectBuiltPackageFromBootedPackageFailsDebugOff(): void
$package1 = $this->stubSimplePackage('1');
$package2 = $this->stubSimplePackage('2');

Monkey\Actions\expectDone($package2->hookName(Package::ACTION_FAILED_CONNECTION))
Monkey\Actions\expectDone($package2->hookName(Package::ACTION_FAILED_CONNECT))
->once()
->with($package1->name(), \Mockery::type(\WP_Error::class));

Expand All @@ -1250,7 +1250,7 @@ public function testConnectBuiltPackageFromBootedPackageFailsDebugOn(): void
$package1 = $this->stubSimplePackage('1');
$package2 = $this->stubSimplePackage('2', true);

Monkey\Actions\expectDone($package2->hookName(Package::ACTION_FAILED_CONNECTION))
Monkey\Actions\expectDone($package2->hookName(Package::ACTION_FAILED_CONNECT))
->once()
->with($package1->name(), \Mockery::type(\WP_Error::class));

Expand Down Expand Up @@ -1293,7 +1293,7 @@ public function testPackageCanOnlyBeConnectedOnce(): void
Monkey\Actions\expectDone($package2->hookName(Package::ACTION_PACKAGE_CONNECTED))
->once();

Monkey\Actions\expectDone($package2->hookName(Package::ACTION_FAILED_CONNECTION))
Monkey\Actions\expectDone($package2->hookName(Package::ACTION_FAILED_CONNECT))
->twice()
->with($package1->name(), \Mockery::type(\WP_Error::class));

Expand Down Expand Up @@ -1321,7 +1321,7 @@ public function testPackageCanNotBeConnectedWithThemselves(): void
{
$package1 = $this->stubSimplePackage('1');

$action = $package1->hookName(Package::ACTION_FAILED_CONNECTION);
$action = $package1->hookName(Package::ACTION_FAILED_CONNECT);
Monkey\Actions\expectDone($action)->never();

static::assertFalse($package1->connect($package1));
Expand Down Expand Up @@ -1435,7 +1435,7 @@ public function testFailureFlowWithFailureOnAddModuleDebugModeOff(): void

$package = Package::new($this->stubProperties());

Monkey\Actions\expectDone($package->hookName(Package::ACTION_FAILED_BUILD))
Monkey\Actions\expectDone($package->hookName(Package::ACTION_FAILED_ADD_MODULE))
->once()
->whenHappen(
static function (\Throwable $throwable) use ($exception, $package): void {
Expand All @@ -1444,6 +1444,15 @@ static function (\Throwable $throwable) use ($exception, $package): void {
}
);

Monkey\Actions\expectDone($package->hookName(Package::ACTION_FAILED_BUILD))
->once()
->whenHappen(
function (\Throwable $throwable) use ($package): void {
$this->assertThrowableMessageMatches($throwable, 'build package');
static::assertTrue($package->statusIs(Package::STATUS_FAILED));
}
);

Monkey\Actions\expectDone($package->hookName(Package::ACTION_FAILED_BOOT))
->once()
->whenHappen(
Expand All @@ -1452,7 +1461,7 @@ function (\Throwable $throwable) use ($exception, $package): void {
$previous = $throwable->getPrevious();
$this->assertThrowableMessageMatches($previous, 'build package');
$previous = $previous->getPrevious();
$this->assertThrowableMessageMatches($previous, 'two');
$this->assertThrowableMessageMatches($previous, 'add module');
static::assertSame($exception, $previous->getPrevious());
static::assertTrue($package->statusIs(Package::STATUS_FAILED));
}
Expand Down Expand Up @@ -1484,7 +1493,7 @@ public function testFailureFlowWithFailureOnAddModuleWithoutBuildDebugModeOff():
$connected = Package::new($this->stubProperties());
$connected->boot();

Monkey\Actions\expectDone($package->hookName(Package::ACTION_FAILED_BUILD))
Monkey\Actions\expectDone($package->hookName(Package::ACTION_FAILED_ADD_MODULE))
->once()
->whenHappen(
static function (\Throwable $throwable) use ($exception, $package): void {
Expand Down

0 comments on commit 127523b

Please sign in to comment.