/** @test */ public function should_convert_segment_to_string() { $segment = new Segment('XXX'); $segment->var = 'value'; $segmentMapping = new SegmentMapping('XXX'); $segmentMapping->addDataElement(1, new DataElementMapping(2345, true, DataElementType::A, 'var')); $message = new Message(); $message->setReferenceNumber(34834); $message->setIdentifier(["type" => 'TEST', "version" => 'S', "release" => '404', "controllingAgency" => 'PG']); $message->addSegment($segment); // $messageMapping = new MessageMapping(); // $messageMapping->setDefaults(["0065" => 'TEST', "0052" => 'S', "0054" => '404', "0051" => 'PG']); // $messageMapping->addSegment(new MessageSegmentMapping('UNH', 1, true)); // $messageMapping->addSegment(new MessageSegmentMapping('XXX', 99, true)); // $messageMapping->addSegment(new MessageSegmentMapping('UNT', 1, true)); $interchange = new Interchange(); $interchange->setSyntax(["name" => 'UNOC', "version" => 3]); $interchange->setSender(['id' => 'zenon']); $interchange->setRecipient(['id' => 'stefan']); $interchange->setTime(['date' => '20150101', 'time' => '1034']); $interchange->setControlReference('17'); $interchange->setMessages([$message]); $mappingLoader = \Phake::mock(MappingLoader::class); \Phake::when($mappingLoader)->loadSegments(\Phake::anyParameters())->thenReturn(['XXX' => $segmentMapping]); $encoder = new Encoder(new AnnotationPrinter(new AnnotationReader()), new SegmentPrinter(), $mappingLoader); $result = $encoder->encode($interchange); $this->assertEquals("UNB+UNOC:3+zenon+stefan+20150101:1034+17'UNH+34834+TEST:S:404:PG'XXX+value'UNT+1+34834'UNZ+1+17'", $result); }
/** @test */ public function should_ignore_missing_nonrequired_fields() { $segmentMapping = new SegmentMapping('XXX'); $segmentMapping->addDataElement(1, new DataElementMapping(7583, true, DataElementType::ID, 'ajdi', '', 3)); $segmentMapping->addDataElement(2, new DataElementMapping(3415, false, DataElementType::A, 'field', '', 35)); $populator = new SegmentPopulator(); $populator->setSegmentConfig(['XXX' => $segmentMapping]); $data = [['XXX', '345']]; $segment = $populator->populate($data); $this->assertEquals(345, $segment->ajdi); $this->assertNull($segment->field); }
/** * @return SegmentPrinter */ protected function givenPrinter() { $segmentMapping = new SegmentMapping('XXX'); $segmentMapping->addDataElement(1, new DataElementMapping(5434, true, DataElementType::ID, 'ajdi')); $segmentMapping->addDataElement(2, new DataElementMapping(4525, true, DataElementType::A, 'fild')); $compDataElement = new CompositeDataElementMapping(2435, true, DataElementType::COMPOSITE, 'araj'); $compDataElement->addDataElement(new DataElementMapping(6456, true, DataElementType::A, 'e1')); $compDataElement->addDataElement(new DataElementMapping(3567, true, DataElementType::A, 'e2')); $segmentMapping->addDataElement(3, $compDataElement); $segmentYMapping = new SegmentMapping('YYY'); $segmentYMapping->addDataElement(1, new DataElementMapping(5434, true, DataElementType::ID, 'ajdi')); $printer = new SegmentPrinter(); $printer->setSegmentMappings(['XXX' => $segmentMapping, 'YYY' => $segmentYMapping]); return $printer; }