Skip to content

Commit a06f108

Browse files
authored
Merge pull request #32 from GeneaLabs/laravel-5.5
- Add test of query scopes in cache key - Add test of nested relationship queries
2 parents 4ed4484 + 5b084ce commit a06f108

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

Diff for: tests/Fixtures/Author.php

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Fixtures;
22

33
use GeneaLabs\LaravelModelCaching\CachedModel;
4+
use Illuminate\Database\Eloquent\Builder;
45
use Illuminate\Database\Eloquent\Relations\HasMany;
56
use Illuminate\Database\Eloquent\Relations\HasOne;
67

@@ -20,4 +21,9 @@ public function profile() : HasOne
2021
{
2122
return $this->hasOne(Profile::class);
2223
}
24+
25+
public function scopeStartsWithA(Builder $query) : Builder
26+
{
27+
return $query->where('name', 'LIKE', 'A%');
28+
}
2329
}

Diff for: tests/Fixtures/UncachedAuthor.php

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Fixtures;
22

3+
use Illuminate\Database\Eloquent\Builder;
34
use Illuminate\Database\Eloquent\Model;
45
use Illuminate\Database\Eloquent\Relations\HasMany;
56
use Illuminate\Database\Eloquent\Relations\HasOne;
@@ -21,4 +22,9 @@ public function profile() : HasOne
2122
{
2223
return $this->hasOne(UncachedProfile::class, 'author_id', 'id');
2324
}
25+
26+
public function scopeStartsWithA(Builder $query) : Builder
27+
{
28+
return $query->where('name', 'LIKE', 'A%');
29+
}
2430
}

Diff for: tests/Unit/CachedBuilderTest.php

+45-2
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ public function testExistsRelationshipWhereClauseParsing()
537537
$this->assertEmpty($liveResults->diffAssoc($cachedResults));
538538
}
539539

540-
public function testDoesntHaveWhereClaseParsing()
540+
public function testDoesntHaveWhereClauseParsing()
541541
{
542542
$authors = (new Author)
543543
->doesntHave('books')
@@ -583,7 +583,8 @@ public function testColumnsRelationshipWhereClauseParsing()
583583
public function testRawWhereClauseParsing()
584584
{
585585
$authors = collect([(new Author)
586-
->whereRaw('name <> \'\'')->first()]);
586+
->whereRaw('name <> \'\'')
587+
->first()]);
587588

588589
$key = 'genealabslaravelmodelcachingtestsfixturesauthor_and_name-first';
589590
$tags = ['genealabslaravelmodelcachingtestsfixturesauthor'];
@@ -596,4 +597,46 @@ public function testRawWhereClauseParsing()
596597
$this->assertTrue($authors->diffAssoc($cachedResults)->isEmpty());
597598
$this->assertTrue($liveResults->diffAssoc($cachedResults)->isEmpty());
598599
}
600+
601+
public function testScopeClauseParsing()
602+
{
603+
$author = factory(Author::class, 1)
604+
->create(['name' => 'Anton'])
605+
->first();
606+
$authors = (new Author)
607+
->startsWithA()
608+
->get();
609+
$key = 'genealabslaravelmodelcachingtestsfixturesauthor-name_A%';
610+
$tags = ['genealabslaravelmodelcachingtestsfixturesauthor'];
611+
612+
$cachedResults = cache()->tags($tags)->get($key);
613+
$liveResults = (new UncachedAuthor)
614+
->startsWithA()
615+
->get();
616+
617+
$this->assertTrue($authors->contains($author));
618+
$this->assertTrue($cachedResults->contains($author));
619+
$this->assertTrue($liveResults->contains($author));
620+
}
621+
622+
public function testRelationshipQueriesAreCached()
623+
{
624+
$books = (new Author)
625+
->first()
626+
->books()
627+
->get();
628+
$key = 'genealabslaravelmodelcachingtestsfixturesbook-books.author_id_1-books.author_id_notnull';
629+
$tags = [
630+
'genealabslaravelmodelcachingtestsfixturesbook'
631+
];
632+
633+
$cachedResults = cache()->tags($tags)->get($key);
634+
$liveResults = (new UncachedAuthor)
635+
->first()
636+
->books()
637+
->get();
638+
639+
$this->assertTrue($cachedResults->diffAssoc($books)->isEmpty());
640+
$this->assertTrue($liveResults->diffAssoc($books)->isEmpty());
641+
}
599642
}

0 commit comments

Comments
 (0)