|
| 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 WithCountTest extends IntegrationTestCase |
| 19 | +{ |
| 20 | + public function testWithCountUpdatesAfterRecordIsAdded() |
| 21 | + { |
| 22 | + $author1 = (new Author) |
| 23 | + ->withCount("books") |
| 24 | + ->first(); |
| 25 | + factory(Book::class, 1) |
| 26 | + ->make() |
| 27 | + ->each(function ($book) use ($author1) { |
| 28 | + $publisher = (new Publisher)->first(); |
| 29 | + $book->author()->associate($author1); |
| 30 | + $book->publisher()->associate($publisher); |
| 31 | + $book->save(); |
| 32 | + }); |
| 33 | + |
| 34 | + $author2 = (new Author) |
| 35 | + ->withCount("books") |
| 36 | + ->where("id", $author1->id) |
| 37 | + ->first(); |
| 38 | + |
| 39 | + $this->assertNotEquals($author1->books_count, $author2->books_count); |
| 40 | + $this->assertEquals($author1->books_count + 1, $author2->books_count); |
| 41 | + } |
| 42 | +} |
0 commit comments