Skip to content

Commit ab6e628

Browse files
committed
Add CUD and BelongsToMany tests
1 parent 8e0f3ba commit ab6e628

File tree

8 files changed

+247
-7
lines changed

8 files changed

+247
-7
lines changed
Lines changed: 99 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,108 @@
11
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Feature\Nova;
22

3+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Book;
4+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Store;
5+
36
class BelongsToManyTest extends NovaTestCase
47
{
58
/** @group test */
6-
public function testBasicNovaIndexRequest()
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()
791
{
8-
$this->getJson('nova-api/authors');
9-
// $response->dump();
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);
10102

11-
$this->response->assertStatus(200)
12-
->assertJsonCount(10, 'resources');
103+
$this->assertTrue($beforeStore->is($store));
104+
$this->assertCount(1, $beforeBooks);
105+
$this->assertCount(0, $books);
106+
$this->assertNull(Book::find($beforeBook->id));
13107
}
14108
}

tests/Feature/Nova/CreateTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Feature\Nova;
2+
3+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Author;
4+
5+
class CreateTest extends NovaTestCase
6+
{
7+
public function testCreateFlushesCacheForModel()
8+
{
9+
$beforeAuthors = (new Author)->get();
10+
11+
$this->postJson('nova-api/authors', [
12+
'name' => 'foo',
13+
'email' => '[email protected]',
14+
]);
15+
16+
$authors = (new Author)->get();
17+
18+
$this->response->assertStatus(201);
19+
$this->assertCount(10, $beforeAuthors);
20+
$this->assertCount(11, $authors);
21+
}
22+
}

tests/Feature/Nova/DeleteTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Feature\Nova;
2+
3+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Author;
4+
5+
class DeleteTest extends NovaTestCase
6+
{
7+
public function testDeleteFlushesCacheForModel()
8+
{
9+
$beforeAuthors = (new Author)->get();
10+
$deleteAuthor = $beforeAuthors->first();
11+
12+
$this->deleteJson('nova-api/authors', ['resources' => [$deleteAuthor->id]]);
13+
14+
$authors = (new Author)->get();
15+
16+
$this->response->assertStatus(200);
17+
$this->assertCount(10, $beforeAuthors);
18+
$this->assertCount(9, $authors);
19+
}
20+
}

tests/Feature/Nova/NovaTestCase.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use GeneaLabs\LaravelModelCaching\Tests\FeatureTestCase;
66
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Nova\AuthorResource;
7+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Nova\BookResource;
8+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Nova\StoreResource;
79
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Providers\NovaServiceProvider;
810
use Illuminate\Contracts\Auth\Authenticatable;
911
use Illuminate\Testing\TestResponse;
@@ -24,7 +26,9 @@ public function setUp(): void
2426
Nova::$resources = [];
2527

2628
Nova::resources([
27-
AuthorResource::class
29+
AuthorResource::class,
30+
BookResource::class,
31+
StoreResource::class,
2832
]);
2933

3034
Nova::auth(function () {

tests/Feature/Nova/UpdateTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Feature\Nova;
2+
3+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Author;
4+
5+
class UpdateTest extends NovaTestCase
6+
{
7+
public function testUpdateFlushesCacheForModel()
8+
{
9+
$beforeAuthors = (new Author)->get();
10+
11+
/** @var Author $author */
12+
$author = $beforeAuthors->first();
13+
14+
$this->putJson('nova-api/authors/' . $author->id, [
15+
'name' => 'foo',
16+
'email' => '[email protected]',
17+
]);
18+
19+
$authors = (new Author)->get();
20+
21+
$this->response->assertStatus(200);
22+
$this->assertCount(10, $beforeAuthors);
23+
$this->assertCount(10, $authors);
24+
25+
/** @var Author $updatedAuthor */
26+
$updatedAuthor = $authors->first();
27+
$this->assertTrue($updatedAuthor->is($author));
28+
$this->assertSame('foo', $updatedAuthor->name);
29+
$this->assertSame('[email protected]', $updatedAuthor->email);
30+
31+
$author->refresh();
32+
$this->assertSame('foo', $author->name);
33+
$this->assertSame('[email protected]', $author->email);
34+
}
35+
}

tests/Fixtures/Nova/AuthorResource.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ public function fields(Request $request)
2020
{
2121
return [
2222
ID::make('ID', 'id'),
23-
2423
Text::make('Name', 'name'),
24+
Text::make('E-Mail', 'email'),
2525
];
2626
}
2727

tests/Fixtures/Nova/BookResource.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace GeneaLabs\LaravelModelCaching\Tests\Fixtures\Nova;
4+
5+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Book;
6+
use Illuminate\Http\Request;
7+
use Laravel\Nova\Fields\BelongsToMany;
8+
use Laravel\Nova\Fields\ID;
9+
use Laravel\Nova\Fields\Text;
10+
11+
class BookResource extends Resource
12+
{
13+
public static $model = Book::class;
14+
15+
public static $search = ['id'];
16+
17+
/**
18+
* @inheritDoc
19+
*/
20+
public function fields(Request $request)
21+
{
22+
return [
23+
ID::make('ID', 'id'),
24+
Text::make('Title', 'title'),
25+
BelongsToMany::make('Stores', 'stores', StoreResource::class),
26+
];
27+
}
28+
29+
public static function uriKey()
30+
{
31+
return 'books';
32+
}
33+
}

tests/Fixtures/Nova/StoreResource.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace GeneaLabs\LaravelModelCaching\Tests\Fixtures\Nova;
4+
5+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Store;
6+
use Illuminate\Http\Request;
7+
use Laravel\Nova\Fields\ID;
8+
use Laravel\Nova\Fields\Text;
9+
10+
class StoreResource extends Resource
11+
{
12+
public static $model = Store::class;
13+
14+
public static $search = ['id'];
15+
16+
/**
17+
* @inheritDoc
18+
*/
19+
public function fields(Request $request)
20+
{
21+
return [
22+
ID::make('ID', 'id'),
23+
Text::make('Name', 'name'),
24+
Text::make('Address', 'address'),
25+
];
26+
}
27+
28+
public static function uriKey()
29+
{
30+
return 'stores';
31+
}
32+
}

0 commit comments

Comments
 (0)