diff --git a/app/Http/Controllers/Api/V1/SiteController.php b/app/Http/Controllers/Api/V1/SiteController.php
index bb1e98e..bae991d 100644
--- a/app/Http/Controllers/Api/V1/SiteController.php
+++ b/app/Http/Controllers/Api/V1/SiteController.php
@@ -4,14 +4,15 @@
use App\Http\Controllers\Controller;
use App\Http\Requests\SiteRequest;
-use App\Http\Traits\ApiResponse;
use App\Jobs\CheckSiteHealth;
use App\Models\Site;
use App\RemoteSite\Connection;
+use App\Http\Traits\ApiResponse;
use Carbon\Carbon;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ServerException;
use Illuminate\Http\JsonResponse;
+use Illuminate\Support\Facades\App;
/**
* Class SiteController
@@ -33,15 +34,18 @@ public function register(SiteRequest $request): JsonResponse
$url = $request->string('url');
$key = $request->string('key');
- $connectionService = new Connection($url, $key);
+ $connectionService = App::makeWith(
+ Connection::class,
+ ["baseUrl" => $url, "key" => $key]
+ );
// Do a health check
try {
$healthResponse = $connectionService->checkHealth();
- } catch (ServerException $e) {
+ } catch (ServerException|ClientException $e) {
+ return $this->error($e->getMessage(), 400);
+ } catch (\Exception $e) {
return $this->error($e->getMessage(), 500);
- } catch (ClientException|\Exception $e) {
- return $this->error($e->getMessage());
}
// If successful save site
diff --git a/app/RemoteSite/Connection.php b/app/RemoteSite/Connection.php
index e7f87c4..467c4da 100644
--- a/app/RemoteSite/Connection.php
+++ b/app/RemoteSite/Connection.php
@@ -67,12 +67,10 @@ public function performExtractionRequest(array $requestData): array
]
);
- $responseData = $this->decodeResponse($response, $request);
-
- return $responseData;
+ return $this->decodeResponse($response, $request);
}
- protected function performWebserviceRequest(
+ public function performWebserviceRequest(
HttpMethod $method,
string $endpoint,
?array $requestData = null
diff --git a/database/migrations/2024_11_17_115646_remove_patch_minor_columns_from_sites.php b/database/migrations/2024_11_17_115646_remove_patch_minor_columns_from_sites.php
index 7529f8b..8ba27de 100644
--- a/database/migrations/2024_11_17_115646_remove_patch_minor_columns_from_sites.php
+++ b/database/migrations/2024_11_17_115646_remove_patch_minor_columns_from_sites.php
@@ -4,8 +4,7 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
-return new class extends Migration
-{
+return new class () extends Migration {
/**
* Run the migrations.
*/
diff --git a/phpunit.xml b/phpunit.xml
index 506b9a3..61c031c 100644
--- a/phpunit.xml
+++ b/phpunit.xml
@@ -22,8 +22,8 @@
-
-
+
+
diff --git a/routes/api.php b/routes/api.php
index decce7f..305c168 100644
--- a/routes/api.php
+++ b/routes/api.php
@@ -7,6 +7,6 @@
Route::controller(SiteController::class)->group(function () {
Route::post('register', 'register');
Route::post('check', 'check');
- Route::delete('delete', 'delete');
+ Route::post('delete', 'delete');
});
});
diff --git a/tests/Feature/Api/SiteControllerTest.php b/tests/Feature/Api/SiteControllerTest.php
new file mode 100644
index 0000000..6470805
--- /dev/null
+++ b/tests/Feature/Api/SiteControllerTest.php
@@ -0,0 +1,87 @@
+postJson(
+ '/api/v1/register',
+ );
+
+ $response->assertStatus(422);
+ }
+
+ public function testRegisteringASiteWithUrlAndKeyCreatesRow(): void
+ {
+ Queue::fake();
+
+ $mock = $this->getMockBuilder(Connection::class)
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $mock->method('__call')->willReturn(HealthCheck::from([
+ "php_version" => "1.0.0",
+ "db_type" => "mysqli",
+ "db_version" => "1.0.0",
+ "cms_version" => "1.0.0",
+ "server_os" => "Joomla OS 1.0.0"
+ ]));
+
+ $this->app->bind(Connection::class, fn () => $mock);
+
+ $response = $this->postJson(
+ '/api/v1/register',
+ ["url" => "https://www.joomla.org", "key" => "foobar123foobar123foobar123foobar123"]
+ );
+
+ $response->assertStatus(200);
+ $row = Site::where('url', 'https://www.joomla.org')->first();
+
+ $this->assertEquals("https://www.joomla.org", $row->url);
+ $this->assertEquals("foobar123foobar123foobar123foobar123", $row->key);
+ $this->assertEquals("1.0.0", $row->php_version);
+
+ Queue::assertPushed(CheckSiteHealth::class);
+ }
+
+ public function testRegisteringASiteFailsWhenHealthCheckFails(): void
+ {
+ $mock = $this->getMockBuilder(Connection::class)
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $mock->method('__call')->willThrowException(new \Exception());
+
+ $this->app->bind(Connection::class, fn () => $mock);
+
+ $response = $this->postJson(
+ '/api/v1/register',
+ ["url" => "https://www.joomla.org", "key" => "foobar123foobar123foobar123foobar123"]
+ );
+
+ $response->assertStatus(500);
+ }
+
+ protected function getConnectionMock(HealthCheck $response)
+ {
+ $mock = $this->getMockBuilder(Connection::class)
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $mock->method('__call')->willReturn($response);
+
+ return $mock;
+ }
+}
diff --git a/tests/Feature/ExampleTest.php b/tests/Feature/ExampleTest.php
deleted file mode 100644
index 8364a84..0000000
--- a/tests/Feature/ExampleTest.php
+++ /dev/null
@@ -1,19 +0,0 @@
-get('/');
-
- $response->assertStatus(200);
- }
-}