File tree Expand file tree Collapse file tree 4 files changed +96
-2
lines changed
Integration/CachedBuilder Expand file tree Collapse file tree 4 files changed +96
-2
lines changed Original file line number Diff line number Diff line change 1+ <?php namespace GeneaLabs \LaravelModelCaching \Tests \Fixtures ;
2+
3+ use GeneaLabs \LaravelModelCaching \Tests \Fixtures \Scopes \NameBeginsWith ;
4+ use GeneaLabs \LaravelModelCaching \Traits \Cachable ;
5+ use Illuminate \Database \Eloquent \Builder ;
6+ use Illuminate \Database \Eloquent \Model ;
7+ use Illuminate \Database \Eloquent \Relations \HasMany ;
8+ use Illuminate \Database \Eloquent \Relations \HasOne ;
9+
10+ class AuthorBeginsWithA extends Model
11+ {
12+ use Cachable;
13+
14+ protected $ table = "authors " ;
15+ protected $ casts = [
16+ "finances " => "array " ,
17+ ];
18+ protected $ fillable = [
19+ 'name ' ,
20+ 'email ' ,
21+ "finances " ,
22+ ];
23+
24+ protected static function boot ()
25+ {
26+ parent ::boot ();
27+
28+ static ::addGlobalScope (new NameBeginsWith );
29+ }
30+
31+ public function books () : HasMany
32+ {
33+ return $ this ->hasMany (Book::class);
34+ }
35+
36+ public function profile () : HasOne
37+ {
38+ return $ this ->hasOne (Profile::class);
39+ }
40+
41+ public function getLatestBookAttribute ()
42+ {
43+ return $ this
44+ ->books ()
45+ ->latest ("id " )
46+ ->first ();
47+ }
48+
49+ public function scopeStartsWithA (Builder $ query ) : Builder
50+ {
51+ return $ query ->where ('name ' , 'LIKE ' , 'A% ' );
52+ }
53+
54+ public function scopeNameStartsWith (Builder $ query , string $ startOfName ) : Builder
55+ {
56+ return $ query ->where ("name " , "LIKE " , "{$ startOfName }% " );
57+ }
58+ }
Original file line number Diff line number Diff line change 1+ <?php namespace GeneaLabs \LaravelModelCaching \Tests \Fixtures \Scopes ;
2+
3+ use Illuminate \Database \Eloquent \Scope ;
4+ use Illuminate \Database \Eloquent \Model ;
5+ use Illuminate \Database \Eloquent \Builder ;
6+
7+ class NameBeginsWith implements Scope
8+ {
9+ public function apply (Builder $ builder , Model $ model )
10+ {
11+ $ builder ->where ('name ' , 'LIKE ' , "A% " );
12+ }
13+ }
Original file line number Diff line number Diff line change 44use Illuminate \Database \Eloquent \Model ;
55use Illuminate \Database \Eloquent \Relations \HasMany ;
66use Illuminate \Database \Eloquent \Relations \HasOne ;
7- use GeneaLabs \LaravelModelCaching \CachedBuilder ;
87
98class UncachedAuthor extends Model
109{
Original file line number Diff line number Diff line change 1414use GeneaLabs \LaravelModelCaching \Tests \IntegrationTestCase ;
1515use Illuminate \Foundation \Testing \RefreshDatabase ;
1616use Illuminate \Support \Collection ;
17+ use GeneaLabs \LaravelModelCaching \Tests \Fixtures \AuthorBeginsWithA ;
1718
18- class ModelScopeTest extends IntegrationTestCase
19+ class ScopeTest extends IntegrationTestCase
1920{
2021 public function testScopeClauseParsing ()
2122 {
@@ -62,4 +63,27 @@ public function testScopeClauseWithParameter()
6263 $ this ->assertTrue ($ cachedResults ->contains ($ author ));
6364 $ this ->assertTrue ($ liveResults ->contains ($ author ));
6465 }
66+
67+ /** @group test */
68+ public function testGlobalScopesAreCached ()
69+ {
70+ $ author = factory (Author::class, 1 )
71+ ->create (['name ' => 'Alois ' ])
72+ ->first ();
73+ $ authors = (new AuthorBeginsWithA )
74+ ->get ();
75+ $ key = sha1 ('genealabs:laravel-model-caching:testing::memory::authors:genealabslaravelmodelcachingtestsfixturesauthorbeginswitha ' );
76+ $ tags = ['genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesauthorbeginswitha ' ];
77+
78+ $ cachedResults = $ this ->cache ()
79+ ->tags ($ tags )
80+ ->get ($ key )['value ' ];
81+ $ liveResults = (new UncachedAuthor )
82+ ->nameStartsWith ("A " )
83+ ->get ();
84+
85+ $ this ->assertTrue ($ authors ->contains ($ author ));
86+ $ this ->assertTrue ($ cachedResults ->contains ($ author ));
87+ $ this ->assertTrue ($ liveResults ->contains ($ author ));
88+ }
6589}
You can’t perform that action at this time.
0 commit comments