Skip to content

Commit dc68057

Browse files
committed
Add redis dependency for tests, bring tests to green
1 parent 9c05868 commit dc68057

8 files changed

+53
-158
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
"phpmd/phpmd": "*",
2626
"phpunit/phpunit": "*",
2727
"sebastian/phpcpd": "*",
28-
"symfony/thanks": "^1.0"
28+
"symfony/thanks": "^1.0",
29+
"predis/predis": "^1.1"
2930
},
3031
"autoload": {
3132
"psr-4": {

tests/CreatesApplication.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,48 @@
11
<?php namespace GeneaLabs\LaravelModelCaching\Tests;
22

33
use GeneaLabs\LaravelModelCaching\Providers\Service as LaravelModelCachingService;
4+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Author;
5+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Book;
6+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Profile;
7+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Publisher;
8+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Store;
49
use Orchestra\Database\ConsoleServiceProvider;
510

611
trait CreatesApplication
712
{
13+
protected $cache;
14+
815
public function setUp()
916
{
1017
parent::setUp();
1118

1219
$this->withFactories(__DIR__ . '/database/factories');
1320
$this->loadMigrationsFrom(__DIR__ . '/database/migrations');
21+
22+
$this->cache = cache()
23+
->store(config('laravel-model-caching.store'));
24+
25+
$this->cache()->flush();
26+
$publishers = factory(Publisher::class, 10)->create();
27+
factory(Author::class, 10)->create()
28+
->each(function ($author) use ($publishers) {
29+
factory(Book::class, random_int(2, 10))->make()
30+
->each(function ($book) use ($author, $publishers) {
31+
$book->author()->associate($author);
32+
$book->publisher()->associate($publishers[rand(0, 9)]);
33+
$book->save();
34+
});
35+
factory(Profile::class)->make([
36+
'author_id' => $author->id,
37+
]);
38+
});
39+
40+
$bookIds = (new Book)->all()->pluck('id');
41+
factory(Store::class, 10)->create()
42+
->each(function ($store) use ($bookIds) {
43+
$store->books()->sync(rand($bookIds->min(), $bookIds->max()));
44+
});
45+
$this->cache()->flush();
1446
}
1547

1648
/**

tests/Unit/CachedBuilderTest.php

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,33 +22,6 @@ class CachedBuilderTest extends UnitTestCase
2222
{
2323
use RefreshDatabase;
2424

25-
public function setUp()
26-
{
27-
parent::setUp();
28-
29-
$this->cache()->flush();
30-
$publishers = factory(Publisher::class, 10)->create();
31-
factory(Author::class, 10)->create()
32-
->each(function ($author) use ($publishers) {
33-
factory(Book::class, random_int(2, 10))->make()
34-
->each(function ($book) use ($author, $publishers) {
35-
$book->author()->associate($author);
36-
$book->publisher()->associate($publishers[rand(0, 9)]);
37-
$book->save();
38-
});
39-
factory(Profile::class)->make([
40-
'author_id' => $author->id,
41-
]);
42-
});
43-
44-
$bookIds = (new Book)->all()->pluck('id');
45-
factory(Store::class, 10)->create()
46-
->each(function ($store) use ($bookIds) {
47-
$store->books()->sync(rand($bookIds->min(), $bookIds->max()));
48-
});
49-
$this->cache()->flush();
50-
}
51-
5225
public function testCacheIsEmptyBeforeLoadingModels()
5326
{
5427
$results = $this->cache()->tags([

tests/Unit/CachedModelTest.php

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,6 @@ class CachedModelTest extends UnitTestCase
1717
{
1818
use RefreshDatabase;
1919

20-
public function setUp()
21-
{
22-
parent::setUp();
23-
24-
cache()->flush();
25-
$publishers = factory(Publisher::class, 10)->create();
26-
factory(Author::class, 10)->create()
27-
->each(function ($author) use ($publishers) {
28-
factory(Book::class, random_int(2, 10))->make()
29-
->each(function ($book) use ($author, $publishers) {
30-
$book->author()->associate($author);
31-
$book->publisher()->associate($publishers[rand(0, 9)]);
32-
$book->save();
33-
});
34-
factory(Profile::class)->make([
35-
'author_id' => $author->id,
36-
]);
37-
});
38-
39-
$bookIds = (new Book)->all()->pluck('id');
40-
factory(Store::class, 10)->create()
41-
->each(function ($store) use ($bookIds) {
42-
$store->books()->sync(rand($bookIds->min(), $bookIds->max()));
43-
});
44-
cache()->flush();
45-
}
46-
4720
public function testAllModelResultsCreatesCache()
4821
{
4922
$authors = (new Author)->all();
@@ -52,7 +25,7 @@ public function testAllModelResultsCreatesCache()
5225
'genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor',
5326
];
5427

55-
$cachedResults = cache()
28+
$cachedResults = $this->cache()
5629
->tags($tags)
5730
->get($key)['value'];
5831
$liveResults = (new UncachedAuthor)
@@ -71,7 +44,7 @@ public function testScopeDisablesCaching()
7144
->disableCache()
7245
->get();
7346

74-
$cachedResults = cache()
47+
$cachedResults = $this->cache()
7548
->tags($tags)
7649
->get($key)['value'];
7750

tests/Unit/Console/Commands/FlushTest.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,10 @@ class FlushTest extends UnitTestCase
1313
{
1414
use RefreshDatabase;
1515

16-
protected $cache;
17-
1816
public function setUp()
1917
{
2018
parent::setUp();
2119

22-
$this->cache = cache()->store(config('laravel-model-caching:store'));
23-
2420
$this->cache->flush();
2521
$publishers = factory(Publisher::class, 10)->create();
2622
factory(Author::class, 10)->create()
@@ -97,7 +93,7 @@ public function testNonCachedModelsCannotBeFlushed()
9793

9894
$this->assertEquals($result, 1);
9995
}
100-
96+
/** @group test */
10197
public function testAllModelsAreFlushed()
10298
{
10399
(new Author)->all();
@@ -124,7 +120,7 @@ public function testAllModelsAreFlushed()
124120
$this->assertNotEmpty($cachedBooks);
125121
$this->assertNotEmpty($cachedStores);
126122

127-
$this->artisan('modelCache:flush', []);
123+
$this->artisan('modelCache:flush');
128124

129125
$key = sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor');
130126
$tags = ['genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor'];

tests/Unit/DisabledCachedBuilderTest.php

Lines changed: 12 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -22,33 +22,6 @@ class DisabledCachedBuilderTest extends UnitTestCase
2222
{
2323
use RefreshDatabase;
2424

25-
public function setUp()
26-
{
27-
parent::setUp();
28-
29-
cache()->flush();
30-
$publishers = factory(Publisher::class, 10)->create();
31-
factory(Author::class, 10)->create()
32-
->each(function ($author) use ($publishers) {
33-
factory(Book::class, random_int(2, 10))->make()
34-
->each(function ($book) use ($author, $publishers) {
35-
$book->author()->associate($author);
36-
$book->publisher()->associate($publishers[rand(0, 9)]);
37-
$book->save();
38-
});
39-
factory(Profile::class)->make([
40-
'author_id' => $author->id,
41-
]);
42-
});
43-
44-
$bookIds = (new Book)->all()->pluck('id');
45-
factory(Store::class, 10)->create()
46-
->each(function ($store) use ($bookIds) {
47-
$store->books()->sync(rand($bookIds->min(), $bookIds->max()));
48-
});
49-
cache()->flush();
50-
}
51-
5225
public function testAvgModelResultsIsNotCached()
5326
{
5427
$authorId = (new Author)
@@ -62,7 +35,7 @@ public function testAvgModelResultsIsNotCached()
6235
'genealabslaravelmodelcachingtestsfixturesprofile',
6336
];
6437

65-
$cachedResult = cache()
38+
$cachedResult = $this->cache()
6639
->tags($tags)
6740
->get($key);
6841
$liveResult = (new UncachedAuthor)
@@ -111,7 +84,8 @@ public function testChunkModelResultsIsNotCached()
11184

11285
for ($index = 0; $index < $cachedChunks['authors']->count(); $index++) {
11386
$key = $cachedChunks['keys'][$index];
114-
$cachedResults = cache()->tags($tags)
87+
$cachedResults = $this->cache()
88+
->tags($tags)
11589
->get($key);
11690

11791
$this->assertNull($cachedResults);
@@ -132,7 +106,7 @@ public function testCountModelResultsIsNotCached()
132106
'genealabslaravelmodelcachingtestsfixturesprofile',
133107
];
134108

135-
$cachedResults = cache()
109+
$cachedResults = $this->cache()
136110
->tags($tags)
137111
->get($key);
138112
$liveResults = (new UncachedAuthor)
@@ -156,7 +130,7 @@ public function testCursorModelResultsIsNotCached()
156130
'genealabslaravelmodelcachingtestsfixturesprofile',
157131
];
158132

159-
$cachedResults = cache()
133+
$cachedResults = $this->cache()
160134
->tags($tags)
161135
->get($key);
162136
$liveResults = collect(
@@ -180,7 +154,7 @@ public function testFindModelResultsIsNotCached()
180154
'genealabslaravelmodelcachingtestsfixturesauthor',
181155
];
182156

183-
$cachedResult = cache()
157+
$cachedResult = $this->cache()
184158
->tags($tags)
185159
->get($key);
186160
$liveResult = (new UncachedAuthor)
@@ -203,7 +177,7 @@ public function testGetModelResultsIsNotCached()
203177
'genealabslaravelmodelcachingtestsfixturesprofile',
204178
];
205179

206-
$cachedResults = cache()
180+
$cachedResults = $this->cache()
207181
->tags($tags)
208182
->get($key);
209183
$liveResults = (new UncachedAuthor)
@@ -227,7 +201,7 @@ public function testMaxModelResultsIsNotCached()
227201
'genealabslaravelmodelcachingtestsfixturesprofile',
228202
];
229203

230-
$cachedResult = cache()
204+
$cachedResult = $this->cache()
231205
->tags($tags)
232206
->get($key);
233207
$liveResult = (new UncachedAuthor)
@@ -251,7 +225,7 @@ public function testMinModelResultsIsNotCached()
251225
'genealabslaravelmodelcachingtestsfixturesprofile',
252226
];
253227

254-
$cachedResult = cache()
228+
$cachedResult = $this->cache()
255229
->tags($tags)
256230
->get($key);
257231
$liveResult = (new UncachedAuthor)
@@ -275,7 +249,7 @@ public function testPluckModelResultsIsNotCached()
275249
'genealabslaravelmodelcachingtestsfixturesprofile',
276250
];
277251

