-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): add public endpoint for querying wikis
- Loading branch information
Showing
7 changed files
with
241 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use Illuminate\Http\Request; | ||
use App\Wiki; | ||
use App\Http\Resources\PublicWikiResource; | ||
use App\Http\Resources\PublicWikiCollection; | ||
|
||
class PublicWikiController extends Controller | ||
{ | ||
/** | ||
* Display a listing of the resource. | ||
*/ | ||
public function index(Request $request) | ||
{ | ||
$perPage = $request->query('per_page', null); | ||
if ($perPage !== null) { | ||
$perPage = intval($perPage); | ||
} | ||
return new PublicWikiCollection(Wiki::query()->paginate($perPage)); | ||
} | ||
|
||
/** | ||
* Store a newly created resource in storage. | ||
*/ | ||
public function store(Request $request): \Illuminate\Http\JsonResponse | ||
{ | ||
return response()->json(['message' => 'Method not allowed'], 405); | ||
} | ||
|
||
/** | ||
* Display the specified resource. | ||
*/ | ||
public function show($id) | ||
{ | ||
return new PublicWikiResource(Wiki::findOrFail($id)); | ||
} | ||
|
||
/** | ||
* Update the specified resource in storage. | ||
*/ | ||
public function update(Request $request, $id): \Illuminate\Http\JsonResponse | ||
{ | ||
return response()->json(['message' => 'Method not allowed'], 405); | ||
} | ||
|
||
/** | ||
* Remove the specified resource from storage. | ||
*/ | ||
public function destroy($id): \Illuminate\Http\JsonResponse | ||
{ | ||
return response()->json(['message' => 'Method not allowed'], 405); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace App\Http\Resources; | ||
|
||
use Illuminate\Http\Resources\Json\ResourceCollection; | ||
|
||
class PublicWikiCollection extends ResourceCollection | ||
{ | ||
/** | ||
* Transform the resource collection into an array. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable | ||
*/ | ||
public function toArray($request) | ||
{ | ||
return parent::toArray($request); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace App\Http\Resources; | ||
|
||
use Illuminate\Http\Resources\Json\JsonResource; | ||
|
||
class PublicWikiResource extends JsonResource | ||
{ | ||
public function toArray($request): array | ||
{ | ||
return [ | ||
'id' => $this->id, | ||
'description' => $this->description, | ||
'domain' => $this->domain, | ||
'sitename' => $this->sitename, | ||
'wiki_site_stats' => $this->wikiSiteStats, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Database\Factories; | ||
|
||
use App\WikiSiteStats; | ||
use Illuminate\Database\Eloquent\Factories\Factory; | ||
|
||
class WikiSiteStatsFactory extends Factory | ||
{ | ||
/** | ||
* The name of the factory's corresponding model. | ||
* | ||
* @var string | ||
*/ | ||
protected $model = WikiSiteStats::class; | ||
|
||
/** | ||
* Define the model's default state. | ||
* | ||
* @return array | ||
*/ | ||
public function definition() | ||
{ | ||
return []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
<?php | ||
|
||
namespace Tests\Routes\Wiki; | ||
|
||
use Tests\Routes\Traits\OptionsRequestAllowed; | ||
use Tests\TestCase; | ||
use App\WikiSiteStats; | ||
use App\Wiki; | ||
use Illuminate\Foundation\Testing\DatabaseTransactions; | ||
|
||
class PublicWikiTest extends TestCase | ||
{ | ||
protected $route = 'wiki'; | ||
|
||
use OptionsRequestAllowed; | ||
use DatabaseTransactions; | ||
|
||
public function setUp(): void { | ||
parent::setUp(); | ||
Wiki::query()->delete(); | ||
WikiSiteStats::query()->delete(); | ||
} | ||
|
||
public function tearDown(): void { | ||
Wiki::query()->delete(); | ||
WikiSiteStats::query()->delete(); | ||
parent::tearDown(); | ||
} | ||
|
||
public function testEmpty() | ||
{ | ||
$this->json('GET', $this->route.'/12') | ||
->assertStatus(404) | ||
->assertJsonStructure(['message']); | ||
|
||
$this->json('GET', $this->route) | ||
->assertStatus(200) | ||
->assertJsonPath('data', []); | ||
} | ||
|
||
public function testBadMethods() | ||
{ | ||
$wiki = Wiki::factory()->create(['domain' => 'one.wikibase.cloud']); | ||
WikiSiteStats::factory()->create(['wiki_id' => $wiki->id, 'pages' => 77]); | ||
|
||
$this->json('DELETE', $this->route.'/'.$wiki->id) | ||
->assertStatus(405) | ||
->assertJsonStructure(['message']); | ||
|
||
$this->assertEquals(Wiki::where('id', $wiki->id)->count(), 1); | ||
|
||
$this->json('PATCH', $this->route.'/'.$wiki->id, ['domain' => 'foo.wikibase.cloud']) | ||
->assertStatus(405) | ||
->assertJsonStructure(['message']); | ||
|
||
$this->assertEquals(Wiki::where('id', $wiki->id)->first()->getAttribute('domain'), 'one.wikibase.cloud'); | ||
|
||
$this->json('POST', $this->route, ['domain' => 'create.wikibase.cloud']) | ||
->assertStatus(405) | ||
->assertJsonStructure(['message']); | ||
} | ||
|
||
public function testGetOne() | ||
{ | ||
$wiki = Wiki::factory()->create(['domain' => 'one.wikibase.cloud']); | ||
WikiSiteStats::factory()->create(['wiki_id' => $wiki->id, 'pages' => 77]); | ||
|
||
Wiki::factory()->create(['domain' => 'two.wikibase.cloud']); | ||
WikiSiteStats::factory()->create(['wiki_id' => $wiki->id, 'pages' => 66]); | ||
|
||
$this->json('GET', $this->route.'/'.$wiki->id) | ||
->assertStatus(200) | ||
->assertJsonPath('data.domain', 'one.wikibase.cloud') | ||
->assertJsonPath('data.wiki_site_stats.pages', 77); | ||
} | ||
|
||
public function testGetAll() | ||
{ | ||
$wiki = Wiki::factory()->create(['domain' => 'one.wikibase.cloud']); | ||
WikiSiteStats::factory()->create(['wiki_id' => $wiki->id, 'pages' => 77]); | ||
|
||
$wiki = Wiki::factory()->create(['domain' => 'two.wikibase.cloud']); | ||
WikiSiteStats::factory()->create(['wiki_id' => $wiki->id, 'pages' => 66]); | ||
|
||
$this->json('GET', $this->route) | ||
->assertStatus(200) | ||
->assertJsonPath('data.0.domain', 'one.wikibase.cloud') | ||
->assertJsonPath('data.0.wiki_site_stats.pages', 77) | ||
->assertJsonPath('data.1.domain', 'two.wikibase.cloud') | ||
->assertJsonPath('data.1.wiki_site_stats.pages', 66); | ||
} | ||
|
||
public function testPagination() | ||
{ | ||
$wiki = Wiki::factory()->create(['domain' => 'one.wikibase.cloud']); | ||
WikiSiteStats::factory()->create(['wiki_id' => $wiki->id, 'pages' => 77]); | ||
|
||
$wiki = Wiki::factory()->create(['domain' => 'two.wikibase.cloud']); | ||
WikiSiteStats::factory()->create(['wiki_id' => $wiki->id, 'pages' => 66]); | ||
|
||
$wiki = Wiki::factory()->create(['domain' => 'three.wikibase.cloud']); | ||
WikiSiteStats::factory()->create(['wiki_id' => $wiki->id, 'pages' => 55]); | ||
|
||
$this->json('GET', $this->route.'?per_page=1') | ||
->assertStatus(200) | ||
->assertJsonPath('data.0.domain', 'one.wikibase.cloud') | ||
->assertJsonPath('data.0.wiki_site_stats.pages', 77) | ||
->assertJsonCount(1, 'data') | ||
->assertJsonPath('meta.total', 3); | ||
|
||
$this->json('GET', $this->route.'?per_page=1&page=3') | ||
->assertStatus(200) | ||
->assertJsonPath('data.0.domain', 'three.wikibase.cloud') | ||
->assertJsonPath('data.0.wiki_site_stats.pages', 55) | ||
->assertJsonCount(1, 'data') | ||
->assertJsonPath('meta.total', 3); | ||
} | ||
} |