Skip to content

Commit 1e4360c

Browse files
author
Andrew Arscott
committed
~ fixes all method not recognising change to global scope params
1 parent fec2792 commit 1e4360c

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

src/CacheKey.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public function make(
4444
$key .= $this->getOrderByClauses();
4545
$key .= $this->getOffsetClause();
4646
$key .= $this->getLimitClause();
47+
$key .= $this->getBindingsSlug();
4748
$key .= $keyDifferentiator;
4849
$key .= $this->macroKey;
4950
// dump($key);
@@ -352,4 +353,13 @@ protected function getWithModels() : string
352353
return "{$carry}-{$relatedConnection}:{$relatedDatabase}:{$related}";
353354
});
354355
}
356+
357+
protected function getBindingsSlug() : string
358+
{
359+
if (! method_exists($this->model, 'query')) {
360+
return '';
361+
}
362+
363+
return Arr::query($this->model->query()->getBindings());
364+
}
355365
}

tests/Integration/CachedBuilder/ScopeTest.php

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function testGlobalScopesAreCached()
6767
->first();
6868
$authors = (new AuthorBeginsWithScoped)
6969
->get();
70-
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthorbeginswithscoped-name_like_A%");
70+
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthorbeginswithscoped-name_like_A%0=A%25");
7171
$tags = ["genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthorbeginswithscoped"];
7272

7373
$cachedResults = $this->cache()
@@ -89,7 +89,7 @@ public function testInlineGlobalScopesAreCached()
8989
->first();
9090
$authors = (new AuthorWithInlineGlobalScope)
9191
->get();
92-
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthorwithinlineglobalscope-authors.deleted_at_null-name_like_A%");
92+
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthorwithinlineglobalscope-authors.deleted_at_null-name_like_A%0=A%25");
9393
$tags = ["genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthorwithinlineglobalscope"];
9494

9595
$cachedResults = $this->cache()
@@ -103,7 +103,33 @@ public function testInlineGlobalScopesAreCached()
103103
$this->assertTrue($liveResults->contains($author));
104104
}
105105

106-
public function testGlobalScopesWhenSwitchingContext()
106+
public function testGlobalScopesWhenSwitchingContextUsingAllMethod()
107+
{
108+
factory(Author::class, 200)->create();
109+
$user = factory(User::class)->create(["name" => "Andrew Junior"]);
110+
$this->actingAs($user);
111+
$authorsA = (new AuthorBeginsWithScoped)
112+
->all()
113+
->map(function ($author) {
114+
return (new Str)->substr($author->name, 0, 1);
115+
})
116+
->unique();
117+
$user = factory(User::class)->create(["name" => "Barry Barry Barry"]);
118+
$this->actingAs($user);
119+
$authorsB = (new AuthorBeginsWithScoped)
120+
->all()
121+
->map(function ($author) {
122+
return (new Str)->substr($author->name, 0, 1);
123+
})
124+
->unique();
125+
126+
$this->assertCount(1, $authorsA);
127+
$this->assertCount(1, $authorsB);
128+
$this->assertEquals("A", $authorsA->first());
129+
$this->assertEquals("B", $authorsB->first());
130+
}
131+
132+
public function testGlobalScopesWhenSwitchingContextUsingGetMethod()
107133
{
108134
factory(Author::class, 200)->create();
109135
$user = factory(User::class)->create(["name" => "Anton Junior"]);

0 commit comments

Comments
 (0)