22
33use GeneaLabs \LaravelModelCaching \Tests \Fixtures \Author ;
44use GeneaLabs \LaravelModelCaching \Tests \Fixtures \Book ;
5+ use GeneaLabs \LaravelModelCaching \Tests \Fixtures \Profile ;
6+ use GeneaLabs \LaravelModelCaching \Tests \Fixtures \Store ;
57use GeneaLabs \LaravelModelCaching \Tests \TestCase ;
68use Illuminate \Foundation \Testing \RefreshDatabase ;
79
@@ -14,15 +16,24 @@ public function setUp()
1416 parent ::setUp ();
1517
1618 cache ()->flush ();
17-
1819 factory (Author::class, 10 )->create ()
1920 ->each (function ($ author ) {
2021 factory (Book::class, random_int (2 , 10 ))->make ()
2122 ->each (function ($ book ) use ($ author ) {
2223 $ book ->author ()->associate ($ author );
2324 $ book ->save ();
2425 });
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 ()));
2535 });
36+ cache ()->flush ();
2637 }
2738
2839 public function testCacheIsEmptyBeforeLoadingModels ()
@@ -77,4 +88,60 @@ public function testDeletingModelClearsCache()
7788
7889 $ this ->assertNull ($ results );
7990 }
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+ }
80147}
0 commit comments