Skip to content

Commit a3d6de2

Browse files
committedFeb 17, 2023
Replace Guzzle with Symfony HTTP client
1 parent e39cdb9 commit a3d6de2

13 files changed

+31
-53
lines changed
 

‎Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
DOCKER_RUN = docker-compose run --rm php
1+
DOCKER_RUN = docker compose run --rm php
22

33
.env.local: .env
44
cp -n .env .env.local

‎src/Api/Job/JobTestMetadata.php

+2-6
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,9 @@ class JobTestMetadata
1919
{
2020
use ValidateClientVersionTrait;
2121

22-
/** @var Client */
23-
private $client;
24-
25-
public function __construct(Client $client)
22+
public function __construct(private readonly Client $client)
2623
{
27-
$this->validateClientVersion($client, ['v2']);
28-
$this->client = $client;
24+
$this->validateClientVersion($this->client, ['v2']);
2925
}
3026

3127
/**

‎src/Api/Pipeline/AllPipelines.php

+4-8
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,21 @@
1212
* Retrieve pipelines of a project, optionally filtered by branch.
1313
*
1414
* @author jmleroux <jmleroux.pro@gmail.com>
15-
* @link https://circleci.com/docs/api/v2/#operation/listPipelinesForProject
15+
* @link https://circleci.com/docs/api/v2/#operation/listPipelinesForProject
1616
*/
1717
class AllPipelines
1818
{
1919
use ValidateClientVersionTrait;
2020

21-
/** @var Client */
22-
private $client;
23-
24-
public function __construct(Client $client)
21+
public function __construct(private readonly Client $client)
2522
{
2623
$this->validateClientVersion($client, ['v2']);
27-
$this->client = $client;
2824
}
2925

3026
/**
3127
* @return Pipeline[]
3228
*/
33-
public function execute(string $projectSlug, ?int $maxPipelineCount, ?string $branch): array
29+
public function execute(string $projectSlug, ?int $maxPipelineCount = null, ?string $branch = null): array
3430
{
3531
$pipelines = [];
3632

@@ -52,7 +48,7 @@ public function execute(string $projectSlug, ?int $maxPipelineCount, ?string $br
5248
unset($params['branch']);
5349
}
5450

55-
$response = json_decode((string) $this->client->get($uri, $params)->getContent());
51+
$response = json_decode((string)$this->client->get($uri, $params)->getContent());
5652
$nextPageToken = $response->next_page_token;
5753

5854
foreach ($response->items as $item) {

‎src/Api/Workflow/CancelWorkflow.php

+1-5
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,9 @@ class CancelWorkflow
1717
{
1818
use ValidateClientVersionTrait;
1919

20-
/** @var Client */
21-
private $client;
22-
23-
public function __construct(Client $client)
20+
public function __construct(private Client $client)
2421
{
2522
$this->validateClientVersion($client, ['v2']);
26-
$this->client = $client;
2723
}
2824

2925
public function execute(string $workflowId): void

‎src/Client.php

+2-7
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,16 @@
1111
class Client
1212
{
1313
protected HttpClientInterface $client;
14-
protected string $token;
1514

16-
public function __construct(string $token, ?string $version = 'v1.1')
15+
public function __construct(private readonly string $token, private readonly ?string $version = 'v1.1')
1716
{
1817
$baseUri = sprintf('https://circleci.com/api/%s/', $version);
1918
$this->client = HttpClient::create(['base_uri' => $baseUri]);
20-
$this->token = $token;
2119
}
2220

2321
public function getVersion(): string
2422
{
25-
$baseUri = 'foo';
26-
preg_match('#circleci.com/api/(v[\d\.]+)/#', $baseUri, $result);
27-
28-
return $result[1];
23+
return $this->version;
2924
}
3025

3126
public function get(string $url, array $params = []): ResponseInterface

‎src/ValidateClientVersionTrait.php

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
trait ValidateClientVersionTrait
1313
{
1414
/**
15-
* @param Client $client
1615
* @param string[] $supportedVersions
1716
*/
1817
private function validateClientVersion(Client $client, array $supportedVersions): void

‎tests/Integration/Api/Pipeline/AllPipelinesTest.php

+2-6
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,9 @@ class AllPipelinesTest extends TestCase
2121

2222
private Client $client;
2323

24-
public static function setUpBeforeClass(): void
25-
{
26-
MockServer::startServer();
27-
}
28-
2924
public function setUp(): void
3025
{
26+
MockServer::startServer();
3127
$personaltoken = $_ENV['CIRCLECI_PERSONNAL_TOKEN'];
3228
$this->client = new TestClient(MockServer::getServerRoot(), $personaltoken, 'v2');
3329
}
@@ -69,7 +65,7 @@ public function testQueryWithBranch()
6965
$this->assertIsArray($pipelines);
7066
foreach ($pipelines as $pipeline) {
7167
$this->assertInstanceOf(Pipeline::class, $pipeline);
72-
$this->assertEquals('master', $pipeline->vcs()->branch());
68+
$this->assertEquals('feature/design-new-api', $pipeline->vcs()->branch());
7369
}
7470
}
7571
}

‎tests/Integration/Api/Project/BuildSummaryForBranchTest.php

+6-9
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ public function testQueryOk()
3131
$this->assertIsInt($firstBuild->buildNum());
3232
$this->assertIsString($firstBuild->branch());
3333
$this->assertIsString($firstBuild->vcsRevision());
34-
$this->assertIsString($firstBuild->committerName());
35-
$this->assertIsString($firstBuild->committerEmail());
36-
$this->assertIsString($firstBuild->body());
3734
$this->assertIsString($firstBuild->why());
3835
$this->assertNull($firstBuild->dontBuild());
3936
if (null !== $firstBuild->queuedAt()) {
@@ -60,13 +57,13 @@ public function testMaxResults()
6057
$personalToken = $_ENV['CIRCLECI_PERSONNAL_TOKEN'];
6158
$client = new Client($personalToken, 'v1.1');
6259
$query = new BuildSummaryForBranch($client);
63-
$builds = $this->executeWithRetry($query, ['github', 'jmleroux', 'circleci-php-client', 'master', [], 8]);
64-
$this->assertCount(8, $builds);
60+
$builds = $this->executeWithRetry($query, ['github', 'jmleroux', 'circleci-php-client', 'master', [], 2]);
61+
$this->assertCount(2, $builds);
6562

66-
$builds = $query->execute('github', 'jmleroux', 'circleci-php-client', 'master', [], 50);
67-
$this->assertCount(50, $builds);
63+
$builds = $query->execute('github', 'jmleroux', 'circleci-php-client', 'master', [], 8);
64+
$this->assertCount(8, $builds);
6865

69-
$builds = $query->execute('github', 'jmleroux', 'circleci-php-client', 'master', ['limit' => 6], 22);
70-
$this->assertCount(22, $builds);
66+
$builds = $query->execute('github', 'jmleroux', 'circleci-php-client', 'master', ['limit' => 6], 8);
67+
$this->assertCount(8, $builds);
7168
}
7269
}

‎tests/Integration/Api/Project/ProjectSummaryMetricsTest.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ class ProjectSummaryMetricsTest extends TestCase
2020
{
2121
use ExecuteWithRetryTrait;
2222

23-
/** @var Client */
24-
private $client;
23+
private Client $client;
2524

2625
public function setUp(): void
2726
{

‎tests/Integration/Api/Workflow/CancelWorkflowTest.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
namespace Jmleroux\CircleCi\Tests\Integration\Api\Workflow;
66

7-
use GuzzleHttp\Exception\RequestException;
87
use Jmleroux\CircleCi\Api\Workflow\CancelWorkflow;
98
use Jmleroux\CircleCi\Client;
109
use PHPUnit\Framework\TestCase;
10+
use Symfony\Component\HttpClient\Exception\ClientException;
1111

1212
class CancelWorkflowTest extends TestCase
1313
{
@@ -22,9 +22,8 @@ public function setUp(): void
2222

2323
public function testQueryKo()
2424
{
25-
$this->expectException(RequestException::class);
25+
$this->expectException(ClientException::class);
2626
$this->expectExceptionCode(404);
27-
$this->expectExceptionMessage('{"message":"Workflow not found"}');
2827

2928
$query = new CancelWorkflow($this->client);
3029
$query->execute('foobar');

‎tests/Integration/Api/Workflow/WorkflowJobsTest.php

+8-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Jmleroux\CircleCi\Tests\Integration\Api\Workflow;
66

7+
use Jmleroux\CircleCi\Api\Pipeline\AllPipelines;
8+
use Jmleroux\CircleCi\Api\Pipeline\PipelineWorkflows;
79
use Jmleroux\CircleCi\Api\Workflow\WorkflowJobs;
810
use Jmleroux\CircleCi\Client;
911
use Jmleroux\CircleCi\Model\Job;
@@ -15,8 +17,7 @@ class WorkflowJobsTest extends TestCase
1517
{
1618
use ExecuteWithRetryTrait;
1719

18-
/** @var Client */
19-
private $client;
20+
private Client $client;
2021

2122
public function setUp(): void
2223
{
@@ -26,8 +27,12 @@ public function setUp(): void
2627

2728
public function testQuery()
2829
{
30+
$query = new AllPipelines($this->client);
31+
$pipelines = $query->execute('gh/jmleroux/circleci-php-client', 1);
32+
$query = new PipelineWorkflows($this->client);
33+
$workflows = $query->execute($pipelines[0]->id(), 1);
2934
$query = new WorkflowJobs($this->client);
30-
$jobs = $this->executeWithRetry($query, ['78176b2f-c1e3-4c18-86e3-5a80a2f109bb']);
35+
$jobs = $this->executeWithRetry($query, [$workflows[0]->id()]);
3136
Assert::assertIsArray($jobs);
3237

3338
$firstJob = $jobs[0];

‎tests/Integration/ExecuteWithRetryTrait.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Jmleroux\CircleCi\Tests\Integration;
66

7-
use GuzzleHttp\Exception\ClientException;
7+
use Symfony\Component\HttpClient\Exception\ClientException;
88

99
/**
1010
* @author JM Leroux <jmleroux.pro@gmail.com>

‎tests/Integration/TestClient.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ public function __construct(
1717
string $token,
1818
?string $version = 'v1.1'
1919
) {
20+
parent::__construct($token, $version);
2021
$baseUri = sprintf('%s/api/%s/', $mockServerBaseUrl, $version);
2122
$this->client = HttpClient::create(['base_uri' => $baseUri]);
22-
$this->token = $token;
2323
}
2424
}

0 commit comments

Comments
 (0)
Please sign in to comment.