Skip to content

Commit e48435f

Browse files
committed
Apply suggestions from reviews
1 parent a993cba commit e48435f

3 files changed

Lines changed: 13 additions & 14 deletions

File tree

app/Http/Controllers/PublicWikiController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,6 @@ public function index(Request $request) {
7272
* Display the specified resource.
7373
*/
7474
public function show($id) {
75-
return new PublicWikiResource(Wiki::query()->with('wikiLatestProfile')->findOrFail($id));
75+
return new PublicWikiResource(Wiki::findOrFail($id));
7676
}
7777
}

app/Http/Resources/PublicWikiResource.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,10 @@ public function toArray($request): array {
1919

2020
// Checking relation load state before reading it to avoid N+1 query
2121
// This relies on the controller to eager load `wikiLatestProfile` relationship
22-
'reuse_prototype' => $this->relationLoaded('wikiLatestProfile')
23-
? ($this->wikiLatestProfile
24-
? $this->wikiLatestProfile->purpose === 'data_hub'
25-
&& $this->wikiLatestProfile->temporality === 'permanent'
26-
&& $this->wikiLatestProfile->audience === 'wide'
27-
: null)
22+
'reuse_prototype' => $this->wikiLatestProfile
23+
? $this->wikiLatestProfile->purpose === 'data_hub'
24+
&& $this->wikiLatestProfile->temporality === 'permanent'
25+
&& $this->wikiLatestProfile->audience === 'wide'
2826
: null,
2927
];
3028
}

tests/Http/Controllers/PublicWikiControllerTest.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@
88
use App\WikiProfile;
99
use Illuminate\Database\Events\QueryExecuted;
1010
use Illuminate\Foundation\Testing\DatabaseTransactions;
11+
use Illuminate\Http\Request;
1112
use Illuminate\Support\Facades\DB;
1213
use Tests\TestCase;
1314

1415
class PublicWikiControllerTest extends TestCase {
1516
use DatabaseTransactions;
1617

17-
public function testShowEagerLoadsWikiLatestProfileForResource(): void {
18+
public function testShowLoadsWikiLatestProfileForResource(): void {
1819
$wiki = Wiki::factory()->create([
1920
'domain' => 'controller-test.wikibase.cloud',
2021
'sitename' => 'controller-test',
@@ -31,14 +32,14 @@ public function testShowEagerLoadsWikiLatestProfileForResource(): void {
3132
$resource = $controller->show($wiki->id);
3233

3334
$this->assertInstanceOf(PublicWikiResource::class, $resource);
34-
$this->assertTrue($resource->resource->relationLoaded('wikiLatestProfile'));
35+
$this->assertSame(true, $resource->toArray(new Request)['reuse_prototype']);
3536
}
3637

3738
public function testIndexEagerLoadsWikiLatestProfileOnceForCollection(): void {
38-
for ($i = 1; $i <= rand(3, 100); $i++) {
39+
for ($i = 1; $i <= 3; $i++) {
3940
$wiki = Wiki::factory()->create([
40-
'domain' => 'index-eager-load-test-' . $i . '.wikibase.cloud',
41-
'sitename' => 'Index Eager Load Test Site ' . $i,
41+
'domain' => "index-eager-load-test-{$i}.wikibase.cloud",
42+
'sitename' => "Index Eager Load Test Site {$i}",
4243
]);
4344

4445
WikiProfile::create([
@@ -57,9 +58,9 @@ public function testIndexEagerLoadsWikiLatestProfileOnceForCollection(): void {
5758
});
5859

5960
$controller = new PublicWikiController;
60-
$resourceCollection = $controller->index(request());
61+
$resourceCollection = $controller->index(new Request);
6162

6263
$this->assertSame(1, $profileQueryCount);
63-
$this->assertTrue($resourceCollection->collection[0]->resource->relationLoaded('wikiLatestProfile'));
64+
$this->assertTrue($resourceCollection->first()->relationLoaded('wikiLatestProfile'));
6465
}
6566
}

0 commit comments

Comments
 (0)