Skip to content

Commit daa718c

Browse files
authored
Merge pull request #156 from cakephp/packagist-best-practice
adhere to packagist.org best practice
2 parents 13e1e1d + 36235f4 commit daa718c

2 files changed

Lines changed: 52 additions & 4 deletions

File tree

src/Command/SyncPackagesCommand.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
use Cake\Log\Log;
1212
use Composer\Semver\Intervals;
1313
use Composer\Semver\VersionParser;
14-
use Packagist\Api\Client;
14+
use GuzzleHttp\Client as HttpClient;
15+
use Packagist\Api\Client as PackagistClient;
1516
use Packagist\Api\Result\Package\Version;
1617
use UnexpectedValueException;
1718

@@ -47,7 +48,7 @@ class SyncPackagesCommand extends Command
4748
'5' => [0, 1, 2, 3],
4849
];
4950

50-
private readonly Client $client;
51+
private readonly PackagistClient $client;
5152

5253
/**
5354
* The name of this command.
@@ -71,7 +72,7 @@ public static function defaultName(): string
7172
*/
7273
public static function getDescription(): string
7374
{
74-
return 'Command description here.';
75+
return 'Sync all packages from packagist.org marked cakephp-plugin';
7576
}
7677

7778
/**
@@ -81,7 +82,23 @@ public function __construct(
8182
?CommandFactoryInterface $factory = null,
8283
) {
8384
parent::__construct($factory);
84-
$this->client = new Client();
85+
$this->client = new PackagistClient($this->createPackagistHttpClient());
86+
}
87+
88+
/**
89+
* @return \GuzzleHttp\Client
90+
*/
91+
private function createPackagistHttpClient(): HttpClient
92+
{
93+
return new HttpClient([
94+
'headers' => [
95+
'User-Agent' => env(
96+
'PACKAGIST_USER_AGENT',
97+
'plugins.cakephp.org (https://plugins.cakephp.org; mailto=security@cakephp.org)',
98+
),
99+
],
100+
'version' => 2.0,
101+
]);
85102
}
86103

87104
/**
@@ -106,6 +123,7 @@ public function execute(Arguments $args, ConsoleIo $io): void
106123
$failed = 0;
107124
$i = 0;
108125

126+
/** @var \Cake\Command\Helper\ProgressHelper $progress */
109127
$progress = $io->helper('Progress');
110128
$progress->init(['total' => $total, 'width' => 60]);
111129
$io->out('', 0);

tests/TestCase/Command/SyncPackagesCommandTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Cake\Console\TestSuite\ConsoleIntegrationTestTrait;
88
use Cake\I18n\Date;
99
use Cake\TestSuite\TestCase;
10+
use GuzzleHttp\Client;
1011
use Packagist\Api\Result\Package\Version;
1112
use ReflectionMethod;
1213

@@ -48,6 +49,35 @@ public function testExtractReleaseDate(): void
4849
$this->assertNull($method->invoke($command, null));
4950
}
5051

52+
/**
53+
* @return void
54+
*/
55+
public function testCreatePackagistHttpClientUsesApiBestPractices(): void
56+
{
57+
$userAgent = 'plugins.cakephp.org-test (mailto=test@example.com)';
58+
$previousUserAgent = $_ENV['PACKAGIST_USER_AGENT'] ?? null;
59+
$_ENV['PACKAGIST_USER_AGENT'] = $userAgent;
60+
61+
try {
62+
$command = new SyncPackagesCommand();
63+
$method = new ReflectionMethod($command, 'createPackagistHttpClient');
64+
65+
/** @var \GuzzleHttp\Client $client */
66+
$client = $method->invoke($command);
67+
} finally {
68+
if ($previousUserAgent === null) {
69+
unset($_ENV['PACKAGIST_USER_AGENT']);
70+
} else {
71+
$_ENV['PACKAGIST_USER_AGENT'] = $previousUserAgent;
72+
}
73+
}
74+
75+
$this->assertInstanceOf(Client::class, $client);
76+
$this->assertSame(2.0, $client->getConfig('version'));
77+
$this->assertSame($userAgent, $client->getConfig('headers')['User-Agent']);
78+
$this->assertStringContainsString('mailto=', $client->getConfig('headers')['User-Agent']);
79+
}
80+
5181
/**
5282
* Test defaultName method
5383
*

0 commit comments

Comments
 (0)