Completed
Push — master ( 3f89d7...f790e9 )
by Mike
30:28 queued 29:00
created

WithTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 97
Duplicated Lines 43.3 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 5
dl 42
loc 97
c 0
b 0
f 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testWithLimitedQuery() 0 24 1
A testWithQuery() 21 21 1
A testMultiLevelWithQuery() 21 21 1
A testWithBelongsToManyRelationshipQuery() 0 25 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\Integration\CachedBuilder;
2
3
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Author;
4
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Book;
5
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedAuthor;
6
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedBook;
7
use GeneaLabs\LaravelModelCaching\Tests\IntegrationTestCase;
8
9
class WithTest extends IntegrationTestCase
10
{
11
    public function testWithLimitedQuery()
12
    {
13
        $authors = (new Author)
14
            ->where("id", 1)
15
            ->with([
16
                'books' => function ($query) {
17
                    $query->where("id", "<", 100)
18
                        ->offset(5)
19
                        ->limit(1);
20
                }
21
            ])
22
            ->first();
23
        $uncachedAuthor = (new UncachedAuthor)->with([
24
                'books' => function ($query) {
25
                    $query->where("id", "<", 100)
26
                        ->offset(5)
27
                        ->limit(1);
28
                }
29
            ])
30
            ->first();
31
32
        $this->assertEquals($uncachedAuthor->books()->pluck("id"), $authors->books()->pluck("id"));
33
        $this->assertEquals($uncachedAuthor->id, $authors->id);
34
    }
35
36 View Code Duplication
    public function testWithQuery()
37
    {
38
        $author = (new Author)
39
            ->where("id", 1)
40
            ->with([
41
                'books' => function ($query) {
42
                    $query->where("id", "<", 100);
43
                }
44
            ])
45
            ->first();
46
        $uncachedAuthor = (new UncachedAuthor)->with([
47
                'books' => function ($query) {
48
                    $query->where("id", "<", 100);
49
                },
50
            ])
51
            ->where("id", 1)
52
            ->first();
53
54
        $this->assertEquals($uncachedAuthor->books()->count(), $author->books()->count());
55
        $this->assertEquals($uncachedAuthor->id, $author->id);
56
    }
57
58 View Code Duplication
    public function testMultiLevelWithQuery()
59
    {
60
        $author = (new Author)
61
            ->where("id", 1)
62
            ->with([
63
                'books.publisher' => function ($query) {
64
                    $query->where("id", "<", 100);
65
                }
66
            ])
67
            ->first();
68
        $uncachedAuthor = (new UncachedAuthor)->with([
69
                'books.publisher' => function ($query) {
70
                    $query->where("id", "<", 100);
71
                },
72
            ])
73
            ->where("id", 1)
74
            ->first();
75
76
        $this->assertEquals($uncachedAuthor->books()->count(), $author->books()->count());
77
        $this->assertEquals($uncachedAuthor->id, $author->id);
78
    }
79
80
    public function testWithBelongsToManyRelationshipQuery()
81
    {
82
        $key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:books:genealabslaravelmodelcachingtestsfixturesbook-books.id_=_3-testing:{$this->testingSqlitePath}testing.sqlite:stores-first");
83
        $tags = [
84
            "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesbook",
85
            "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesstore",
86
        ];
87
88
        $stores = (new Book)
89
            ->with("stores")
90
            ->find(3)
91
            ->stores;
92
        $cachedResults = $this
93
            ->cache()
94
            ->tags($tags)
95
            ->get($key)['value']
96
            ->stores;
97
        $liveResults = (new UncachedBook)
98
            ->with("stores")
99
            ->find(3)
100
            ->stores;
101
102
        $this->assertEquals($liveResults->pluck("id"), $stores->pluck("id"));
103
        $this->assertEquals($liveResults->pluck("id"), $cachedResults->pluck("id"));
104
    }
105
}
106