|
| 1 | +<?php namespace GeneaLabs\LaravelModelCaching\Tests\Feature\Nova; |
| 2 | + |
| 3 | +use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Book; |
| 4 | +use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Store; |
| 5 | + |
| 6 | +class BelongsToManyTest extends NovaTestCase |
| 7 | +{ |
| 8 | + /** @group test */ |
| 9 | + public function testAttachRelationFlushesCache() |
| 10 | + { |
| 11 | + $beforeStore = Store::with(['books'])->get()->first(); |
| 12 | + $beforeBooks = $beforeStore->books; |
| 13 | + |
| 14 | + /** @var Book $beforeBook */ |
| 15 | + $beforeBook = $beforeStore->books->first()->replicate(); |
| 16 | + $beforeBook->title = 'new foo'; |
| 17 | + $beforeBook->save(); |
| 18 | + |
| 19 | + $this->postJson('nova-api/books/' . $beforeBook->id . '/attach/stores' , [ |
| 20 | + 'stores' => $beforeStore->id, |
| 21 | + 'viaRelationship' => 'stores' |
| 22 | + ]); |
| 23 | + |
| 24 | + $this->response->assertStatus(200); |
| 25 | + |
| 26 | + $store = Store::with(['books'])->all()->first(); |
| 27 | + $books = $store->books; |
| 28 | + $book = $store->books->sortByDesc('id')->first(); |
| 29 | + |
| 30 | + $this->assertTrue($beforeStore->is($store)); |
| 31 | + $this->assertTrue($beforeBook->is($book)); |
| 32 | + $this->assertCount(1, $beforeBooks); |
| 33 | + $this->assertCount(2, $books); |
| 34 | + $this->assertCount(0, $beforeBook->stores); |
| 35 | + $this->assertSame('new foo', $book->title); |
| 36 | + } |
| 37 | + |
| 38 | + /** @group test */ |
| 39 | + public function testDetachRelationFlushesCache() |
| 40 | + { |
| 41 | + /** @var Store $store */ |
| 42 | + $store = Store::with(['books'])->get()->first(); |
| 43 | + |
| 44 | + /** @var Book $beforeBook */ |
| 45 | + $newBook = $store->books->first()->replicate(); |
| 46 | + $newBook->title = 'new foo'; |
| 47 | + $newBook->save(); |
| 48 | + |
| 49 | + $store->books()->attach($newBook); |
| 50 | + |
| 51 | + $beforeStore = Store::with(['books'])->get()->first(); |
| 52 | + $beforeBooks = $beforeStore->books; |
| 53 | + $beforeBook = $beforeBooks->first(); |
| 54 | + |
| 55 | + $this->deleteJson('/nova-api/stores/detach?viaResource=books&viaResourceId=' . $beforeBook->id . '&viaRelationship=stores' , [ |
| 56 | + 'resources' => [$beforeStore->id], |
| 57 | + ]); |
| 58 | + |
| 59 | + $this->response->assertStatus(200); |
| 60 | + |
| 61 | + $store = Store::with(['books'])->all()->first(); |
| 62 | + $books = $store->books; |
| 63 | + |
| 64 | + $this->assertTrue($beforeStore->is($store)); |
| 65 | + $this->assertCount(2, $beforeBooks); |
| 66 | + $this->assertCount(1, $books); |
| 67 | + } |
| 68 | + |
| 69 | + /** @group test */ |
| 70 | + public function testUpdateRelationFlushesCache() |
| 71 | + { |
| 72 | + $beforeStore = Store::with(['books'])->get()->first(); |
| 73 | + $beforeBook = $beforeStore->books->first(); |
| 74 | + |
| 75 | + $this->putJson('nova-api/books/' . $beforeBook->id, [ |
| 76 | + 'title' => 'foo', |
| 77 | + ]); |
| 78 | + |
| 79 | + $store = Store::with(['books'])->all()->first(); |
| 80 | + $book = $store->books->first(); |
| 81 | + |
| 82 | + $this->response->assertStatus(200); |
| 83 | + |
| 84 | + $this->assertTrue($beforeStore->is($store)); |
| 85 | + $this->assertTrue($beforeBook->is($book)); |
| 86 | + $this->assertSame('foo', $book->title); |
| 87 | + } |
| 88 | + |
| 89 | + /** @group test */ |
| 90 | + public function testDeleteRelationFlushesCache() |
| 91 | + { |
| 92 | + $beforeStore = Store::with(['books'])->get()->first(); |
| 93 | + $beforeBooks = $beforeStore->books; |
| 94 | + $beforeBook = $beforeBooks->first(); |
| 95 | + |
| 96 | + $this->deleteJson('nova-api/books', ['resources' => [$beforeBook->id]]); |
| 97 | + |
| 98 | + $store = Store::with(['books'])->all()->first(); |
| 99 | + $books = $store->books; |
| 100 | + |
| 101 | + $this->response->assertStatus(200); |
| 102 | + |
| 103 | + $this->assertTrue($beforeStore->is($store)); |
| 104 | + $this->assertCount(1, $beforeBooks); |
| 105 | + $this->assertCount(0, $books); |
| 106 | + $this->assertNull(Book::find($beforeBook->id)); |
| 107 | + } |
| 108 | +} |
0 commit comments