|
| 1 | +<?php namespace GeneaLabs\LaravelModelCaching\Tests\Unit\Console\Commands; |
| 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\TestCase; |
| 9 | +use Illuminate\Foundation\Testing\RefreshDatabase; |
| 10 | + |
| 11 | +class FlushTest extends TestCase |
| 12 | +{ |
| 13 | + use RefreshDatabase; |
| 14 | + |
| 15 | + public function setUp() |
| 16 | + { |
| 17 | + parent::setUp(); |
| 18 | + |
| 19 | + cache()->flush(); |
| 20 | + $publishers = factory(Publisher::class, 10)->create(); |
| 21 | + factory(Author::class, 10)->create() |
| 22 | + ->each(function ($author) use ($publishers) { |
| 23 | + factory(Book::class, random_int(2, 10))->make() |
| 24 | + ->each(function ($book) use ($author, $publishers) { |
| 25 | + $book->author()->associate($author); |
| 26 | + $book->publisher()->associate($publishers[rand(0, 9)]); |
| 27 | + $book->save(); |
| 28 | + }); |
| 29 | + factory(Profile::class)->make([ |
| 30 | + 'author_id' => $author->id, |
| 31 | + ]); |
| 32 | + }); |
| 33 | + |
| 34 | + $bookIds = (new Book)->all()->pluck('id'); |
| 35 | + factory(Store::class, 10)->create() |
| 36 | + ->each(function ($store) use ($bookIds) { |
| 37 | + $store->books()->sync(rand($bookIds->min(), $bookIds->max())); |
| 38 | + }); |
| 39 | + cache()->flush(); |
| 40 | + } |
| 41 | + |
| 42 | + public function testGivenModelIsFlushed() |
| 43 | + { |
| 44 | + $authors = (new Author)->all(); |
| 45 | + $key = 'genealabslaravelmodelcachingtestsfixturesauthor'; |
| 46 | + $tags = ['genealabslaravelmodelcachingtestsfixturesauthor']; |
| 47 | + |
| 48 | + $cachedResults = cache() |
| 49 | + ->tags($tags) |
| 50 | + ->get($key); |
| 51 | + $result = $this->artisan('modelCache:flush', ['--model' => Author::class]); |
| 52 | + $flushedResults = cache() |
| 53 | + ->tags($tags) |
| 54 | + ->get($key); |
| 55 | + |
| 56 | + $this->assertEquals($authors, $cachedResults); |
| 57 | + $this->assertEmpty($flushedResults); |
| 58 | + $this->assertEquals($result, 0); |
| 59 | + } |
| 60 | + |
| 61 | + public function testGivenModelWithRelationshipIsFlushed() |
| 62 | + { |
| 63 | + $authors = (new Author)->with('books')->get(); |
| 64 | + $key = 'genealabslaravelmodelcachingtestsfixturesauthor-books'; |
| 65 | + $tags = [ |
| 66 | + 'genealabslaravelmodelcachingtestsfixturesauthor', |
| 67 | + 'genealabslaravelmodelcachingtestsfixturesbook', |
| 68 | + ]; |
| 69 | + |
| 70 | + $cachedResults = cache() |
| 71 | + ->tags($tags) |
| 72 | + ->get($key); |
| 73 | + $result = $this->artisan( |
| 74 | + 'modelCache:flush', |
| 75 | + ['--model' => Author::class] |
| 76 | + ); |
| 77 | + $flushedResults = cache() |
| 78 | + ->tags($tags) |
| 79 | + ->get($key); |
| 80 | + |
| 81 | + $this->assertEquals($authors, $cachedResults); |
| 82 | + $this->assertEmpty($flushedResults); |
| 83 | + $this->assertEquals($result, 0); |
| 84 | + } |
| 85 | +} |
0 commit comments