Skip to content

Commit 894e95a

Browse files
committed
move stuff and add tests
1 parent 99d5aa9 commit 894e95a

File tree

4 files changed

+34
-9
lines changed

4 files changed

+34
-9
lines changed

app/Console/Commands/PerformUpdate.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,6 @@ public function handle(): int
3636
/** @var Site $site */
3737
$site = Site::findOrFail($this->input->getArgument('siteId'));
3838

39-
$updateCount = $site->updates()->where('new_version', $targetVersion)->count();
40-
41-
if ($updateCount >= config('autoupdates.max_update_tries')) {
42-
Log::info("Update Loop detected for Site: " . $site->id . '; TargetVersion: ' . $targetVersion);
43-
44-
return Command::SUCCESS;
45-
}
46-
4739
UpdateSite::dispatchSync(
4840
$site,
4941
$targetVersion

app/Jobs/UpdateSite.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use App\Models\Update;
1010
use App\RemoteSite\Connection;
1111
use App\RemoteSite\Responses\PrepareUpdate;
12+
use Illuminate\Console\Command;
1213
use Illuminate\Contracts\Queue\ShouldQueue;
1314
use Illuminate\Foundation\Queue\Queueable;
1415
use Illuminate\Support\Facades\App;
@@ -31,6 +32,14 @@ public function __construct(protected readonly Site $site, protected string $tar
3132
*/
3233
public function handle(): void
3334
{
35+
$updateCount = $this->site->getUpdateCount($this->targetVersion);
36+
37+
if ($updateCount >= config('autoupdates.max_update_tries')) {
38+
Log::info("Update Loop detected for Site: " . $this->site->id . '; TargetVersion: ' . $this->targetVersion);
39+
40+
return;
41+
}
42+
3443
/** @var Connection $connection */
3544
$connection = $this->site->connection;
3645

app/Models/Site.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ public function getFrontendStatus(): int
6060
return $httpClient->get($this->url)->getStatusCode();
6161
}
6262

63+
public function getUpdateCount(string $targetVersion): int
64+
{
65+
return $this->updates()->where('new_version', $targetVersion)->count();
66+
}
67+
6368
/**
6469
* @return HasMany<Update, $this>
6570
*/

tests/Unit/Jobs/UpdateSiteTest.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,24 @@ public function testJobQuitsIfNoUpdateIsAvailable()
5858
$this->assertTrue(true);
5959
}
6060

61+
public function testJobQuitsIfWeDetectALoopForAVersion()
62+
{
63+
$site = $this->getSiteMock([], null, 6);
64+
65+
Log::spy();
66+
67+
$object = new UpdateSite($site, "1.0.1");
68+
$object->handle();
69+
70+
Log::shouldHaveReceived('info')
71+
->once()
72+
->withArgs(function ($message) {
73+
return str_contains($message, 'Update Loop detected for Site');
74+
});
75+
76+
$this->assertTrue(true);
77+
}
78+
6179
public function testJobQuitsIfAvailableUpdateDoesNotMatchTargetVersion()
6280
{
6381
$site = $this->getSiteMock(
@@ -144,7 +162,7 @@ public function testJobWritesSuccessLogForSuccessfulJobs()
144162
$object->handle();
145163
}
146164

147-
protected function getSiteMock(array $responses, array $expectedLogRow = null)
165+
protected function getSiteMock(array $responses, array $expectedLogRow = null, int $updateCount = 0)
148166
{
149167
$connectionMock = $this->getMockBuilder(Connection::class)
150168
->disableOriginalConstructor()
@@ -183,6 +201,7 @@ function ($method) use ($responses) {
183201
$siteMock->method('updates')->willReturn($updateMock);
184202
$siteMock->method('getConnectionAttribute')->willReturn($connectionMock);
185203
$siteMock->method('getFrontendStatus')->willReturn(200);
204+
$siteMock->method('getUpdateCount')->willReturn($updateCount);
186205
$siteMock->id = 1;
187206
$siteMock->url = "http://example.org";
188207
$siteMock->cms_version = "1.0.0";

0 commit comments

Comments
 (0)