Skip to content

Commit

Permalink
Update test
Browse files Browse the repository at this point in the history
  • Loading branch information
jmleroux committed Feb 17, 2023
1 parent 9883ef1 commit 5c867c6
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 17 deletions.
15 changes: 9 additions & 6 deletions tests/Integration/Api/Project/ProjectSummaryMetricsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use Jmleroux\CircleCi\Model\JobMetrics;
use Jmleroux\CircleCi\Model\WorkflowSummaryResult;
use Jmleroux\CircleCi\Tests\Integration\ExecuteWithRetryTrait;
use Jmleroux\CircleCi\Tests\Integration\TestClient;
use Jmleroux\CircleCi\Tests\MockServer;
use PHPUnit\Framework\TestCase;

/**
Expand All @@ -24,18 +26,19 @@ class ProjectSummaryMetricsTest extends TestCase

public function setUp(): void
{
$personnalToken = $_ENV['CIRCLECI_PERSONNAL_TOKEN'];
$this->client = new Client($personnalToken, 'v2');
MockServer::startServer();
$personalToken = $_ENV['CIRCLECI_PERSONNAL_TOKEN'];
$this->client = new TestClient(MockServer::getServerRoot(), $personalToken, 'v2');
}

public function testQuery()
{
$query = new ProjectSummaryMetrics($this->client);

$summaryResults = $this->executeWithRetry($query, ['gh/jmleroux/circleci-php-client']);
$summaryResults = $query->execute('gh/jmleroux/my_project');
$this->assertIsArray($summaryResults);
$this->assertCount(1, $summaryResults);
$this->assertSame('build_test', $summaryResults[0]->name());
$this->assertSame('build-and-test', $summaryResults[0]->name());

$firstResult = $summaryResults[0];
$this->assertInstanceOf(WorkflowSummaryResult::class, $firstResult);
Expand Down Expand Up @@ -65,9 +68,9 @@ public function testQueryMaxResults()
{
$query = new ProjectSummaryMetrics($this->client);

$summaryResults = $this->executeWithRetry($query, ['gh/jmleroux/circleci-php-client', [], 1]);
$summaryResults = $query->execute('gh/jmleroux/my_project', [], 1);
$this->assertIsArray($summaryResults);
$this->assertCount(1, $summaryResults);
$this->assertSame('build_test', $summaryResults[0]->name());
$this->assertSame('build-and-test', $summaryResults[0]->name());
}
}
27 changes: 16 additions & 11 deletions tests/Integration/Api/Workflow/WorkflowSummaryMetricsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use Jmleroux\CircleCi\Model\JobMetrics;
use Jmleroux\CircleCi\Model\JobSummaryResult;
use Jmleroux\CircleCi\Tests\Integration\ExecuteWithRetryTrait;
use Jmleroux\CircleCi\Tests\Integration\TestClient;
use Jmleroux\CircleCi\Tests\MockServer;
use PHPUnit\Framework\TestCase;

/**
Expand All @@ -20,24 +22,24 @@ class WorkflowSummaryMetricsTest extends TestCase
{
use ExecuteWithRetryTrait;

/** @var Client */
private $client;
private Client $client;

public function setUp(): void
{
$personnalToken = $_ENV['CIRCLECI_PERSONNAL_TOKEN'];
$this->client = new Client($personnalToken, 'v2');
MockServer::startServer();
$personalToken = $_ENV['CIRCLECI_PERSONNAL_TOKEN'];
$this->client = new TestClient(MockServer::getServerRoot(), $personalToken, 'v2');
}

