Skip to content

Commit a559ad9

Browse files
committed
Fixes #66
1 parent b49e122 commit a559ad9

File tree

8 files changed

+88
-51
lines changed

8 files changed

+88
-51
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"fzaninotto/faker": "~1.4",
1919
"mockery/mockery": "0.9.*",
2020
"orchestra/database": "3.6.x-dev@dev",
21-
"orchestra/testbench-browser-kit": "3.6.x-dev@dev",
21+
"orchestra/testbench-browser-kit": "^3.6",
2222
"orchestra/testbench-dusk": "3.6.x-dev@dev",
2323
"orchestra/testbench": "^3.6",
2424
"php-coveralls/php-coveralls" : "*",

phpunit.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
convertWarningsToExceptions="true"
1010
printerClass="Codedungeon\PHPUnitPrettyResultPrinter\Printer"
1111
processIsolation="false"
12-
stopOnFailure="false"
12+
stopOnFailure="true"
1313
syntaxCheck="false"
1414
>
1515
<testsuites>

src/CacheKey.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ protected function getQueryColumns(array $columns) : string
9090

9191
protected function getTypeClause($where) : string
9292
{
93-
$type =in_array($where['type'], ['In', 'Null', 'NotNull'])
93+
$type =in_array($where['type'], ['In', 'Null', 'NotNull', 'between'])
9494
? strtolower($where['type'])
9595
: strtolower($where['operator']);
9696

@@ -99,9 +99,19 @@ protected function getTypeClause($where) : string
9999

100100
protected function getValuesClause(array $where = null) : string
101101
{
102-
return is_array(array_get($where, 'values'))
103-
? '_' . implode('_', $where['values'])
102+
if (in_array($where['type'], ['NotNull'])) {
103+
return '';
104+
}
105+
106+
$values = is_array(array_get($where, 'values'))
107+
? implode('_', $where['values'])
104108
: '';
109+
110+
if (! $values && $this->query->bindings['where'] ?? false) {
111+
$values = implode('_', $this->query->bindings['where']);
112+
}
113+
114+
return '_' . $values;
105115
}
106116

107117
protected function getWhereClauses(array $wheres = []) : string
@@ -152,7 +162,6 @@ protected function getOtherClauses(array $where, string $carry = null) : string
152162
}
153163

154164
$value = $this->getTypeClause($where);
155-
$value .= "_" . array_get($where, 'value');
156165
$value .= $this->getValuesClause($where);
157166

158167
return "{$carry}-{$where['column']}_{$value}";

tests/Fixtures/Book.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@
66

77
class Book extends CachedModel
88
{
9+
protected $casts = [
10+
'price' => 'float',
11+
];
912
protected $dates = [
1013
'published_at',
1114
];
1215
protected $fillable = [
1316
'description',
1417
'published_at',
1518
'title',
19+
'price',
1620
];
1721

1822
public function author() : BelongsTo

tests/Unit/CachedBuilderTest.php

Lines changed: 64 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public function testHasManyRelationshipIsCached()
137137
->get(sha1("genealabslaravelmodelcachingtestsfixturesauthor-books")));
138138

139139
$this->assertNotNull($results);
140-
$this->assertEmpty($authors->diffAssoc($results));
140+
$this->assertEmpty($authors->diffKeys($results));
141141
$this->assertNotEmpty($authors);
142142
$this->assertNotEmpty($results);
143143
$this->assertEquals($authors->count(), $results->count());
@@ -154,7 +154,7 @@ public function testBelongsToRelationshipIsCached()
154154
->get(sha1("genealabslaravelmodelcachingtestsfixturesbook-author")));
155155

