|
| 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\Profile; |
| 6 | +use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Publisher; |
| 7 | +use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Store; |
| 8 | +use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedAuthor; |
| 9 | +use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedBook; |
| 10 | +use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedProfile; |
| 11 | +use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedPublisher; |
| 12 | +use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedStore; |
| 13 | +use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Http\Resources\Author as AuthorResource; |
| 14 | +use GeneaLabs\LaravelModelCaching\Tests\IntegrationTestCase; |
| 15 | +use Illuminate\Foundation\Testing\RefreshDatabase; |
| 16 | +use Illuminate\Support\Collection; |
| 17 | + |
| 18 | +class GetTest extends IntegrationTestCase |
| 19 | +{ |
| 20 | + use RefreshDatabase; |
| 21 | + |
| 22 | + public function testGetModelResultsCreatesCache() |
| 23 | + { |
| 24 | + $authors = (new Author)->with('books', 'profile') |
| 25 | + ->get(); |
| 26 | + $key = sha1('genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesauthor-books-profile'); |
| 27 | + $tags = [ |
| 28 | + 'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesauthor', |
| 29 | + 'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesbook', |
| 30 | + 'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesprofile', |
| 31 | + ]; |
| 32 | + |
| 33 | + $cachedResults = $this->cache()->tags($tags) |
| 34 | + ->get($key)['value']; |
| 35 | + $liveResults = (new UncachedAuthor)->with('books', 'profile') |
| 36 | + ->get(); |
| 37 | + |
| 38 | + $this->assertEquals($authors, $cachedResults); |
| 39 | + $this->assertEmpty($liveResults->diffKeys($cachedResults)); |
| 40 | + } |
| 41 | + |
| 42 | + public function testAccessingGetResultsViaArrayIndexDoesNotError() |
| 43 | + { |
| 44 | + $author = (new Author) |
| 45 | + ->where('id', 1) |
| 46 | + ->get()[0]; |
| 47 | + $cachedAuthor = (new Author) |
| 48 | + ->where('id', 1) |
| 49 | + ->get()[0]; |
| 50 | + $uncachedAuthor = (new UncachedAuthor) |
| 51 | + ->where('id', 1) |
| 52 | + ->get()[0]; |
| 53 | + |
| 54 | + $this->assertEquals(1, $author->id); |
| 55 | + $this->assertEquals($author, $cachedAuthor); |
| 56 | + $this->assertEquals($author->toArray(), $uncachedAuthor->toArray()); |
| 57 | + } |
| 58 | + |
| 59 | + public function testGetWithFieldArrayCachesResults() |
| 60 | + { |
| 61 | + $key = sha1('genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesauthor_id_name'); |
| 62 | + $tags = [ |
| 63 | + 'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesauthor', |
| 64 | + ]; |
| 65 | + |
| 66 | + $authors = (new Author) |
| 67 | + ->get(["id", "name"]); |
| 68 | + $cachedResults = $this |
| 69 | + ->cache() |
| 70 | + ->tags($tags) |
| 71 | + ->get($key)['value']; |
| 72 | + $liveResults = (new UncachedAuthor) |
| 73 | + ->get(); |
| 74 | + |
| 75 | + $this->assertEquals($liveResults->pluck("id"), $authors->pluck("id")); |
| 76 | + $this->assertEquals($liveResults->pluck("id"), $cachedResults->pluck("id")); |
| 77 | + } |
| 78 | +} |
0 commit comments