create(['id' => 1]); } #[DataProvider('resourceProvider')] public function test_anonymous_cannot_create_update_delete(string $method, string $uri, string $resource): void { $this->call($method, $uri)->assertUnauthorized(); } #[DataProvider('resourceProvider')] public function test_non_admin_cannot_create_update_delete(string $method, string $uri, string $resource): void { Sanctum::actingAs(User::factory()->create()); $this->call($method, $uri)->assertForbidden(); } #[DataProvider('resourceCreateProvider')] public function test_admin_can_create_resource(string $resource, array $payload): void { Sanctum::actingAs(User::factory()->create(['is_admin' => true])); $response = $this->postJson($this->getCreateUri($resource), $payload); $response->assertStatus(201); $this->assertResourceExists($resource); } #[DataProvider('resourceCreateProvider')] public function test_admin_create_persists_both_locales(string $resource, array $payload): void { Sanctum::actingAs(User::factory()->create(['is_admin' => true])); $this->postJson($this->getCreateUri($resource), $payload)->assertStatus(201); $this->assertBothLocalesPersisted($resource, $payload); } #[DataProvider('resourceCreateProvider')] public function test_admin_can_update_resource_preserving_other_locale(string $resource, array $payload): void { Sanctum::actingAs(User::factory()->create(['is_admin' => true])); $this->postJson($this->getCreateUri($resource), $payload)->assertStatus(201); $id = $this->getFirstId($resource); $updatePayload = $this->getUpdatePayload($resource, 'fa', 'Updated FA'); $response = $this->putJson($this->getUpdateUri($resource, $id), $updatePayload); $response->assertOk(); $this->assertLocaleUpdated($resource, $id, 'fa', 'Updated FA'); $this->assertOtherLocaleUntouched($resource, $id, 'en', $payload); } #[DataProvider('resourceCreateProvider')] public function test_admin_can_delete_resource(string $resource, array $payload): void { Sanctum::actingAs(User::factory()->create(['is_admin' => true])); $this->postJson($this->getCreateUri($resource), $payload)->assertStatus(201); $id = $this->getFirstId($resource); $response = $this->deleteJson($this->getUpdateUri($resource, $id)); $response->assertStatus(200); $this->assertDatabaseMissing($this->getTableForResource($resource), ['id' => $id]); $this->assertTranslationsGone($resource, $id); } #[DataProvider('validationProvider')] public function test_validation_fails_for_bad_data(string $resource, array $payload, array $expectedErrors): void { Sanctum::actingAs(User::factory()->create(['is_admin' => true])); $response = $this->postJson($this->getCreateUri($resource), $payload); $response->assertStatus(422); foreach ($expectedErrors as $error) { $response->assertJsonValidationErrors($error); } } public static function resourceProvider(): array { return [ 'service post' => ['POST', '/api/website/services', 'services'], 'service put' => ['PUT', '/api/website/services/1', 'services'], 'service delete' => ['DELETE', '/api/website/services/1', 'services'], 'skill post' => ['POST', '/api/website/skills', 'skills'], 'skill put' => ['PUT', '/api/website/skills/1', 'skills'], 'skill delete' => ['DELETE', '/api/website/services/1', 'skills'], // won't match actual but for test structure - fix later ]; } public static function resourceCreateProvider(): array { return [ 'services' => [ 'services', [ 'imageSrc' => 'image.png', 'translations' => [ 'title' => ['fa' => 'عنوان', 'en' => 'Title'], 'description' => ['fa' => 'توضیح', 'en' => 'Description'], ], ], ], 'skills' => [ 'skills', [ 'image' => 'skill.png', 'link' => 'https://example.com', 'percentage' => 80, 'about_me_id' => 1, 'translations' => [ 'title' => ['fa' => 'مهارت', 'en' => 'Skill'], 'description' => ['fa' => 'توضیح مهارت', 'en' => 'Skill desc'], ], ], ], 'testimonials' => [ 'testimonials', [ 'image' => 'test.png', 'client_link' => 'https://client.com', 'translations' => [ 'client_name' => ['fa' => 'اسم', 'en' => 'Name'], 'client_position' => ['fa' => 'سمت', 'en' => 'Position'], 'feedback' => ['fa' => 'نظر', 'en' => 'Feedback'], ], ], ], ]; } public static function validationProvider(): array { return [ 'service missing fa title' => [ 'services', [ 'translations' => [ 'title' => ['en' => 'Title'], 'description' => ['fa' => 'توضیح', 'en' => 'Desc'], ], ], ['translations.title.fa'], ], 'skill bad percentage' => [ 'skills', [ 'percentage' => 150, 'translations' => [ 'title' => ['fa' => 'ت', 'en' => 't'], 'description' => ['fa' => 'ت', 'en' => 't'], ], ], ['percentage'], ], 'skill bad about_me_id' => [ 'skills', [ 'about_me_id' => 999, 'translations' => [ 'title' => ['fa' => 'ت', 'en' => 't'], 'description' => ['fa' => 'ت', 'en' => 't'], ], ], ['about_me_id'], ], 'testimonial missing fa name' => [ 'testimonials', [ 'translations' => [ 'client_name' => ['en' => 'Name'], 'client_position' => ['fa' => 'پ', 'en' => 'p'], 'feedback' => ['fa' => 'ف', 'en' => 'f'], ], ], ['translations.client_name.fa'], ], ]; } protected function getCreateUri(string $resource): string { return match ($resource) { 'services' => '/api/website/services', 'skills' => '/api/website/skills', 'testimonials' => '/api/website/testimonials', default => '/api/website/services', }; } protected function getUpdateUri(string $resource, $id): string { return match ($resource) { 'services' => "/api/website/services/{$id}", 'skills' => "/api/website/skills/{$id}", 'testimonials' => "/api/website/testimonials/{$id}", default => "/api/website/services/{$id}", }; } protected function getTableForResource(string $resource): string { return match ($resource) { 'services' => 'website_services', 'skills' => 'website_skills', 'testimonials' => 'website_testimonials', default => 'website_services', }; } protected function assertResourceExists(string $resource): void { $this->assertDatabaseCount($this->getTableForResource($resource), 1); } protected function getFirstId(string $resource): int { return match ($resource) { 'services' => Service::first()->id, 'skills' => Skill::first()->id, 'testimonials' => Testimonial::first()->id, default => 1, }; } protected function assertBothLocalesPersisted(string $resource, array $payload): void { if ($resource === 'services') { $service = Service::first(); $this->assertEquals('عنوان', $service->getTranslation('title', 'fa')); $this->assertEquals('Title', $service->getTranslation('title', 'en')); } elseif ($resource === 'skills') { $skill = Skill::first(); $this->assertEquals('مهارت', $skill->getTranslation('title', 'fa')); $this->assertEquals('Skill', $skill->getTranslation('title', 'en')); } elseif ($resource === 'testimonials') { $testimonial = Testimonial::with('translations')->first(); $this->assertNotNull($testimonial); $testimonial->load('translations'); $fa = $testimonial->translations()->where('locale', 'fa')->first(); $en = $testimonial->translations()->where('locale', 'en')->first(); $this->assertEquals('اسم', $fa->client_name); $this->assertEquals('Name', $en->client_name); } } protected function getUpdatePayload(string $resource, string $locale, string $value): array { if ($resource === 'services') { return [ 'translations' => [ 'title' => [$locale => $value], ], ]; } elseif ($resource === 'skills') { return [ 'translations' => [ 'title' => [$locale => $value], ], ]; } elseif ($resource === 'testimonials') { return [ 'translations' => [ 'client_name' => [$locale => $value], ], ]; } return []; } protected function assertLocaleUpdated(string $resource, $id, string $locale, string $value): void { if ($resource === 'services') { $service = Service::find($id); $this->assertEquals($value, $service->getTranslation('title', $locale)); } elseif ($resource === 'skills') { $skill = Skill::find($id); $this->assertEquals($value, $skill->getTranslation('title', $locale)); } elseif ($resource === 'testimonials') { $testimonial = Testimonial::with('translations')->find($id); if (! $testimonial->relationLoaded('translations')) { $testimonial->load('translations'); } $trans = $testimonial->translations()->where('locale', $locale)->first(); $this->assertEquals($value, $trans->client_name); } } protected function assertOtherLocaleUntouched(string $resource, $id, string $locale, array $payload): void { if ($resource === 'services') { $service = Service::find($id); $expected = $payload['translations']['title'][$locale]; $this->assertEquals($expected, $service->getTranslation('title', $locale)); } elseif ($resource === 'skills') { $skill = Skill::find($id); $expected = $payload['translations']['title'][$locale]; $this->assertEquals($expected, $skill->getTranslation('title', $locale)); } elseif ($resource === 'testimonials') { $testimonial = Testimonial::with('translations')->find($id); if (! $testimonial->relationLoaded('translations')) { $testimonial->load('translations'); } $trans = $testimonial->translations()->where('locale', $locale)->first(); $expected = $payload['translations']['client_name'][$locale]; $this->assertEquals($expected, $trans->client_name); } } protected function assertTranslationsGone(string $resource, $id): void { if ($resource === 'testimonials') { $this->assertDatabaseMissing('website_testimonial_translations', ['testimonial_id' => $id]); } } }