2
2
3
3
use GeneaLabs \LaravelModelCaching \Tests \Fixtures \Author ;
4
4
use GeneaLabs \LaravelModelCaching \Tests \Fixtures \Book ;
5
+ use GeneaLabs \LaravelModelCaching \Tests \Fixtures \Profile ;
6
+ use GeneaLabs \LaravelModelCaching \Tests \Fixtures \Store ;
5
7
use GeneaLabs \LaravelModelCaching \Tests \TestCase ;
6
8
use Illuminate \Foundation \Testing \RefreshDatabase ;
7
9
@@ -14,15 +16,24 @@ public function setUp()
14
16
parent ::setUp ();
15
17
16
18
cache ()->flush ();
17
-
18
19
factory (Author::class, 10 )->create ()
19
20
->each (function ($ author ) {
20
21
factory (Book::class, random_int (2 , 10 ))->make ()
21
22
->each (function ($ book ) use ($ author ) {
22
23
$ book ->author ()->associate ($ author );
23
24
$ book ->save ();
24
25
});
26
+ factory (Profile::class)->make ([
27
+ 'author_id ' => $ author ->id ,
28
+ ]);
29
+ });
30
+
31
+ $ bookIds = (new Book )->all ()->pluck ('id ' );
32
+ factory (Store::class, 10 )->create ()
33
+ ->each (function ($ store ) use ($ bookIds ) {
34
+ $ store ->books ()->sync (rand ($ bookIds ->min (), $ bookIds ->max ()));
25
35
});
36
+ cache ()->flush ();
26
37
}
27
38
28
39
public function testCacheIsEmptyBeforeLoadingModels ()
@@ -77,4 +88,60 @@ public function testDeletingModelClearsCache()
77
88
78
89
$ this ->assertNull ($ results );
79
90
}
91
+
92
+ public function testHasManyRelationshipIsCached ()
93
+ {
94
+ $ authors = (new Author )->with ('books ' )->get ();
95
+ $ authorIds = implode ('_ ' , $ authors ->pluck ('id ' )->toArray ());
96
+
97
+ $ results = cache ()->tags ([
98
+ 'genealabslaravelmodelcachingtestsfixturesauthor ' ,
99
+ 'genealabslaravelmodelcachingtestsfixturesbook '
100
+ ])
101
+ ->get ("genealabslaravelmodelcachingtestsfixturesauthor_ {$ authorIds }-genealabslaravelmodelcachingtestsfixturesbooks " );
102
+
103
+ $ this ->assertNotNull ($ results );
104
+ }
105
+
106
+ public function testBelongsToRelationshipIsCached ()
107
+ {
108
+ $ books = (new Book )->with ('author ' )->get ()->first ();
109
+ $ bookIds = implode ('_ ' , $ books ->pluck ('id ' )->toArray ());
110
+
111
+ $ results = cache ()->tags ([
112
+ 'genealabslaravelmodelcachingtestsfixturesbook ' ,
113
+ 'genealabslaravelmodelcachingtestsfixturesauthor '
114
+ ])
115
+ ->get ("genealabslaravelmodelcachingtestsfixturesbook_ {$ bookIds }-genealabslaravelmodelcachingtestsfixturesauthors " );
116
+
117
+ $ this ->assertNotNull ($ results );
118
+ }
119
+
120
+ public function testBelongsToManyRelationshipIsCached ()
121
+ {
122
+ $ books = (new Book )->with ('stores ' )->get ();
123
+ $ bookIds = implode ('_ ' , $ books ->pluck ('id ' )->toArray ());
124
+
125
+ $ results = cache ()->tags ([
126
+ 'genealabslaravelmodelcachingtestsfixturesbook ' ,
127
+ 'genealabslaravelmodelcachingtestsfixturesstore '
128
+ ])
129
+ ->get ("genealabslaravelmodelcachingtestsfixturesbook_ {$ bookIds }-genealabslaravelmodelcachingtestsfixturesstores " );
130
+
131
+ $ this ->assertNotNull ($ results );
132
+ }
133
+
134
+ public function testHasOneRelationshipIsCached ()
135
+ {
136
+ $ authors = (new Author )->with ('profile ' )->get ();
137
+ $ authorIds = implode ('_ ' , $ authors ->pluck ('id ' )->toArray ());
138
+
139
+ $ results = cache ()->tags ([
140
+ 'genealabslaravelmodelcachingtestsfixturesauthor ' ,
141
+ 'genealabslaravelmodelcachingtestsfixturesprofile '
142
+ ])
143
+ ->get ("genealabslaravelmodelcachingtestsfixturesauthor_ {$ authorIds }-genealabslaravelmodelcachingtestsfixturesprofiles " );
144
+
145
+ $ this ->assertNotNull ($ results );
146
+ }
80
147
}
0 commit comments