278-
$cachedResults = cache()
252+
$cachedResults = $this->cache()
279253
->tags($tags)
280254
->get($key);
281255
$liveResults = (new UncachedAuthor)
@@ -299,7 +273,7 @@ public function testSumModelResultsIsNotCached()
299273
'genealabslaravelmodelcachingtestsfixturesprofile',
300274
];
301275

302-
$cachedResult = cache()
276+
$cachedResult = $this->cache()
303277
->tags($tags)
304278
->get($key);
305279
$liveResult = (new UncachedAuthor)
@@ -323,7 +297,7 @@ public function testValueModelResultsIsNotCached()
323297
'genealabslaravelmodelcachingtestsfixturesprofile',
324298
];
325299

326-
$cachedResult = cache()
300+
$cachedResult = $this->cache()
327301
->tags($tags)
328302
->get($key);
329303

tests/Unit/DisabledCachedModelTest.php

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,6 @@ class DisabledCachedModelTest extends UnitTestCase
1717
{
1818
use RefreshDatabase;
1919

20-
public function setUp()
21-
{
22-
parent::setUp();
23-
24-
cache()->flush();
25-
$publishers = factory(Publisher::class, 10)->create();
26-
factory(Author::class, 10)->create()
27-
->each(function ($author) use ($publishers) {
28-
factory(Book::class, random_int(2, 10))->make()
29-
->each(function ($book) use ($author, $publishers) {
30-
$book->author()->associate($author);
31-
$book->publisher()->associate($publishers[rand(0, 9)]);
32-
$book->save();
33-
});
34-
factory(Profile::class)->make([
35-
'author_id' => $author->id,
36-
]);
37-
});
38-
39-
$bookIds = (new Book)->all()->pluck('id');
40-
factory(Store::class, 10)->create()
41-
->each(function ($store) use ($bookIds) {
42-
$store->books()->sync(rand($bookIds->min(), $bookIds->max()));
43-
});
44-
cache()->flush();
45-
}
46-
4720
public function testAllModelResultsIsNotCached()
4821
{
4922
$key = sha1('genealabslaravelmodelcachingtestsfixturesauthor');
@@ -52,7 +25,7 @@ public function testAllModelResultsIsNotCached()
5225
->disableCache()
5326
->all();
5427

55-
$cachedResults = cache()
28+
$cachedResults = $this->cache()
5629
->tags($tags)
5730
->get($key);
5831
$liveResults = (new UncachedAuthor)

0 commit comments

Comments
 (0)