public function testQuery()
{
$query = new WorkflowSummaryMetrics($this->client);

$summaryResults = $this->executeWithRetry($query, ['gh/jmleroux/circleci-php-client', 'build_test']);
$summaryResults = $query->execute('gh/jmleroux/my_project', 'my_workflow_name');
$this->assertIsArray($summaryResults);
$this->assertCount(2, $summaryResults);
$this->assertSame('build', $summaryResults[0]->name());
$this->assertSame('tests', $summaryResults[1]->name());
$this->assertSame('job-summary-1', $summaryResults[0]->name());
$this->assertSame('job-summary-2', $summaryResults[1]->name());

$firstResult = $summaryResults[0];
$this->assertInstanceOf(JobSummaryResult::class, $firstResult);
Expand Down Expand Up @@ -67,17 +69,20 @@ public function testQueryMaxResults()
{
$query = new WorkflowSummaryMetrics($this->client);

$summaryResults = $this->executeWithRetry($query, ['gh/jmleroux/circleci-php-client', 'build_test', [], 1]);
$summaryResults = $query->execute('gh/jmleroux/my_project', 'my_workflow_name', [], 1);
$this->assertIsArray($summaryResults);
$this->assertCount(1, $summaryResults);
$this->assertSame('build', $summaryResults[0]->name());
$this->assertSame('job-summary-1', $summaryResults[0]->name());
}

public function testQueryUnknownWorkflow()
{
$query = new WorkflowSummaryMetrics($this->client);
$personnalToken = $_ENV['CIRCLECI_PERSONNAL_TOKEN'];
$client = new Client($personnalToken, 'v2');

$query = new WorkflowSummaryMetrics($client);

$summaryResults = $this->executeWithRetry($query, ['gh/jmleroux/circleci-php-client', 'unknown_workflow']);
$summaryResults = $query->execute('gh/jmleroux/circleci-php-client', 'unknown_workflow');
$this->assertIsArray($summaryResults);
$this->assertEmpty($summaryResults);
}
Expand Down
16 changes: 16 additions & 0 deletions tests/MockServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,22 @@ public static function startServer(): void
)
)
);
self::$server->setResponseOfPath(
'/api/v2/insights/gh/jmleroux/my_project/workflows',
new Response(
file_get_contents(__DIR__ . '/resources/response/insights-workflow.json'),
['Cache-Control' => 'no-cache'],
200
),
);
self::$server->setResponseOfPath(
'/api/v2/insights/gh/jmleroux/my_project/workflows/my_workflow_name/jobs',
new Response(
file_get_contents(__DIR__ . '/resources/response/insights-workflow-jobs.json'),
['Cache-Control' => 'no-cache'],
200
),
);
}

public static function getServerRoot(): string
Expand Down
47 changes: 47 additions & 0 deletions tests/resources/response/insights-workflow-jobs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"items": [
{
"name": "job-summary-1",
"window_start": "2019-08-24T14:15:22Z",
"window_end": "2019-08-24T14:15:22Z",
"metrics": {
"success_rate": 0,
"total_runs": 0,
"failed_runs": 0,
"successful_runs": 0,
"throughput": 0,
"total_credits_used": 0,
"duration_metrics": {
"min": 0,
"mean": 0,
"median": 0,
"p95": 0,
"max": 0,
"standard_deviation": 0
}
}
},
{
"name": "job-summary-2",
"window_start": "2019-08-24T14:15:22Z",
"window_end": "2019-08-24T14:15:22Z",
"metrics": {
"success_rate": 0,
"total_runs": 0,
"failed_runs": 0,
"successful_runs": 0,
"throughput": 0,
"total_credits_used": 0,
"duration_metrics": {
"min": 0,
"mean": 0,
"median": 0,
"p95": 0,
"max": 0,
"standard_deviation": 0
}
}
}
],
"next_page_token": null
}
28 changes: 28 additions & 0 deletions tests/resources/response/insights-workflow.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"items": [
{
"name": "build-and-test",
"window_start": "2019-08-24T14:15:22Z",
"window_end": "2019-08-24T14:15:22Z",
"metrics": {
"total_runs": 0,
"successful_runs": 0,
"mttr": 0,
"total_credits_used": 0,
"failed_runs": 0,
"success_rate": 0,
"duration_metrics": {
"min": 0,
"mean": 0,
"median": 0,
"p95": 0,
"max": 0,
"standard_deviation": 0
},
"total_recoveries": 0,
"throughput": 0
}
}
],
"next_page_token": null
}

0 comments on commit 5c867c6

Please sign in to comment.