Skip to content

Commit 1f524a6

Browse files
author
Bill Harding
committed
Add unit test for testWithoutGlobalScope
1 parent 832a3da commit 1f524a6

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

src/CacheKey.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@ protected function getBindingsSlug() : string
6363
return '';
6464
}
6565

66-
if ($this->withoutAllScopes || ($this->withoutScopes != null && count($this->withoutScopes) == 0)) {
66+
if ($this->withoutAllScopes) {
67+
return Arr::query($this->model->query()->withoutGlobalScopes()->getBindings());
68+
}
69+
70+
if (count($this->withoutScopes) > 0) {
6771
return Arr::query($this->model->query()->withoutGlobalScopes($this->withoutScopes)->getBindings());
6872
}
6973

src/Traits/BuilderCaching.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,16 @@ public function truncate()
2424

2525
public function withoutGlobalScope($scope)
2626
{
27-
if ($this->withoutScopes == null) {
28-
$this->withoutScopes = [];
29-
}
30-
3127
array_push($this->withoutScopes, $scope);
3228

3329
return parent::withoutGlobalScope($scope);
3430
}
3531

3632
public function withoutGlobalScopes(array $scopes = null)
3733
{
38-
$this->withoutScopes = $scopes;
34+
if ($scopes != null) {
35+
$this->withoutScopes = $scopes;
36+
}
3937

4038
if ($scopes == null || ($scopes != null && count($scopes) == 0)) {
4139
$this->withoutAllScopes = true;

tests/Integration/CachedBuilder/ScopeTest.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,33 @@ public function testWithoutGlobalScopes()
171171
$user = factory(User::class)->create(["name" => "Barry Barry Barry"]);
172172
$this->actingAs($user);
173173
$authorsB = (new AuthorBeginsWithScoped)
174-
->withoutGlobalScopes()
174+
->withoutGlobalScopes(['GeneaLabs\LaravelModelCaching\Tests\Fixtures\Scopes\NameBeginsWith'])
175+
->get()
176+
->map(function ($author) {
177+
return (new Str)->substr($author->name, 0, 1);
178+
})
179+
->unique();
180+
181+
$this->assertGreaterThan(1, count($authorsA));
182+
$this->assertGreaterThan(1, count($authorsB));
183+
}
184+
185+
public function testWithoutGlobalScope()
186+
{
187+
factory(Author::class, 200)->create();
188+
$user = factory(User::class)->create(["name" => "Andrew Junior"]);
189+
$this->actingAs($user);
190+
$authorsA = (new AuthorBeginsWithScoped)
191+
->withoutGlobalScope('GeneaLabs\LaravelModelCaching\Tests\Fixtures\Scopes\NameBeginsWith')
192+
->get()
193+
->map(function ($author) {
194+
return (new Str)->substr($author->name, 0, 1);
195+
})
196+
->unique();
197+
$user = factory(User::class)->create(["name" => "Barry Barry Barry"]);
198+
$this->actingAs($user);
199+
$authorsB = (new AuthorBeginsWithScoped)
200+
->withoutGlobalScope('GeneaLabs\LaravelModelCaching\Tests\Fixtures\Scopes\NameBeginsWith')
175201
->get()
176202
->map(function ($author) {
177203
return (new Str)->substr($author->name, 0, 1);

0 commit comments

Comments
 (0)