|
1
|
|
|
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Unit; |
|
2
|
|
|
|
|
3
|
|
|
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Author; |
|
4
|
|
|
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Book; |
|
5
|
|
|
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Profile; |
|
6
|
|
|
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Store; |
|
7
|
|
|
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedAuthor; |
|
8
|
|
|
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedBook; |
|
9
|
|
|
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedProfile; |
|
10
|
|
|
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedStore; |
|
11
|
|
|
use GeneaLabs\LaravelModelCaching\Tests\TestCase; |
|
12
|
|
|
use Illuminate\Foundation\Testing\RefreshDatabase; |
|
13
|
|
|
|
|
14
|
|
|
class CachedModelTest extends TestCase |
|
15
|
|
|
{ |
|
16
|
|
|
use RefreshDatabase; |
|
17
|
|
|
|
|
18
|
|
|
public function setUp() |
|
19
|
|
|
{ |
|
20
|
|
|
parent::setUp(); |
|
21
|
|
|
|
|
22
|
|
|
cache()->flush(); |
|
23
|
|
|
factory(Author::class, 10)->create() |
|
24
|
|
|
->each(function($author) { |
|
25
|
|
|
factory(Book::class, random_int(2, 10))->make() |
|
26
|
|
|
->each(function ($book) use ($author) { |
|
27
|
|
|
$book->author()->associate($author); |
|
28
|
|
|
$book->save(); |
|
29
|
|
|
}); |
|
30
|
|
|
factory(Profile::class)->make([ |
|
31
|
|
|
'author_id' => $author->id, |
|
32
|
|
|
]); |
|
33
|
|
|
}); |
|
34
|
|
|
|
|
35
|
|
|
$bookIds = (new Book)->all()->pluck('id'); |
|
36
|
|
|
factory(Store::class, 10)->create() |
|
37
|
|
|
->each(function ($store) use ($bookIds) { |
|
38
|
|
|
$store->books()->sync(rand($bookIds->min(), $bookIds->max())); |
|
39
|
|
|
}); |
|
40
|
|
|
cache()->flush(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function testAllModelResultsCreatesCache() |
|
44
|
|
|
{ |
|
45
|
|
|
$authors = (new Author)->all(); |
|
46
|
|
|
$key = 'genealabslaravelmodelcachingtestsfixturesauthor'; |
|
47
|
|
|
$tags = [ |
|
48
|
|
|
'genealabslaravelmodelcachingtestsfixturesauthor', |
|
49
|
|
|
]; |
|
50
|
|
|
|
|
51
|
|
|
$cachedResults = cache()->tags($tags) |
|
52
|
|
|
->get($key); |
|
53
|
|
|
$liveResults = (new UncachedAuthor)->all(); |
|
54
|
|
|
|
|
55
|
|
|
$this->assertEquals($authors, $cachedResults); |
|
56
|
|
|
$this->assertEmpty($liveResults->diffAssoc($cachedResults)); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|