Skip to content

Commit a29d64e

Browse files
committed
Fix InRaw caching
Fixes #239
1 parent 089b48e commit a29d64e

File tree

9 files changed

+128
-5
lines changed

9 files changed

+128
-5
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ The following items currently do no work with this package:
3737
- caching of lazy-loaded relationships, see #127
3838
- using select() clauses in Eloquent queries, see #238 (work-around discussed in the issue)
3939
- using SoftDeletes on Models, see #237
40-
- using polymorphic relationships, see #239
4140
```
4241

4342
[![installation guide cover](https://user-images.githubusercontent.com/1791050/36356190-fc1982b2-14a2-11e8-85ed-06f8e3b57ae8.png)](https://vimeo.com/256318402)

src/CacheKey.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ protected function getInClauses(array $where) : string
218218

219219
protected function getInAndNotInClauses(array $where) : string
220220
{
221-
if (! in_array($where["type"], ["In", "NotIn"])) {
221+
if (! in_array($where["type"], ["In", "NotIn", "InRaw"])) {
222222
return "";
223223
}
224224

@@ -264,7 +264,7 @@ protected function recursiveImplode(array $items, string $glue = ",") : string
264264

265265
protected function getRawClauses(array $where) : string
266266
{
267-
if ($where["type"] !== "raw") {
267+
if (! in_array($where["type"], ["raw"])) {
268268
return "";
269269
}
270270

@@ -288,7 +288,7 @@ protected function getRawClauses(array $where) : string
288288

289289
protected function getOtherClauses(array $where) : string
290290
{
291-
if (in_array($where["type"], ["Exists", "Nested", "NotExists", "Column", "raw", "In", "NotIn"])) {
291+
if (in_array($where["type"], ["Exists", "Nested", "NotExists", "Column", "raw", "In", "NotIn", "InRaw"])) {
292292
return "";
293293
}
294294

tests/Fixtures/Book.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Database\Eloquent\Relations\BelongsTo;
66
use Illuminate\Database\Eloquent\Relations\MorphMany;
77
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
8+
use Illuminate\Database\Eloquent\Relations\MorphOne;
89

910
class Book extends Model
1011
{
@@ -33,6 +34,11 @@ public function comments() : MorphMany
3334
return $this->morphMany(Comment::class, "commentable");
3435
}
3536

37+
public function image() : MorphOne
38+
{
39+
return $this->morphOne(Image::class, "imagable");
40+
}
41+
3642
public function publisher() : BelongsTo
3743
{
3844
return $this->belongsTo(Publisher::class);

tests/Fixtures/Image.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Fixtures;
2+
3+
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
4+
use Illuminate\Database\Eloquent\Model;
5+
use Illuminate\Database\Eloquent\Relations\MorphTo;
6+
7+
class Image extends Model
8+
{
9+
use Cachable;
10+
11+
protected $fillable = [
12+
'path',
13+
];
14+
15+
public function imagable() : MorphTo
16+
{
17+
return $this->morphTo();
18+
}
19+
}

tests/Fixtures/UncachedBook.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22

33
use Illuminate\Database\Eloquent\Model;
44
use Illuminate\Database\Eloquent\Relations\BelongsTo;
5+
use Illuminate\Database\Eloquent\Relations\MorphMany;
56
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
7+
use Illuminate\Database\Eloquent\Relations\MorphOne;
68

79
class UncachedBook extends Model
810
{
11+
protected $casts = [
12+
'price' => 'float',
13+
];
914
protected $dates = [
1015
'published_at',
1116
];
@@ -21,6 +26,16 @@ public function author() : BelongsTo
2126
return $this->belongsTo(UncachedAuthor::class);
2227
}
2328

29+
public function comments() : MorphMany
30+
{
31+
return $this->morphMany(Comment::class, "commentable");
32+
}
33+
34+
public function image() : MorphOne
35+
{
36+
return $this->morphOne(Image::class, "imagable");
37+
}
38+
2439
public function publisher() : BelongsTo
2540
{
2641
return $this->belongsTo(UncachedPublisher::class);
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Integration\CachedBuilder;
2+
3+
use Faker\Generator as Faker;
4+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Book;
5+
use GeneaLabs\LaravelModelCaching\Tests\IntegrationTestCase;
6+
7+
class MorphOneTest extends IntegrationTestCase
8+
{
9+
public function setUp() : void
10+
{
11+
parent::setUp();
12+
13+
(new Book)
14+
->get()
15+
->each(function ($book) {
16+
$book->image()->create([
17+
"path" => app(Faker::class)->imageUrl(),
18+
]);
19+
});
20+
$this->cache()->flush();
21+
}
22+
23+
public function testMorphTo()
24+
{
25+
$key1 = sha1('genealabs:laravel-model-caching:testing::memory::books:genealabslaravelmodelcachingtestsfixturesbook-author_id_=_1-testing::memory::image');
26+
$key2 = sha1('genealabs:laravel-model-caching:testing::memory::books:genealabslaravelmodelcachingtestsfixturesbook-author_id_=_2-testing::memory::image');
27+
$tags = [
28+
"genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesbook",
29+
"genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesimage",
30+
];
31+
32+
$books1 = (new Book)
33+
->with("image")
34+
->where("author_id", 1)
35+
->get();
36+
$cachedResults1 = $this->cache()
37+
->tags($tags)
38+
->get($key1)['value'];
39+
$books2 = (new Book)
40+
->with("image")
41+
->where("author_id", 2)
42+
->get();
43+
$cachedResults2 = $this->cache()
44+
->tags($tags)
45+
->get($key2)['value'];
46+
47+
$this->assertEquals($cachedResults1->pluck("images.id"), $books1->pluck("images.id"));
48+
$this->assertEquals($cachedResults2->pluck("images.id"), $books2->pluck("images.id"));
49+
$this->assertNotEquals($cachedResults1->pluck("images.id"), $cachedResults2->pluck("images.id"));
50+
$this->assertNotEquals($books1->pluck("images.id"), $books2->pluck("images.id"));
51+
$this->assertNotNull($books1->first()->image);
52+
$this->assertNotNull($books2->first()->image);
53+
$this->assertNotNull($cachedResults1->first()->image);
54+
$this->assertNotNull($cachedResults2->first()->image);
55+
}
56+
}

tests/Integration/CachedBuilder/WhereInTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ public function testBindingsAreCorrectWithMultipleWhereInClauses()
7979
$this->assertEquals($liveResults->pluck("id"), $cachedResults->pluck("id"));
8080
}
8181

82-
/** @group test */
8382
public function testWhereInUsesCorrectBindings()
8483
{
8584
$key = sha1('genealabs:laravel-model-caching:testing::memory::authors:genealabslaravelmodelcachingtestsfixturesauthor-id_in_1_2_3_4_5-id_between_1_99999');
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
use Faker\Generator as Faker;
4+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Image;
5+
6+
$factory->define(Image::class, function (Faker $faker) {
7+
return [
8+
'path' => $faker->imageUrl(),
9+
];
10+
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
use Illuminate\Database\Schema\Blueprint;
4+
use Illuminate\Database\Migrations\Migration;
5+
6+
class CreateImages extends Migration
7+
{
8+
public function up()
9+
{
10+
Schema::create('images', function (Blueprint $table) {
11+
$table->increments('id');
12+
$table->timestamps();
13+
14+
$table->unsignedInteger("imagable_id")->nullable();
15+
$table->string("imagable_type")->nullable();
16+
$table->text("path");
17+
});
18+
}
19+
}

0 commit comments

Comments
 (0)