diff --git a/src/Package.php b/src/Package.php index 11d5975..1337de4 100644 --- a/src/Package.php +++ b/src/Package.php @@ -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. @@ -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; @@ -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) ); diff --git a/tests/unit/PackageTest.php b/tests/unit/PackageTest.php index f08354e..55785fa 100644 --- a/tests/unit/PackageTest.php +++ b/tests/unit/PackageTest.php @@ -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)); @@ -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)); @@ -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)); @@ -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)); @@ -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)); @@ -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)); @@ -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 { @@ -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( @@ -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)); } @@ -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 {