Add support for "ordering" field in motive import.

- Extended motive import logic to parse and set the "ordering" field from YAML.
- Ensured invalid "ordering" values throw a runtime exception.
- Updated test cases to verify "ordering" is correctly set for child and standalone motives.
- Verified parent motives retain default "ordering" value (0).
This commit is contained in:
2025-12-16 14:07:38 +01:00
parent 022c0aaebf
commit 46b1399c3c
2 changed files with 22 additions and 5 deletions

View File

@@ -108,6 +108,15 @@ final readonly class ImportMotivesFromDirectory
$motive->setParent($parent);
}
// ordering: applies only to the current (child/standalone) motive when provided
if (array_key_exists('ordering', $item)) {
$ordering = $item['ordering'];
if (!\is_int($ordering) && !\is_float($ordering) && !(\is_string($ordering) && is_numeric($ordering))) {
throw new \RuntimeException(sprintf('Item %d: invalid "ordering" value (must be numeric).', $index));
}
$motive->setOrdering((float) $ordering);
}
// urgent: if true => YES, if explicitly false => NO, if absent => leave null
if (array_key_exists('urgent', $item)) {
$urgent = (bool) $item['urgent'];

View File

@@ -43,6 +43,7 @@ class ImportMotivesFromDirectoryTest extends TestCase
$yaml = <<<'YAML'
- label:
fr: "Test Motive"
ordering: 12.5
urgent: true
supplementary_informations:
- label:
@@ -58,9 +59,12 @@ YAML;
$emProphecy = $this->prophesize(EntityManagerInterface::class);
// persist should be called for StoredObject and Motive at least once
$emProphecy->persist(Argument::type(StoredObject::class))->shouldBeCalled();
$emProphecy->persist(Argument::that(fn ($arg) =>
// Motive class is in another namespace; just check it's an object with setLabel method
\is_object($arg) && method_exists($arg, 'setLabel')))->shouldBeCalled();
$capturedMotive = null;
$emProphecy->persist(Argument::type(Motive::class))
->will(function ($args) use (&$capturedMotive) {
$capturedMotive = $args[0];
})
->shouldBeCalled();
$emProphecy->flush()->shouldBeCalled();
$somMock = $this->createMock(StoredObjectManagerInterface::class);
@@ -90,8 +94,9 @@ YAML;
@unlink($tmpBase.DIRECTORY_SEPARATOR.'motives.yaml');
@rmdir($tmpBase);
// If we reached here, it's a successful smoke test
$this->assertTrue(true);
// Verify that ordering has been properly set on the created motive
$this->assertInstanceOf(Motive::class, $capturedMotive);
$this->assertSame(12.5, $capturedMotive->getOrdering());
}
public function testImportWithParentCreatesAndLinks(): void
@@ -103,6 +108,7 @@ YAML;
$yaml = <<<'YAML'
- label:
fr: " Parent > Child "
ordering: 7
YAML;
file_put_contents($tmpBase.DIRECTORY_SEPARATOR.'motives.yaml', $yaml);
@@ -155,6 +161,8 @@ YAML;
$this->assertInstanceOf(Motive::class, $parent, 'Parent motive must be created');
$this->assertSame($parent, $child->getParent(), 'Child must reference the created parent');
$this->assertTrue($parent->isParent(), 'Parent must have at least one child');
$this->assertSame(7.0, $child->getOrdering(), 'Child ordering must be set from YAML');
$this->assertSame(0.0, $parent->getOrdering(), 'Parent ordering must remain untouched (default)');
// 6) Cleanup
@unlink($tmpBase.DIRECTORY_SEPARATOR.'motives.yaml');