|
| 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 FindTest extends IntegrationTestCase |
| 19 | +{ |
| 20 | + use RefreshDatabase; |
| 21 | + |
| 22 | + public function testFindModelResultsCreatesCache() |
| 23 | + { |
| 24 | + $author = collect()->push((new Author)->find(1)); |
| 25 | + $key = sha1('genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor_1'); |
| 26 | + $tags = [ |
| 27 | + 'genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor', |
| 28 | + ]; |
| 29 | + |
| 30 | + $cachedResults = collect()->push($this->cache()->tags($tags) |
| 31 | + ->get($key)); |
| 32 | + $liveResults = collect()->push((new UncachedAuthor)->find(1)); |
| 33 | + |
| 34 | + $this->assertEmpty($author->diffKeys($cachedResults)); |
| 35 | + $this->assertEmpty($liveResults->diffKeys($cachedResults)); |
| 36 | + } |
| 37 | + |
| 38 | + public function testSubsequentFindsReturnDifferentModels() |
| 39 | + { |
| 40 | + $author1 = (new Author)->find(1); |
| 41 | + $author2 = (new Author)->find(2); |
| 42 | + |
| 43 | + $this->assertNotEquals($author1, $author2); |
| 44 | + $this->assertEquals($author1->id, 1); |
| 45 | + $this->assertEquals($author2->id, 2); |
| 46 | + } |
| 47 | + |
| 48 | + public function testFindWithArrayReturnsResults() |
| 49 | + { |
| 50 | + $author = (new Author)->find([1, 2]); |
| 51 | + $uncachedAuthor = (new UncachedAuthor)->find([1, 2]); |
| 52 | + |
| 53 | + $this->assertEquals($uncachedAuthor->count(), $author->count()); |
| 54 | + $this->assertEquals($uncachedAuthor->pluck("id"), $author->pluck("id")); |
| 55 | + } |
| 56 | +} |
0 commit comments