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

GetTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 83
Duplicated Lines 22.89 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 4
dl 19
loc 83
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetModelResultsCreatesCache() 0 23 1
A testAccessingGetResultsViaArrayIndexDoesNotError() 0 16 1
A testGetWithFieldArrayCachesResults() 19 19 1
A testGetWithFieldStringCachesResults() 0 19 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\UncachedAuthor;
5
use GeneaLabs\LaravelModelCaching\Tests\IntegrationTestCase;
6
7
class GetTest 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...
8
{
9
    public function testGetModelResultsCreatesCache()
10
    {
11
        $authors = (new Author)
12
            ->with('books', 'profile')
13
            ->get();
14
        $key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-authors.deleted_at_null-testing:{$this->testingSqlitePath}testing.sqlite:books-testing:{$this->testingSqlitePath}testing.sqlite:profile");
15
        $tags = [
16
            "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor",
17
            "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesbook",
18
            "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesprofile",
19
        ];
20
21
        $cachedResults = $this
22
            ->cache()
23
            ->tags($tags)
24
            ->get($key)['value'];
25
        $liveResults = (new UncachedAuthor)
26
            ->with('books', 'profile')
27
            ->get();
28
29
        $this->assertEquals($authors, $cachedResults);
30
        $this->assertEmpty($liveResults->diffKeys($cachedResults));
31
    }
32
33
    public function testAccessingGetResultsViaArrayIndexDoesNotError()
34
    {
35
        $author = (new Author)
36
            ->where('id', 1)
37
            ->get()[0];
38
        $cachedAuthor = (new Author)
39
            ->where('id', 1)
40
            ->get()[0];
41
        $uncachedAuthor = (new UncachedAuthor)
42
            ->where('id', 1)
43
            ->get()[0];
44
45
        $this->assertEquals(1, $author->id);
46
        $this->assertEquals($author, $cachedAuthor);
47
        $this->assertEquals($author->toArray(), $uncachedAuthor->toArray());
48
    }
49
50 View Code Duplication
    public function testGetWithFieldArrayCachesResults()
51
    {
52
        $key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor_id_name-authors.deleted_at_null");
53
        $tags = [
54
            "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor",
55
        ];
56
57
        $authors = (new Author)
58
            ->get(["id", "name"]);
59
        $cachedResults = $this
60
            ->cache()
61
            ->tags($tags)
62
            ->get($key)['value'];
63
        $liveResults = (new UncachedAuthor)
64
            ->get(["id", "name"]);
65
66
        $this->assertEquals($liveResults->pluck("id"), $authors->pluck("id"));
67
        $this->assertEquals($liveResults->pluck("id"), $cachedResults->pluck("id"));
68
    }
69
70
    public function testGetWithFieldStringCachesResults()
71
    {
72
        $key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor_id-authors.deleted_at_null");
73
        $tags = [
74
            "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor",
75
        ];
76
77
        $authors = (new Author)
78
            ->get("id");
79
        $cachedResults = $this
80
            ->cache()
81
            ->tags($tags)
82
            ->get($key)['value'];
83
        $liveResults = (new UncachedAuthor)
84
            ->get("id");
85
86
        $this->assertEquals($liveResults->pluck("id"), $authors->pluck("id"));
87
        $this->assertEquals($liveResults->pluck("id"), $cachedResults->pluck("id"));
88
    }
89
}
90