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 4
4
use Illuminate \Database \Eloquent \Model ;
5
5
use Illuminate \Database \Eloquent \Relations \HasMany ;
6
6
use Illuminate \Database \Eloquent \Relations \HasOne ;
7
- use GeneaLabs \LaravelModelCaching \CachedBuilder ;
8
7
9
8
class UncachedAuthor extends Model
10
9
{
Original file line number Diff line number Diff line change 14
14
use GeneaLabs \LaravelModelCaching \Tests \IntegrationTestCase ;
15
15
use Illuminate \Foundation \Testing \RefreshDatabase ;
16
16
use Illuminate \Support \Collection ;
17
+ use GeneaLabs \LaravelModelCaching \Tests \Fixtures \AuthorBeginsWithA ;
17
18
18
- class ModelScopeTest extends IntegrationTestCase
19
+ class ScopeTest extends IntegrationTestCase
19
20
{
20
21
public function testScopeClauseParsing ()
21
22
{
@@ -62,4 +63,27 @@ public function testScopeClauseWithParameter()
62
63
$ this ->assertTrue ($ cachedResults ->contains ($ author ));
63
64
$ this ->assertTrue ($ liveResults ->contains ($ author ));
64
65
}
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
+ }
65
89
}
You can’t perform that action at this time.
0 commit comments