Skip to content

Commit 1242c19

Browse files
feat: add ActiveState facade for shared state management
1 parent ddb54a7 commit 1242c19

File tree

2 files changed

+62
-3
lines changed

2 files changed

+62
-3
lines changed

src/Facades/ActiveState.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Farbcode\StatefulResources\Facades;
4+
5+
use Farbcode\StatefulResources\ActiveState as ActiveStateService;
6+
use Illuminate\Support\Facades\Facade;
7+
8+
/**
9+
* @method static void setShared(string $state)
10+
* @method static string getShared()
11+
* @method static void setForResource(string $resourceClass, string $state)
12+
* @method static string getForResource(string $resourceClass)
13+
*/
14+
class ActiveState extends Facade
15+
{
16+
/**
17+
* Get the registered name of the component.
18+
*/
19+
protected static function getFacadeAccessor(): string
20+
{
21+
return ActiveStateService::class;
22+
}
23+
}

tests/Feature/SharedStateTest.php

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

3-
use Farbcode\StatefulResources\ActiveState;
3+
use Farbcode\StatefulResources\ActiveState as ActiveStateService;
4+
use Farbcode\StatefulResources\Facades\ActiveState;
45
use Workbench\App\Http\Resources\CatResource;
56
use Workbench\App\Models\Cat;
67
use Workbench\App\Models\Dog;
@@ -13,7 +14,42 @@
1314

1415
$resource = CatResource::state('table')->make($cat)->toJson();
1516

16-
expect(app(ActiveState::class)->getShared())->toBe('table');
17+
expect(app(ActiveStateService::class)->getShared())->toBe('table');
18+
19+
expect($resource)->toBeJson();
20+
21+
expect($resource)->json()->toEqual([
22+
'id' => $cat->id,
23+
'name' => $cat->name,
24+
'breed' => $cat->breed,
25+
'enemies' => [
26+
[
27+
'id' => $dogs[0]->id,
28+
'name' => $dogs[0]->name,
29+
],
30+
[
31+
'id' => $dogs[1]->id,
32+
'name' => $dogs[1]->name,
33+
],
34+
[
35+
'id' => $dogs[2]->id,
36+
'name' => $dogs[2]->name,
37+
],
38+
],
39+
]);
40+
});
41+
42+
it('can set the shared state through the ActiveState facade', function () {
43+
$cat = Cat::factory()->new()->createOne();
44+
$dogs = Dog::factory()->count(3)->create();
45+
46+
$cat->enemies()->attach($dogs->pluck('id'));
47+
48+
ActiveState::setShared('table');
49+
50+
$resource = CatResource::make($cat)->toJson();
51+
52+
expect(app(ActiveStateService::class)->getShared())->toBe('table');
1753

1854
expect($resource)->toBeJson();
1955

@@ -48,7 +84,7 @@
4884

4985
$resource = CatResource::state('table')->make($cat)->toJson();
5086

51-
expect(app(ActiveState::class)->getForResource(CatResource::class))->toBe('table');
87+
expect(app(ActiveStateService::class)->getForResource(CatResource::class))->toBe('table');
5288

5389
expect($resource)->toBeJson();
5490

0 commit comments

Comments
 (0)