Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion app/Http/Resources/PolicyResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
];
Expand Down
59 changes: 59 additions & 0 deletions tests/Resources/PolicyResourceTest.php
Comment thread
deer-wmde marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Tests\Resources;

use App\Http\Resources\PolicyResource;
use App\Policy;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class PolicyResourceTest extends TestCase {
use RefreshDatabase;

public function testActiveFrom(): void {
$policy = Policy::create([
'policy_type' => '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'),
);
}
}
2 changes: 1 addition & 1 deletion tests/Routes/Policies/PolicyControllerTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Http\Controllers;
namespace Tests\Routes;

use App\Policy;
use Carbon\CarbonImmutable;
Expand Down
Loading