156156
$this->assertNotNull($results);
157-
$this->assertEmpty($books->diffAssoc($results));
157+
$this->assertEmpty($books->diffKeys($results));
158158
$this->assertNotEmpty($books);
159159
$this->assertNotEmpty($results);
160160
$this->assertEquals($books->count(), $results->count());
@@ -171,7 +171,7 @@ public function testBelongsToManyRelationshipIsCached()
171171
->get(sha1("genealabslaravelmodelcachingtestsfixturesbook-stores")));
172172

173173
$this->assertNotNull($results);
174-
$this->assertEmpty($books->diffAssoc($results));
174+
$this->assertEmpty($books->diffKeys($results));
175175
$this->assertNotEmpty($books);
176176
$this->assertNotEmpty($results);
177177
$this->assertEquals($books->count(), $results->count());
@@ -189,7 +189,7 @@ public function testHasOneRelationshipIsCached()
189189
->get(sha1("genealabslaravelmodelcachingtestsfixturesauthor-profile")));
190190

191191
$this->assertNotNull($results);
192-
$this->assertEmpty($authors->diffAssoc($results));
192+
$this->assertEmpty($authors->diffKeys($results));
193193
$this->assertNotEmpty($authors);
194194
$this->assertNotEmpty($results);
195195
$this->assertEquals($authors->count(), $results->count());
@@ -240,8 +240,7 @@ public function testChunkModelResultsCreatesCache()
240240

241241
$cachedChunks['authors']->push($chunk);
242242
$cachedChunks['keys']->push(sha1(
243-
"genealabslaravelmodelcachingtestsfixturesauthor-books-pr" .
244-
"ofile_orderBy_authors.id_asc{$offset}-limit_3"
243+
"genealabslaravelmodelcachingtestsfixturesauthor-books-profile_orderBy_authors.id_asc{$offset}-limit_3"
245244
));
246245
});
247246

@@ -255,8 +254,11 @@ public function testChunkModelResultsCreatesCache()
255254
$cachedResults = cache()->tags($tags)
256255
->get($key);
257256

258-
$this->assertEmpty($cachedChunks['authors'][$index]->diffAssoc($cachedResults));
259-
$this->assertEmpty($uncachedChunks[$index]->diffAssoc($cachedResults));
257+
// $this->assertTrue($cachedChunks['authors'][$index]->diffKeys($cachedResults)->isEmpty());
258+
// $this->assertTrue($uncachedChunks[$index]->diffKeys($cachedResults)->isEmpty());
259+
260+
$this->assertEmpty($cachedChunks['authors'][$index]->diffKeys($cachedResults));
261+
$this->assertEmpty($uncachedChunks[$index]->diffKeys($cachedResults));
260262
}
261263
}
262264

@@ -302,8 +304,8 @@ public function testCursorModelResultsCreatesCache()
302304
->cursor()
303305
);
304306

305-
$this->assertEmpty($authors->diffAssoc($cachedResults));
306-
$this->assertEmpty($liveResults->diffAssoc($cachedResults));
307+
$this->assertEmpty($authors->diffKeys($cachedResults));
308+
$this->assertEmpty($liveResults->diffKeys($cachedResults));
307309
}
308310

309311
public function testFindModelResultsCreatesCache()
@@ -318,8 +320,8 @@ public function testFindModelResultsCreatesCache()
318320
->get($key));
319321
$liveResults = collect()->push((new UncachedAuthor)->find(1));
320322

321-
$this->assertEmpty($author->diffAssoc($cachedResults));
322-
$this->assertEmpty($liveResults->diffAssoc($cachedResults));
323+
$this->assertEmpty($author->diffKeys($cachedResults));
324+
$this->assertEmpty($liveResults->diffKeys($cachedResults));
323325
}
324326

325327
public function testGetModelResultsCreatesCache()
@@ -339,7 +341,7 @@ public function testGetModelResultsCreatesCache()
339341
->get();
340342

341343
$this->assertEquals($authors, $cachedResults);
342-
$this->assertEmpty($liveResults->diffAssoc($cachedResults));
344+
$this->assertEmpty($liveResults->diffKeys($cachedResults));
343345
}
344346

