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\TestCase; |
6
|
|
|
use Illuminate\Foundation\Testing\RefreshDatabase; |
7
|
|
|
|
8
|
|
|
class CacheTest extends TestCase |
9
|
|
|
{ |
10
|
|
|
use RefreshDatabase; |
11
|
|
|
|
12
|
|
|
public function setUp() |
13
|
|
|
{ |
14
|
|
|
parent::setUp(); |
15
|
|
|
|
16
|
|
|
cache()->flush(); |
17
|
|
|
|
18
|
|
|
factory(Author::class, 10)->create() |
19
|
|
|
->each(function($author) { |
20
|
|
|
factory(Book::class, random_int(2, 10))->make() |
21
|
|
|
->each(function ($book) use ($author) { |
22
|
|
|
$book->author()->associate($author); |
23
|
|
|
$book->save(); |
24
|
|
|
}); |
25
|
|
|
}); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function testCacheIsEmptyBeforeLoadingModels() |
29
|
|
|
{ |
30
|
|
|
$results = cache()->tags([ |
31
|
|
|
'genealabslaravelmodelcachingtestsfixturesauthor', |
32
|
|
|
'genealabslaravelmodelcachingtestsfixturesbook' |
33
|
|
|
]) |
34
|
|
|
->get('genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks'); |
35
|
|
|
|
36
|
|
|
$this->assertNull($results); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testCacheIsNotEmptyAfterLoadingModels() |
40
|
|
|
{ |
41
|
|
|
(new Author)->with('books')->get()->first(); |
42
|
|
|
|
43
|
|
|
$results = cache()->tags([ |
44
|
|
|
'genealabslaravelmodelcachingtestsfixturesauthor', |
45
|
|
|
'genealabslaravelmodelcachingtestsfixturesbook' |
46
|
|
|
]) |
47
|
|
|
->get('genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks'); |
48
|
|
|
|
49
|
|
|
$this->assertNotNull($results); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
View Code Duplication |
public function testCreatingModelClearsCache() |
|
|
|
|
53
|
|
|
{ |
54
|
|
|
$author = (new Author)->with('books')->get()->first(); |
55
|
|
|
$author->name = "John Jinglheimer"; |
56
|
|
|
$author->save(); |
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->assertNull($results); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
View Code Duplication |
public function testDeletingModelClearsCache() |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
$author = (new Author)->with('books')->get()->first(); |
70
|
|
|
$author->delete(); |
71
|
|
|
|
72
|
|
|
$results = cache()->tags([ |
73
|
|
|
'genealabslaravelmodelcachingtestsfixturesauthor', |
74
|
|
|
'genealabslaravelmodelcachingtestsfixturesbook' |
75
|
|
|
]) |
76
|
|
|
->get('genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks'); |
77
|
|
|
|
78
|
|
|
$this->assertNull($results); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.