1
|
|
|
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Integration\Console\Commands; |
2
|
|
|
|
3
|
|
|
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Author; |
4
|
|
|
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Book; |
5
|
|
|
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Store; |
6
|
|
|
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedAuthor; |
7
|
|
|
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\PrefixedAuthor; |
8
|
|
|
use GeneaLabs\LaravelModelCaching\Tests\IntegrationTestCase; |
9
|
|
|
use Illuminate\Support\Str; |
10
|
|
|
|
11
|
|
|
class FlushTest extends IntegrationTestCase |
|
|
|
|
12
|
|
|
{ |
13
|
|
|
public function setUp() : void |
14
|
|
|
{ |
15
|
|
|
parent::setUp(); |
16
|
|
|
|
17
|
|
|
if (Str::startsWith($this->app->version(), '5.7')) { |
18
|
|
|
$this->withoutMockingConsoleOutput(); |
|
|
|
|
19
|
|
|
} |
20
|
|
|
} |
21
|
|
|
|
22
|
|
View Code Duplication |
public function testGivenModelIsFlushed() |
23
|
|
|
{ |
24
|
|
|
$authors = (new Author)->all(); |
25
|
|
|
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-authors.deleted_at_null"); |
26
|
|
|
$tags = ["genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor"]; |
27
|
|
|
|
28
|
|
|
$cachedResults = $this |
29
|
|
|
->cache |
30
|
|
|
->tags($tags) |
31
|
|
|
->get($key)['value']; |
32
|
|
|
$result = $this |
33
|
|
|
->artisan('modelCache:clear', ['--model' => Author::class]) |
34
|
|
|
->execute(); |
35
|
|
|
$flushedResults = $this |
36
|
|
|
->cache |
37
|
|
|
->tags($tags) |
38
|
|
|
->get($key)['value'] |
39
|
|
|
?? null; |
40
|
|
|
|
41
|
|
|
$this->assertEquals($authors, $cachedResults); |
42
|
|
|
$this->assertEmpty($flushedResults); |
43
|
|
|
$this->assertEquals($result, 0); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
View Code Duplication |
public function testExtendedModelIsFlushed() |
47
|
|
|
{ |
48
|
|
|
$authors = (new PrefixedAuthor) |
49
|
|
|
->get(); |
50
|
|
|
|
51
|
|
|
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:model-prefix:authors:genealabslaravelmodelcachingtestsfixturesprefixedauthor-authors.deleted_at_null"); |
52
|
|
|
$tags = ["genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:model-prefix:genealabslaravelmodelcachingtestsfixturesprefixedauthor"]; |
53
|
|
|
|
54
|
|
|
$cachedResults = $this |
55
|
|
|
->cache |
56
|
|
|
->tags($tags) |
57
|
|
|
->get($key)['value']; |
58
|
|
|
$result = $this |
59
|
|
|
->artisan('modelCache:clear', ['--model' => PrefixedAuthor::class]) |
60
|
|
|
->execute(); |
61
|
|
|
$flushedResults = $this |
62
|
|
|
->cache |
63
|
|
|
->tags($tags) |
64
|
|
|
->get($key)['value'] |
65
|
|
|
?? null; |
66
|
|
|
|
67
|
|
|
$this->assertEquals($authors, $cachedResults); |
68
|
|
|
$this->assertEmpty($flushedResults); |
69
|
|
|
$this->assertEquals($result, 0); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testGivenModelWithRelationshipIsFlushed() |
73
|
|
|
{ |
74
|
|
|
$authors = (new Author)->with('books')->get(); |
75
|
|
|
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-authors.deleted_at_null-testing:{$this->testingSqlitePath}testing.sqlite:books"); |
76
|
|
|
$tags = [ |
77
|
|
|
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor", |
78
|
|
|
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesbook", |
79
|
|
|
]; |
80
|
|
|
|
81
|
|
|
$cachedResults = $this->cache |
82
|
|
|
->tags($tags) |
83
|
|
|
->get($key)['value']; |
84
|
|
|
$result = $this |
85
|
|
|
->artisan( |
86
|
|
|
'modelCache:clear', |
87
|
|
|
['--model' => Author::class] |
88
|
|
|
) |
89
|
|
|
->execute(); |
90
|
|
|
$flushedResults = $this->cache |
91
|
|
|
->tags($tags) |
92
|
|
|
->get($key)['value'] |
93
|
|
|
?? null; |
94
|
|
|
|
95
|
|
|
$this->assertEquals($authors, $cachedResults); |
96
|
|
|
$this->assertEmpty($flushedResults); |
97
|
|
|
$this->assertEquals($result, 0); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function testNonCachedModelsCannotBeFlushed() |
101
|
|
|
{ |
102
|
|
|
$result = $this->artisan( |
103
|
|
|
'modelCache:clear', |
104
|
|
|
['--model' => UncachedAuthor::class] |
105
|
|
|
) |
106
|
|
|
->execute(); |
107
|
|
|
|
108
|
|
|
$this->assertEquals($result, 1); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function testAllModelsAreFlushed() |
112
|
|
|
{ |
113
|
|
|
(new Author)->all(); |
114
|
|
|
(new Book)->all(); |
115
|
|
|
(new Store)->all(); |
116
|
|
|
|
117
|
|
|
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-authors.deleted_at_null"); |
118
|
|
|
$tags = ["genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor"]; |
119
|
|
|
$cachedAuthors = $this->cache |
120
|
|
|
->tags($tags) |
121
|
|
|
->get($key)['value']; |
122
|
|
|
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:books:genealabslaravelmodelcachingtestsfixturesbook"); |
123
|
|
|
$tags = ["genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesbook"]; |
124
|
|
|
$cachedBooks = $this->cache |
125
|
|
|
->tags($tags) |
126
|
|
|
->get($key)['value']; |
127
|
|
|
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:stores:genealabslaravelmodelcachingtestsfixturesstore"); |
128
|
|
|
$tags = ["genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesstore"]; |
129
|
|
|
$cachedStores = $this->cache |
130
|
|
|
->tags($tags) |
131
|
|
|
->get($key)['value']; |
132
|
|
|
|
133
|
|
|
$this->assertNotEmpty($cachedAuthors); |
134
|
|
|
$this->assertNotEmpty($cachedBooks); |
135
|
|
|
$this->assertNotEmpty($cachedStores); |
136
|
|
|
|
137
|
|
|
$this->artisan('modelCache:clear') |
138
|
|
|
->execute(); |
139
|
|
|
|
140
|
|
|
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor"); |
141
|
|
|
$tags = ["genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor"]; |
142
|
|
|
$cachedAuthors = $this->cache |
143
|
|
|
->tags($tags) |
144
|
|
|
->get($key)['value'] |
145
|
|
|
?? null; |
146
|
|
|
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:books:genealabslaravelmodelcachingtestsfixturesbook"); |
147
|
|
|
$tags = ["genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesbook"]; |
148
|
|
|
$cachedBooks = $this->cache |
149
|
|
|
->tags($tags) |
150
|
|
|
->get($key)['value'] |
151
|
|
|
?? null; |
152
|
|
|
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:stores:genealabslaravelmodelcachingtestsfixturesstore"); |
153
|
|
|
$tags = ["genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesstore"]; |
154
|
|
|
$cachedStores = $this->cache |
155
|
|
|
->tags($tags) |
156
|
|
|
->get($key)['value'] |
157
|
|
|
?? null; |
158
|
|
|
|
159
|
|
|
$this->assertEmpty($cachedAuthors); |
160
|
|
|
$this->assertEmpty($cachedBooks); |
161
|
|
|
$this->assertEmpty($cachedStores); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|