diff --git a/app/Http/Resources/PolicyResource.php b/app/Http/Resources/PolicyResource.php index 40b5fa8c..f5e030fa 100644 --- a/app/Http/Resources/PolicyResource.php +++ b/app/Http/Resources/PolicyResource.php @@ -19,7 +19,9 @@ public function toArray(Request $request): array { 'metadata' => [ 'policy_id' => $this->id, 'type' => $this->policy_type, - 'active_from' => Carbon::parse($this->active_from)->format('Y-m-d'), + 'active_from' => $this->active_from === null + ? null + : Carbon::parse($this->active_from)->format('Y-m-d'), 'content_vue_file' => $this->content_vue_file, ], ]; diff --git a/tests/Resources/PolicyResourceTest.php b/tests/Resources/PolicyResourceTest.php new file mode 100644 index 00000000..819061df --- /dev/null +++ b/tests/Resources/PolicyResourceTest.php @@ -0,0 +1,59 @@ + 'terms-of-use', + 'content_vue_file' => 'terms-of-use/example.vue', + 'active_from' => '2022-02-02', + ]); + + $resource = new PolicyResource($policy); + $data = $resource->resolve(); + + $this->assertSame( + '2022-02-02', + data_get($data, 'metadata.active_from'), + ); + } + + public function testActiveFromNull(): void { + $policy = Policy::create([ + 'policy_type' => 'terms-of-use', + 'content_vue_file' => 'terms-of-use/example.vue', + ]); + + $resource = new PolicyResource($policy); + $data = $resource->resolve(); + + $this->assertSame( + null, + data_get($data, 'metadata.active_from'), + ); + } + + public function testActiveFromNullExplicit(): void { + $policy = Policy::create([ + 'policy_type' => 'terms-of-use', + 'content_vue_file' => 'terms-of-use/example.vue', + 'active_from' => null, + ]); + + $resource = new PolicyResource($policy); + $data = $resource->resolve(); + + $this->assertSame( + null, + data_get($data, 'metadata.active_from'), + ); + } +} diff --git a/tests/Routes/Policies/PolicyControllerTest.php b/tests/Routes/Policies/PolicyControllerTest.php index a973c769..74858ec5 100644 --- a/tests/Routes/Policies/PolicyControllerTest.php +++ b/tests/Routes/Policies/PolicyControllerTest.php @@ -1,6 +1,6 @@