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
26 changes: 22 additions & 4 deletions src/Command/SyncPackagesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
use Cake\Log\Log;
use Composer\Semver\Intervals;
use Composer\Semver\VersionParser;
use Packagist\Api\Client;
use GuzzleHttp\Client as HttpClient;
use Packagist\Api\Client as PackagistClient;
use Packagist\Api\Result\Package\Version;
use UnexpectedValueException;

Expand Down Expand Up @@ -47,7 +48,7 @@ class SyncPackagesCommand extends Command
'5' => [0, 1, 2, 3],
];

private readonly Client $client;
private readonly PackagistClient $client;

/**
* The name of this command.
Expand All @@ -71,7 +72,7 @@ public static function defaultName(): string
*/
public static function getDescription(): string
{
return 'Command description here.';
return 'Sync all packages from packagist.org marked cakephp-plugin';
}

/**
Expand All @@ -81,7 +82,23 @@ public function __construct(
?CommandFactoryInterface $factory = null,
) {
parent::__construct($factory);
$this->client = new Client();
$this->client = new PackagistClient($this->createPackagistHttpClient());
}

/**
* @return \GuzzleHttp\Client
*/
private function createPackagistHttpClient(): HttpClient
{
return new HttpClient([
'headers' => [
'User-Agent' => env(
'PACKAGIST_USER_AGENT',
'plugins.cakephp.org (https://plugins.cakephp.org; mailto=security@cakephp.org)',
),
],
'version' => 2.0,
]);
}

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

/** @var \Cake\Command\Helper\ProgressHelper $progress */
$progress = $io->helper('Progress');
$progress->init(['total' => $total, 'width' => 60]);
$io->out('', 0);
Expand Down
30 changes: 30 additions & 0 deletions tests/TestCase/Command/SyncPackagesCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Cake\Console\TestSuite\ConsoleIntegrationTestTrait;
use Cake\I18n\Date;
use Cake\TestSuite\TestCase;
use GuzzleHttp\Client;
use Packagist\Api\Result\Package\Version;
use ReflectionMethod;

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

/**
* @return void
*/
public function testCreatePackagistHttpClientUsesApiBestPractices(): void
{
$userAgent = 'plugins.cakephp.org-test (mailto=test@example.com)';
$previousUserAgent = $_ENV['PACKAGIST_USER_AGENT'] ?? null;
$_ENV['PACKAGIST_USER_AGENT'] = $userAgent;

try {
$command = new SyncPackagesCommand();
$method = new ReflectionMethod($command, 'createPackagistHttpClient');

/** @var \GuzzleHttp\Client $client */
$client = $method->invoke($command);
} finally {
if ($previousUserAgent === null) {
unset($_ENV['PACKAGIST_USER_AGENT']);
} else {
$_ENV['PACKAGIST_USER_AGENT'] = $previousUserAgent;
}
}

$this->assertInstanceOf(Client::class, $client);
$this->assertSame(2.0, $client->getConfig('version'));
$this->assertSame($userAgent, $client->getConfig('headers')['User-Agent']);
$this->assertStringContainsString('mailto=', $client->getConfig('headers')['User-Agent']);
}

/**
* Test defaultName method
*
Expand Down
Loading