345347
public function testMaxModelResultsCreatesCache()
@@ -398,8 +400,8 @@ public function testPluckModelResultsCreatesCache()
398400
$liveResults = (new UncachedAuthor)->with('books', 'profile')
399401
->pluck('name', 'id');
400402

401-
$this->assertEmpty($authors->diffAssoc($cachedResults));
402-
$this->assertEmpty($liveResults->diffAssoc($cachedResults));
403+
$this->assertEmpty($authors->diffKeys($cachedResults));
404+
$this->assertEmpty($liveResults->diffKeys($cachedResults));
403405
}
404406

405407
public function testSumModelResultsCreatesCache()
@@ -459,38 +461,38 @@ public function testNestedRelationshipEagerLoading()
459461
$liveResults = collect([(new UncachedAuthor)->with('books.publisher')
460462
->first()]);
461463

462-
$this->assertEmpty($authors->diffAssoc($cachedResults));
463-
$this->assertEmpty($liveResults->diffAssoc($cachedResults));
464+
$this->assertEmpty($authors->diffKeys($cachedResults));
465+
$this->assertEmpty($liveResults->diffKeys($cachedResults));
464466
}
465467

466468
public function testLazyLoadedRelationshipResolvesThroughCachedBuilder()
467469
{
468470
$books = (new Author)->first()->books;
469-
$key = sha1('genealabslaravelmodelcachingtestsfixturesbook-books.author_id_=_1-books.author_id_notnull_');
471+
$key = sha1('genealabslaravelmodelcachingtestsfixturesbook-books.author_id_=_1-books.author_id_notnull');
470472
$tags = [
471473
'genealabslaravelmodelcachingtestsfixturesbook',
472474
];
473475

474476
$cachedResults = cache()->tags($tags)->get($key);
475477
$liveResults = (new UncachedAuthor)->first()->books;
476478

477-
$this->assertEmpty($books->diffAssoc($cachedResults));
478-
$this->assertEmpty($liveResults->diffAssoc($cachedResults));
479+
$this->assertEmpty($books->diffKeys($cachedResults));
480+
$this->assertEmpty($liveResults->diffKeys($cachedResults));
479481
}
480482

481483
public function testLazyLoadingOnResourceIsCached()
482484
{
483485
$books = (new AuthorResource((new Author)->first()))->books;
484-
$key = sha1('genealabslaravelmodelcachingtestsfixturesbook-books.author_id_=_1-books.author_id_notnull_');
486+
$key = sha1('genealabslaravelmodelcachingtestsfixturesbook-books.author_id_=_1-books.author_id_notnull');
485487
$tags = [
486488
'genealabslaravelmodelcachingtestsfixturesbook',
487489
];
488490

489491
$cachedResults = cache()->tags($tags)->get($key);
490492
$liveResults = (new UncachedAuthor)->first()->books;
491493

492-
$this->assertEmpty($books->diffAssoc($cachedResults));
493-
$this->assertEmpty($liveResults->diffAssoc($cachedResults));
494+
$this->assertEmpty($books->diffKeys($cachedResults));
495+
$this->assertEmpty($liveResults->diffKeys($cachedResults));
494496
}
495497

496498
public function testOrderByClauseParsing()
@@ -505,8 +507,8 @@ public function testOrderByClauseParsing()
505507
$cachedResults = cache()->tags($tags)->get($key);
506508
$liveResults = (new UncachedAuthor)->orderBy('name')->get();
507509

508-
$this->assertEmpty($authors->diffAssoc($cachedResults));
509-
$this->assertEmpty($liveResults->diffAssoc($cachedResults));
510+
$this->assertEmpty($authors->diffKeys($cachedResults));
511+
$this->assertEmpty($liveResults->diffKeys($cachedResults));
510512
}
511513

