|
1
|
|
|
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Integration\CachedBuilder; |
|
2
|
|
|
|
|
3
|
|
|
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Book; |
|
4
|
|
|
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Publisher; |
|
5
|
|
|
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedBook; |
|
6
|
|
|
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedPublisher; |
|
7
|
|
|
use GeneaLabs\LaravelModelCaching\Tests\IntegrationTestCase; |
|
8
|
|
|
use Illuminate\Database\Eloquent\Collection; |
|
9
|
|
|
|
|
10
|
|
|
class SubQueryOrderByTest extends IntegrationTestCase |
|
|
|
|
|
|
11
|
|
|
{ |
|
12
|
|
|
public function testOrderByDesc() |
|
13
|
|
|
{ |
|
14
|
|
|
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:books:genealabslaravelmodelcachingtestsfixturesbook-publisher_id_in_11_12_13_14_15_orderBy_(select \"name\" from \"publishers\" where \"id\" = \"books\".\"publisher_id\" limit 1)_desc"); |
|
15
|
|
|
$tags = [ |
|
16
|
|
|
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesbook", |
|
17
|
|
|
]; |
|
18
|
|
|
|
|
19
|
|
|
/** @var Collection $publishers */ |
|
20
|
|
|
$publishers = factory(UncachedPublisher::class, 5)->create(); |
|
21
|
|
|
|
|
22
|
|
|
$publishers->each(function (UncachedPublisher $publisher) { |
|
23
|
|
|
factory(UncachedBook::class, 2)->create(['publisher_id' => $publisher->id]); |
|
24
|
|
|
}); |
|
25
|
|
|
|
|
26
|
|
|
$publisherIds = $publishers->pluck('id')->toArray(); |
|
27
|
|
|
|
|
28
|
|
|
$books = Book::whereIn('publisher_id', $publisherIds)->orderByDesc( |
|
29
|
|
|
Publisher::select('name') |
|
30
|
|
|
->whereColumn('id', 'books.publisher_id') |
|
31
|
|
|
->limit(1) |
|
32
|
|
|
) ->get()->pluck('id')->filter()->toArray(); |
|
33
|
|
|
|
|
34
|
|
|
$cachedResults = $this |
|
35
|
|
|
->cache() |
|
36
|
|
|
->tags($tags) |
|
37
|
|
|
->get($key)['value']; |
|
38
|
|
|
|
|
39
|
|
|
$liveResults = UncachedBook::whereIn('publisher_id', $publisherIds)->orderByDesc( |
|
40
|
|
|
UncachedPublisher::select('name') |
|
41
|
|
|
->whereColumn('id', 'books.publisher_id') |
|
42
|
|
|
->limit(1) |
|
43
|
|
|
)->get()->pluck('id')->filter()->toArray(); |
|
44
|
|
|
|
|
45
|
|
|
$this->assertCount(10, $books); |
|
46
|
|
|
$this->assertSame($liveResults, $books); |
|
47
|
|
|
$this->assertSame($liveResults, $cachedResults->pluck('id')->filter()->toArray()); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|