Skip to content

Commit 38a76ee

Browse files
committed
test(select): cover options persistence and serialization in FieldDefinitionServiceTest
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
1 parent 44f3379 commit 38a76ee

1 file changed

Lines changed: 64 additions & 0 deletions

File tree

tests/php/Unit/Service/FieldDefinitionServiceTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,70 @@ public function testUpdateRejectsTypeChangeWhenValuesExist(): void {
149149
]);
150150
}
151151

152+
public function testCreateSelectFieldPersistsOptions(): void {
153+
$this->fieldDefinitionMapper
154+
->method('findByFieldKey')
155+
->willReturn(null);
156+
157+
$this->fieldDefinitionMapper
158+
->expects($this->once())
159+
->method('insert')
160+
->with($this->callback(function (FieldDefinition $definition): bool {
161+
$this->assertSame(FieldType::SELECT->value, $definition->getType());
162+
$this->assertSame('["CLT","PJ","Cooperado"]', $definition->getOptions());
163+
return true;
164+
}))
165+
->willReturnCallback(static fn (FieldDefinition $definition): FieldDefinition => $definition);
166+
167+
$this->service->create([
168+
'field_key' => 'contract_type',
169+
'label' => 'Contract Type',
170+
'type' => FieldType::SELECT->value,
171+
'options' => ['CLT', 'PJ', 'Cooperado'],
172+
]);
173+
}
174+
175+
public function testJsonSerializeSelectIncludesOptions(): void {
176+
$definition = new FieldDefinition();
177+
$definition->setId(1);
178+
$definition->setFieldKey('contract_type');
179+
$definition->setLabel('Contract Type');
180+
$definition->setType(FieldType::SELECT->value);
181+
$definition->setAdminOnly(false);
182+
$definition->setUserEditable(true);
183+
$definition->setUserVisible(true);
184+
$definition->setInitialVisibility('private');
185+
$definition->setSortOrder(0);
186+
$definition->setActive(true);
187+
$definition->setOptions('["CLT","PJ"]');
188+
$definition->setCreatedAt(new \DateTime('2026-01-01T00:00:00+00:00'));
189+
$definition->setUpdatedAt(new \DateTime('2026-01-01T00:00:00+00:00'));
190+
191+
$serialized = $definition->jsonSerialize();
192+
193+
$this->assertSame(['CLT', 'PJ'], $serialized['options']);
194+
}
195+
196+
public function testJsonSerializeTextHasNullOptions(): void {
197+
$definition = new FieldDefinition();
198+
$definition->setId(1);
199+
$definition->setFieldKey('cpf');
200+
$definition->setLabel('CPF');
201+
$definition->setType(FieldType::TEXT->value);
202+
$definition->setAdminOnly(false);
203+
$definition->setUserEditable(false);
204+
$definition->setUserVisible(false);
205+
$definition->setInitialVisibility('private');
206+
$definition->setSortOrder(0);
207+
$definition->setActive(true);
208+
$definition->setCreatedAt(new \DateTime('2026-01-01T00:00:00+00:00'));
209+
$definition->setUpdatedAt(new \DateTime('2026-01-01T00:00:00+00:00'));
210+
211+
$serialized = $definition->jsonSerialize();
212+
213+
$this->assertNull($serialized['options']);
214+
}
215+
152216
public function testUpdatePreservesImportedUpdatedAt(): void {
153217
$existing = new FieldDefinition();
154218
$existing->setId(7);

0 commit comments

Comments
 (0)