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
35 changes: 21 additions & 14 deletions src/Deployer/Task/After/CachetoolTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ class CachetoolTask extends TaskBase
/**
* @var string[]
*
* CacheTool 9.x/10.x works with PHP >=8.1
* CacheTool 8.x works with PHP >=8.0
* CacheTool 7.x works with PHP >=7.3
* CacheTool 6.x works with PHP >=7.3
* CacheTool 5.x works with PHP >=7.2
* CacheTool 4.x works with PHP >=7.1
*/
private $versionBinaryMapping = [
8 => 'https://github.com/gordalina/cachetool/releases/download/8.4.0/cachetool.phar',
10 => 'https://github.com/gordalina/cachetool/releases/download/10.0.0/cachetool.phar',
8 => 'https://github.com/gordalina/cachetool/releases/download/8.6.1/cachetool.phar',
7 => 'https://github.com/gordalina/cachetool/releases/download/7.1.0/cachetool.phar',
6 => 'https://github.com/gordalina/cachetool/releases/download/6.6.0/cachetool.phar',
5 => 'https://github.com/gordalina/cachetool/releases/download/5.1.3/cachetool.phar',
Expand All @@ -35,7 +37,6 @@ class CachetoolTask extends TaskBase

public function register(): void
{
after('deploy:symlink', 'cachetool:clear:opcache');
after('cachetool:clear:opcache', 'cachetool:cleanup');
}

Expand All @@ -52,10 +53,10 @@ public function configure(Configuration $config): void
$cachetoolBinary = get('cachetool_binary');

within('{{release_path}}', function () {
run('curl -L -o cachetool.phar ' . $this->getCachetoolUrl());
$phpVersion = $this->getPhpVersion();
$cachetoolBinary = '{{release_path}}/cachetool.phar';

writeln(sprintf("Downloaded cachetool %s for PHP %f", $cachetoolBinary, $this->getPhpVersion()));
run('curl -L -o cachetool.phar ' . $this->getCachetoolUrl($phpVersion));
writeln(sprintf("Downloaded cachetool %s for PHP %s", $cachetoolBinary, $phpVersion));
return $cachetoolBinary;
});
return $cachetoolBinary;
Expand Down Expand Up @@ -103,30 +104,36 @@ public function configure(Configuration $config): void
});
}

protected function getPhpVersion(): float
protected function getPhpVersion(): string
{
return (float) run('{{bin/php}} -r "echo PHP_VERSION . \" - \" . PHP_VERSION_ID;"');
return run('{{bin/php}} -r "echo PHP_VERSION;"');
}

public function getCachetoolUrl(): string
public function getCachetoolUrl(?string $phpVersion = null): string
{
$phpVersion = $this->getPhpVersion();
if ($phpVersion >= 8.0) {
$phpVersion = $phpVersion ?? $this->getPhpVersion();

if (version_compare($phpVersion, '8.1.0', '>=')) {
return $this->versionBinaryMapping[10];
}

if (version_compare($phpVersion, '8.0.0', '>=')) {
return $this->versionBinaryMapping[8];
}

if ($phpVersion >= 7.3) {
if (version_compare($phpVersion, '7.3.0', '>=')) {
return $this->versionBinaryMapping[7];
}

if ($phpVersion >= 7.2) {
if (version_compare($phpVersion, '7.2.0', '>=')) {
return $this->versionBinaryMapping[5];
}

if ($phpVersion >= 7.1) {
if (version_compare($phpVersion, '7.1.0', '>=')) {
return $this->versionBinaryMapping[4];
}

return $this->versionBinaryMapping[8];
// Default to latest for unknown/newer PHP versions
return $this->versionBinaryMapping[10];
}
}
85 changes: 85 additions & 0 deletions tests/Unit/Deployer/Task/After/CachetoolTaskTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

declare(strict_types=1);

namespace Hypernode\Deploy\Tests\Unit\Deployer\Task\After;

use Hypernode\Deploy\Deployer\Task\After\CachetoolTask;
use PHPUnit\Framework\TestCase;

class CachetoolTaskTest extends TestCase
{
private CachetoolTask $task;

protected function setUp(): void
{
$this->task = new CachetoolTask();
}

/**
* @dataProvider phpVersionToCachetoolUrlProvider
*/
public function testGetCachetoolUrlReturnsCorrectVersionForPhpVersion(
string $phpVersion,
string $expectedUrlPart
): void {
$result = $this->task->getCachetoolUrl($phpVersion);

$this->assertStringContainsString($expectedUrlPart, $result);
}

/**
* @return array<string, array{string, string}>
*/
public static function phpVersionToCachetoolUrlProvider(): array
{
return [
// PHP 8.1+ should get cachetool 10.x
'PHP 8.3.15 gets cachetool 10' => ['8.3.15', '10.0.0'],
'PHP 8.2.0 gets cachetool 10' => ['8.2.0', '10.0.0'],
'PHP 8.1.0 gets cachetool 10' => ['8.1.0', '10.0.0'],
'PHP 8.1.27 gets cachetool 10' => ['8.1.27', '10.0.0'],

// PHP 8.0.x should get cachetool 8.x
'PHP 8.0.0 gets cachetool 8' => ['8.0.0', '8.6.1'],
'PHP 8.0.30 gets cachetool 8' => ['8.0.30', '8.6.1'],

// PHP 7.3+ should get cachetool 7.x
'PHP 7.4.33 gets cachetool 7' => ['7.4.33', '7.1.0'],
'PHP 7.3.0 gets cachetool 7' => ['7.3.0', '7.1.0'],
'PHP 7.3.33 gets cachetool 7' => ['7.3.33', '7.1.0'],

// PHP 7.2.x should get cachetool 5.x
'PHP 7.2.0 gets cachetool 5' => ['7.2.0', '5.1.3'],
'PHP 7.2.34 gets cachetool 5' => ['7.2.34', '5.1.3'],

// PHP 7.1.x should get cachetool 4.x
'PHP 7.1.0 gets cachetool 4' => ['7.1.0', '4.1.1'],
'PHP 7.1.33 gets cachetool 4' => ['7.1.33', '4.1.1'],

// Unsupported/old PHP versions fall back to latest (10.x)
'PHP 7.0.33 falls back to cachetool 10' => ['7.0.33', '10.0.0'],
'PHP 5.6.40 falls back to cachetool 10' => ['5.6.40', '10.0.0'],

// Future PHP versions should get latest (10.x)
'PHP 9.0.0 gets cachetool 10' => ['9.0.0', '10.0.0'],
'PHP 8.4.0 gets cachetool 10' => ['8.4.0', '10.0.0'],
];
}

public function testGetCachetoolUrlReturnsFullGithubUrl(): void
{
$result = $this->task->getCachetoolUrl('8.2.0');

$this->assertStringStartsWith('https://github.com/gordalina/cachetool/releases/download/', $result);
$this->assertStringEndsWith('/cachetool.phar', $result);
}

public function testGetCachetoolUrlForPhp71ReturnsLegacyUrl(): void
{
$result = $this->task->getCachetoolUrl('7.1.0');

// Cachetool 4.x uses a different URL format (gordalina.github.io)
$this->assertStringStartsWith('https://gordalina.github.io/cachetool/downloads/', $result);
}
}
Loading