Total Complexity | 10 |
Total Lines | 168 |
Duplicated Lines | 72.02 % |
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() |
||
37 | } |
||
38 | |||
39 | public function testCacheIsEmptyBeforeLoadingModels() |
||
48 | } |
||
49 | |||
50 | View Code Duplication | public function testCacheIsNotEmptyAfterLoadingModels() |
|
1 ignored issue
–
show
|
|||
51 | { |
||
52 | (new Author)->with('books')->get(); |
||
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() |
|
64 | { |
||
65 | $author = (new Author)->with('books')->get(); |
||
66 | |||
67 | factory(Author::class)->create(); |
||
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 testUpdatingModelClearsCache() |
|
79 | { |
||
80 | $author = (new Author)->with('books')->get()->first(); |
||
81 | $author->name = "John Jinglheimer"; |
||
82 | $author->save(); |
||
83 | |||
84 | $results = cache()->tags([ |
||
85 | 'genealabslaravelmodelcachingtestsfixturesauthor', |
||
86 | 'genealabslaravelmodelcachingtestsfixturesbook' |
||
87 | ]) |
||
88 | ->get('genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks'); |
||
89 | |||
90 | $this->assertNull($results); |
||
91 | } |
||
92 | |||
93 | View Code Duplication | public function testDeletingModelClearsCache() |
|
105 | } |
||
106 | |||
107 | View Code Duplication | public function testHasManyRelationshipIsCached() |
|
108 | { |
||
109 | $authors = (new Author)->with('books')->get(); |
||
110 | $authorIds = implode('_', $authors->pluck('id')->toArray()); |
||
111 | |||
112 | $results = collect(cache()->tags([ |
||
113 | 'genealabslaravelmodelcachingtestsfixturesauthor', |
||
114 | 'genealabslaravelmodelcachingtestsfixturesbook' |
||
115 | ]) |
||
116 | ->get("genealabslaravelmodelcachingtestsfixturesauthor_{$authorIds}-genealabslaravelmodelcachingtestsfixturesbooks")); |
||
117 | |||
118 | $this->assertNotNull($results); |
||
119 | $this->assertEmpty($authors->diffAssoc($results)); |
||
120 | $this->assertNotEmpty($authors); |
||
121 | $this->assertNotEmpty($results); |
||
122 | $this->assertEquals($authors->count(), $results->count()); |
||
123 | } |
||
124 | |||
125 | View Code Duplication | public function testBelongsToRelationshipIsCached() |
|
126 | { |
||
127 | $books = (new Book)->with('author')->get(); |
||
128 | $bookIds = implode('_', $books->pluck('id')->toArray()); |
||
129 | |||
130 | $results = collect(cache()->tags([ |
||
131 | 'genealabslaravelmodelcachingtestsfixturesbook', |
||
132 | 'genealabslaravelmodelcachingtestsfixturesauthor' |
||
133 | ]) |
||
134 | ->get("genealabslaravelmodelcachingtestsfixturesbook_{$bookIds}-genealabslaravelmodelcachingtestsfixturesauthors")); |
||
135 | |||
136 | $this->assertNotNull($results); |
||
137 | $this->assertEmpty($books->diffAssoc($results)); |
||
138 | $this->assertNotEmpty($books); |
||
139 | $this->assertNotEmpty($results); |
||
140 | $this->assertEquals($books->count(), $results->count()); |
||
141 | } |
||
142 | |||
143 | View Code Duplication | public function testBelongsToManyRelationshipIsCached() |
|
144 | { |
||
145 | $books = (new Book)->with('stores')->get(); |
||
146 | $bookIds = implode('_', $books->pluck('id')->toArray()); |
||
147 | |||
148 | $results = collect(cache()->tags([ |
||
149 | 'genealabslaravelmodelcachingtestsfixturesbook', |
||
150 | 'genealabslaravelmodelcachingtestsfixturesstore' |
||
151 | ]) |
||
152 | ->get("genealabslaravelmodelcachingtestsfixturesbook_{$bookIds}-genealabslaravelmodelcachingtestsfixturesstores")); |
||
153 | |||
154 | $this->assertNotNull($results); |
||
155 | $this->assertEmpty($books->diffAssoc($results)); |
||
156 | $this->assertNotEmpty($books); |
||
157 | $this->assertNotEmpty($results); |
||
158 | $this->assertEquals($books->count(), $results->count()); |
||
159 | } |
||
160 | |||
161 | View Code Duplication | public function testHasOneRelationshipIsCached() |
|
178 | } |
||
179 | } |
||
180 |
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.