Skip to content

Commit

Permalink
test: cover all cases
Browse files Browse the repository at this point in the history
  • Loading branch information
m90 committed Aug 14, 2023
1 parent 877e394 commit 2f9f8fb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
10 changes: 5 additions & 5 deletions app/Http/Controllers/Backend/WikiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class WikiController extends Controller
{
private static $with = ['wikiDb', 'wikiQueryserviceNamespace', 'settings'];

public function getWikiForDomain(Request $request): \Illuminate\Http\Response
public function getWikiForDomain(Request $request): \Illuminate\Http\JsonResponse
{
$domain = $request->input('domain');

Expand All @@ -24,17 +24,17 @@ public function getWikiForDomain(Request $request): \Illuminate\Http\Response
$result = Wiki::where('domain', $domain)->with(self::$with)->get();
}
} catch (\Exception $ex) {
return response($ex->getMessage(), 500);
return response()->json($ex->getMessage(), 500);
}

switch (count($result)) {
case 0:
return response('Not found', 404);
return response()->json(['error' => 'Not found'], 404);
case 1:
$res['data'] = $result[0];
return response($res);
return response()->json($res, 200);
default:
return response('Query yields multiple results', 500);
return response()->json(['error' => 'Query yields multiple results'], 500);
}

}
Expand Down
25 changes: 25 additions & 0 deletions tests/Routes/Wiki/GetWikiForDomainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tests\Routes\Wiki;

use Tests\TestCase;
use App\Wiki;
use Illuminate\Foundation\Testing\RefreshDatabase;

class CreateTest extends TestCase
Expand All @@ -11,9 +12,33 @@ class CreateTest extends TestCase

use RefreshDatabase;

public function tearDown(): void
{
Wiki::query()->delete();
}

public function testNotFound()
{
Wiki::factory()->create(['domain' => 'found.wikibase.cloud']);

$response = $this->json('GET', $this->route."?domain=notfound.wikibase.cloud");
$response->assertStatus(404);
}
public function testFoundOne()
{
Wiki::factory()->create(['domain' => 'found.wikibase.cloud']);

$response = $this->json('GET', $this->route."?domain=found.wikibase.cloud");
$response->assertStatus(200);
}

public function testFoundMultiple()
{
Wiki::factory()->create(['domain' => 'found.wikibase.cloud']);
Wiki::factory()->create(['domain' => 'broken.wikibase.cloud']);
Wiki::factory()->create(['domain' => 'broken.wikibase.cloud']);

$response = $this->json('GET', $this->route."?domain=broken.wikibase.cloud");
$response->assertStatus(500);
}
}

0 comments on commit 2f9f8fb

Please sign in to comment.