create(); $channel = PlatformChannel::factory()->create(); $route = Route::factory()->create([ 'feed_id' => $feed->id, 'platform_channel_id' => $channel->id, 'is_active' => true, 'priority' => 1, ]); $request = Request::create('/test'); $resource = new RouteResource($route); $result = $resource->toArray($request); $this->assertIsArray($result); $this->assertArrayHasKey('id', $result); $this->assertArrayHasKey('feed_id', $result); $this->assertArrayHasKey('platform_channel_id', $result); $this->assertArrayHasKey('is_active', $result); $this->assertArrayHasKey('priority', $result); $this->assertArrayHasKey('created_at', $result); $this->assertArrayHasKey('updated_at', $result); $this->assertArrayHasKey('feed', $result); $this->assertArrayHasKey('platform_channel', $result); $this->assertArrayHasKey('keywords', $result); $this->assertEquals($route->id, $result['id']); $this->assertEquals($route->feed_id, $result['feed_id']); $this->assertEquals($route->platform_channel_id, $result['platform_channel_id']); $this->assertTrue($result['is_active']); $this->assertEquals(1, $result['priority']); } public function test_to_array_formats_dates_as_iso_strings(): void { $feed = Feed::factory()->create(); $channel = PlatformChannel::factory()->create(); $route = Route::factory()->create([ 'feed_id' => $feed->id, 'platform_channel_id' => $channel->id, ]); $request = Request::create('/test'); $resource = new RouteResource($route); $result = $resource->toArray($request); // Check that dates are formatted as ISO strings $this->assertStringContainsString('T', $result['created_at']); $this->assertStringContainsString('Z', $result['created_at']); $this->assertStringContainsString('T', $result['updated_at']); $this->assertStringContainsString('Z', $result['updated_at']); } public function test_to_array_includes_loaded_feed_relationship(): void { $feed = Feed::factory()->create(['name' => 'Test Feed']); $channel = PlatformChannel::factory()->create(); $route = Route::factory()->create([ 'feed_id' => $feed->id, 'platform_channel_id' => $channel->id, ]); // Load the relationship $route->load('feed'); $request = Request::create('/test'); $resource = new RouteResource($route); $result = $resource->toArray($request); $this->assertArrayHasKey('feed', $result); // Feed relationship returns a FeedResource object when loaded $this->assertInstanceOf(\Domains\Feed\Resources\FeedResource::class, $result['feed']); } public function test_to_array_includes_loaded_platform_channel_relationship(): void { $feed = Feed::factory()->create(); $channel = PlatformChannel::factory()->create(['name' => 'Test Channel']); $route = Route::factory()->create([ 'feed_id' => $feed->id, 'platform_channel_id' => $channel->id, ]); // Load the relationship $route->load('platformChannel'); $request = Request::create('/test'); $resource = new RouteResource($route); $result = $resource->toArray($request); $this->assertArrayHasKey('platform_channel', $result); // Platform channel relationship returns a PlatformChannelResource object when loaded $this->assertInstanceOf(\Domains\Platform\Resources\PlatformChannelResource::class, $result['platform_channel']); } public function test_to_array_includes_keywords_field(): void { $feed = Feed::factory()->create(); $channel = PlatformChannel::factory()->create(); $route = Route::factory()->create([ 'feed_id' => $feed->id, 'platform_channel_id' => $channel->id, ]); // Load the relationship (even if empty) $route->load('keywords'); $request = Request::create('/test'); $resource = new RouteResource($route); $result = $resource->toArray($request); // Verify that keywords field exists and has proper structure $this->assertArrayHasKey('keywords', $result); // The keywords field should be present, whether empty or not $this->assertTrue(is_array($result['keywords']) || $result['keywords'] instanceof \Illuminate\Support\Collection); // Convert to array if it's a Collection $keywords = $result['keywords']; if ($keywords instanceof \Illuminate\Support\Collection) { $keywords = $keywords->toArray(); } // Should be an array (could be empty) $this->assertIsArray($keywords); } public function test_to_array_handles_empty_keywords(): void { $feed = Feed::factory()->create(); $channel = PlatformChannel::factory()->create(); $route = Route::factory()->create([ 'feed_id' => $feed->id, 'platform_channel_id' => $channel->id, ]); // Load the relationship (but no keywords attached) $route->load('keywords'); $request = Request::create('/test'); $resource = new RouteResource($route); $result = $resource->toArray($request); $this->assertArrayHasKey('keywords', $result); // Empty keywords relationship should return empty array or empty Collection $this->assertTrue(is_array($result['keywords']) || $result['keywords'] instanceof \Illuminate\Support\Collection); // Convert to array if it's a Collection $keywords = $result['keywords']; if ($keywords instanceof \Illuminate\Support\Collection) { $keywords = $keywords->toArray(); } $this->assertCount(0, $keywords); } public function test_resource_can_be_created_and_converted_to_json(): void { $feed = Feed::factory()->create(); $channel = PlatformChannel::factory()->create(); $route = Route::factory()->create([ 'feed_id' => $feed->id, 'platform_channel_id' => $channel->id, ]); $resource = new RouteResource($route); // Test that the resource can be serialized to JSON $json = $resource->toJson(); $this->assertIsString($json); $this->assertJson($json); $decoded = json_decode($json, true); $this->assertArrayHasKey('id', $decoded); $this->assertArrayHasKey('feed_id', $decoded); $this->assertArrayHasKey('platform_channel_id', $decoded); } }