512514
public function testNestedRelationshipWhereClauseParsing()
@@ -527,8 +529,8 @@ public function testNestedRelationshipWhereClauseParsing()
527529
$liveResults = (new UncachedAuthor)->with('books.publisher')
528530
->get();
529531

530-
$this->assertEmpty($authors->diffAssoc($cachedResults));
531-
$this->assertEmpty($liveResults->diffAssoc($cachedResults));
532+
$this->assertEmpty($authors->diffKeys($cachedResults));
533+
$this->assertEmpty($liveResults->diffKeys($cachedResults));
532534
}
533535

534536
public function testExistsRelationshipWhereClauseParsing()
@@ -543,8 +545,8 @@ public function testExistsRelationshipWhereClauseParsing()
543545
$liveResults = (new UncachedAuthor)->whereHas('books')
544546
->get();
545547

546-
$this->assertEmpty($authors->diffAssoc($cachedResults));
547-
$this->assertEmpty($liveResults->diffAssoc($cachedResults));
548+
$this->assertEmpty($authors->diffKeys($cachedResults));
549+
$this->assertEmpty($liveResults->diffKeys($cachedResults));
548550
}
549551

550552
public function testDoesntHaveWhereClauseParsing()
@@ -563,8 +565,8 @@ public function testDoesntHaveWhereClauseParsing()
563565
->doesntHave('books')
564566
->get();
565567

566-
$this->assertEmpty($authors->diffAssoc($cachedResults));
567-
$this->assertEmpty($liveResults->diffAssoc($cachedResults));
568+
$this->assertEmpty($authors->diffKeys($cachedResults));
569+
$this->assertEmpty($liveResults->diffKeys($cachedResults));
568570
}
569571

570572
public function testColumnsRelationshipWhereClauseParsing()
@@ -586,8 +588,8 @@ public function testColumnsRelationshipWhereClauseParsing()
586588
->where('name', '=', $author->name)
587589
->get();
588590

589-
$this->assertEmpty($authors->diffAssoc($cachedResults));
590-
$this->assertEmpty($liveResults->diffAssoc($cachedResults));
591+
$this->assertEmpty($authors->diffKeys($cachedResults));
592+
$this->assertEmpty($liveResults->diffKeys($cachedResults));
591593
}
592594

593595
public function testRawWhereClauseParsing()
@@ -604,8 +606,8 @@ public function testRawWhereClauseParsing()
604606
$liveResults = collect([(new UncachedAuthor)
605607
->whereRaw('name <> \'\'')->first()]);
606608

607-
$this->assertTrue($authors->diffAssoc($cachedResults)->isEmpty());
608-
$this->assertTrue($liveResults->diffAssoc($cachedResults)->isEmpty());
609+
$this->assertTrue($authors->diffKeys($cachedResults)->isEmpty());
610+
$this->assertTrue($liveResults->diffKeys($cachedResults)->isEmpty());
609611
}
610612

611613
public function testScopeClauseParsing()
@@ -635,7 +637,7 @@ public function testRelationshipQueriesAreCached()
635637
->first()
636638
->books()
637639
->get();
638-
$key = sha1('genealabslaravelmodelcachingtestsfixturesbook-books.author_id_=_1-books.author_id_notnull_');
640+
$key = sha1('genealabslaravelmodelcachingtestsfixturesbook-books.author_id_=_1-books.author_id_notnull');
639641
$tags = [
640642
'genealabslaravelmodelcachingtestsfixturesbook'
641643
];
@@ -646,8 +648,8 @@ public function testRelationshipQueriesAreCached()
646648
->books()
647649
->get();
648650

649-
$this->assertTrue($cachedResults->diffAssoc($books)->isEmpty());
650-
$this->assertTrue($liveResults->diffAssoc($books)->isEmpty());
651+
$this->assertTrue($cachedResults->diffKeys($books)->isEmpty());
652+
$this->assertTrue($liveResults->diffKeys($books)->isEmpty());
651653
}
652654

