From 970708ea83ea0e642dc01078bb91c2a13716caa4 Mon Sep 17 00:00:00 2001 From: r-dev Date: Fri, 3 Apr 2026 11:01:23 +0200 Subject: [PATCH] test(custom-fields) : add tests for machine context custom fields Co-Authored-By: Claude Opus 4.6 (1M context) --- .../Entity/MachineContextCustomFieldTest.php | 228 ++++++++++++++++++ 1 file changed, 228 insertions(+) create mode 100644 tests/Api/Entity/MachineContextCustomFieldTest.php diff --git a/tests/Api/Entity/MachineContextCustomFieldTest.php b/tests/Api/Entity/MachineContextCustomFieldTest.php new file mode 100644 index 0000000..05bf451 --- /dev/null +++ b/tests/Api/Entity/MachineContextCustomFieldTest.php @@ -0,0 +1,228 @@ +createGestionnaireClient(); + + $site = $this->createSite('Site A'); + $modelType = $this->createModelType('Motor', 'MOT', ModelCategory::COMPONENT); + + $contextField = $this->createCustomField( + name: 'Voltage', + type: 'number', + typeComposant: $modelType, + machineContextOnly: true, + ); + $normalField = $this->createCustomField( + name: 'Serial', + type: 'text', + typeComposant: $modelType, + ); + + $machine = $this->createMachine('Machine A', $site); + $composant = $this->createComposant('Motor 1', 'MOT-001', $modelType); + $link = $this->createMachineComponentLink($machine, $composant); + + $this->createCustomFieldValue( + customField: $contextField, + value: '220', + composant: $composant, + machineComponentLink: $link, + ); + + $response = $client->request('GET', '/api/machines/'.$machine->getId().'/structure'); + $this->assertResponseIsSuccessful(); + + $data = $response->toArray(); + $componentLink = $data['componentLinks'][0]; + + $this->assertArrayHasKey('contextCustomFields', $componentLink); + $this->assertCount(1, $componentLink['contextCustomFields']); + $this->assertSame('Voltage', $componentLink['contextCustomFields'][0]['name']); + $this->assertTrue($componentLink['contextCustomFields'][0]['machineContextOnly']); + + $this->assertArrayHasKey('contextCustomFieldValues', $componentLink); + $this->assertCount(1, $componentLink['contextCustomFieldValues']); + $this->assertSame('220', $componentLink['contextCustomFieldValues'][0]['value']); + + $normalFields = array_filter( + $componentLink['composant']['customFields'], + fn (array $f) => $f['name'] === 'Serial', + ); + $this->assertCount(1, $normalFields); + } + + public function testStructureReturnsContextFieldsOnPieceLink(): void + { + $client = $this->createGestionnaireClient(); + + $site = $this->createSite('Site B'); + $modelType = $this->createModelType('Bearing', 'BRG', ModelCategory::PIECE); + $contextField = $this->createCustomField( + name: 'Wear Level', + type: 'select', + typePiece: $modelType, + machineContextOnly: true, + ); + $contextField->setOptions(['Good', 'Fair', 'Replace']); + $this->getEntityManager()->flush(); + + $machine = $this->createMachine('Machine B', $site); + $piece = $this->createPiece('Bearing 1', 'BRG-001', $modelType); + $link = $this->createMachinePieceLink($machine, $piece); + + $this->createCustomFieldValue( + customField: $contextField, + value: 'Fair', + piece: $piece, + machinePieceLink: $link, + ); + + $response = $client->request('GET', '/api/machines/'.$machine->getId().'/structure'); + $data = $response->toArray(); + + $pieceLink = $data['pieceLinks'][0]; + $this->assertCount(1, $pieceLink['contextCustomFields']); + $this->assertSame('Wear Level', $pieceLink['contextCustomFields'][0]['name']); + $this->assertCount(1, $pieceLink['contextCustomFieldValues']); + $this->assertSame('Fair', $pieceLink['contextCustomFieldValues'][0]['value']); + } + + public function testUpsertContextFieldValueViaComponentLink(): void + { + $client = $this->createGestionnaireClient(); + + $site = $this->createSite('Site C'); + $modelType = $this->createModelType('Pump', 'PMP', ModelCategory::COMPONENT); + $contextField = $this->createCustomField( + name: 'Flow Rate', + type: 'number', + typeComposant: $modelType, + machineContextOnly: true, + ); + + $machine = $this->createMachine('Machine C', $site); + $composant = $this->createComposant('Pump 1', 'PMP-001', $modelType); + $link = $this->createMachineComponentLink($machine, $composant); + + $response = $client->request('POST', '/api/custom-fields/values/upsert', [ + 'json' => [ + 'customFieldId' => $contextField->getId(), + 'machineComponentLinkId' => $link->getId(), + 'value' => '380', + ], + ]); + + $this->assertResponseIsSuccessful(); + $data = $response->toArray(); + $this->assertSame('380', $data['value']); + } + + public function testSameComposantDifferentValuesPerMachine(): void + { + $client = $this->createGestionnaireClient(); + + $site = $this->createSite('Site D'); + $modelType = $this->createModelType('Valve', 'VLV', ModelCategory::COMPONENT); + $contextField = $this->createCustomField( + name: 'Pressure', + type: 'number', + typeComposant: $modelType, + machineContextOnly: true, + ); + + $machineA = $this->createMachine('Machine A', $site); + $machineB = $this->createMachine('Machine B', $site); + $composant = $this->createComposant('Valve 1', 'VLV-001', $modelType); + + $linkA = $this->createMachineComponentLink($machineA, $composant); + $linkB = $this->createMachineComponentLink($machineB, $composant); + + $this->createCustomFieldValue( + customField: $contextField, + value: '100', + composant: $composant, + machineComponentLink: $linkA, + ); + $this->createCustomFieldValue( + customField: $contextField, + value: '200', + composant: $composant, + machineComponentLink: $linkB, + ); + + $dataA = $client->request('GET', '/api/machines/'.$machineA->getId().'/structure')->toArray(); + $this->assertSame('100', $dataA['componentLinks'][0]['contextCustomFieldValues'][0]['value']); + + $dataB = $client->request('GET', '/api/machines/'.$machineB->getId().'/structure')->toArray(); + $this->assertSame('200', $dataB['componentLinks'][0]['contextCustomFieldValues'][0]['value']); + } + + public function testMachineContextOnlyFieldSerialization(): void + { + $client = $this->createGestionnaireClient(); + + $site = $this->createSite('Site E'); + $modelType = $this->createModelType('Sensor', 'SNS', ModelCategory::COMPONENT); + $contextField = $this->createCustomField( + name: 'Calibration Date', + type: 'date', + typeComposant: $modelType, + machineContextOnly: true, + ); + + $response = $client->request('GET', '/api/custom_fields/'.$contextField->getId()); + $this->assertResponseIsSuccessful(); + $data = $response->toArray(); + $this->assertTrue($data['machineContextOnly']); + } + + public function testCloneMachineCopiesContextFieldValues(): void + { + $client = $this->createGestionnaireClient(); + + $site = $this->createSite('Site F'); + $modelType = $this->createModelType('Motor Clone', 'MOTC', ModelCategory::COMPONENT); + $contextField = $this->createCustomField( + name: 'RPM Setting', + type: 'number', + typeComposant: $modelType, + machineContextOnly: true, + ); + + $source = $this->createMachine('Source Machine', $site); + $composant = $this->createComposant('Motor C', 'MOTC-001', $modelType); + $link = $this->createMachineComponentLink($source, $composant); + + $this->createCustomFieldValue( + customField: $contextField, + value: '3000', + composant: $composant, + machineComponentLink: $link, + ); + + $response = $client->request('POST', '/api/machines/'.$source->getId().'/clone', [ + 'json' => [ + 'name' => 'Cloned Machine', + 'siteId' => $site->getId(), + ], + ]); + + $this->assertResponseStatusCodeSame(201); + $data = $response->toArray(); + + $clonedLink = $data['componentLinks'][0] ?? null; + $this->assertNotNull($clonedLink); + $this->assertCount(1, $clonedLink['contextCustomFieldValues']); + $this->assertSame('3000', $clonedLink['contextCustomFieldValues'][0]['value']); + } +}