Passed
Push — master ( bc3293...4020a2 )
by Mike
07:13
created

CacheTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 168
Duplicated Lines 72.02 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 10
dl 121
loc 168
rs 10
c 2
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 23 1
A testCacheIsEmptyBeforeLoadingModels() 0 9 1
A testDeletingModelClearsCache() 12 12 1
A testHasOneRelationshipIsCached() 17 17 1
A testBelongsToManyRelationshipIsCached() 16 16 1
A testBelongsToRelationshipIsCached() 16 16 1
A testUpdatingModelClearsCache() 13 13 1
A testCacheIsNotEmptyAfterLoadingModels() 11 11 1
A testHasManyRelationshipIsCached() 16 16 1
A testCreatingModelClearsCache() 13 13 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\TestCase;
8
use Illuminate\Foundation\Testing\RefreshDatabase;
9
10
class CacheTest extends TestCase
11
{
12
    use RefreshDatabase;
13
14
    public function setUp()
15
    {
16
        parent::setUp();
17
18
        cache()->flush();
19
        factory(Author::class, 10)->create()
20
            ->each(function($author) {
21
                factory(Book::class, random_int(2, 10))->make()
22
                    ->each(function ($book) use ($author) {
23
                        $book->author()->associate($author);
24
                        $book->save();
25
                    });
26
                factory(Profile::class)->make([
27
                    'author_id' => $author->id,
28
                ]);
29
            });
30
31
        $bookIds = (new Book)->all()->pluck('id');
32
        factory(Store::class, 10)->create()
33
            ->each(function ($store) use ($bookIds) {
34
                $store->books()->sync(rand($bookIds->min(), $bookIds->max()));
35
            });
36
        cache()->flush();
37
    }
38
39
    public function testCacheIsEmptyBeforeLoadingModels()
40
    {
41
        $results = cache()->tags([
42
                'genealabslaravelmodelcachingtestsfixturesauthor',
43
                'genealabslaravelmodelcachingtestsfixturesbook'
44
            ])
45
            ->get('genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks');
46
47
        $this->assertNull($results);
48
    }
49
50 View Code Duplication
    public function testCacheIsNotEmptyAfterLoadingModels()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
51
    {
52
        (new Author)->with('books')->get();
53
54
        $results = cache()->tags([
55
                'genealabslaravelmodelcachingtestsfixturesauthor',
56
                'genealabslaravelmodelcachingtestsfixturesbook'
57
            ])
58
            ->get('genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks');
59
60
        $this->assertNotNull($results);
61
    }
62
63 View Code Duplication
    public function testCreatingModelClearsCache()
64
    {
65
        $author = (new Author)->with('books')->get();
0 ignored issues
show
Unused Code introduced by
The assignment to $author is dead and can be removed.
Loading history...
66
67
        factory(Author::class)->create();
68
69
        $results = cache()->tags([
70
                'genealabslaravelmodelcachingtestsfixturesauthor',
71
                'genealabslaravelmodelcachingtestsfixturesbook'
72
            ])
73
            ->get('genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks');
74
75
        $this->assertNull($results);
76
    }
77
78 View Code Duplication
    public function testUpdatingModelClearsCache()
79
    {
80
        $author = (new Author)->with('books')->get()->first();
81
        $author->name = "John Jinglheimer";
82
        $author->save();
83
84
        $results = cache()->tags([
85
                'genealabslaravelmodelcachingtestsfixturesauthor',
86
                'genealabslaravelmodelcachingtestsfixturesbook'
87
            ])
88
            ->get('genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks');
89
90
        $this->assertNull($results);
91
    }
92
93 View Code Duplication
    public function testDeletingModelClearsCache()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
94
    {
95
        $author = (new Author)->with('books')->get()->first();
96
        $author->delete();
97
98
        $results = cache()->tags([
99
                'genealabslaravelmodelcachingtestsfixturesauthor',
100
                'genealabslaravelmodelcachingtestsfixturesbook'
101
            ])
102
            ->get('genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks');
103
104
        $this->assertNull($results);
105
    }
106
107 View Code Duplication
    public function testHasManyRelationshipIsCached()
108
    {
109
        $authors = (new Author)->with('books')->get();
110
        $authorIds = implode('_', $authors->pluck('id')->toArray());
111
112
        $results = collect(cache()->tags([
113
                'genealabslaravelmodelcachingtestsfixturesauthor',
114
                'genealabslaravelmodelcachingtestsfixturesbook'
115
            ])
116
            ->get("genealabslaravelmodelcachingtestsfixturesauthor_{$authorIds}-genealabslaravelmodelcachingtestsfixturesbooks"));
117
118
        $this->assertNotNull($results);
119
        $this->assertEmpty($authors->diffAssoc($results));
120
        $this->assertNotEmpty($authors);
121
        $this->assertNotEmpty($results);
122
        $this->assertEquals($authors->count(), $results->count());
123
    }
124
125 View Code Duplication
    public function testBelongsToRelationshipIsCached()
126
    {
127
        $books = (new Book)->with('author')->get();
128
        $bookIds = implode('_', $books->pluck('id')->toArray());
129
130
        $results = collect(cache()->tags([
131
                'genealabslaravelmodelcachingtestsfixturesbook',
132
                'genealabslaravelmodelcachingtestsfixturesauthor'
133
            ])
134
            ->get("genealabslaravelmodelcachingtestsfixturesbook_{$bookIds}-genealabslaravelmodelcachingtestsfixturesauthors"));
135
136
        $this->assertNotNull($results);
137
        $this->assertEmpty($books->diffAssoc($results));
138
        $this->assertNotEmpty($books);
139
        $this->assertNotEmpty($results);
140
        $this->assertEquals($books->count(), $results->count());
141
    }
142
143 View Code Duplication
    public function testBelongsToManyRelationshipIsCached()
144
    {
145
        $books = (new Book)->with('stores')->get();
146
        $bookIds = implode('_', $books->pluck('id')->toArray());
147
148
        $results = collect(cache()->tags([
149
                'genealabslaravelmodelcachingtestsfixturesbook',
150
                'genealabslaravelmodelcachingtestsfixturesstore'
151
            ])
152
            ->get("genealabslaravelmodelcachingtestsfixturesbook_{$bookIds}-genealabslaravelmodelcachingtestsfixturesstores"));
153
154
        $this->assertNotNull($results);
155
        $this->assertEmpty($books->diffAssoc($results));
156
        $this->assertNotEmpty($books);
157
        $this->assertNotEmpty($results);
158
        $this->assertEquals($books->count(), $results->count());
159
    }
160
161 View Code Duplication
    public function testHasOneRelationshipIsCached()
162
    {
163
        $authors = (new Author)->with('profile')->get();
164
        $authorIds = implode('_', $authors->pluck('id')->toArray());
165
166
        $results = collect(cache()
167
            ->tags([
168
                'genealabslaravelmodelcachingtestsfixturesauthor',
169
                'genealabslaravelmodelcachingtestsfixturesprofile'
170
            ])
171
            ->get("genealabslaravelmodelcachingtestsfixturesauthor_{$authorIds}-genealabslaravelmodelcachingtestsfixturesprofiles"));
172
173
        $this->assertNotNull($results);
174
        $this->assertEmpty($authors->diffAssoc($results));
175
        $this->assertNotEmpty($authors);
176
        $this->assertNotEmpty($results);
177
        $this->assertEquals($authors->count(), $results->count());
178
    }
179
}
180