|
1
|
|
|
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Integration\CachedBuilder; |
|
2
|
|
|
|
|
3
|
|
|
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Author; |
|
4
|
|
|
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Book; |
|
5
|
|
|
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedAuthor; |
|
6
|
|
|
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedBook; |
|
7
|
|
|
use GeneaLabs\LaravelModelCaching\Tests\IntegrationTestCase; |
|
8
|
|
|
|
|
9
|
|
|
class WithTest extends IntegrationTestCase |
|
10
|
|
|
{ |
|
11
|
|
|
public function testWithLimitedQuery() |
|
12
|
|
|
{ |
|
13
|
|
|
$authors = (new Author) |
|
14
|
|
|
->where("id", 1) |
|
15
|
|
|
->with([ |
|
16
|
|
|
'books' => function ($query) { |
|
17
|
|
|
$query->where("id", "<", 100) |
|
18
|
|
|
->offset(5) |
|
19
|
|
|
->limit(1); |
|
20
|
|
|
} |
|
21
|
|
|
]) |
|
22
|
|
|
->first(); |
|
23
|
|
|
$uncachedAuthor = (new UncachedAuthor)->with([ |
|
24
|
|
|
'books' => function ($query) { |
|
25
|
|
|
$query->where("id", "<", 100) |
|
26
|
|
|
->offset(5) |
|
27
|
|
|
->limit(1); |
|
28
|
|
|
} |
|
29
|
|
|
]) |
|
30
|
|
|
->first(); |
|
31
|
|
|
|
|
32
|
|
|
$this->assertEquals($uncachedAuthor->books()->pluck("id"), $authors->books()->pluck("id")); |
|
33
|
|
|
$this->assertEquals($uncachedAuthor->id, $authors->id); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
View Code Duplication |
public function testWithQuery() |
|
37
|
|
|
{ |
|
38
|
|
|
$author = (new Author) |
|
39
|
|
|
->where("id", 1) |
|
40
|
|
|
->with([ |
|
41
|
|
|
'books' => function ($query) { |
|
42
|
|
|
$query->where("id", "<", 100); |
|
43
|
|
|
} |
|
44
|
|
|
]) |
|
45
|
|
|
->first(); |
|
46
|
|
|
$uncachedAuthor = (new UncachedAuthor)->with([ |
|
47
|
|
|
'books' => function ($query) { |
|
48
|
|
|
$query->where("id", "<", 100); |
|
49
|
|
|
}, |
|
50
|
|
|
]) |
|
51
|
|
|
->where("id", 1) |
|
52
|
|
|
->first(); |
|
53
|
|
|
|
|
54
|
|
|
$this->assertEquals($uncachedAuthor->books()->count(), $author->books()->count()); |
|
55
|
|
|
$this->assertEquals($uncachedAuthor->id, $author->id); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
View Code Duplication |
public function testMultiLevelWithQuery() |
|
59
|
|
|
{ |
|
60
|
|
|
$author = (new Author) |
|
61
|
|
|
->where("id", 1) |
|
62
|
|
|
->with([ |
|
63
|
|
|
'books.publisher' => function ($query) { |
|
64
|
|
|
$query->where("id", "<", 100); |
|
65
|
|
|
} |
|
66
|
|
|
]) |
|
67
|
|
|
->first(); |
|
68
|
|
|
$uncachedAuthor = (new UncachedAuthor)->with([ |
|
69
|
|
|
'books.publisher' => function ($query) { |
|
70
|
|
|
$query->where("id", "<", 100); |
|
71
|
|
|
}, |
|
72
|
|
|
]) |
|
73
|
|
|
->where("id", 1) |
|
74
|
|
|
->first(); |
|
75
|
|
|
|
|
76
|
|
|
$this->assertEquals($uncachedAuthor->books()->count(), $author->books()->count()); |
|
77
|
|
|
$this->assertEquals($uncachedAuthor->id, $author->id); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function testWithBelongsToManyRelationshipQuery() |
|
81
|
|
|
{ |
|
82
|
|
|
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:books:genealabslaravelmodelcachingtestsfixturesbook-books.id_=_3-testing:{$this->testingSqlitePath}testing.sqlite:stores-first"); |
|
83
|
|
|
$tags = [ |
|
84
|
|
|
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesbook", |
|
85
|
|
|
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesstore", |
|
86
|
|
|
]; |
|
87
|
|
|
|
|
88
|
|
|
$stores = (new Book) |
|
89
|
|
|
->with("stores") |
|
90
|
|
|
->find(3) |
|
91
|
|
|
->stores; |
|
92
|
|
|
$cachedResults = $this |
|
93
|
|
|
->cache() |
|
94
|
|
|
->tags($tags) |
|
95
|
|
|
->get($key)['value'] |
|
96
|
|
|
->stores; |
|
97
|
|
|
$liveResults = (new UncachedBook) |
|
98
|
|
|
->with("stores") |
|
99
|
|
|
->find(3) |
|
100
|
|
|
->stores; |
|
101
|
|
|
|
|
102
|
|
|
$this->assertEquals($liveResults->pluck("id"), $stores->pluck("id")); |
|
103
|
|
|
$this->assertEquals($liveResults->pluck("id"), $cachedResults->pluck("id")); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|