| Total Complexity | 8 |
| Total Lines | 280 |
| Duplicated Lines | 12.5 % |
| 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; |
||
| 14 | class CacheTest extends TestCase |
||
| 15 | { |
||
| 16 | use RefreshDatabase; |
||
| 17 | |||
| 18 | public function setUp() |
||
| 19 | { |
||
| 20 | parent::setUp(); |
||
| 21 | |||
| 22 | cache()->flush(); |
||
| 23 | factory(Author::class, 10)->create() |
||
| 24 | ->each(function($author) { |
||
| 25 | factory(Book::class, random_int(2, 10))->make() |
||
| 26 | ->each(function ($book) use ($author) { |
||
| 27 | $book->author()->associate($author); |
||
| 28 | $book->save(); |
||
| 29 | }); |
||
| 30 | factory(Profile::class)->make([ |
||
| 31 | 'author_id' => $author->id, |
||
| 32 | ]); |
||
| 33 | }); |
||
| 34 | |||
| 35 | $bookIds = (new Book)->all()->pluck('id'); |
||
| 36 | factory(Store::class, 10)->create() |
||
| 37 | ->each(function ($store) use ($bookIds) { |
||
| 38 | $store->books()->sync(rand($bookIds->min(), $bookIds->max())); |
||
| 39 | }); |
||
| 40 | cache()->flush(); |
||
| 41 | } |
||
| 42 | |||
| 43 | // public function testCacheIsEmptyBeforeLoadingModels() |
||
| 44 | // { |
||
| 45 | // $results = cache()->tags([ |
||
| 46 | // 'genealabslaravelmodelcachingtestsfixturesauthor', |
||
| 47 | // 'genealabslaravelmodelcachingtestsfixturesbook' |
||
| 48 | // ]) |
||
| 49 | // ->get('genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks'); |
||
| 50 | // |
||
| 51 | // $this->assertNull($results); |
||
| 52 | // } |
||
| 53 | // |
||
| 54 | // public function testCacheIsNotEmptyAfterLoadingModels() |
||
| 55 | // { |
||
| 56 | // (new Author)->with('books')->get(); |
||
| 57 | // |
||
| 58 | // $results = cache()->tags([ |
||
| 59 | // 'genealabslaravelmodelcachingtestsfixturesauthor', |
||
| 60 | // 'genealabslaravelmodelcachingtestsfixturesbook' |
||
| 61 | // ]) |
||
| 62 | // ->get('genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks'); |
||
| 63 | // |
||
| 64 | // $this->assertNotNull($results); |
||
| 65 | // } |
||
| 66 | // |
||
| 67 | // public function testCreatingModelClearsCache() |
||
| 68 | // { |
||
| 69 | // (new Author)->with('books')->get(); |
||
| 70 | // |
||
| 71 | // factory(Author::class)->create(); |
||
| 72 | // |
||
| 73 | // $results = cache()->tags([ |
||
| 74 | // 'genealabslaravelmodelcachingtestsfixturesauthor', |
||
| 75 | // 'genealabslaravelmodelcachingtestsfixturesbook' |
||
| 76 | // ]) |
||
| 77 | // ->get('genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks'); |
||
| 78 | // |
||
| 79 | // $this->assertNull($results); |
||
| 80 | // } |
||
| 81 | // |
||
| 82 | // public function testUpdatingModelClearsCache() |
||
| 83 | // { |
||
| 84 | // $author = (new Author)->with('books')->get()->first(); |
||
| 85 | // $author->name = "John Jinglheimer"; |
||
| 86 | // $author->save(); |
||
| 87 | // |
||
| 88 | // $results = cache()->tags([ |
||
| 89 | // 'genealabslaravelmodelcachingtestsfixturesauthor', |
||
| 90 | // 'genealabslaravelmodelcachingtestsfixturesbook' |
||
| 91 | // ]) |
||
| 92 | // ->get('genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks'); |
||
| 93 | // |
||
| 94 | // $this->assertNull($results); |
||
| 95 | // } |
||
| 96 | // |
||
| 97 | // public function testDeletingModelClearsCache() |
||
| 98 | // { |
||
| 99 | // $author = (new Author)->with('books')->get()->first(); |
||
| 100 | // $author->delete(); |
||
| 101 | // |
||
| 102 | // $results = cache()->tags([ |
||
| 103 | // 'genealabslaravelmodelcachingtestsfixturesauthor', |
||
| 104 | // 'genealabslaravelmodelcachingtestsfixturesbook' |
||
| 105 | // ]) |
||
| 106 | // ->get('genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks'); |
||
| 107 | // |
||
| 108 | // $this->assertNull($results); |
||
| 109 | // } |
||
| 110 | // |
||
| 111 | // public function testHasManyRelationshipIsCached() |
||
| 112 | // { |
||
| 113 | // $authors = (new Author)->with('books')->get(); |
||
| 114 | // $authorIds = implode('_', $authors->pluck('id')->toArray()); |
||
| 115 | // |
||
| 116 | // $results = collect(cache()->tags([ |
||
| 117 | // 'genealabslaravelmodelcachingtestsfixturesauthor', |
||
| 118 | // 'genealabslaravelmodelcachingtestsfixturesbook' |
||
| 119 | // ]) |
||
| 120 | // ->get("genealabslaravelmodelcachingtestsfixturesauthor_{$authorIds}-genealabslaravelmodelcachingtestsfixturesbooks")); |
||
| 121 | // |
||
| 122 | // $this->assertNotNull($results); |
||
| 123 | // $this->assertEmpty($authors->diffAssoc($results)); |
||
| 124 | // $this->assertNotEmpty($authors); |
||
| 125 | // $this->assertNotEmpty($results); |
||
| 126 | // $this->assertEquals($authors->count(), $results->count()); |
||
| 127 | // } |
||
| 128 | // |
||
| 129 | // public function testBelongsToRelationshipIsCached() |
||
| 130 | // { |
||
| 131 | // $books = (new Book)->with('author')->get(); |
||
| 132 | // $bookIds = implode('_', $books->pluck('id')->toArray()); |
||
| 133 | // |
||
| 134 | // $results = collect(cache()->tags([ |
||
| 135 | // 'genealabslaravelmodelcachingtestsfixturesbook', |
||
| 136 | // 'genealabslaravelmodelcachingtestsfixturesauthor' |
||
| 137 | // ]) |
||
| 138 | // ->get("genealabslaravelmodelcachingtestsfixturesbook_{$bookIds}-genealabslaravelmodelcachingtestsfixturesauthors")); |
||
| 139 | // |
||
| 140 | // $this->assertNotNull($results); |
||
| 141 | // $this->assertEmpty($books->diffAssoc($results)); |
||
| 142 | // $this->assertNotEmpty($books); |
||
| 143 | // $this->assertNotEmpty($results); |
||
| 144 | // $this->assertEquals($books->count(), $results->count()); |
||
| 145 | // } |
||
| 146 | // |
||
| 147 | // public function testBelongsToManyRelationshipIsCached() |
||
| 148 | // { |
||
| 149 | // $books = (new Book)->with('stores')->get(); |
||
| 150 | // $bookIds = implode('_', $books->pluck('id')->toArray()); |
||
| 151 | // |
||
| 152 | // $results = collect(cache()->tags([ |
||
| 153 | // 'genealabslaravelmodelcachingtestsfixturesbook', |
||
| 154 | // 'genealabslaravelmodelcachingtestsfixturesstore' |
||
| 155 | // ]) |
||
| 156 | // ->get("genealabslaravelmodelcachingtestsfixturesbook_{$bookIds}-genealabslaravelmodelcachingtestsfixturesstores")); |
||
| 157 | // |
||
| 158 | // $this->assertNotNull($results); |
||
| 159 | // $this->assertEmpty($books->diffAssoc($results)); |
||
| 160 | // $this->assertNotEmpty($books); |
||
| 161 | // $this->assertNotEmpty($results); |
||
| 162 | // $this->assertEquals($books->count(), $results->count()); |
||
| 163 | // } |
||
| 164 | // |
||
| 165 | // public function testHasOneRelationshipIsCached() |
||
| 166 | // { |
||
| 167 | // $authors = (new Author)->with('profile')->get(); |
||
| 168 | // $authorIds = implode('_', $authors->pluck('id')->toArray()); |
||
| 169 | // |
||
| 170 | // $results = collect(cache() |
||
| 171 | // ->tags([ |
||
| 172 | // 'genealabslaravelmodelcachingtestsfixturesauthor', |
||
| 173 | // 'genealabslaravelmodelcachingtestsfixturesprofile' |
||
| 174 | // ]) |
||
| 175 | // ->get("genealabslaravelmodelcachingtestsfixturesauthor_{$authorIds}-genealabslaravelmodelcachingtestsfixturesprofiles")); |
||
| 176 | // |
||
| 177 | // $this->assertNotNull($results); |
||
| 178 | // $this->assertEmpty($authors->diffAssoc($results)); |
||
| 179 | // $this->assertNotEmpty($authors); |
||
| 180 | // $this->assertNotEmpty($results); |
||
| 181 | // $this->assertEquals($authors->count(), $results->count()); |
||
| 182 | // } |
||
| 183 | |||
| 184 | public function testAllModelResultsCreatesCache() |
||
| 185 | { |
||
| 186 | $authors = (new Author)->all(); |
||
| 187 | $key = 'genealabslaravelmodelcachingtestsfixturesauthor'; |
||
| 188 | $tags = [ |
||
| 189 | 'genealabslaravelmodelcachingtestsfixturesauthor', |
||
| 190 | ]; |
||
| 191 | |||
| 192 | $cachedResults = cache()->tags($tags) |
||
| 193 | ->get($key); |
||
| 194 | $liveResults = (new UncachedAuthor)->all(); |
||
| 195 | |||
| 196 | $this->assertEquals($authors, $cachedResults); |
||
| 197 | $this->assertEmpty($liveResults->diffAssoc($cachedResults)); |
||
| 198 | } |
||
| 199 | |||
| 200 | public function testChunkModelResultsCreatesCache() |
||
| 201 | { |
||
| 202 | $cachedChunks = collect([ |
||
| 203 | 'authors' => collect(), |
||
| 204 | 'keys' => collect(), |
||
| 205 | ]); |
||
| 206 | $chunkSize = 3; |
||
| 207 | $tags = [ |
||
| 208 | 'genealabslaravelmodelcachingtestsfixturesauthor', |
||
| 209 | 'genealabslaravelmodelcachingtestsfixturesbook', |
||
| 210 | 'genealabslaravelmodelcachingtestsfixturesprofile', |
||
| 211 | ]; |
||
| 212 | $uncachedChunks = collect(); |
||
| 213 | |||
| 214 | (new Author)->with('books', 'profile') |
||
| 215 | ->chunk($chunkSize, function ($chunk) use (&$cachedChunks, $chunkSize) { |
||
| 216 | $offset = ''; |
||
| 217 | |||
| 218 | if ($cachedChunks['authors']->count()) { |
||
| 219 | $offsetIncrement = $cachedChunks['authors']->count() * $chunkSize; |
||
| 220 | $offset = "-offset_{$offsetIncrement}"; |
||
| 221 | } |
||
| 222 | |||
| 223 | $cachedChunks['authors']->push($chunk); |
||
| 224 | $cachedChunks['keys']->push("genealabslaravelmodelcachingtestsfixturesauthor-books-profile{$offset}-limit_3"); |
||
| 225 | }); |
||
| 226 | |||
| 227 | (new UncachedAuthor)->with('books', 'profile') |
||
| 228 | ->chunk($chunkSize, function ($chunk) use (&$uncachedChunks) { |
||
| 229 | $uncachedChunks->push($chunk); |
||
| 230 | }); |
||
| 231 | |||
| 232 | for ($index = 0; $index < $cachedChunks['authors']->count(); $index++) { |
||
| 233 | $key = $cachedChunks['keys'][$index]; |
||
| 234 | $cachedResults = cache()->tags($tags) |
||
| 235 | ->get($key); |
||
| 236 | |||
| 237 | $this->assertEmpty($cachedChunks['authors'][$index]->diffAssoc($cachedResults)); |
||
| 238 | $this->assertEmpty($uncachedChunks[$index]->diffAssoc($cachedResults)); |
||
| 239 | } |
||
| 240 | } |
||
| 241 | |||
| 242 | View Code Duplication | public function testCountModelResultsCreatesCache() |
|
|
1 ignored issue
–
show
|
|||
| 243 | { |
||
| 244 | $authors = (new Author)->with('books', 'profile') |
||
| 245 | ->count(); |
||
| 246 | $key = 'genealabslaravelmodelcachingtestsfixturesauthor-count'; |
||
| 247 | $tags = [ |
||
| 248 | 'genealabslaravelmodelcachingtestsfixturesauthor', |
||
| 249 | ]; |
||
| 250 | |||
| 251 | $cachedResults = cache()->tags($tags) |
||
| 252 | ->get($key); |
||
| 253 | $liveResults = (new UncachedAuthor)->with('books', 'profile') |
||
| 254 | ->count(); |
||
| 255 | |||
| 256 | $this->assertEquals($authors, $cachedResults); |
||
| 257 | $this->assertEquals($liveResults, $cachedResults); |
||
| 258 | } |
||
| 259 | |||
| 260 | public function testFindModelResultsCreatesCache() |
||
| 274 | } |
||
| 275 | |||
| 276 | View Code Duplication | public function testGetModelResultsCreatesCache() |
|
| 294 | } |
||
| 295 | |||
| 296 | // test cursor() |
||
| 308 |
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.