Skip to content

Commit 86dc12c

Browse files
committed
tests
1 parent 4d03674 commit 86dc12c

File tree

4 files changed

+165
-4
lines changed

4 files changed

+165
-4
lines changed

src/Laravel/ApiPlatformProvider.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@
8989
use ApiPlatform\Laravel\Eloquent\PropertyAccess\PropertyAccessor as EloquentPropertyAccessor;
9090
use ApiPlatform\Laravel\Eloquent\PropertyInfo\EloquentExtractor;
9191
use ApiPlatform\Laravel\Eloquent\Serializer\EloquentNameConverter;
92-
use ApiPlatform\Laravel\Eloquent\Serializer\Mapping\Loader\RelationMetadataLoader;
9392
use ApiPlatform\Laravel\Eloquent\Serializer\SerializerContextBuilder as EloquentSerializerContextBuilder;
9493
use ApiPlatform\Laravel\GraphQl\Controller\EntrypointController as GraphQlEntrypointController;
9594
use ApiPlatform\Laravel\GraphQl\Controller\GraphiQlController;
@@ -573,7 +572,8 @@ public function register(): void
573572
$app->make(LoggerInterface::class),
574573
$app->make(ResourceMetadataCollectionFactoryInterface::class),
575574
$app->make(ResourceAccessCheckerInterface::class),
576-
$defaultContext
575+
$defaultContext,
576+
// $app->make(TagCollectorInterface::class)
577577
);
578578
});
579579

@@ -621,6 +621,7 @@ public function register(): void
621621
$defaultContext,
622622
$app->make(ResourceMetadataCollectionFactoryInterface::class),
623623
$app->make(ResourceAccessCheckerInterface::class),
624+
// $app->make(TagCollectorInterface::class),
624625
);
625626
});
626627

@@ -866,7 +867,6 @@ public function register(): void
866867
$defaultContext,
867868
$app->make(ResourceMetadataCollectionFactoryInterface::class),
868869
$app->make(ResourceAccessCheckerInterface::class),
869-
null
870870
// $app->make(TagCollectorInterface::class),
871871
);
872872
});
@@ -961,7 +961,8 @@ public function register(): void
961961
$app->make(NameConverterInterface::class),
962962
$app->make(ClassMetadataFactoryInterface::class),
963963
$defaultContext,
964-
$app->make(ResourceAccessCheckerInterface::class)
964+
$app->make(ResourceAccessCheckerInterface::class),
965+
// $app->make(TagCollectorInterface::class)
965966
);
966967
});
967968

src/Laravel/Tests/PurgerTest.php

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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+
}

src/Laravel/workbench/app/Providers/WorkbenchServiceProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Orchestra\Testbench\Workbench\Workbench;
2323
use Symfony\Component\Console\Input\ArrayInput;
2424
use Symfony\Component\Console\Output\NullOutput;
25+
use Workbench\App\Purger\MockPurger;
2526
use Workbench\App\Services\DummyService;
2627
use Workbench\App\State\CustomProviderWithDependency;
2728

@@ -36,6 +37,7 @@ public function register(): void
3637
$config->set('api-platform.resources', [app_path('Models'), app_path('ApiResource')]);
3738
$config->set('cache.default', 'null');
3839

40+
$this->app->singleton(MockPurger::class, fn ($app) => new MockPurger));
3941
$this->app->singleton(DummyService::class, fn ($app) => new DummyService($app['config']->get('api-platform.title')));
4042
$this->app->singleton(CustomProviderWithDependency::class, fn ($app) => new CustomProviderWithDependency($app->make(DummyService::class)));
4143
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 Workbench\App\Purger;
15+
16+
use App\Contracts\ApiCache\PurgerInterface;
17+
18+
class MockPurger implements PurgerInterface
19+
{
20+
/**
21+
* @var string[]
22+
*/
23+
public static array $purgedTags = [];
24+
25+
public function purge(array $tags): void
26+
{
27+
self::$purgedTags = array_merge(self::$purgedTags, $tags);
28+
}
29+
30+
public static function reset(): void
31+
{
32+
self::$purgedTags = [];
33+
}
34+
35+
/**
36+
* @return string[]
37+
*/
38+
public static function getPurgedTags(): array
39+
{
40+
// Return unique and sorted tags for consistent assertions
41+
$tags = array_unique(self::$purgedTags);
42+
sort($tags);
43+
44+
return $tags;
45+
}
46+
}

0 commit comments

Comments
 (0)