Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions legacy/phpstan-stubs/Activity.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Platformsh\Client\Model;

/**
* Local PHPStan stub for the Activity model.
*
* The upstream docblock types properties as non-nullable strings, but the API
* returns null for several properties until the activity reaches a particular
* state (e.g. result is null until the activity finishes). The list and
* nullability here follow the Upsun OpenAPI spec
* (https://developer.upsun.com/openapi.json).
*
* @property-read string $id
* @property-read int $completion_percent
* @property-read string $log
* @property-read string|null $created_at
* @property-read string|null $updated_at
* @property-read string[] $environments
* @property-read string|null $started_at
* @property-read string|null $completed_at
* @property-read string|null $cancelled_at
* @property-read string|null $expires_at
* @property-read array $parameters
* @property-read string $project
* @property-read string|null $integration
* @property-read string $state
* @property-read string|null $result
* @property-read string $type
* @property-read string|null $description
* @property-read string|null $text
* @property-read array $payload
* @property-read array $timings
* @property-read array $commands
*/
Comment thread
pjcdawkins marked this conversation as resolved.
class Activity
{
}
2 changes: 2 additions & 0 deletions legacy/phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ parameters:
- dist/installer.php
excludePaths:
- dist/installer.php
stubFiles:
- phpstan-stubs/Activity.stub

includes:
- phpstan-baseline.neon
3 changes: 2 additions & 1 deletion legacy/src/Service/ActivityMonitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,8 @@ public static function formatState(string $state): string
*/
public static function formatResult(Activity $activity, bool $decorate = true): string
{
$result = $activity->result;
// The result is null until the activity has finished.
$result = $activity->result ?? '';
$name = self::RESULT_NAMES[$result] ?? $result;

foreach ($activity->commands ?? [] as $command) {
Expand Down
60 changes: 60 additions & 0 deletions legacy/tests/Service/ActivityMonitorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace Platformsh\Cli\Tests\Service;

use PHPUnit\Framework\TestCase;
use Platformsh\Cli\Service\ActivityMonitor;
use Platformsh\Client\Model\Activity;

class ActivityMonitorTest extends TestCase
{
public function testFormatResultSuccess(): void
{
$activity = new Activity(['id' => 'a', 'result' => Activity::RESULT_SUCCESS]);
$this->assertSame('success', ActivityMonitor::formatResult($activity, false));
}

public function testFormatResultFailure(): void
{
$activity = new Activity(['id' => 'a', 'result' => Activity::RESULT_FAILURE]);
$this->assertSame('failure', ActivityMonitor::formatResult($activity, false));
$this->assertSame('<error>failure</error>', ActivityMonitor::formatResult($activity, true));
}

public function testFormatResultNull(): void
{
// An in-progress activity has no result yet.
$activity = new Activity(['id' => 'a', 'state' => Activity::STATE_IN_PROGRESS, 'result' => null]);
$this->assertSame('', ActivityMonitor::formatResult($activity, false));
$this->assertSame('', ActivityMonitor::formatResult($activity, true));
Comment thread
pjcdawkins marked this conversation as resolved.
}

public function testFormatResultFailedCommandOverridesSuccess(): void
{
$activity = new Activity([
'id' => 'a',
'result' => Activity::RESULT_SUCCESS,
'commands' => [
['exit_code' => 0],
['exit_code' => 1],
],
]);
$this->assertSame('failure', ActivityMonitor::formatResult($activity, false));
$this->assertSame('<error>failure</error>', ActivityMonitor::formatResult($activity, true));
}

public function testFormatResultFailedCommandWithNullResult(): void
{
$activity = new Activity([
'id' => 'a',
'state' => Activity::STATE_IN_PROGRESS,
'result' => null,
'commands' => [
['exit_code' => 1],
],
]);
Comment thread
pjcdawkins marked this conversation as resolved.
$this->assertSame('failure', ActivityMonitor::formatResult($activity, false));
}
}
Loading