Skip to content

Commit

Permalink
Make Package::hasReachedStatus() stricter in statis checking
Browse files Browse the repository at this point in the history
Not allowing random states passed as argument.
Moreover, add test for hasReachedStatus.
  • Loading branch information
gmazzap committed Aug 28, 2024
1 parent 4e8e910 commit 18f63b1
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,14 @@ class Package
public const STATUS_BOOTED = 8;
public const STATUS_FAILED = -8;

private const SUCCESS_STATUSES = [
self::STATUS_IDLE => self::STATUS_IDLE,
self::STATUS_INITIALIZED => self::STATUS_INITIALIZED,
self::STATUS_BOOTING => self::STATUS_BOOTING,
self::STATUS_READY => self::STATUS_READY,
self::STATUS_BOOTED => self::STATUS_BOOTED,
];

private const OPERATORS = [
'<' => '<',
'<=' => '<=',
Expand Down Expand Up @@ -275,7 +283,7 @@ public function connect(Package $package): bool

// Don't connect, if already booted or boot failed
$failed = $this->hasFailed();
if ($failed || $this->checkStatus(self::STATUS_INITIALIZED, '>=')) {
if ($failed || $this->hasReachedStatus(self::STATUS_INITIALIZED)) {
$reason = $failed ? 'an errored package' : 'a package with a built container';
$status = $failed ? 'failed' : 'built_container';
$error = "{$errorMessage} to {$reason}.";
Expand Down Expand Up @@ -645,7 +653,11 @@ public function hasFailed(): bool
*/
public function hasReachedStatus(int $status): bool
{
return ($this->status !== self::STATUS_FAILED) && $this->checkStatus($status, '>=');
if (!isset(self::SUCCESS_STATUSES[$status])) {
return false;

Check warning on line 657 in src/Package.php

View check run for this annotation

Codecov / codecov/patch

src/Package.php#L657

Added line #L657 was not covered by tests
}

return !$this->hasFailed() && $this->checkStatus($status, '>=');
}

/**
Expand Down
20 changes: 20 additions & 0 deletions tests/unit/PackageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,27 @@ public function testBasic(): void
$package = Package::new($propertiesStub);

static::assertTrue($package->statusIs(Package::STATUS_IDLE));
static::assertTrue($package->hasReachedStatus(Package::STATUS_IDLE));
static::assertFalse($package->hasReachedStatus(Package::STATUS_INITIALIZED));
static::assertFalse($package->hasReachedStatus(Package::STATUS_BOOTING));
static::assertFalse($package->hasReachedStatus(Package::STATUS_BOOTED));

$package->build();

static::assertFalse($package->statusIs(Package::STATUS_IDLE));
static::assertTrue($package->hasReachedStatus(Package::STATUS_IDLE));
static::assertTrue($package->hasReachedStatus(Package::STATUS_INITIALIZED));
static::assertFalse($package->hasReachedStatus(Package::STATUS_BOOTING));
static::assertFalse($package->hasReachedStatus(Package::STATUS_BOOTED));

static::assertTrue($package->boot());

static::assertTrue($package->statusIs(Package::STATUS_BOOTED));
static::assertTrue($package->hasReachedStatus(Package::STATUS_IDLE));
static::assertTrue($package->hasReachedStatus(Package::STATUS_INITIALIZED));
static::assertTrue($package->hasReachedStatus(Package::STATUS_BOOTING));
static::assertTrue($package->hasReachedStatus(Package::STATUS_BOOTED));

static::assertSame($expectedName, $package->name());
static::assertInstanceOf(Properties::class, $package->properties());
static::assertInstanceOf(ContainerInterface::class, $package->container());
Expand Down Expand Up @@ -901,6 +920,7 @@ public function testFailureFlowWithFailureOnBootDebugModeOff(): void
static::assertFalse($package->boot());
static::assertTrue($package->statusIs(Package::STATUS_FAILED));
static::assertTrue($package->hasFailed());
static::assertFalse($package->hasReachedStatus(Package::STATUS_IDLE));
}

/**
Expand Down

0 comments on commit 18f63b1

Please sign in to comment.