Skip to content

Commit 15116de

Browse files
authored
Added controller tests for missing endpoints (#23)
* Added controller tests for missing endpoints * cs fix
1 parent 1089b02 commit 15116de

File tree

2 files changed

+101
-1
lines changed

2 files changed

+101
-1
lines changed

app/Models/Site.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ public function getUrlAttribute(string $value): string
4646

4747
public function getConnectionAttribute(): Connection
4848
{
49-
return new Connection($this->url, $this->key);
49+
return App::makeWith(
50+
Connection::class,
51+
["baseUrl" => $this->url, "key" => $this->key]
52+
);
5053
}
5154

5255
public function getFrontendStatus(): int

tests/Feature/Api/SiteControllerTest.php

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use App\Models\Site;
77
use App\RemoteSite\Connection;
88
use App\RemoteSite\Responses\HealthCheck;
9+
use Carbon\Carbon;
910
use Illuminate\Foundation\Testing\RefreshDatabase;
1011
use Illuminate\Support\Facades\Queue;
1112
use Tests\TestCase;
@@ -74,6 +75,82 @@ public function testRegisteringASiteFailsWhenHealthCheckFails(): void
7475
$response->assertStatus(500);
7576
}
7677

78+
public function testCheckingASiteReturnsSuccessIfCheckIsSuccessful(): void
79+
{
80+
$site = $this->createMockSiteInDb();
81+
82+
$mock = $this->getMockBuilder(Connection::class)
83+
->disableOriginalConstructor()
84+
->getMock();
85+
86+
$mock->method('__call')->willReturn(HealthCheck::from($site->toArray()));
87+
88+
$this->app->bind(Connection::class, fn () => $mock);
89+
90+
$response = $this->postJson(
91+
'/api/v1/check',
92+
["url" => "https://www.joomla.org", "key" => "foobar123foobar123foobar123foobar123"]
93+
);
94+
95+
$response->assertStatus(200);
96+
}
97+
98+
public function testCheckingASiteReturnErrorIfCheckIsUnsuccessful(): void
99+
{
100+
$this->createMockSiteInDb();
101+
102+
$mock = $this->getMockBuilder(Connection::class)
103+
->disableOriginalConstructor()
104+
->getMock();
105+
106+
$mock->method('__call')->willThrowException(new \Exception());
107+
108+
$this->app->bind(Connection::class, fn () => $mock);
109+
110+
$response = $this->postJson(
111+
'/api/v1/check',
112+
["url" => "https://www.joomla.org", "key" => "foobar123foobar123foobar123foobar123"]
113+
);
114+
115+
$response->assertStatus(500);
116+
}
117+
118+
public function testCheckingASiteReturns404ForInvalidSite(): void
119+
{
120+
$response = $this->postJson(
121+
'/api/v1/check',
122+
["url" => "https://www.joomlaf.org", "key" => "foobar123foobar123foobar123foobar123"]
123+
);
124+
125+
$response->assertStatus(404);
126+
}
127+
128+
public function testDeleteASiteReturns404ForInvalidSite(): void
129+
{
130+
$response = $this->postJson(
131+
'/api/v1/delete',
132+
["url" => "https://www.joomlaf.org", "key" => "foobar123foobar123foobar123foobar123"]
133+
);
134+
135+
$response->assertStatus(404);
136+
}
137+
138+
public function testDeleteASiteRemovesRow(): void
139+
{
140+
$this->createMockSiteInDb();
141+
142+
$this->assertEquals(1, Site::get()->count());
143+
144+
$response = $this->postJson(
145+
'/api/v1/delete',
146+
["url" => "https://www.joomla.org", "key" => "foobar123foobar123foobar123foobar123"]
147+
);
148+
149+
$response->assertStatus(200);
150+
151+
$this->assertEquals(0, Site::get()->count());
152+
}
153+
77154
protected function getConnectionMock(HealthCheck $response)
78155
{
79156
$mock = $this->getMockBuilder(Connection::class)
@@ -84,4 +161,24 @@ protected function getConnectionMock(HealthCheck $response)
84161

85162
return $mock;
86163
}
164+
165+
protected function createMockSiteInDb(): Site
166+
{
167+
$site = new Site([
168+
"php_version" => "1.0.0",
169+
"db_type" => "mysqli",
170+
"db_version" => "1.0.0",
171+
"cms_version" => "1.0.0",
172+
"server_os" => "Joomla OS 1.0.0",
173+
"last_seen" => Carbon::now()
174+
]);
175+
176+
$site->key = 'foobar123foobar123foobar123foobar123';
177+
$site->url = 'https://www.joomla.org';
178+
$site->last_seen = Carbon::now();
179+
180+
$site->save();
181+
182+
return $site;
183+
}
87184
}

0 commit comments

Comments
 (0)