Skip to content

Commit 905b4d1

Browse files
committed
Added tests for lazy-loading
- HasOne - HasMany - BelongsTo
1 parent 81dba5a commit 905b4d1

File tree

10 files changed

+189
-17
lines changed

10 files changed

+189
-17
lines changed

src/CachedBelongsToMany.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,17 @@ class CachedBelongsToMany extends BelongsToMany
1212
use BuilderCaching;
1313
use Caching;
1414
use FiresPivotEventsTrait;
15+
16+
public function getRelation($name)
17+
{
18+
$relation = parent::getRelation($name);
19+
20+
if (! $this->isCachable()
21+
&& is_a($relation->getQuery(), self::class)
22+
) {
23+
$relation->getQuery()->disableModelCaching();
24+
}
25+
26+
return $relation;
27+
}
1528
}

src/Traits/Buildable.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -162,19 +162,6 @@ public function paginate(
162162
return $this->cachedValue(func_get_args(), $cacheKey);
163163
}
164164

165-
public function getRelation($name)
166-
{
167-
$relation = parent::getRelation($name);
168-
169-
if (! $this->isCachable()
170-
&& is_a($relation->getQuery(), self::class)
171-
) {
172-
$relation->getQuery()->disableModelCaching();
173-
}
174-
175-
return $relation;
176-
}
177-
178165
protected function recursiveImplodeWithKey(array $items, string $glue = "_") : string
179166
{
180167
$result = "";

src/Traits/Caching.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,16 @@ protected function makeCacheKey(
6969
string $keyDifferentiator = ''
7070
) : string {
7171
$eagerLoad = $this->eagerLoad ?? [];
72-
$model = $this->model
73-
?? $this;
72+
$model = $this;
73+
74+
if (property_exists($this, "model")) {
75+
$model = $this->model;
76+
}
77+
78+
if (method_exists($this, "getModel")) {
79+
$model = $this->getModel();
80+
}
81+
7482
$query = $this->query
7583
?? app('db')->query();
7684

tests/CreatesApplication.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ public function setUp() : void
4242
(new Author)->observe(AuthorObserver::class);
4343
factory(Author::class, 10)->create()
4444
->each(function ($author) use ($publishers) {
45+
$profile = factory(Profile::class)
46+
->make();
47+
$profile->author_id = $author->id;
48+
$profile->save();
4549
factory(Book::class, random_int(5, 25))->make()
4650
->each(function ($book) use ($author, $publishers) {
4751
$book->author()->associate($author);
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Integration\CachedBuilder;
2+
3+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Author;
4+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedAuthor;
5+
use GeneaLabs\LaravelModelCaching\Tests\IntegrationTestCase;
6+
7+
class BooleanTest extends IntegrationTestCase
8+
{
9+
public function testBooleanWhereCreatesCorrectCacheKey()
10+
{
11+
$key = sha1('genealabs:laravel-model-caching:testing::memory::authors:genealabslaravelmodelcachingtestsfixturesauthor-is_famous_=_1');
12+
$tags = [
13+
'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesauthor',
14+
];
15+
16+
$authors = (new Author)
17+
->where("is_famous", true)
18+
->get();
19+
$cachedResults = $this->cache()
20+
->tags($tags)
21+
->get($key)['value'];
22+
$liveResults = (new UncachedAuthor)
23+
->where("is_famous", true)
24+
->get();
25+
26+
$this->assertEquals($liveResults->pluck("id"), $authors->pluck("id"));
27+
$this->assertEquals($liveResults->pluck("id"), $cachedResults->pluck("id"));
28+
$this->assertNotEmpty($authors);
29+
$this->assertNotEmpty($cachedResults);
30+
$this->assertNotEmpty($liveResults);
31+
}
32+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Integration\CachedBuilder;
2+
3+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Book;
4+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedBook;
5+
use GeneaLabs\LaravelModelCaching\Tests\IntegrationTestCase;
6+
7+
class DateTimeTest extends IntegrationTestCase
8+
{
9+
public function testDateWhereCreatesCorrectCacheKey()
10+
{
11+
$key = sha1('genealabs:laravel-model-caching:testing::memory::books:genealabslaravelmodelcachingtestsfixturesbook-publish_at_>_2009-05-18');
12+
$tags = [
13+
'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesbook',
14+
];
15+
$dateTime = now()->subYears(10)->toDateString();
16+
17+
$results = (new Book)
18+
->where("publish_at", ">", $dateTime)
19+
->get();
20+
$cachedResults = $this->cache()
21+
->tags($tags)
22+
->get($key)['value'];
23+
$liveResults = (new UncachedBook)
24+
->where("publish_at", ">", $dateTime)
25+
->get();
26+
27+
$this->assertEquals($liveResults->pluck("id"), $results->pluck("id"));
28+
$this->assertEquals($liveResults->pluck("id"), $cachedResults->pluck("id"));
29+
$this->assertNotEmpty($results);
30+
$this->assertNotEmpty($cachedResults);
31+
$this->assertNotEmpty($liveResults);
32+
}
33+
}

tests/Integration/CachedBuilder/LazyLoadTest.php

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,47 @@
11
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Integration\CachedBuilder;
22

3+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Author;
34
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Book;
5+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Profile;
46
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Store;
7+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedAuthor;
58
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedBook;
9+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedProfile;
610
use GeneaLabs\LaravelModelCaching\Tests\IntegrationTestCase;
711

812
class LazyLoadTest extends IntegrationTestCase
913
{
10-
public function testLazyLoadingRelationshipQuery()
14+
public function testBelongsToRelationship()
15+
{
16+
$key = sha1("genealabs:laravel-model-caching:testing::memory::authors:genealabslaravelmodelcachingtestsfixturesauthor-authors.id_=_1-first");
17+
$tags = [
18+
'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesauthor',
19+
];
20+
21+
$result = (new Book)
22+
->where("id", 1)
23+
->first()
24+
->author;
25+
$cachedResult = $this
26+
->cache()
27+
->tags($tags)
28+
->get($key)['value'];
29+
$uncachedResult = (new UncachedBook)
30+
->where("id", 1)
31+
->first()
32+
->author;
33+
34+
$this->assertEquals($uncachedResult->id, $result->id);
35+
$this->assertEquals($uncachedResult->id, $cachedResult->id);
36+
$this->assertEquals(Author::class, get_class($result));
37+
$this->assertEquals(Author::class, get_class($cachedResult));
38+
$this->assertEquals(UncachedAuthor::class, get_class($uncachedResult));
39+
$this->assertNotNull($result);
40+
$this->assertNotNull($cachedResult);
41+
$this->assertNotNull($uncachedResult);
42+
}
43+
44+
public function testBelongsToManyRelationship()
1145
{
1246
$bookId = (new Store)
1347
->disableModelCaching()
@@ -37,4 +71,63 @@ public function testLazyLoadingRelationshipQuery()
3771
$this->assertNotNull($cachedStores);
3872
$this->assertNotNull($uncachedStores);
3973
}
74+
75+
public function testHasManyRelationship()
76+
{
77+
$key = sha1("genealabs:laravel-model-caching:testing::memory::books:genealabslaravelmodelcachingtestsfixturesbook-books.author_id_=_1-books.author_id_notnull");
78+
$tags = [
79+
'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesbook',
80+
];
81+
82+
$result = (new Author)
83+
->find(1)
84+
->books;
85+
$cachedResult = $this
86+
->cache()
87+
->tags($tags)
88+
->get($key)['value'];
89+
$uncachedResult = (new UncachedAuthor)
90+
->find(1)
91+
->books;
92+
93+
$this->assertEquals($uncachedResult->pluck("id"), $result->pluck("id"));
94+
$this->assertEquals($uncachedResult->pluck("id"), $cachedResult->pluck("id"));
95+
$this->assertEquals(Book::class, get_class($result->first()));
96+
$this->assertEquals(Book::class, get_class($cachedResult->first()));
97+
$this->assertEquals(UncachedBook::class, get_class($uncachedResult->first()));
98+
$this->assertNotEmpty($result);
99+
$this->assertNotEmpty($cachedResult);
100+
$this->assertNotEmpty($uncachedResult);
101+
}
102+
103+
public function testHasOneRelationship()
104+
{
105+
$authorId = (new UncachedProfile)
106+
->first()
107+
->author_id;
108+
$key = sha1("genealabs:laravel-model-caching:testing::memory::profiles:genealabslaravelmodelcachingtestsfixturesprofile-profiles.author_id_=_{$authorId}-profiles.author_id_notnull-first");
109+
$tags = [
110+
'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesprofile',
111+
];
112+
113+
$result = (new Author)
114+
->find($authorId)
115+
->profile;
116+
$cachedResult = $this
117+
->cache()
118+
->tags($tags)
119+
->get($key)['value'];
120+
$uncachedResult = (new UncachedAuthor)
121+
->find($authorId)
122+
->profile;
123+
124+
$this->assertEquals($uncachedResult->id, $result->id);
125+
$this->assertEquals($uncachedResult->id, $cachedResult->id);
126+
$this->assertEquals(Profile::class, get_class($result->first()));
127+
$this->assertEquals(Profile::class, get_class($cachedResult->first()));
128+
$this->assertEquals(UncachedProfile::class, get_class($uncachedResult->first()));
129+
$this->assertNotEmpty($result);
130+
$this->assertNotEmpty($cachedResult);
131+
$this->assertNotEmpty($uncachedResult);
132+
}
40133
}

tests/database/factories/AuthorFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@
1212
"weekly" => 100,
1313
"daily" => 20,
1414
],
15+
"is_famous" => $faker->boolean(),
1516
];
1617
});

tests/database/factories/ProfileFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
$factory->define(Profile::class, function (Faker $faker) {
77
return [
88
'first_name' => $faker->firstName,
9-
'first_name' => $faker->lastName,
9+
'last_name' => $faker->lastName,
1010
];
1111
});

tests/database/migrations/2017_09_21_010055_create_authors.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public function up()
1313
$table->softDeletes();
1414

1515
$table->string('email');
16+
$table->boolean("is_famous")->default(false);
1617
$table->string('name');
1718
$table->json("finances")->nullable();
1819
});

0 commit comments

Comments
 (0)