Skip to content

Commit 5f76c19

Browse files
committed
Add test to check that lazy loading is also cached in resource objects
1 parent 2923ea5 commit 5f76c19

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Fixtures\Http\Resources;
2+
3+
use Illuminate\Http\Resources\Json\Resource;
4+
5+
class Author extends Resource
6+
{
7+
public function toArray($request)
8+
{
9+
return [
10+
'name' => $this->name,
11+
'books' => $this->books,
12+
];
13+
}
14+
}

tests/Unit/CachedBuilderTest.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedProfile;
1111
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedPublisher;
1212
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedStore;
13+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Http\Resources\Author as AuthorResource;
1314
use GeneaLabs\LaravelModelCaching\Tests\TestCase;
1415
use Illuminate\Foundation\Testing\RefreshDatabase;
1516

@@ -53,7 +54,7 @@ public function testCacheIsEmptyBeforeLoadingModels()
5354
'genealabslaravelmodelcachingtestsfixturesauthor',
5455
'genealabslaravelmodelcachingtestsfixturesbook'
5556
])
56-
->get('genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks');
57+
->get('genealabslaravelmodelcachingtestsfixturesauthor-books');
5758

5859
$this->assertNull($results);
5960
}
@@ -453,4 +454,20 @@ public function testNonEagerloadedRelationshipResolvesThroughCachedBuilder()
453454
$this->assertEmpty($books->diffAssoc($cachedResults));
454455
$this->assertEmpty($liveResults->diffAssoc($cachedResults));
455456
}
457+
458+
public function testLazyLoadingOnResourceIsCached()
459+
{
460+
$books = (new AuthorResource((new Author)->first()))->books;
461+
$key = 'genealabslaravelmodelcachingtestsfixturesbook-books.author_id_1-books.author_id_';
462+
$tags = [
463+
'genealabslaravelmodelcachingtestsfixturesbook',
464+
];
465+
466+
$cachedResults = cache()->tags($tags)->get($key);
467+
$liveResults = (new UncachedAuthor)->first()->books;
468+
469+
$this->assertEmpty($books->diffAssoc($cachedResults));
470+
$this->assertEmpty($liveResults->diffAssoc($cachedResults));
471+
}
472+
456473
}

0 commit comments

Comments
 (0)