|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the API Platform project. |
| 5 | + * |
| 6 | + * (c) Kévin Dunglas <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace ApiPlatform\Laravel\Tests; |
| 15 | + |
| 16 | +use ApiPlatform\Laravel\Test\ApiTestAssertionsTrait; |
| 17 | +use Illuminate\Foundation\Testing\RefreshDatabase; |
| 18 | +use Orchestra\Testbench\Concerns\WithWorkbench; |
| 19 | +use Orchestra\Testbench\TestCase; |
| 20 | +use Workbench\App\Models\Author; |
| 21 | +use Workbench\App\Models\Book; |
| 22 | +use Workbench\App\Purger\MockPurger; |
| 23 | + |
| 24 | +class PurgerTest extends TestCase |
| 25 | +{ |
| 26 | + use ApiTestAssertionsTrait; |
| 27 | + use RefreshDatabase; |
| 28 | + use WithWorkbench; |
| 29 | + |
| 30 | + protected function getEnvironmentSetUp($app): void |
| 31 | + { |
| 32 | + $app['config']->set('api-platform.http_cache.invalidation.purger', MockPurger::class); |
| 33 | + } |
| 34 | + |
| 35 | + protected function setUp(): void |
| 36 | + { |
| 37 | + parent::setUp(); |
| 38 | + MockPurger::reset(); |
| 39 | + } |
| 40 | + |
| 41 | + public function testPurgeOnCreate(): void |
| 42 | + { |
| 43 | + $author = Author::factory()->create(); |
| 44 | + |
| 45 | + $this->postJson('/api/books', [ |
| 46 | + 'isbn' => '978-3-16-148410-0', |
| 47 | + 'name' => 'The Test Book', |
| 48 | + 'author' => '/api/authors/'.$author->id, |
| 49 | + ], ['Accept' => 'application/ld+json']); |
| 50 | + |
| 51 | + $this->assertTagsWerePurged([ |
| 52 | + '/api/books', // The collection of the created resource |
| 53 | + '/api/authors/'.$author->id, // The related resource |
| 54 | + ]); |
| 55 | + } |
| 56 | + |
| 57 | + public function testPurgeOnUpdate(): void |
| 58 | + { |
| 59 | + $book = Book::factory()->has(Author::factory())->create(); |
| 60 | + |
| 61 | + $this->patchJson('/api/books/'.$book->id, [ |
| 62 | + 'name' => 'An Updated Name', |
| 63 | + ], [ |
| 64 | + 'Accept' => 'application/ld+json', |
| 65 | + 'Content-Type' => 'application/merge-patch+json', |
| 66 | + ]); |
| 67 | + |
| 68 | + $this->assertTagsWerePurged([ |
| 69 | + '/api/books', // The collection of the updated resource |
| 70 | + '/api/books/'.$book->id, // The item itself |
| 71 | + ]); |
| 72 | + } |
| 73 | + |
| 74 | + public function testPurgeOnUpdateChangingRelation(): void |
| 75 | + { |
| 76 | + $book = Book::factory()->has(Author::factory())->create(); |
| 77 | + $oldAuthor = $book->author; |
| 78 | + $newAuthor = Author::factory()->create(); |
| 79 | + |
| 80 | + $this->putJson('/api/books/'.$book->id, [ |
| 81 | + 'name' => $book->name, // PUT requires all fields |
| 82 | + 'author' => '/api/authors/'.$newAuthor->id, |
| 83 | + ], ['Accept' => 'application/ld+json']); |
| 84 | + |
| 85 | + $this->assertTagsWerePurged([ |
| 86 | + '/api/books', |
| 87 | + '/api/books/'.$book->id, |
| 88 | + '/api/authors/'.$oldAuthor->id, // The old related resource |
| 89 | + '/api/authors/'.$newAuthor->id, // The new related resource |
| 90 | + ]); |
| 91 | + } |
| 92 | + |
| 93 | + public function testPurgeOnDelete(): void |
| 94 | + { |
| 95 | + $book = Book::factory()->has(Author::factory())->create(); |
| 96 | + $author = $book->author; |
| 97 | + |
| 98 | + $this->deleteJson('/api/books/'.$book->id); |
| 99 | + |
| 100 | + $this->assertTagsWerePurged([ |
| 101 | + '/api/books', |
| 102 | + '/api/books/'.$book->id, |
| 103 | + '/api/authors/'.$author->id, // The related resource |
| 104 | + ]); |
| 105 | + } |
| 106 | + |
| 107 | + private function assertTagsWerePurged(array $expectedTags): void |
| 108 | + { |
| 109 | + sort($expectedTags); |
| 110 | + $this->assertEquals($expectedTags, MockPurger::getPurgedTags()); |
| 111 | + } |
| 112 | +} |
0 commit comments