653655
public function testRawOrderByWithoutColumnReference()
@@ -667,8 +669,8 @@ public function testRawOrderByWithoutColumnReference()
667669
->orderByRaw('DATE()')
668670
->get();
669671

670-
$this->assertTrue($cachedResults->diffAssoc($authors)->isEmpty());
671-
$this->assertTrue($liveResults->diffAssoc($authors)->isEmpty());
672+
$this->assertTrue($cachedResults->diffKeys($authors)->isEmpty());
673+
$this->assertTrue($liveResults->diffKeys($authors)->isEmpty());
672674
}
673675

674676
public function testDelete()
@@ -717,8 +719,8 @@ private function processWhereClauseTestWithOperator(string $operator)
717719
->where('name', $operator, $author->name)
718720
->get();
719721

720-
$this->assertEmpty($authors->diffAssoc($cachedResults));
721-
$this->assertEmpty($liveResults->diffAssoc($cachedResults));
722+
$this->assertEmpty($authors->diffKeys($cachedResults));
723+
$this->assertEmpty($liveResults->diffKeys($cachedResults));
722724
}
723725

724726
public function testWhereClauseParsingOfOperators()
@@ -731,4 +733,24 @@ public function testWhereClauseParsingOfOperators()
731733
$this->processWhereClauseTestWithOperator('LIKE');
732734
$this->processWhereClauseTestWithOperator('NOT LIKE');
733735
}
736+
737+
public function testWhereBetweenResults()
738+
{
739+
$books = (new Book)
740+
->whereBetween('price', [5, 10])
741+
->get();
742+
$key = sha1('genealabslaravelmodelcachingtestsfixturesbook-price_between_5_10');
743+
$tags = [
744+
'genealabslaravelmodelcachingtestsfixturesbook',
745+
];
746+
747+
$cachedResults = cache()->tags($tags)
748+
->get($key);
749+
$liveResults = (new UncachedAuthor)
750+
->whereBetween('price', [5, 10])
751+
->get();
752+
753+
$this->assertTrue($cachedResults->diffKeys($books)->isEmpty());
754+
$this->assertTrue($liveResults->diffKeys($books)->isEmpty());
755+
}
734756
}

tests/Unit/DisabledCachedBuilderTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public function testCursorModelResultsIsNotCached()
165165
->cursor()
166166
);
167167

168-
$this->assertEmpty($liveResults->diffAssoc($authors));
168+
$this->assertEmpty($liveResults->diffKeys($authors));
169169
$this->assertNull($cachedResults);
170170
}
171171

@@ -210,7 +210,7 @@ public function testGetModelResultsIsNotCached()
210210
->with('books', 'profile')
211211
->get();
212212

213-
$this->assertEmpty($liveResults->diffAssoc($authors));
213+
$this->assertEmpty($liveResults->diffKeys($authors));
214214
$this->assertNull($cachedResults);
215215
}
216216

@@ -282,7 +282,7 @@ public function testPluckModelResultsIsNotCached()
282282
->with('books', 'profile')
283283
->pluck('name', 'id');
284284

285-
$this->assertEmpty($liveResults->diffAssoc($authors));
285+
$this->assertEmpty($liveResults->diffKeys($authors));
286286
$this->assertNull($cachedResults);
287287
}
288288

tests/database/factories/BookFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88
'title' => $faker->title,
99
'description' => $faker->paragraphs(3, true),
1010
'published_at' => $faker->dateTime,
11+
'price' => $faker->randomFloat(2),
1112
];
1213
});

tests/database/migrations/2017_09_21_010109_create_books.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public function up()
1616
$table->text('description');
1717
$table->dateTime('published_at');
1818
$table->string('title');
19+
$table->decimal('price')->default(0);
1920

2021
$table->foreign('author_id')
2122
->references('id')

0 commit comments

Comments
 (0)