Completed
Pull Request — master (#372)
by Mike
31:01 queued 16:07
created

FlushTest::testAllModelsAreFlushed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 52
rs 9.0472
c 0
b 0
f 0
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
0 ignored issues
show
Bug introduced by
There is at least one abstract method in this class. Maybe declare it as abstract, or implement the remaining methods: artisan, be, call, seed
Loading history...
12
{
13
    public function setUp() : void
14
    {
15
        parent::setUp();
16
17
        if (Str::startsWith($this->app->version(), '5.7')) {
18
            $this->withoutMockingConsoleOutput();
0 ignored issues
show
Bug introduced by
The method withoutMockingConsoleOutput() does not seem to exist on object<GeneaLabs\Laravel...ole\Commands\FlushTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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