|
| 1 | +<?php namespace GeneaLabs\LaravelModelCaching\Tests\Integration\CachedBuilder; |
| 2 | + |
| 3 | +use Faker\Generator as Faker; |
| 4 | +use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Book; |
| 5 | +use GeneaLabs\LaravelModelCaching\Tests\IntegrationTestCase; |
| 6 | + |
| 7 | +class MorphOneTest extends IntegrationTestCase |
| 8 | +{ |
| 9 | + public function setUp() : void |
| 10 | + { |
| 11 | + parent::setUp(); |
| 12 | + |
| 13 | + (new Book) |
| 14 | + ->get() |
| 15 | + ->each(function ($book) { |
| 16 | + $book->image()->create([ |
| 17 | + "path" => app(Faker::class)->imageUrl(), |
| 18 | + ]); |
| 19 | + }); |
| 20 | + $this->cache()->flush(); |
| 21 | + } |
| 22 | + |
| 23 | + public function testMorphTo() |
| 24 | + { |
| 25 | + $key1 = sha1('genealabs:laravel-model-caching:testing::memory::books:genealabslaravelmodelcachingtestsfixturesbook-author_id_=_1-testing::memory::image'); |
| 26 | + $key2 = sha1('genealabs:laravel-model-caching:testing::memory::books:genealabslaravelmodelcachingtestsfixturesbook-author_id_=_2-testing::memory::image'); |
| 27 | + $tags = [ |
| 28 | + "genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesbook", |
| 29 | + "genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesimage", |
| 30 | + ]; |
| 31 | + |
| 32 | + $books1 = (new Book) |
| 33 | + ->with("image") |
| 34 | + ->where("author_id", 1) |
| 35 | + ->get(); |
| 36 | + $cachedResults1 = $this->cache() |
| 37 | + ->tags($tags) |
| 38 | + ->get($key1)['value']; |
| 39 | + $books2 = (new Book) |
| 40 | + ->with("image") |
| 41 | + ->where("author_id", 2) |
| 42 | + ->get(); |
| 43 | + $cachedResults2 = $this->cache() |
| 44 | + ->tags($tags) |
| 45 | + ->get($key2)['value']; |
| 46 | + |
| 47 | + $this->assertEquals($cachedResults1->pluck("images.id"), $books1->pluck("images.id")); |
| 48 | + $this->assertEquals($cachedResults2->pluck("images.id"), $books2->pluck("images.id")); |
| 49 | + $this->assertNotEquals($cachedResults1->pluck("images.id"), $cachedResults2->pluck("images.id")); |
| 50 | + $this->assertNotEquals($books1->pluck("images.id"), $books2->pluck("images.id")); |
| 51 | + $this->assertNotNull($books1->first()->image); |
| 52 | + $this->assertNotNull($books2->first()->image); |
| 53 | + $this->assertNotNull($cachedResults1->first()->image); |
| 54 | + $this->assertNotNull($cachedResults2->first()->image); |
| 55 | + } |
| 56 | +} |
0 commit comments