diff --git a/database/factories/Member/MemberFactory.php b/database/factories/Member/MemberFactory.php index f14552d0..e7fcbed7 100644 --- a/database/factories/Member/MemberFactory.php +++ b/database/factories/Member/MemberFactory.php @@ -2,9 +2,13 @@ namespace Database\Factories\Member; -use App\Member\Member; -use Illuminate\Database\Eloquent\Factories\Factory; use App\Country; +use App\Fee; +use App\Group; +use App\Member\Member; +use App\Nationality; +use App\Payment\Subscription; +use Illuminate\Database\Eloquent\Factories\Factory; class MemberFactory extends Factory { @@ -33,4 +37,27 @@ class MemberFactory extends Factory 'location' => $this->faker->city, ]; } + + public function defaults(): self + { + $country = Country::count() + ? Country::get()->random() + : Country::factory()->create(); + $group = Group::count() + ? Group::get()->random() + : Group::factory()->create(); + $nationality = Nationality::count() + ? Nationality::get()->random() + : Nationality::factory()->create(); + $subscription = Subscription::count() + ? Subscription::get()->random() + : Subscription::factory()->for(Fee::factory())->create(); + + return $this + ->for($country) + ->for($group) + ->for($nationality) + ->for($subscription); + } + } diff --git a/tests/Feature/Member/IndexTest.php b/tests/Feature/Member/IndexTest.php new file mode 100644 index 00000000..870777ed --- /dev/null +++ b/tests/Feature/Member/IndexTest.php @@ -0,0 +1,23 @@ +<?php + +namespace Tests\Feature\Member; + +use App\Member\Member; +use Illuminate\Foundation\Testing\RefreshDatabase; +use Tests\TestCase; + +class IndexTest extends TestCase +{ + + use RefreshDatabase; + + public function testItGetsMembers(): void + { + $this->withoutExceptionHandling(); + $this->login(); + + Member::factory()->defaults()->create(['firstname' => '::firstname']); + $this->get('/member')->assertInertia('member/Index', ['firstname' => '::firstname'], 'data.data.0'); + } + +} diff --git a/tests/Lib/InertiaMixin.php b/tests/Lib/InertiaMixin.php new file mode 100644 index 00000000..b2722b8c --- /dev/null +++ b/tests/Lib/InertiaMixin.php @@ -0,0 +1,128 @@ +<?php + +namespace Tests\Lib; + +use Illuminate\Support\Collection; +use Illuminate\Testing\TestResponse; +use PHPUnit\Framework\Assert as PHPUnit; + +class InertiaMixin { + + public function assertInertia() { + return function($component, $props, $key = null) { + PHPUnit::assertEquals($component, $this->viewData('page')['component']); + + $this->assertInertiaHas($props, $key); + + return $this; + }; + } + + public function assertInertiaComponent() { + return function($component) { + PHPUnit::assertEquals($component, $this->viewData('page')['component']); + + return $this; + }; + } + + public function assertInertiaHasShared() { + return function($bindings, $key = null) { + $bindings = json_decode(json_encode($bindings), true); + + $viewData = json_decode(json_encode( + data_get($this->viewData('page'), $key) + ), true); + + $this->assertDeepNest($bindings, $viewData); + }; + } + + public function assertInertiaHas() { + return function($bindings, $key = null) { + $bindings = json_decode(json_encode($bindings), true); + + $viewData = json_decode(json_encode( + data_get($this->viewData('page')['props'], $key) + ), true); + + $bindings = is_array($bindings) ? $bindings : [$bindings]; + $viewData = is_array($viewData) ? $viewData : [$viewData]; + + $this->assertDeepNest($bindings, $viewData); + }; + } + + public function assertDeepNest() { + return function($should, $is) { + foreach ($should as $key => $value) { + PHPUnit::assertArrayHasKey($key, $is); + + if (is_array($value)) { + $this->assertDeepNest($value, $is[$key]); + continue; + } + + PHPUnit::assertSame($value, $is[$key]); + } + }; + } + + public function assertInertiaHasItem() { + + return function($should, $nestedKey) { + $is = data_get($this->viewData('page')['props'], $nestedKey); + $is = collect(json_decode(json_encode($is), true)); + + $should = collect(json_decode(json_encode($should), true)); + + $has = $is->contains(function($isItem) use ($should) { + return $this->isDeepEqual($should, Collection::wrap($isItem)); + }); + + PHPUnit::assertTrue($has, 'Failed asserting that inertia attribute '.$nestedKey.' has Data '.print_r($should, true)); + + return $this; + }; + + } + + public function inertia() { + return function($item) { + return data_get($this->viewData('page')['props'], $item); + }; + } + + public function assertInertiaEquals() { + return function($should, $nestedKey) { + $is = data_get($this->viewData('page')['props'], $nestedKey); + + PHPUnit::assertSame($should, $is); + + return $this; + }; + } + + public function ddp() { + return function ($value) { + dd(data_get($this->viewData('page'), $value)); + }; + } + + public function ddi() { + return function ($value) { + dd(data_get($this->viewData('page')['props'], $value)); + }; + } + + public function isDeepEqual() { + return function (Collection $subset, Collection $compare) { + $subset = $subset->filter(fn($item) => !is_array($item)); + $compare = $compare->filter(fn($item) => !is_array($item)); + + return $subset->diffAssoc($compare)->isEmpty(); + }; + } + +} + diff --git a/tests/TestCase.php b/tests/TestCase.php index 16dfc5f5..f4132b5c 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -3,6 +3,8 @@ namespace Tests; use Illuminate\Foundation\Testing\TestCase as BaseTestCase; +use Illuminate\Testing\TestResponse; +use Tests\Lib\InertiaMixin; use Zoomyboy\LaravelNami\FakesNami; use Zoomyboy\LaravelNami\Nami; use Zoomyboy\LaravelNami\NamiUser; @@ -16,6 +18,7 @@ abstract class TestCase extends BaseTestCase parent::setUp(); $this->fakeNami(); + TestResponse::mixin(new InertiaMixin()); } public function login() {