Skip to content

Commit d8eefcd

Browse files
committed
Added tests for check health test
1 parent d2a3237 commit d8eefcd

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
3+
namespace Tests\Unit\Jobs;
4+
5+
use App\Jobs\CheckSiteHealth;
6+
use App\Jobs\UpdateSite;
7+
use App\Models\Site;
8+
use App\RemoteSite\Connection;
9+
use App\RemoteSite\Responses\HealthCheck;
10+
use App\TUF\TufFetcher;
11+
use Illuminate\Support\Facades\App;
12+
use Illuminate\Support\Facades\Queue;
13+
use Tests\TestCase;
14+
15+
class CheckSiteHealthTest extends TestCase
16+
{
17+
public function testCheckSiteHealthUpdatesDbRow()
18+
{
19+
$healthMock = $this->getHealthCheckMock();
20+
21+
$siteMock = $this->getSiteMock($healthMock);
22+
$siteMock->expects($this->once())
23+
->method('fill')
24+
->with($healthMock->toArray());
25+
26+
$tufMock = $this->getMockBuilder(TufFetcher::class)
27+
->disableOriginalConstructor()
28+
->getMock();
29+
30+
$tufMock->method('getLatestVersionForBranch')->willReturn(null);
31+
32+
App::bind(TufFetcher::class, fn () => $tufMock);
33+
34+
$object = new CheckSiteHealth($siteMock);
35+
$object->handle();
36+
}
37+
38+
public function testCheckHealthTriggersUpdateJobIfNewerVersionIsAvailable()
39+
{
40+
Queue::fake();
41+
42+
$healthMock = $this->getHealthCheckMock();
43+
44+
$siteMock = $this->getSiteMock($healthMock);
45+
$siteMock->expects($this->once())
46+
->method('fill')
47+
->with($healthMock->toArray());
48+
49+
$tufMock = $this->getMockBuilder(TufFetcher::class)
50+
->disableOriginalConstructor()
51+
->getMock();
52+
53+
$tufMock->method('getLatestVersionForBranch')->willReturn("2.0.0");
54+
55+
App::bind(TufFetcher::class, fn () => $tufMock);
56+
57+
$object = new CheckSiteHealth($siteMock);
58+
$object->handle();
59+
60+
Queue::assertPushed(UpdateSite::class);
61+
}
62+
63+
protected function getSiteMock($healthMock)
64+
{
65+
$connectionMock = $this->getMockBuilder(Connection::class)
66+
->disableOriginalConstructor()
67+
->getMock();
68+
69+
$connectionMock
70+
->method("__call")
71+
->willReturnCallback(
72+
function () use ($healthMock) {
73+
return $healthMock;
74+
}
75+
);
76+
77+
$siteMock = $this->getMockBuilder(Site::class)
78+
->disableOriginalConstructor()
79+
->onlyMethods(['getConnectionAttribute', 'fill', 'save'])
80+
->getMock();
81+
82+
$siteMock->method('getConnectionAttribute')->willReturn($connectionMock);
83+
$siteMock->method('save')->willReturn(true);
84+
$siteMock->id = 1;
85+
$siteMock->url = "http://example.org";
86+
$siteMock->cms_version = "1.0.0";
87+
88+
return $siteMock;
89+
}
90+
91+
protected function getHealthCheckMock($overrides = [])
92+
{
93+
$defaults = [
94+
"php_version" => "1.0.0",
95+
"db_type" => "mysqli",
96+
"db_version" => "1.0.0",
97+
"cms_version" => "1.0.0",
98+
"server_os" => "Joomla OS 1.0.0"
99+
];
100+
101+
return HealthCheck::from([
102+
...$defaults,
103+
...$overrides
104+
]);
105+
}
106+
107+
}

0 commit comments

Comments
 (0)