Completed
Push — master ( 7187a0...60d66c )
by Mike
02:58
created

CacheTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 136
Duplicated Lines 57.35 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
dl 78
loc 136
rs 10
c 2
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A testDeletingModelClearsCache() 12 12 1
A testHasOneRelationshipIsCached() 12 12 1
A testBelongsToManyRelationshipIsCached() 12 12 1
A testBelongsToRelationshipIsCached() 12 12 1
A setUp() 0 23 1
A testCacheIsEmptyBeforeLoadingModels() 0 9 1
A testCacheIsNotEmptyAfterLoadingModels() 0 11 1
A testHasManyRelationshipIsCached() 12 12 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
    public function testCacheIsNotEmptyAfterLoadingModels()
51
    {
52
        (new Author)->with('books')->get()->first();
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()
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...
64
    {
65
        $author = (new Author)->with('books')->get()->first();
66
        $author->name = "John Jinglheimer";
67
        $author->save();
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 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...
79
    {
80
        $author = (new Author)->with('books')->get()->first();
81
        $author->delete();
82
83
        $results = cache()->tags([
84
                'genealabslaravelmodelcachingtestsfixturesauthor',
85
                'genealabslaravelmodelcachingtestsfixturesbook'
86
            ])
87
            ->get('genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks');
88
89
        $this->assertNull($results);
90
    }
91
92 View Code Duplication
    public function testHasManyRelationshipIsCached()
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...
93
    {
94
        $authors = (new Author)->with('books')->get();
95
        $authorIds = implode('_', $authors->pluck('id')->toArray());
96
97
        $results = cache()->tags([
98
                'genealabslaravelmodelcachingtestsfixturesauthor',
99
                'genealabslaravelmodelcachingtestsfixturesbook'
100
            ])
101
            ->get("genealabslaravelmodelcachingtestsfixturesauthor_{$authorIds}-genealabslaravelmodelcachingtestsfixturesbooks");
102
103
        $this->assertNotNull($results);
104
    }
105
106 View Code Duplication
    public function testBelongsToRelationshipIsCached()
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...
107
    {
108
        $books = (new Book)->with('author')->get()->first();
109
        $bookIds = implode('_', $books->pluck('id')->toArray());
110
111
        $results = cache()->tags([
112
                'genealabslaravelmodelcachingtestsfixturesbook',
113
                'genealabslaravelmodelcachingtestsfixturesauthor'
114
            ])
115
            ->get("genealabslaravelmodelcachingtestsfixturesbook_{$bookIds}-genealabslaravelmodelcachingtestsfixturesauthors");
116
117
        $this->assertNotNull($results);
118
    }
119
120 View Code Duplication
    public function testBelongsToManyRelationshipIsCached()
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...
121
    {
122
        $books = (new Book)->with('stores')->get();
123
        $bookIds = implode('_', $books->pluck('id')->toArray());
124
125
        $results = cache()->tags([
126
                'genealabslaravelmodelcachingtestsfixturesbook',
127
                'genealabslaravelmodelcachingtestsfixturesstore'
128
            ])
129
            ->get("genealabslaravelmodelcachingtestsfixturesbook_{$bookIds}-genealabslaravelmodelcachingtestsfixturesstores");
130
131
        $this->assertNotNull($results);
132
    }
133
134 View Code Duplication
    public function testHasOneRelationshipIsCached()
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...
135
    {
136
        $authors = (new Author)->with('profile')->get();
137
        $authorIds = implode('_', $authors->pluck('id')->toArray());
138
139
        $results = cache()->tags([
140
                'genealabslaravelmodelcachingtestsfixturesauthor',
141
                'genealabslaravelmodelcachingtestsfixturesprofile'
142
            ])
143
            ->get("genealabslaravelmodelcachingtestsfixturesauthor_{$authorIds}-genealabslaravelmodelcachingtestsfixturesprofiles");
144
145
        $this->assertNotNull($results);
146
    }
147
}
148