|
3 | 3 | use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Author;
|
4 | 4 | use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedAuthor;
|
5 | 5 | use GeneaLabs\LaravelModelCaching\Tests\IntegrationTestCase;
|
6 |
| -use GeneaLabs\LaravelModelCaching\Tests\Fixtures\AuthorBeginsWithA; |
| 6 | +use GeneaLabs\LaravelModelCaching\Tests\Fixtures\AuthorBeginsWithScoped; |
7 | 7 | use GeneaLabs\LaravelModelCaching\Tests\Fixtures\AuthorWithInlineGlobalScope;
|
8 | 8 | use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedAuthorWithInlineGlobalScope;
|
| 9 | +use GeneaLabs\LaravelModelCaching\Tests\Fixtures\User; |
9 | 10 | use Illuminate\Database\Eloquent\Relations\HasMany;
|
| 11 | +use Illuminate\Support\Str; |
10 | 12 |
|
11 | 13 | class ScopeTest extends IntegrationTestCase
|
12 | 14 | {
|
@@ -58,13 +60,15 @@ public function testScopeClauseWithParameter()
|
58 | 60 |
|
59 | 61 | public function testGlobalScopesAreCached()
|
60 | 62 | {
|
| 63 | + $user = factory(User::class)->create(["name" => "Abernathy Kings"]); |
| 64 | + $this->actingAs($user); |
61 | 65 | $author = factory(UncachedAuthor::class, 1)
|
62 | 66 | ->create(['name' => 'Alois'])
|
63 | 67 | ->first();
|
64 |
| - $authors = (new AuthorBeginsWithA) |
| 68 | + $authors = (new AuthorBeginsWithScoped) |
65 | 69 | ->get();
|
66 |
| - $key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthorbeginswitha-name_like_A%"); |
67 |
| - $tags = ["genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthorbeginswitha"]; |
| 70 | + $key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthorbeginswithscoped-name_like_A%"); |
| 71 | + $tags = ["genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthorbeginswithscoped"]; |
68 | 72 |
|
69 | 73 | $cachedResults = $this->cache()
|
70 | 74 | ->tags($tags)
|
@@ -99,6 +103,32 @@ public function testInlineGlobalScopesAreCached()
|
99 | 103 | $this->assertTrue($liveResults->contains($author));
|
100 | 104 | }
|
101 | 105 |
|
| 106 | + public function testGlobalScopesWhenSwitchingContext() |
| 107 | + { |
| 108 | + factory(Author::class, 200)->create(); |
| 109 | + $user = factory(User::class)->create(["name" => "Anton Junior"]); |
| 110 | + $this->actingAs($user); |
| 111 | + $authorsA = (new AuthorBeginsWithScoped) |
| 112 | + ->get() |
| 113 | + ->map(function ($author) { |
| 114 | + return (new Str)->substr($author->name, 0, 1); |
| 115 | + }) |
| 116 | + ->unique(); |
| 117 | + $user = factory(User::class)->create(["name" => "Burli Burli Burli"]); |
| 118 | + $this->actingAs($user); |
| 119 | + $authorsB = (new AuthorBeginsWithScoped) |
| 120 | + ->get() |
| 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 | + |
102 | 132 | public function testLocalScopesInRelationship()
|
103 | 133 | {
|
104 | 134 | $first = "A";
|
|
0 commit comments