Skip to content

Commit

Permalink
added controller tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SniperSister committed Nov 17, 2024
1 parent 7890f00 commit 1835439
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 22 deletions.
3 changes: 2 additions & 1 deletion app/Http/Controllers/Api/V1/SiteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use GuzzleHttp\Exception\ServerException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;

/**
* Class SiteController
Expand All @@ -34,7 +35,7 @@ public function register(SiteRequest $request): JsonResponse
$url = $request->string('url');
$key = $request->string('key');

$connectionService = new Connection($url, $key);
$connectionService = App::makeWith(Connection::class, [$url, $key]);

// Do a health check
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('sites', function (Blueprint $table) {
$table->dropColumn('update_patch');
$table->dropColumn('update_minor');
$table->dropColumn('update_major');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('sites', function (Blueprint $table) {
$table->boolean('update_patch');
$table->boolean('update_minor');
$table->boolean('update_major');
});
}
};
4 changes: 2 additions & 2 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
<env name="APP_MAINTENANCE_DRIVER" value="file"/>
<env name="BCRYPT_ROUNDS" value="4"/>
<env name="CACHE_STORE" value="array"/>
<!-- <env name="DB_CONNECTION" value="sqlite"/> -->
<!-- <env name="DB_DATABASE" value=":memory:"/> -->
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
<env name="MAIL_MAILER" value="array"/>
<env name="PULSE_ENABLED" value="false"/>
<env name="QUEUE_CONNECTION" value="sync"/>
Expand Down
59 changes: 59 additions & 0 deletions tests/Feature/Api/SiteControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Tests\Api\Feature;

use App\Models\Site;
use App\RemoteSite\Connection;
use App\RemoteSite\Responses\HealthCheck;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\App;
use Tests\TestCase;

class SiteControllerTest extends TestCase
{
use RefreshDatabase;

public function testRegisteringASiteWithoutUrlOrKeyFails(): void
{
$response = $this->postJson(
'/api/v1/register',
);

$response->assertStatus(422);
}

public function testRegisteringASiteWithUrlAndKeyCreatesRow(): void
{
App::bind(Connection::class, fn() => $this->getConnectionMock(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"
])));

$response = $this->postJson(
'/api/v1/register',
["url" => "https://www.joomla.org", "key" => "foobar123foobar123foobar123foobar123"]
);

$response->assertStatus(200);
$result = Site::where('url', 'https://www.joomla.org')->first();

$this->assertEquals([

], $result->toArray());
}

protected function getConnectionMock(HealthCheck $response)
{
$mock = $this->getMockBuilder(Connection::class)
->disableOriginalConstructor()
->onlyMethods(['__call'])
->getMock();

$mock->method('__call')->willReturn($response);

return $mock;
}
}
19 changes: 0 additions & 19 deletions tests/Feature/ExampleTest.php

This file was deleted.

0 comments on commit 1835439

Please sign in to comment.