Skip to content

Commit 97bad6e

Browse files
feat: setup testing, add basic feature tests
1 parent 3066192 commit 97bad6e

File tree

10 files changed

+277
-5
lines changed

10 files changed

+277
-5
lines changed

tests/Feature/CustomStatesTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
use Workbench\App\Enums\CustomResourceStates;
4+
use Workbench\App\Http\Resources\CatResource;
5+
use Workbench\App\Models\Cat;
6+
use Workbench\Database\Factories\CatFactory;
7+
8+
beforeEach(function () {
9+
CatFactory::new()->createOne();
10+
});
11+
12+
it('can use a custom user-defined state', function () {
13+
/** @var TestCase $this */
14+
$cat = Cat::firstOrFail();
15+
16+
$resource = CatResource::state(CustomResourceStates::Custom)->make($cat)->toJson();
17+
18+
expect($resource)->toBeJson();
19+
20+
expect($resource)->json()->toEqual([
21+
'id' => $cat->id,
22+
'name' => $cat->name,
23+
'custom_field' => 'custom_value',
24+
]);
25+
});
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
use Farbcode\StatefulResources\Enums\Variant;
4+
use Farbcode\StatefulResources\Tests\TestCase;
5+
use Workbench\App\Http\Resources\CatResource;
6+
use Workbench\App\Models\Cat;
7+
use Workbench\Database\Factories\CatFactory;
8+
9+
beforeEach(function () {
10+
CatFactory::new()->createOne();
11+
});
12+
13+
it('can return a stateful resource with the default state', function () {
14+
/** @var TestCase $this */
15+
$cat = Cat::firstOrFail();
16+
17+
$resource = CatResource::make($cat)->toJson();
18+
19+
expect($resource)->toBeJson();
20+
21+
expect($resource)->json()->toEqual([
22+
'id' => $cat->id,
23+
'name' => $cat->name,
24+
'breed' => $cat->breed,
25+
'fluffyness' => $cat->fluffyness,
26+
'color' => $cat->color,
27+
]);
28+
29+
});
30+
31+
it('can return a stateful resource with the correct "full" state', function () {
32+
/** @var TestCase $this */
33+
$cat = Cat::firstOrFail();
34+
35+
$resource = CatResource::state(Variant::Full)->make($cat)->toJson();
36+
37+
expect($resource)->toBeJson();
38+
39+
expect($resource)->json()->toEqual([
40+
'id' => $cat->id,
41+
'name' => $cat->name,
42+
'breed' => $cat->breed,
43+
'fluffyness' => $cat->fluffyness,
44+
'color' => $cat->color,
45+
]);
46+
});
47+
48+
it('can use a stateful resource with the "minimal" state', function () {
49+
/** @var TestCase $this */
50+
$cat = Cat::firstOrFail();
51+
52+
$resource = CatResource::state(Variant::Minimal)->make($cat)->toJson();
53+
54+
expect($resource)->toBeJson();
55+
56+
expect($resource)->json()->toEqual([
57+
'id' => $cat->id,
58+
'name' => $cat->name,
59+
]);
60+
});
61+
62+
it('can use a stateful resource with the "table" state', function () {
63+
/** @var TestCase $this */
64+
$cat = Cat::firstOrFail();
65+
66+
$resource = CatResource::state(Variant::Table)->make($cat)->toJson();
67+
68+
expect($resource)->toBeJson();
69+
70+
expect($resource)->json()->toEqual([
71+
'id' => $cat->id,
72+
'name' => $cat->name,
73+
'breed' => $cat->breed,
74+
]);
75+
});

tests/TestCase.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
namespace Farbcode\StatefulResources\Tests;
44

55
use Farbcode\StatefulResources\StatefulResourcesServiceProvider;
6+
use Illuminate\Foundation\Testing\RefreshDatabase;
67
use Orchestra\Testbench\Concerns\WithWorkbench;
78
use Orchestra\Testbench\TestCase as Orchestra;
9+
use Workbench\App\Enums\CustomResourceStates;
810

