|
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 FindTest extends IntegrationTestCase |
|
|
|
|
|
|
8
|
|
|
{ |
|
9
|
|
|
public function testFindModelResultsCreatesCache() |
|
10
|
|
|
{ |
|
11
|
|
|
$author = collect()->push((new Author)->find(1)); |
|
12
|
|
|
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor_1"); |
|
13
|
|
|
$tags = [ |
|
14
|
|
|
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor", |
|
15
|
|
|
]; |
|
16
|
|
|
|
|
17
|
|
|
$cachedResults = collect()->push($this->cache()->tags($tags) |
|
18
|
|
|
->get($key)); |
|
19
|
|
|
$liveResults = collect()->push((new UncachedAuthor)->find(1)); |
|
20
|
|
|
|
|
21
|
|
|
$this->assertEmpty($author->diffKeys($cachedResults)); |
|
22
|
|
|
$this->assertEmpty($liveResults->diffKeys($cachedResults)); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function testFindMultipleModelResultsCreatesCache() |
|
26
|
|
|
{ |
|
27
|
|
|
$authors = (new Author) |
|
28
|
|
|
->find([1, 2, 3]); |
|
29
|
|
|
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-authors.deleted_at_null-find_list_1_2_3"); |
|
30
|
|
|
$tags = [ |
|
31
|
|
|
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor", |
|
32
|
|
|
]; |
|
33
|
|
|
|
|
34
|
|
|
$cachedResults = $this |
|
35
|
|
|
->cache() |
|
36
|
|
|
->tags($tags) |
|
37
|
|
|
->get($key)["value"]; |
|
38
|
|
|
$liveResults = (new UncachedAuthor)->find([1, 2, 3]); |
|
39
|
|
|
|
|
40
|
|
|
$this->assertEquals($authors->pluck("id"), $cachedResults->pluck("id")); |
|
41
|
|
|
$this->assertEquals($liveResults->pluck("id"), $cachedResults->pluck("id")); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function testSubsequentFindsReturnDifferentModels() |
|
45
|
|
|
{ |
|
46
|
|
|
$author1 = (new Author)->find(1); |
|
47
|
|
|
$author2 = (new Author)->find(2); |
|
48
|
|
|
|
|
49
|
|
|
$this->assertNotEquals($author1, $author2); |
|
50
|
|
|
$this->assertEquals($author1->id, 1); |
|
51
|
|
|
$this->assertEquals($author2->id, 2); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
View Code Duplication |
public function testFindWithArrayReturnsResults() |
|
55
|
|
|
{ |
|
56
|
|
|
$author = (new Author)->find([1, 2]); |
|
57
|
|
|
$uncachedAuthor = (new UncachedAuthor)->find([1, 2]); |
|
58
|
|
|
|
|
59
|
|
|
$this->assertEquals($uncachedAuthor->count(), $author->count()); |
|
60
|
|
|
$this->assertEquals($uncachedAuthor->pluck("id"), $author->pluck("id")); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function testFindWithSingleElementArrayDoesntConflictWithNormalFind() |
|
64
|
|
|
{ |
|
65
|
|
|
$author1 = (new Author) |
|
66
|
|
|
->find(1); |
|
67
|
|
|
$author2 = (new Author) |
|
68
|
|
|
->find([1]); |
|
69
|
|
|
|
|
70
|
|
|
$this->assertNotEquals($author1, $author2); |
|
71
|
|
|
$this->assertIsIterable($author2); |
|
72
|
|
|
$this->assertEquals(Author::class, get_class($author1)); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|