Skip to content

Commit bf92bc6

Browse files
committed
Refactor test classes
1 parent 5d64164 commit bf92bc6

File tree

3 files changed

+194
-182
lines changed

3 files changed

+194
-182
lines changed

src/Builder.php

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,35 +20,6 @@ protected function cache(array $tags = [])
2020
return $cache;
2121
}
2222

23-
// protected function cacheResults(Relation $relation, array $models, string $name) : array
24-
// {
25-
// $parentIds = implode('_', collect($models)->pluck('id')->toArray());
26-
// $parentName = str_slug(get_class($relation->getParent()));
27-
// $childName = str_slug(get_class($relation->getRelated()));
28-
//
29-
// $cachedResults = $this->cache([$parentName, $childName])->rememberForever(
30-
// "{$parentName}_{$parentIds}-{$childName}s",
31-
// function () use ($relation, $models, $name) {
32-
// return $relation->match(
33-
// $relation->initRelation($models, $name),
34-
// $relation->getEager(),
35-
// $name
36-
// );
37-
// }
38-
// );
39-
//
40-
// return $cachedResults;
41-
// }
42-
//
43-
// protected function eagerLoadRelation(array $models, $name, Closure $constraints)
44-
// {
45-
// $relation = $this->getRelation($name);
46-
// $relation->addEagerConstraints($models);
47-
// $constraints($relation);
48-
//
49-
// return $this->cacheResults($relation, $models, $name);
50-
// }
51-
5223
protected function getCacheKey(array $columns = ['*'], $ids = null) : string
5324
{
5425
$key = str_slug(get_class($this->model));

tests/Unit/CacheTest.php renamed to tests/Unit/CachedBuilderTest.php

Lines changed: 136 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
use GeneaLabs\LaravelModelCaching\Tests\TestCase;
1212
use Illuminate\Foundation\Testing\RefreshDatabase;
1313

14-
class CacheTest extends TestCase
14+
/**
15+
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
16+
*/
17+
class CachedBuilderTest extends TestCase
1518
{
1619
use RefreshDatabase;
1720

@@ -40,161 +43,141 @@ public function setUp()
4043
cache()->flush();
4144
}
4245

43-
// public function testCacheIsEmptyBeforeLoadingModels()
44-
// {
45-
// $results = cache()->tags([
46-
// 'genealabslaravelmodelcachingtestsfixturesauthor',
47-
// 'genealabslaravelmodelcachingtestsfixturesbook'
48-
// ])
49-
// ->get('genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks');
50-
//
51-
// $this->assertNull($results);
52-
// }
53-
//
54-
// public function testCacheIsNotEmptyAfterLoadingModels()
55-
// {
56-
// (new Author)->with('books')->get();
57-
//
58-
// $results = cache()->tags([
59-
// 'genealabslaravelmodelcachingtestsfixturesauthor',
60-
// 'genealabslaravelmodelcachingtestsfixturesbook'
61-
// ])
62-
// ->get('genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks');
63-
//
64-
// $this->assertNotNull($results);
65-
// }
66-
//
67-
// public function testCreatingModelClearsCache()
68-
// {
69-
// (new Author)->with('books')->get();
70-
//
71-
// factory(Author::class)->create();
72-
//
73-
// $results = cache()->tags([
74-
// 'genealabslaravelmodelcachingtestsfixturesauthor',
75-
// 'genealabslaravelmodelcachingtestsfixturesbook'
76-
// ])
77-
// ->get('genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks');
78-
//
79-
// $this->assertNull($results);
80-
// }
81-
//
82-
// public function testUpdatingModelClearsCache()
83-
// {
84-
// $author = (new Author)->with('books')->get()->first();
85-
// $author->name = "John Jinglheimer";
86-
// $author->save();
87-
//
88-
// $results = cache()->tags([
89-
// 'genealabslaravelmodelcachingtestsfixturesauthor',
90-
// 'genealabslaravelmodelcachingtestsfixturesbook'
91-
// ])
92-
// ->get('genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks');
93-
//
94-
// $this->assertNull($results);
95-
// }
96-
//
97-
// public function testDeletingModelClearsCache()
98-
// {
99-
// $author = (new Author)->with('books')->get()->first();
100-
// $author->delete();
101-
//
102-
// $results = cache()->tags([
103-
// 'genealabslaravelmodelcachingtestsfixturesauthor',
104-
// 'genealabslaravelmodelcachingtestsfixturesbook'
105-
// ])
106-
// ->get('genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks');
107-
//
108-
// $this->assertNull($results);
109-
// }
110-
//
111-
// public function testHasManyRelationshipIsCached()
112-
// {
113-
// $authors = (new Author)->with('books')->get();
114-
// $authorIds = implode('_', $authors->pluck('id')->toArray());
115-
//
116-
// $results = collect(cache()->tags([
117-
// 'genealabslaravelmodelcachingtestsfixturesauthor',
118-
// 'genealabslaravelmodelcachingtestsfixturesbook'
119-
// ])
120-
// ->get("genealabslaravelmodelcachingtestsfixturesauthor_{$authorIds}-genealabslaravelmodelcachingtestsfixturesbooks"));
121-
//
122-
// $this->assertNotNull($results);
123-
// $this->assertEmpty($authors->diffAssoc($results));
124-
// $this->assertNotEmpty($authors);
125-
// $this->assertNotEmpty($results);
126-
// $this->assertEquals($authors->count(), $results->count());
127-
// }
128-
//
129-
// public function testBelongsToRelationshipIsCached()
130-
// {
131-
// $books = (new Book)->with('author')->get();
132-
// $bookIds = implode('_', $books->pluck('id')->toArray());
133-
//
134-
// $results = collect(cache()->tags([
135-
// 'genealabslaravelmodelcachingtestsfixturesbook',
136-
// 'genealabslaravelmodelcachingtestsfixturesauthor'
137-
// ])
138-
// ->get("genealabslaravelmodelcachingtestsfixturesbook_{$bookIds}-genealabslaravelmodelcachingtestsfixturesauthors"));
139-
//
140-
// $this->assertNotNull($results);
141-
// $this->assertEmpty($books->diffAssoc($results));
142-
// $this->assertNotEmpty($books);
143-
// $this->assertNotEmpty($results);
144-
// $this->assertEquals($books->count(), $results->count());
145-
// }
146-
//
147-
// public function testBelongsToManyRelationshipIsCached()
148-
// {
149-
// $books = (new Book)->with('stores')->get();
150-
// $bookIds = implode('_', $books->pluck('id')->toArray());
151-
//
152-
// $results = collect(cache()->tags([
153-
// 'genealabslaravelmodelcachingtestsfixturesbook',
154-
// 'genealabslaravelmodelcachingtestsfixturesstore'
155-
// ])
156-
// ->get("genealabslaravelmodelcachingtestsfixturesbook_{$bookIds}-genealabslaravelmodelcachingtestsfixturesstores"));
157-
//
158-
// $this->assertNotNull($results);
159-
// $this->assertEmpty($books->diffAssoc($results));
160-
// $this->assertNotEmpty($books);
161-
// $this->assertNotEmpty($results);
162-
// $this->assertEquals($books->count(), $results->count());
163-
// }
164-
//
165-
// public function testHasOneRelationshipIsCached()
166-
// {
167-
// $authors = (new Author)->with('profile')->get();
168-
// $authorIds = implode('_', $authors->pluck('id')->toArray());
169-
//
170-
// $results = collect(cache()
171-
// ->tags([
172-
// 'genealabslaravelmodelcachingtestsfixturesauthor',
173-
// 'genealabslaravelmodelcachingtestsfixturesprofile'
174-
// ])
175-
// ->get("genealabslaravelmodelcachingtestsfixturesauthor_{$authorIds}-genealabslaravelmodelcachingtestsfixturesprofiles"));
176-
//
177-
// $this->assertNotNull($results);
178-
// $this->assertEmpty($authors->diffAssoc($results));
179-
// $this->assertNotEmpty($authors);
180-
// $this->assertNotEmpty($results);
181-
// $this->assertEquals($authors->count(), $results->count());
182-
// }
183-
184-
public function testAllModelResultsCreatesCache()
46+
public function testCacheIsEmptyBeforeLoadingModels()
18547
{
186-
$authors = (new Author)->all();
187-
$key = 'genealabslaravelmodelcachingtestsfixturesauthor';
188-
$tags = [
189-
'genealabslaravelmodelcachingtestsfixturesauthor',
190-
];
48+
$results = cache()->tags([
49+
'genealabslaravelmodelcachingtestsfixturesauthor',
50+
'genealabslaravelmodelcachingtestsfixturesbook'
51+
])
52+
->get('genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks');
19153

192-
$cachedResults = cache()->tags($tags)
193-
->get($key);
194-
$liveResults = (new UncachedAuthor)->all();
54+
$this->assertNull($results);
55+
}
19556

196-
$this->assertEquals($authors, $cachedResults);
197-
$this->assertEmpty($liveResults->diffAssoc($cachedResults));
57+
public function testCacheIsNotEmptyAfterLoadingModels()
58+
{
59+
(new Author)->with('books')->get();
60+
61+
$results = cache()->tags([
62+
'genealabslaravelmodelcachingtestsfixturesauthor',
63+
'genealabslaravelmodelcachingtestsfixturesbook'
64+
])
65+
->get('genealabslaravelmodelcachingtestsfixturesauthor-books');
66+
67+
$this->assertNotNull($results);
68+
}
69+
70+
public function testCreatingModelClearsCache()
71+
{
72+
(new Author)->with('books')->get();
73+
74+
factory(Author::class)->create();
75+
76+
$results = cache()->tags([
77+
'genealabslaravelmodelcachingtestsfixturesauthor',
78+
'genealabslaravelmodelcachingtestsfixturesbook'
79+
])
80+
->get('genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks');
81+
82+
$this->assertNull($results);
83+
}
84+
85+
public function testUpdatingModelClearsCache()
86+
{
87+
$author = (new Author)->with('books')->get()->first();
88+
$author->name = "John Jinglheimer";
89+
$author->save();
90+
91+
$results = cache()->tags([
92+
'genealabslaravelmodelcachingtestsfixturesauthor',
93+
'genealabslaravelmodelcachingtestsfixturesbook'
94+
])
95+
->get('genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks');
96+
97+
$this->assertNull($results);
98+
}
99+
100+
public function testDeletingModelClearsCache()
101+
{
102+
$author = (new Author)->with('books')->get()->first();
103+
$author->delete();
104+
105+
$results = cache()->tags([
106+
'genealabslaravelmodelcachingtestsfixturesauthor',
107+
'genealabslaravelmodelcachingtestsfixturesbook'
108+
])
109+
->get('genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks');
110+
111+
$this->assertNull($results);
112+
}
113+
114+
public function testHasManyRelationshipIsCached()
115+
{
116+
$authors = (new Author)->with('books')->get();
117+
118+
$results = collect(cache()->tags([
119+
'genealabslaravelmodelcachingtestsfixturesauthor',
120+
'genealabslaravelmodelcachingtestsfixturesbook'
121+
])
122+
->get("genealabslaravelmodelcachingtestsfixturesauthor-books"));
123+
124+
$this->assertNotNull($results);
125+
$this->assertEmpty($authors->diffAssoc($results));
126+
$this->assertNotEmpty($authors);
127+
$this->assertNotEmpty($results);
128+
$this->assertEquals($authors->count(), $results->count());
129+
}
130+
131+
public function testBelongsToRelationshipIsCached()
132+
{
133+
$books = (new Book)->with('author')->get();
134+
135+
$results = collect(cache()->tags([
136+
'genealabslaravelmodelcachingtestsfixturesbook',
137+
'genealabslaravelmodelcachingtestsfixturesauthor'
138+
])
139+
->get("genealabslaravelmodelcachingtestsfixturesbook-author"));
140+
141+
$this->assertNotNull($results);
142+
$this->assertEmpty($books->diffAssoc($results));
143+
$this->assertNotEmpty($books);
144+
$this->assertNotEmpty($results);
145+
$this->assertEquals($books->count(), $results->count());
146+
}
147+
148+
public function testBelongsToManyRelationshipIsCached()
149+
{
150+
$books = (new Book)->with('stores')->get();
151+
152+
$results = collect(cache()->tags([
153+
'genealabslaravelmodelcachingtestsfixturesbook',
154+
'genealabslaravelmodelcachingtestsfixturesstore'
155+
])
156+
->get("genealabslaravelmodelcachingtestsfixturesbook-stores"));
157+
158+
$this->assertNotNull($results);
159+
$this->assertEmpty($books->diffAssoc($results));
160+
$this->assertNotEmpty($books);
161+
$this->assertNotEmpty($results);
162+
$this->assertEquals($books->count(), $results->count());
163+
}
164+
165+
public function testHasOneRelationshipIsCached()
166+
{
167+
$authors = (new Author)->with('profile')->get();
168+
169+
$results = collect(cache()
170+
->tags([
171+
'genealabslaravelmodelcachingtestsfixturesauthor',
172+
'genealabslaravelmodelcachingtestsfixturesprofile'
173+
])
174+
->get("genealabslaravelmodelcachingtestsfixturesauthor-profile"));
175+
176+
$this->assertNotNull($results);
177+
$this->assertEmpty($authors->diffAssoc($results));
178+
$this->assertNotEmpty($authors);
179+
$this->assertNotEmpty($results);
180+
$this->assertEquals($authors->count(), $results->count());
198181
}
199182

200183
public function testAvgModelResultsCreatesCache()

0 commit comments

Comments
 (0)