911
class TestCase extends Orchestra
1012
{
11-
use WithWorkbench;
13+
use RefreshDatabase, WithWorkbench;
1214

1315
protected $loadEnvironmentVariables = false;
1416

@@ -26,7 +28,9 @@ protected function getPackageProviders($app)
2628

2729
protected function getEnvironmentSetUp($app)
2830
{
29-
//
31+
$app['config']->set('stateful-resources.custom_states', [
32+
CustomResourceStates::class,
33+
]);
3034
}
3135

3236
/**
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Workbench\App\Enums;
4+
5+
use Farbcode\StatefulResources\Concerns\AsResourceState;
6+
use Farbcode\StatefulResources\Contracts\ResourceState;
7+
8+
enum CustomResourceStates: string implements ResourceState
9+
{
10+
use AsResourceState;
11+
12+
case Custom = 'custom';
13+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Workbench\App\Http\Resources;
4+
5+
use Farbcode\StatefulResources\Enums\Variant;
6+
use Farbcode\StatefulResources\StatefulJsonResource;
7+
use Illuminate\Http\Request;
8+
use Workbench\App\Enums\CustomResourceStates;
9+
10+
class CatResource extends StatefulJsonResource
11+
{
12+
/**
13+
* Transform the resource into an array.
14+
*
15+
* @return array<string, mixed>
16+
*/
17+
public function toArray(Request $request): array
18+
{
19+
return [
20+
'id' => $this->id,
21+
'name' => $this->name,
22+
'breed' => $this->whenStateIn([Variant::Full, Variant::Table], $this->breed),
23+
'fluffyness' => $this->whenStateIn([Variant::Full], $this->fluffyness),
24+
'color' => $this->whenStateIn([Variant::Full], $this->color),
25+
'custom_field' => $this->whenState(CustomResourceStates::Custom, 'custom_value'),
26+
];
27+
}
28+
}

workbench/app/Models/Cat.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Workbench\App\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
/**
8+
* @property int $id
9+
* @property string $name
10+
* @property string $breed
11+
* @property string $fluffyness
12+
* @property string $color
13+
* @property \Illuminate\Support\Carbon|null $created_at
14+
* @property \Illuminate\Support\Carbon|null $updated_at
15+
*/
16+
class Cat extends Model
17+
{
18+
protected $fillable = ['name', 'breed', 'fluffyness', 'color'];
19+
}

workbench/app/Providers/WorkbenchServiceProvider.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ public function register(): void
1919
*/
2020
public function boot(): void
2121
{
22-
$this->publishes([
23-
__DIR__.'/../../../workbench/test-data' => public_path('test-data'),
24-
], 'test-assets');
22+
//
2523
}
2624
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
namespace Workbench\Database\Factories;
4+
5+
use Illuminate\Database\Eloquent\Factories\Factory;
6+
use Workbench\App\Models\Cat;
7+
8+
/**
9+
* @template TModel of \Workbench\App\Models\Cat
10+
*
11+
* @extends \Illuminate\Database\Eloquent\Factories\Factory<TModel>
12+
*/
13+
class CatFactory extends Factory
14+
{
15+
/**
16+
* The name of the factory's corresponding model.
17+
*
18+
* @var class-string<TModel>
19+
*/
20+
protected $model = Cat::class;
21+
22+
protected $names = [
23+
'Whiskers',
24+
'Mittens',
25+
'Shadow',
26+
'Simba',
27+
'Luna',
28+
'Oliver',
29+
'Bella',
30+
'Charlie',
31+
'Lucy',
32+
'Max',
33+
];
34+
35+
protected $breeds = [
36+
'Siamese',
37+
'Persian',
38+
'Maine Coon',
39+
'Bengal',
40+
'British Shorthair',
41+
'Ragdoll',
42+
'Sphynx',
43+
'Norwegian Forest',
44+
'Scottish Fold',
45+
'Abyssinian',
46+
];
47+
48+
protected $fluffyness = [
49+
'superfluffy',
50+
'fluffy',
51+
'not-fluffy',
52+
];
53+
54+
protected $color = [
55+
'black',
56+
'white',
57+
'gray',
58+
'orange',
59+
'brown',
60+
];
61+
62+
/**
63+
* Define the model's default state.
64+
*
65+
* @return array<string, mixed>
66+
*/
67+
public function definition(): array
68+
{
69+
return [
70+
'name' => $this->faker->randomElement($this->names),
71+
'breed' => $this->faker->randomElement($this->breeds),
72+
'fluffyness' => $this->faker->randomElement($this->fluffyness),
73+
'color' => $this->faker->randomElement($this->color),
74+
];
75+
}
76+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up(): void
13+
{
14+
Schema::create('cats', function (Blueprint $table) {
15+
$table->id();
16+
$table->string('name');
17+
$table->string('breed');
18+
$table->string('fluffyness');
19+
$table->string('color');
20+
$table->timestamps();
21+
});
22+
}
23+
24+
/**
25+
* Reverse the migrations.
26+
*/
27+
public function down(): void
28+
{
29+
Schema::dropIfExists('cats');
30+
}
31+
};

workbench/database/seeders/DatabaseSeeder.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Workbench\Database\Seeders;
44

55
use Illuminate\Database\Seeder;
6+
use Workbench\Database\Factories\CatFactory;
67
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
78
use Workbench\Database\Factories\UserFactory;
89

@@ -19,5 +20,7 @@ public function run(): void
1920
'name' => 'Test User',
2021
'email' => 'test@example.com',
2122
]);
23+
24+
CatFactory::new()->times(10)->create();
2225
}
2326
}

0 commit comments

Comments
 (0)