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

FindTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 68
Duplicated Lines 11.76 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testFindModelResultsCreatesCache() 0 15 1
A testFindMultipleModelResultsCreatesCache() 0 18 1
A testSubsequentFindsReturnDifferentModels() 0 9 1
A testFindWithArrayReturnsResults() 8 8 1
A testFindWithSingleElementArrayDoesntConflictWithNormalFind() 0 11 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 FindTest 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 testFindModelResultsCreatesCache()
10
    {
11
        $author = collect()->push((new Author)->find(1));
12
        $key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor_1");
13
        $tags = [
14
            "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor",
15
        ];
16
17
        $cachedResults = collect()->push($this->cache()->tags($tags)
18
            ->get($key));
19
        $liveResults = collect()->push((new UncachedAuthor)->find(1));
20
21
        $this->assertEmpty($author->diffKeys($cachedResults));
22
        $this->assertEmpty($liveResults->diffKeys($cachedResults));
23
    }
24
25
    public function testFindMultipleModelResultsCreatesCache()
26
    {
27
        $authors = (new Author)
28
            ->find([1, 2, 3]);
29
        $key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-authors.deleted_at_null-find_list_1_2_3");
30
        $tags = [
31
            "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor",
32
        ];
33
34
        $cachedResults = $this
35
            ->cache()
36
            ->tags($tags)
37
            ->get($key)["value"];
38
        $liveResults = (new UncachedAuthor)->find([1, 2, 3]);
39
40
        $this->assertEquals($authors->pluck("id"), $cachedResults->pluck("id"));
41
        $this->assertEquals($liveResults->pluck("id"), $cachedResults->pluck("id"));
42
    }
43
44
    public function testSubsequentFindsReturnDifferentModels()
45
    {
46
        $author1 = (new Author)->find(1);
47
        $author2 = (new Author)->find(2);
48
49
        $this->assertNotEquals($author1, $author2);
50
        $this->assertEquals($author1->id, 1);
51
        $this->assertEquals($author2->id, 2);
52
    }
53
54 View Code Duplication
    public function testFindWithArrayReturnsResults()
55
    {
56
        $author = (new Author)->find([1, 2]);
57
        $uncachedAuthor = (new UncachedAuthor)->find([1, 2]);
58
59
        $this->assertEquals($uncachedAuthor->count(), $author->count());
60
        $this->assertEquals($uncachedAuthor->pluck("id"), $author->pluck("id"));
61
    }
62
63
    public function testFindWithSingleElementArrayDoesntConflictWithNormalFind()
64
    {
65
        $author1 = (new Author)
66
            ->find(1);
67
        $author2 = (new Author)
68
            ->find([1]);
69
        
70
        $this->assertNotEquals($author1, $author2);
71
        $this->assertIsIterable($author2);
72
        $this->assertEquals(Author::class, get_class($author1));
73
    }
74
}
75