11<?php namespace GeneaLabs \LaravelModelCaching \Tests \Integration \CachedBuilder ;
22
33use GeneaLabs \LaravelModelCaching \Tests \Fixtures \Author ;
4+ use GeneaLabs \LaravelModelCaching \Tests \Fixtures \Book ;
45use GeneaLabs \LaravelModelCaching \Tests \Fixtures \UncachedAuthor ;
6+ use GeneaLabs \LaravelModelCaching \Tests \Fixtures \UncachedBook ;
57use GeneaLabs \LaravelModelCaching \Tests \IntegrationTestCase ;
68
79class BooleanTest extends IntegrationTestCase
810{
9- public function testBooleanWhereCreatesCorrectCacheKey ()
11+ public function testBooleanWhereTrueCreatesCorrectCacheKey ()
1012 {
1113 $ key = sha1 ("genealabs:laravel-model-caching:testing: {$ this ->testingSqlitePath }testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-is_famous_=_1-authors.deleted_at_null " );
1214 $ tags = [
@@ -29,4 +31,61 @@ public function testBooleanWhereCreatesCorrectCacheKey()
2931 $ this ->assertNotEmpty ($ cachedResults );
3032 $ this ->assertNotEmpty ($ liveResults );
3133 }
34+
35+ public function testBooleanWhereFalseCreatesCorrectCacheKey ()
36+ {
37+ $ key = sha1 ("genealabs:laravel-model-caching:testing: {$ this ->testingSqlitePath }testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-is_famous_=_-authors.deleted_at_null " );
38+ $ tags = [
39+ "genealabs:laravel-model-caching:testing: {$ this ->testingSqlitePath }testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor " ,
40+ ];
41+
42+ $ authors = (new Author )
43+ ->where ("is_famous " , false )
44+ ->get ();
45+ $ cachedResults = $ this ->cache ()
46+ ->tags ($ tags )
47+ ->get ($ key )['value ' ];
48+ $ liveResults = (new UncachedAuthor )
49+ ->where ("is_famous " , false )
50+ ->get ();
51+
52+ $ this ->assertEquals ($ liveResults ->pluck ("id " ), $ authors ->pluck ("id " ));
53+ $ this ->assertEquals ($ liveResults ->pluck ("id " ), $ cachedResults ->pluck ("id " ));
54+ $ this ->assertNotEmpty ($ authors );
55+ $ this ->assertNotEmpty ($ cachedResults );
56+ $ this ->assertNotEmpty ($ liveResults );
57+ }
58+
59+ public function testBooleanWhereHasRelationWithFalseConditionAndAdditionalParentRawCondition ()
60+ {
61+ $ key = sha1 ("genealabs:laravel-model-caching:testing: {$ this ->testingSqlitePath }testing.sqlite:books:genealabslaravelmodelcachingtestsfixturesbook-exists-and_books.author_id_=_authors.id-is_famous_=_-authors.deleted_at_null-_and_title_=_Mixed_Clause " );
62+ $ tags = [
63+ "genealabs:laravel-model-caching:testing: {$ this ->testingSqlitePath }testing.sqlite:genealabslaravelmodelcachingtestsfixturesbook " ,
64+ ];
65+
66+ $ expectedAuthor = factory (Author::class)->create (['is_famous ' => false ]);
67+ factory (Book::class)->create (['author_id ' => $ expectedAuthor ->getKey (), 'title ' => 'Mixed Clause ' ]);
68+
69+ $ books = (new Book )
70+ ->whereHas ('author ' , function ($ query ) {
71+ return $ query ->where ('is_famous ' , false );
72+ })
73+ ->whereRaw ("title = ? " , ['Mixed Clause ' ]) // Test ensures this binding is included in the key
74+ ->get ();
75+ $ cachedResults = $ this ->cache ()
76+ ->tags ($ tags )
77+ ->get ($ key )['value ' ];
78+ $ liveResults = (new UncachedBook )
79+ ->whereHas ('author ' , function ($ query ) {
80+ return $ query ->where ('is_famous ' , false );
81+ })
82+ ->whereRaw ("title = ? " , ['Mixed Clause ' ])
83+ ->get ();
84+
85+ $ this ->assertEquals ($ liveResults ->pluck ("id " ), $ books ->pluck ("id " ));
86+ $ this ->assertEquals ($ liveResults ->pluck ("id " ), $ cachedResults ->pluck ("id " ));
87+ $ this ->assertNotEmpty ($ books );
88+ $ this ->assertNotEmpty ($ cachedResults );
89+ $ this ->assertNotEmpty ($ liveResults );
90+ }
3291}
0 commit comments