Total Complexity | 9 |
Total Lines | 136 |
Duplicated Lines | 57.35 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
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; |
||
10 | class CacheTest extends TestCase |
||
11 | { |
||
12 | use RefreshDatabase; |
||
13 | |||
14 | public function setUp() |
||
15 | { |
||
16 | parent::setUp(); |
||
17 | |||
18 | cache()->flush(); |
||
19 | factory(Author::class, 10)->create() |
||
20 | ->each(function($author) { |
||
21 | factory(Book::class, random_int(2, 10))->make() |
||
22 | ->each(function ($book) use ($author) { |
||
23 | $book->author()->associate($author); |
||
24 | $book->save(); |
||
25 | }); |
||
26 | factory(Profile::class)->make([ |
||
27 | 'author_id' => $author->id, |
||
28 | ]); |
||
29 | }); |
||
30 | |||
31 | $bookIds = (new Book)->all()->pluck('id'); |
||
32 | factory(Store::class, 10)->create() |
||
33 | ->each(function ($store) use ($bookIds) { |
||
34 | $store->books()->sync(rand($bookIds->min(), $bookIds->max())); |
||
35 | }); |
||
36 | cache()->flush(); |
||
37 | } |
||
38 | |||
39 | public function testCacheIsEmptyBeforeLoadingModels() |
||
40 | { |
||
41 | $results = cache()->tags([ |
||
42 | 'genealabslaravelmodelcachingtestsfixturesauthor', |
||
43 | 'genealabslaravelmodelcachingtestsfixturesbook' |
||
44 | ]) |
||
45 | ->get('genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks'); |
||
46 | |||
47 | $this->assertNull($results); |
||
48 | } |
||
49 | |||
50 | public function testCacheIsNotEmptyAfterLoadingModels() |
||
51 | { |
||
52 | (new Author)->with('books')->get()->first(); |
||
53 | |||
54 | $results = cache()->tags([ |
||
55 | 'genealabslaravelmodelcachingtestsfixturesauthor', |
||
56 | 'genealabslaravelmodelcachingtestsfixturesbook' |
||
57 | ]) |
||
58 | ->get('genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks'); |
||
59 | |||
60 | $this->assertNotNull($results); |
||
61 | } |
||
62 | |||
63 | View Code Duplication | public function testCreatingModelClearsCache() |
|
1 ignored issue
–
show
|
|||
64 | { |
||
65 | $author = (new Author)->with('books')->get()->first(); |
||
66 | $author->name = "John Jinglheimer"; |
||
67 | $author->save(); |
||
68 | |||
69 | $results = cache()->tags([ |
||
70 | 'genealabslaravelmodelcachingtestsfixturesauthor', |
||
71 | 'genealabslaravelmodelcachingtestsfixturesbook' |
||
72 | ]) |
||
73 | ->get('genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks'); |
||
74 | |||
75 | $this->assertNull($results); |
||
76 | } |
||
77 | |||
78 | View Code Duplication | public function testDeletingModelClearsCache() |
|
90 | } |
||
91 | |||
92 | View Code Duplication | public function testHasManyRelationshipIsCached() |
|
1 ignored issue
–
show
|
|||
93 | { |
||
94 | $authors = (new Author)->with('books')->get(); |
||
95 | $authorIds = implode('_', $authors->pluck('id')->toArray()); |
||
96 | |||
97 | $results = cache()->tags([ |
||
98 | 'genealabslaravelmodelcachingtestsfixturesauthor', |
||
99 | 'genealabslaravelmodelcachingtestsfixturesbook' |
||
100 | ]) |
||
101 | ->get("genealabslaravelmodelcachingtestsfixturesauthor_{$authorIds}-genealabslaravelmodelcachingtestsfixturesbooks"); |
||
102 | |||
103 | $this->assertNotNull($results); |
||
104 | } |
||
105 | |||
106 | View Code Duplication | public function testBelongsToRelationshipIsCached() |
|
1 ignored issue
–
show
|
|||
107 | { |
||
108 | $books = (new Book)->with('author')->get()->first(); |
||
109 | $bookIds = implode('_', $books->pluck('id')->toArray()); |
||
110 | |||
111 | $results = cache()->tags([ |
||
112 | 'genealabslaravelmodelcachingtestsfixturesbook', |
||
113 | 'genealabslaravelmodelcachingtestsfixturesauthor' |
||
114 | ]) |
||
115 | ->get("genealabslaravelmodelcachingtestsfixturesbook_{$bookIds}-genealabslaravelmodelcachingtestsfixturesauthors"); |
||
116 | |||
117 | $this->assertNotNull($results); |
||
118 | } |
||
119 | |||
120 | View Code Duplication | public function testBelongsToManyRelationshipIsCached() |
|
1 ignored issue
–
show
|
|||
121 | { |
||
122 | $books = (new Book)->with('stores')->get(); |
||
123 | $bookIds = implode('_', $books->pluck('id')->toArray()); |
||
124 | |||
125 | $results = cache()->tags([ |
||
126 | 'genealabslaravelmodelcachingtestsfixturesbook', |
||
127 | 'genealabslaravelmodelcachingtestsfixturesstore' |
||
128 | ]) |
||
129 | ->get("genealabslaravelmodelcachingtestsfixturesbook_{$bookIds}-genealabslaravelmodelcachingtestsfixturesstores"); |
||
130 | |||
131 | $this->assertNotNull($results); |
||
132 | } |
||
133 | |||
134 | View Code Duplication | public function testHasOneRelationshipIsCached() |
|
146 | } |
||
147 | } |
||
148 |
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.