Skip to content

Commit b023112

Browse files
author
Daniel Mason
authored
Merge pull request #1 from GeneaLabs/master
master
2 parents 6be9445 + 82be9a1 commit b023112

File tree

5 files changed

+137
-2
lines changed

5 files changed

+137
-2
lines changed

tests/Integration/CachedBuilder/LimitTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public function testGetModelResultsCreatesCache()
1111
$authors = (new Author)
1212
->limit(5)
1313
->get();
14-
$key = sha1("genealabs:laravel-model-caching:testing:/Users/mike/Developer/Sites/laravel-model-caching/tests/database/testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-authors.deleted_at_null-limit_5");
14+
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-authors.deleted_at_null-limit_5");
1515
$tags = [
1616
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor",
1717
];
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Integration\CachedBuilder;
2+
3+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Comment;
4+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Post;
5+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedComment;
6+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedPost;
7+
use GeneaLabs\LaravelModelCaching\Tests\IntegrationTestCase;
8+
use Illuminate\Database\Eloquent\Builder;
9+
10+
class WhereHasMorphTest extends IntegrationTestCase
11+
{
12+
public function testWithSingleMorphModel()
13+
{
14+
$comments = (new Comment)
15+
->whereHasMorph('commentable', Post::class)
16+
->get();
17+
$uncachedComments = (new UncachedComment())
18+
->whereHasMorph('commentable', Post::class)
19+
->get();
20+
21+
$cacheResults = $this->cache()->tags([
22+
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturescomment",
23+
])
24+
->get(sha1(
25+
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:comments:genealabslaravelmodelcachingtestsfixturescomment-nested-nested-comments.commentable_type_=_GeneaLabs\LaravelModelCaching\Tests\Fixtures\Post-exists-and_comments.commentable_id_=_posts.id"
26+
))['value'];
27+
28+
$this->assertCount(5, $comments);
29+
$this->assertEquals($comments->pluck("id"), $uncachedComments->pluck("id"));
30+
$this->assertEquals($uncachedComments->pluck("id"), $cacheResults->pluck("id"));
31+
}
32+
33+
public function testWithMultipleMorphModels()
34+
{
35+
$comments = (new Comment)
36+
->whereHasMorph('commentable', [Post::class, UncachedPost::class])
37+
->get();
38+
$uncachedComments = (new UncachedComment())
39+
->whereHasMorph('commentable', [Post::class, UncachedPost::class])
40+
->get();
41+
42+
$cacheResults = $this->cache()->tags([
43+
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturescomment",
44+
])
45+
->get(sha1(
46+
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:comments:genealabslaravelmodelcachingtestsfixturescomment-nested-nested-comments.commentable_type_=_GeneaLabs\LaravelModelCaching\Tests\Fixtures\Post-exists-and_comments.commentable_id_=_posts.id-nested-comments.commentable_type_=_GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedPost-exists-and_comments.commentable_id_=_posts.id"
47+
))['value'];
48+
49+
$this->assertCount(10, $comments);
50+
$this->assertEquals($comments->pluck("id"), $uncachedComments->pluck("id"));
51+
$this->assertEquals($uncachedComments->pluck("id"), $cacheResults->pluck("id"));
52+
}
53+
54+
public function testWithMultipleMorphModelsWithClosure()
55+
{
56+
$comments = (new Comment)
57+
->whereHasMorph('commentable', [Post::class, UncachedPost::class], function (Builder $query) {
58+
return $query->where('subject', 'like', '%uncached post');
59+
})
60+
->get();
61+
$uncachedComments = (new UncachedComment())
62+
->whereHasMorph('commentable', [Post::class, UncachedPost::class], function (Builder $query) {
63+
return $query->where('subject', 'like', '%uncached post');
64+
})
65+
->get();
66+
67+
$cacheResults = $this->cache()->tags([
68+
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturescomment",
69+
])
70+
->get(sha1(
71+
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:comments:genealabslaravelmodelcachingtestsfixturescomment-nested-nested-comments.commentable_type_=_GeneaLabs\LaravelModelCaching\Tests\Fixtures\Post-exists-and_comments.commentable_id_=_posts.id-subject_like_%uncached post-nested-comments.commentable_type_=_GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedPost-exists-and_comments.commentable_id_=_posts.id-subject_like_%uncached post"
72+
))['value'];
73+
74+
$this->assertCount(5, $comments);
75+
$this->assertEquals($comments->pluck("id"), $uncachedComments->pluck("id"));
76+
$this->assertEquals($uncachedComments->pluck("id"), $cacheResults->pluck("id"));
77+
}
78+
}

