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

WhereNotInTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 73
Duplicated Lines 32.88 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 4
dl 24
loc 73
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testWhereNotInQuery() 24 24 1
A testWhereNotInResults() 0 20 1
A testWhereNotInSubquery() 0 24 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\Book;
4
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedAuthor;
5
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedBook;
6
use GeneaLabs\LaravelModelCaching\Tests\IntegrationTestCase;
7
8
class WhereNotInTest 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...
9
{
10 View Code Duplication
    public function testWhereNotInQuery()
11
    {
12
        $key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:books:genealabslaravelmodelcachingtestsfixturesbook-author_id_notin_1_2_3_4");
13
        $tags = [
14
            "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesbook",
15
        ];
16
        $authors = (new UncachedAuthor)
17
            ->where("id", "<", 5)
18
            ->get(["id"]);
19
20
        $books = (new Book)
21
            ->whereNotIn("author_id", $authors)
22
            ->get();
23
        $cachedResults = $this
24
            ->cache()
25
            ->tags($tags)
26
            ->get($key)['value'];
27
        $liveResults = (new UncachedBook)
28
            ->whereNotIn("author_id", $authors)
29
            ->get();
30
31
        $this->assertEquals($liveResults->pluck("id"), $books->pluck("id"));
32
        $this->assertEquals($liveResults->pluck("id"), $cachedResults->pluck("id"));
33
    }
34
35
    public function testWhereNotInResults()
36
    {
37
        $key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:books:genealabslaravelmodelcachingtestsfixturesbook-id_notin_1_2");
38
        $tags = [
39
            "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesbook",
40
        ];
41
42
        $results = (new Book)
43
            ->whereNotIn('id', [1, 2])
44
            ->get();
45
        $cachedResults = $this->cache()
46
            ->tags($tags)
47
            ->get($key)['value'];
48
        $liveResults = (new UncachedBook)
49
            ->whereNotIn('id', [1, 2])
50
            ->get();
51
52
        $this->assertEquals($liveResults->pluck("id"), $results->pluck("id"));
53
        $this->assertEquals($liveResults->pluck("id"), $cachedResults->pluck("id"));
54
    }
55
56
    public function testWhereNotInSubquery()
57
    {
58
        $key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:books:genealabslaravelmodelcachingtestsfixturesbook-id_notin_select_id_from_authors_where_id_<_10");
59
        $tags = [
60
            "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesbook",
61
        ];
62
        $results = (new Book)
63
            ->whereNotIn("id", function ($query) {
64
                $query->select("id")->from("authors")->where("id", "<", 10);
65
            })
66
            ->get();
67
        $cachedResults = $this
68
            ->cache()
69
            ->tags($tags)
70
            ->get($key)['value'];
71
        $liveResults = (new UncachedBook)
72
            ->whereNotIn("id", function ($query) {
73
                $query->select("id")->from("authors")->where("id", "<", 10);
74
            })
75
            ->get();
76
77
        $this->assertEquals($liveResults->pluck("id"), $results->pluck("id"));
78
        $this->assertEquals($liveResults->pluck("id"), $cachedResults->pluck("id"));
79
    }
80
}
81