Skip to content

Commit d5eec1b

Browse files
authored
Add controller tests for register endpoint (#15)
* wip commit * added controller tests * fix webservice request headers * Added controller tests for register endpoint * cs fix
1 parent 69dac9f commit d5eec1b

File tree

7 files changed

+102
-33
lines changed

7 files changed

+102
-33
lines changed

app/Http/Controllers/Api/V1/SiteController.php

+9-5
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44

55
use App\Http\Controllers\Controller;
66
use App\Http\Requests\SiteRequest;
7-
use App\Http\Traits\ApiResponse;
87
use App\Jobs\CheckSiteHealth;
98
use App\Models\Site;
109
use App\RemoteSite\Connection;
10+
use App\Http\Traits\ApiResponse;
1111
use Carbon\Carbon;
1212
use GuzzleHttp\Exception\ClientException;
1313
use GuzzleHttp\Exception\ServerException;
1414
use Illuminate\Http\JsonResponse;
15+
use Illuminate\Support\Facades\App;
1516

1617
/**
1718
* Class SiteController
@@ -33,15 +34,18 @@ public function register(SiteRequest $request): JsonResponse
3334
$url = $request->string('url');
3435
$key = $request->string('key');
3536

36-
$connectionService = new Connection($url, $key);
37+
$connectionService = App::makeWith(
38+
Connection::class,
39+
["baseUrl" => $url, "key" => $key]
40+
);
3741

3842
// Do a health check
3943
try {
4044
$healthResponse = $connectionService->checkHealth();
41-
} catch (ServerException $e) {
45+
} catch (ServerException|ClientException $e) {
46+
return $this->error($e->getMessage(), 400);
47+
} catch (\Exception $e) {
4248
return $this->error($e->getMessage(), 500);
43-
} catch (ClientException|\Exception $e) {
44-
return $this->error($e->getMessage());
4549
}
4650

4751
// If successful save site

app/RemoteSite/Connection.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,10 @@ public function performExtractionRequest(array $requestData): array
6767
]
6868
);
6969

70-
$responseData = $this->decodeResponse($response, $request);
71-
72-
return $responseData;
70+
return $this->decodeResponse($response, $request);
7371
}
7472

75-
protected function performWebserviceRequest(
73+
public function performWebserviceRequest(
7674
HttpMethod $method,
7775
string $endpoint,
7876
?array $requestData = null

database/migrations/2024_11_17_115646_remove_patch_minor_columns_from_sites.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
use Illuminate\Database\Schema\Blueprint;
55
use Illuminate\Support\Facades\Schema;
66

7-
return new class extends Migration
8-
{
7+
return new class () extends Migration {
98
/**
109
* Run the migrations.
1110
*/

phpunit.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
<env name="APP_MAINTENANCE_DRIVER" value="file"/>
2323
<env name="BCRYPT_ROUNDS" value="4"/>
2424
<env name="CACHE_STORE" value="array"/>
25-
<!-- <env name="DB_CONNECTION" value="sqlite"/> -->
26-
<!-- <env name="DB_DATABASE" value=":memory:"/> -->
25+
<env name="DB_CONNECTION" value="sqlite"/>
26+
<env name="DB_DATABASE" value=":memory:"/>
2727
<env name="MAIL_MAILER" value="array"/>
2828
<env name="PULSE_ENABLED" value="false"/>
2929
<env name="QUEUE_CONNECTION" value="sync"/>

routes/api.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
Route::controller(SiteController::class)->group(function () {
88
Route::post('register', 'register');
99
Route::post('check', 'check');
10-
Route::delete('delete', 'delete');
10+
Route::post('delete', 'delete');
1111
});
1212
});
+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
namespace Tests\Api\Feature;
4+
5+
use App\Jobs\CheckSiteHealth;
6+
use App\Models\Site;
7+
use App\RemoteSite\Connection;
8+
use App\RemoteSite\Responses\HealthCheck;
9+
use Illuminate\Foundation\Testing\RefreshDatabase;
10+
use Illuminate\Support\Facades\Queue;
11+
use Tests\TestCase;
12+
13+
class SiteControllerTest extends TestCase
14+
{
15+
use RefreshDatabase;
16+
17+
public function testRegisteringASiteWithoutUrlOrKeyFails(): void
18+
{
19+
$response = $this->postJson(
20+
'/api/v1/register',
21+
);
22+
23+
$response->assertStatus(422);
24+
}
25+
26+
public function testRegisteringASiteWithUrlAndKeyCreatesRow(): void
27+
{
28+
Queue::fake();
29+
30+
$mock = $this->getMockBuilder(Connection::class)
31+
->disableOriginalConstructor()
32+
->getMock();
33+
34+
$mock->method('__call')->willReturn(HealthCheck::from([
35+
"php_version" => "1.0.0",
36+
"db_type" => "mysqli",
37+
"db_version" => "1.0.0",
38+
"cms_version" => "1.0.0",
39+
"server_os" => "Joomla OS 1.0.0"
40+
]));
41+
42+
$this->app->bind(Connection::class, fn () => $mock);
43+
44+
$response = $this->postJson(
45+
'/api/v1/register',
46+
["url" => "https://www.joomla.org", "key" => "foobar123foobar123foobar123foobar123"]
47+
);
48+
49+
$response->assertStatus(200);
50+
$row = Site::where('url', 'https://www.joomla.org')->first();
51+
52+
$this->assertEquals("https://www.joomla.org", $row->url);
53+
$this->assertEquals("foobar123foobar123foobar123foobar123", $row->key);
54+
$this->assertEquals("1.0.0", $row->php_version);
55+
56+
Queue::assertPushed(CheckSiteHealth::class);
57+
}
58+
59+
public function testRegisteringASiteFailsWhenHealthCheckFails(): void
60+
{
61+
$mock = $this->getMockBuilder(Connection::class)
62+
->disableOriginalConstructor()
63+
->getMock();
64+
65+
$mock->method('__call')->willThrowException(new \Exception());
66+
67+
$this->app->bind(Connection::class, fn () => $mock);
68+
69+
$response = $this->postJson(
70+
'/api/v1/register',
71+
["url" => "https://www.joomla.org", "key" => "foobar123foobar123foobar123foobar123"]
72+
);
73+
74+
$response->assertStatus(500);
75+
}
76+
77+
protected function getConnectionMock(HealthCheck $response)
78+
{
79+
$mock = $this->getMockBuilder(Connection::class)
80+
->disableOriginalConstructor()
81+
->getMock();
82+
83+
$mock->method('__call')->willReturn($response);
84+
85+
return $mock;
86+
}
87+
}

tests/Feature/ExampleTest.php

-19
This file was deleted.

0 commit comments

Comments
 (0)