Skip to content

Commit 16b1bbe

Browse files
committed
Add test for updating pivot tables
1 parent 9a7d28f commit 16b1bbe

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

src/Traits/ModelCaching.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public static function bootCachable()
2727
static::deleted(function ($instance) {
2828
$instance->checkCooldownAndFlushAfterPersiting($instance);
2929
});
30+
3031
static::saved(function ($instance) {
3132
$instance->checkCooldownAndFlushAfterPersiting($instance);
3233
});
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Integration;
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 UpdateExistingPivotTest extends IntegrationTestCase
19+
{
20+
use RefreshDatabase;
21+
22+
public function testInRandomOrderCachesResults()
23+
{
24+
$book = (new Book)
25+
->with("stores")
26+
->whereHas("stores")
27+
->first();
28+
$book->stores()
29+
->updateExistingPivot(
30+
$book->stores->first()->id,
31+
["test" => "value"]
32+
);
33+
$updatedCount = (new Book)
34+
->with("stores")
35+
->whereHas("stores")
36+
->first()
37+
->stores()
38+
->wherePivot("test", "value")
39+
->count();
40+
41+
$this->assertEquals(1, $updatedCount);
42+
}
43+
}

tests/database/migrations/2017_09_21_202000_create_book_store.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ public function up()
1313
$table->unsignedInteger('store_id');
1414
$table->timestamps();
1515

16+
$table->string("test")->nullable();
17+
1618
$table->foreign('book_id')
1719
->references('id')
1820
->on('books')

0 commit comments

Comments
 (0)