Skip to content

Commit

Permalink
Merge pull request #13 from Chemaclass/6-end-message-UNT
Browse files Browse the repository at this point in the history
#6 End message when UNT segment is found
  • Loading branch information
Chemaclass committed Jun 18, 2020
2 parents 581f90d + 4e87cdc commit 0da3473
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 7 deletions.
31 changes: 24 additions & 7 deletions src/TransactionMessage.php
Expand Up @@ -6,6 +6,7 @@

use EdifactParser\Segments\SegmentInterface;
use EdifactParser\Segments\UNHMessageHeader;
use EdifactParser\Segments\UNTMessageFooter;

/** @psalm-immutable */
final class TransactionMessage
Expand All @@ -14,8 +15,8 @@ final class TransactionMessage
private array $groupedSegments;

/**
* A message starts with the "UNHMessageHeader" segment until another
* "UNHMessageHeader" segment appears, then starts another segment and so on.
* A transaction message starts with the "UNHMessageHeader" segment and finalizes with
* the "UNTMessageFooter" segment, this process is repeated for each pair of segments.
*
* @psalm-pure
* @psalm-return list<TransactionMessage>
Expand All @@ -26,17 +27,21 @@ public static function groupSegmentsByMessage(SegmentInterface...$segments): arr
$groupedSegments = [];

foreach ($segments as $segment) {
if ($segment instanceof UNHMessageHeader && !empty($groupedSegments)) {
$messages[] = self::groupSegmentsByName(...$groupedSegments);
if ($segment instanceof UNHMessageHeader) {
$groupedSegments = [];
}

$groupedSegments[] = $segment;
}

$messages[] = self::groupSegmentsByName(...$groupedSegments);
if ($segment instanceof UNTMessageFooter
&& self::groupedSegmentsNotEmpty($groupedSegments)
) {
$messages[] = self::groupSegmentsByName(...$groupedSegments);
}
}

return array_values(
array_filter($messages, function (self $m) {
array_filter($messages, static function (self $m) {
return !empty($m->segmentByName(UNHMessageHeader::class));
})
);
Expand All @@ -53,6 +58,18 @@ public function segmentByName(string $name): array
return $this->groupedSegments[$name] ?? [];
}

/**
* We add automatically all items to the $groupedSegments array in the loop,
* one message is made of "UNHMessageHeader" and "UNTMessageFooter" segments.
* So the minimum messages are two, only one segment is not possible.
*
* @psalm-pure
*/
private static function groupedSegmentsNotEmpty(array $groupedSegments): bool
{
return count($groupedSegments) > 1;
}

/** @psalm-pure */
private static function groupSegmentsByName(SegmentInterface...$segments): self
{
Expand Down
75 changes: 75 additions & 0 deletions tests/Unit/TransactionMessageTest.php
Expand Up @@ -89,6 +89,81 @@ public function oneMessageWithMultipleSegmentsWithTheSameName(): void
], $this->transactionMessages($fileContent));
}

/** @test */
public function oneMessageIsCreatedWhenStartWithUNHAndEndsWithUNT(): void
{
$fileContent = <<<EDI
UNA:+.? '
UNH+1+anything'
CNT+7:0.1:KGM'
UNT+19+1'
IGN+ORE:ME'
UNH+2+anything'
UNT+19+2'
UNZ+2+3'
EDI;
self::assertEquals([
new TransactionMessage([
UNHMessageHeader::class => [
'1' => new UNHMessageHeader(['UNH', '1', 'anything']),
],
CNTControl::class => [
'7' => new CNTControl(['CNT', ['7', '0.1', 'KGM']]),
],
UNTMessageFooter::class => [
'19' => new UNTMessageFooter(['UNT', '19', '1']),
],
]),
new TransactionMessage([
UNHMessageHeader::class => [
'2' => new UNHMessageHeader(['UNH', '2', 'anything']),
],
UNTMessageFooter::class => [
'19' => new UNTMessageFooter(['UNT', '19', '2']),
],
]),
], $this->transactionMessages($fileContent));
}

/** @test */
public function previousUNHAreOverriddenIfTheyDoesntHaveUNT(): void
{
$fileContent = <<<EDI
UNA:+.? '
UNH+1+anything'
UNH+2+anything'
CNT+5:0.1:KGM'
UNT+10+2'
UNH+3+anything'
UNZ+2+3'
EDI;
self::assertEquals([
new TransactionMessage([
UNHMessageHeader::class => [
'2' => new UNHMessageHeader(['UNH', '2', 'anything']),
],
CNTControl::class => [
'5' => new CNTControl(['CNT', ['5', '0.1', 'KGM']]),
],
UNTMessageFooter::class => [
'10' => new UNTMessageFooter(['UNT', '10', '2']),
],
]),
], $this->transactionMessages($fileContent));
}

/** @test */
public function messageNotCreatedIfUNTDoesntHaveUNHOrViceVersa(): void
{
$fileContent = <<<EDI
UNA:+.? '
UNT+10+2'
UNH+3+anything'
UNZ+2+3'
EDI;
self::assertEquals([], $this->transactionMessages($fileContent));
}

private function transactionMessages(string $fileContent): array
{
return TransactionMessage::groupSegmentsByMessage(
Expand Down

0 comments on commit 0da3473

Please sign in to comment.