Passed
Push — master ( f0f114...6b401d )
by Mike
02:41
created

CacheTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 71
Duplicated Lines 36.62 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
dl 26
loc 71
rs 10
c 2
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testDeletingModelClearsCache() 12 12 1
A setUp() 0 12 1
A testCacheIsEmptyBeforeLoadingModels() 0 9 1
A testCacheIsNotEmptyAfterLoadingModels() 0 11 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\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()
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...
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()
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...
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