66use App \Http \Resources \PublicWikiResource ;
77use App \Wiki ;
88use App \WikiProfile ;
9+ use Illuminate \Database \Events \QueryExecuted ;
910use Illuminate \Foundation \Testing \DatabaseTransactions ;
11+ use Illuminate \Support \Facades \DB ;
1012use Tests \TestCase ;
1113
1214class PublicWikiControllerTest extends TestCase {
@@ -31,4 +33,35 @@ public function testShowEagerLoadsWikiLatestProfileForResource(): void {
3133 $ this ->assertInstanceOf (PublicWikiResource::class, $ resource );
3234 $ this ->assertTrue ($ resource ->resource ->relationLoaded ('wikiLatestProfile ' ));
3335 }
36+
37+ public function testIndexEagerLoadsWikiLatestProfileOnceForCollection (): void {
38+ $ n = rand (3 , 100 );
39+ echo $ n ;
40+ for ($ i = 1 ; $ i <= $ n ; $ i ++) {
41+ $ wiki = Wiki::factory ()->create ([
42+ 'domain ' => 'index-eager-load-test- ' . $ i . '.wikibase.cloud ' ,
43+ 'sitename ' => 'Index Eager Load Test Site ' . $ i ,
44+ ]);
45+
46+ WikiProfile::create ([
47+ 'wiki_id ' => $ wiki ->id ,
48+ 'purpose ' => 'data_hub ' ,
49+ 'temporality ' => 'permanent ' ,
50+ 'audience ' => 'wide ' ,
51+ ]);
52+ }
53+
54+ $ profileQueryCount = 0 ;
55+ DB ::listen (function (QueryExecuted $ query ) use (&$ profileQueryCount ): void {
56+ if (str_contains ($ query ->sql , 'wiki_profiles ' )) {
57+ $ profileQueryCount ++;
58+ }
59+ });
60+
61+ $ controller = new PublicWikiController ;
62+ $ resourceCollection = $ controller ->index (request ());
63+
64+ $ this ->assertSame (1 , $ profileQueryCount );
65+ $ this ->assertTrue ($ resourceCollection ->collection [0 ]->resource ->relationLoaded ('wikiLatestProfile ' ));
66+ }
3467}
0 commit comments