From 46b1399c3ca4f53ca8a3a6dc43d12abac7cef7af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Tue, 16 Dec 2025 14:07:38 +0100 Subject: [PATCH] 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). --- .../Import/ImportMotivesFromDirectory.php | 9 +++++++++ .../Import/ImportMotivesFromDirectoryTest.php | 18 +++++++++++++----- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/Bundle/ChillTicketBundle/src/Service/Import/ImportMotivesFromDirectory.php b/src/Bundle/ChillTicketBundle/src/Service/Import/ImportMotivesFromDirectory.php index a963d0f5a..3d9bf9d84 100644 --- a/src/Bundle/ChillTicketBundle/src/Service/Import/ImportMotivesFromDirectory.php +++ b/src/Bundle/ChillTicketBundle/src/Service/Import/ImportMotivesFromDirectory.php @@ -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']; diff --git a/src/Bundle/ChillTicketBundle/tests/Service/Import/ImportMotivesFromDirectoryTest.php b/src/Bundle/ChillTicketBundle/tests/Service/Import/ImportMotivesFromDirectoryTest.php index 07e94f2be..ba52d32fc 100644 --- a/src/Bundle/ChillTicketBundle/tests/Service/Import/ImportMotivesFromDirectoryTest.php +++ b/src/Bundle/ChillTicketBundle/tests/Service/Import/ImportMotivesFromDirectoryTest.php @@ -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');