create(); $schema = FormSchema::factory()->create(['organisation_id' => $org->id]); $library = FormFieldLibrary::factory()->create([ 'organisation_id' => $org->id, 'usage_count' => 0, ]); FormFieldBinding::factory()->forLibrary($library)->entityOwned('person', 'email')->create(); FormFieldBinding::factory()->forLibrary($library)->mirrored('user_profile', 'bio')->create(); $service = $this->app->make(FormFieldService::class); $field = $service->insertFromLibrary($schema, $library); $copied = FormFieldBinding::query() ->withoutGlobalScopes() ->where('owner_type', 'form_field') ->where('owner_id', $field->id) ->orderBy('target_attribute') ->get(); $this->assertCount(2, $copied); $this->assertSame(['bio', 'email'], $copied->pluck('target_attribute')->all()); $this->assertSame(FormFieldBindingMode::Mirrored, $copied->firstWhere('target_attribute', 'bio')->mode); $this->assertSame(FormFieldBindingMode::EntityOwned, $copied->firstWhere('target_attribute', 'email')->mode); $this->assertSame(1, (int) $library->fresh()->usage_count); } public function test_insert_from_library_without_bindings_creates_field_without_bindings(): void { $org = Organisation::factory()->create(); $schema = FormSchema::factory()->create(['organisation_id' => $org->id]); $library = FormFieldLibrary::factory()->create(['organisation_id' => $org->id]); $service = $this->app->make(FormFieldService::class); $field = $service->insertFromLibrary($schema, $library); $this->assertSame(0, FormFieldBinding::query() ->withoutGlobalScopes() ->where('owner_type', 'form_field') ->where('owner_id', $field->id) ->count(), ); } }