create(); PlatformChannel::factory()->count(3)->create(['platform_instance_id' => $instance->id]); $response = $this->getJson('/api/v1/platform-channels'); $response->assertStatus(200) ->assertJsonStructure([ 'success', 'message', 'data' => [ '*' => [ 'id', 'platform_instance_id', 'channel_id', 'name', 'display_name', 'description', 'is_active', 'created_at', 'updated_at', 'platform_instance' ] ] ]) ->assertJson([ 'success' => true, 'message' => 'Platform channels retrieved successfully.' ]); } public function test_store_creates_platform_channel_successfully(): void { $instance = PlatformInstance::factory()->create(); // Create a platform account for this instance first PlatformAccount::factory()->create([ 'instance_url' => $instance->url, 'is_active' => true, 'settings' => ['api_token' => 'test-token'] ]); // Create a language for the test $language = Language::factory()->create([ 'short_code' => 'en', 'name' => 'English', 'is_active' => true ]); // Mock the ChannelLanguageDetectionService $mockService = Mockery::mock(ChannelLanguageDetectionService::class); $mockService->shouldReceive('detectChannelLanguages') ->with('Test Channel', $instance->id) ->once() ->andReturn([ 'language_id' => $language->id, 'matched_languages' => [$language->id], 'community_details' => ['community' => ['id' => 123]] ]); $this->app->bind(ChannelLanguageDetectionService::class, function () use ($mockService) { return $mockService; }); $data = [ 'platform_instance_id' => $instance->id, 'channel_id' => 'test_channel', 'name' => 'Test Channel', 'display_name' => 'Test Channel Display', 'description' => 'A test channel', 'is_active' => true ]; $response = $this->postJson('/api/v1/platform-channels', $data); $response->assertStatus(201) ->assertJsonStructure([ 'success', 'message', 'data' => [ 'id', 'platform_instance_id', 'channel_id', 'name', 'display_name', 'description', 'is_active', 'created_at', 'updated_at', ] ]) ->assertJson([ 'success' => true, 'message' => 'Platform channel created successfully and linked to platform account!' ]); $this->assertDatabaseHas('platform_channels', [ 'platform_instance_id' => $instance->id, 'channel_id' => 'test_channel', 'name' => 'Test Channel', ]); } public function test_store_validates_required_fields(): void { $response = $this->postJson('/api/v1/platform-channels', []); $response->assertStatus(422) ->assertJsonValidationErrors(['platform_instance_id', 'channel_id', 'name']); } public function test_store_validates_platform_instance_exists(): void { $data = [ 'platform_instance_id' => 999, 'channel_id' => 'test_channel', 'name' => 'Test Channel' ]; $response = $this->postJson('/api/v1/platform-channels', $data); $response->assertStatus(422) ->assertJsonValidationErrors(['platform_instance_id']); } public function test_show_returns_platform_channel_successfully(): void { $instance = PlatformInstance::factory()->create(); $channel = PlatformChannel::factory()->create(['platform_instance_id' => $instance->id]); $response = $this->getJson("/api/v1/platform-channels/{$channel->id}"); $response->assertStatus(200) ->assertJsonStructure([ 'success', 'message', 'data' => [ 'id', 'platform_instance_id', 'channel_id', 'name', 'display_name', 'description', 'is_active', 'created_at', 'updated_at', 'platform_instance' ] ]) ->assertJson([ 'success' => true, 'message' => 'Platform channel retrieved successfully.', 'data' => [ 'id' => $channel->id, 'name' => $channel->name, ] ]); } public function test_update_modifies_platform_channel_successfully(): void { $instance = PlatformInstance::factory()->create(); $channel = PlatformChannel::factory()->create(['platform_instance_id' => $instance->id]); $updateData = [ 'name' => 'Updated Channel', 'display_name' => 'Updated Display Name', 'description' => 'Updated description', 'is_active' => false ]; $response = $this->putJson("/api/v1/platform-channels/{$channel->id}", $updateData); $response->assertStatus(200) ->assertJson([ 'success' => true, 'message' => 'Platform channel updated successfully!' ]); $this->assertDatabaseHas('platform_channels', [ 'id' => $channel->id, 'name' => 'Updated Channel', 'display_name' => 'Updated Display Name', 'is_active' => false, ]); } public function test_destroy_deletes_platform_channel_successfully(): void { $instance = PlatformInstance::factory()->create(); $channel = PlatformChannel::factory()->create(['platform_instance_id' => $instance->id]); $response = $this->deleteJson("/api/v1/platform-channels/{$channel->id}"); $response->assertStatus(200) ->assertJson([ 'success' => true, 'message' => 'Platform channel deleted successfully!' ]); $this->assertDatabaseMissing('platform_channels', [ 'id' => $channel->id ]); } public function test_toggle_activates_inactive_channel(): void { $instance = PlatformInstance::factory()->create(); $channel = PlatformChannel::factory()->create([ 'platform_instance_id' => $instance->id, 'is_active' => false ]); $response = $this->postJson("/api/v1/platform-channels/{$channel->id}/toggle"); $response->assertStatus(200) ->assertJson([ 'success' => true, 'message' => 'Platform channel activated successfully!' ]); $this->assertDatabaseHas('platform_channels', [ 'id' => $channel->id, 'is_active' => true ]); } public function test_toggle_deactivates_active_channel(): void { $instance = PlatformInstance::factory()->create(); $channel = PlatformChannel::factory()->create([ 'platform_instance_id' => $instance->id, 'is_active' => true ]); $response = $this->postJson("/api/v1/platform-channels/{$channel->id}/toggle"); $response->assertStatus(200) ->assertJson([ 'success' => true, 'message' => 'Platform channel deactivated successfully!' ]); $this->assertDatabaseHas('platform_channels', [ 'id' => $channel->id, 'is_active' => false ]); } }