tests/Integration/CachedBuilder/WhereJsonContainsTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public function setUp() : void
2525
parent::setUp();
2626

2727
$this->loadMigrationsFrom(__DIR__ . '/../../database/migrations');
28+
factory(Author::class, 10)->create();
2829
}
2930

3031
public function testWithInUsingCollectionQuery()
@@ -37,6 +38,7 @@ public function testWithInUsingCollectionQuery()
3738
$authors = (new Author)
3839
->whereJsonContains("finances->total", 5000)
3940
->get();
41+
4042
$liveResults = (new UncachedAuthor)
4143
->whereJsonContains("finances->total", 5000)
4244
->get();
@@ -46,6 +48,8 @@ public function testWithInUsingCollectionQuery()
4648
->tags($tags)
4749
->get($key)['value'];
4850

51+
$this->assertCount(10, $cachedResults);
52+
$this->assertCount(10, $liveResults);
4953
$this->assertEquals($liveResults->pluck("id"), $authors->pluck("id"));
5054
$this->assertEquals($liveResults->pluck("id"), $cachedResults->pluck("id"));
5155
}
@@ -69,6 +73,8 @@ public function testWithInUsingCollectionQueryWithArrayValues()
6973
->tags($tags)
7074
->get($key)['value'];
7175

76+
$this->assertCount(10, $liveResults);
77+
$this->assertCount(10, $cachedResults);
7278
$this->assertEquals($liveResults->pluck("id"), $authors->pluck("id"));
7379
$this->assertEquals($liveResults->pluck("id"), $cachedResults->pluck("id"));
7480
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Integration\CachedBuilder;
2+
3+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Author;
4+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedAuthor;
5+
use GeneaLabs\LaravelModelCaching\Tests\IntegrationTestCase;
6+
7+
class WhereJsonLengthTest extends IntegrationTestCase
8+
{
9+
public function testWithInUsingCollectionQueryNoOperator()
10+
{
11+
$length = 2;
12+
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-finances->tags_=_$length-authors.deleted_at_null");
13+
$tags = [
14+
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor",
15+
];
16+
17+
$authors = (new Author)->whereJsonLength("finances->tags", $length)->get();
18+
$liveResults = (new UncachedAuthor)->whereJsonLength("finances->tags", $length)->get();
19+
20+
$cachedResults = $this
21+
->cache()
22+
->tags($tags)
23+
->get($key)['value'];
24+
25+
$this->assertCount(10, $liveResults);
26+
$this->assertCount(10, $cachedResults);
27+
$this->assertEquals($liveResults->pluck("id"), $authors->pluck("id"));
28+
$this->assertEquals($liveResults->pluck("id"), $cachedResults->pluck("id"));
29+
}
30+
31+
public function testWithInUsingCollectionQueryWithOperator()
32+
{
33+
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-finances->tags_>_1-authors.deleted_at_null");
34+
$tags = [
35+
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor",
36+
];
37+
38+
$authors = (new Author)->whereJsonLength("finances->tags", '>', 1)->get();
39+
$liveResults = (new UncachedAuthor)->whereJsonLength("finances->tags", '>', 1)->get();
40+
41+
$cachedResults = $this
42+
->cache()
43+
->tags($tags)
44+
->get($key)['value'];
45+
46+
$this->assertCount(10, $liveResults);
47+
$this->assertCount(10, $cachedResults);
48+
$this->assertEquals($liveResults->pluck("id"), $authors->pluck("id"));
49+
$this->assertEquals($liveResults->pluck("id"), $cachedResults->pluck("id"));
50+
}
51+
}

tests/database/seeds/DatabaseSeeder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function run()
4747
"commentable_id" => $comment->commentable_id,
4848
"commentable_type" => UncachedPost::class,
4949
"description" => $comment->description,
50-
"subject" => $comment->subject,
50+
"subject" => $comment->subject . ' - uncached post',
5151
]);
5252
});
5353
$publishers = factory(Publisher::class, 10)->create();

0 commit comments

Comments
 (0)