Skip to content

Commit

Permalink
Added test exceptionWhenTagTooLarge and cleaning segmentFromArray()
Browse files Browse the repository at this point in the history
  • Loading branch information
Chemaclass committed Jun 5, 2020
1 parent 9a2f5cc commit 07273ec
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/Segments/SegmentFactory.php
Expand Up @@ -12,6 +12,7 @@
/** @psalm-immutable */
final class SegmentFactory implements SegmentFactoryInterface
{
/** @var array<string,string> */
public const DEFAULT_SEGMENTS = [
'UNH' => UNHMessageHeader::class,
'DTM' => DTMDateTimePeriod::class,
Expand Down Expand Up @@ -71,14 +72,14 @@ public function segmentFromArray(array $rawArray): SegmentInterface
Assert::length($tag, self::TAG_LENGTH);
$className = $this->segments[$tag] ?? '';

if (!empty($className)) {
$segment = new $className($rawArray);
Assert::isInstanceOf($segment, SegmentInterface::class);

return $segment;
if (empty($className)) {
return new UnknownSegment($rawArray);
}

return new UnknownSegment($rawArray);
$segment = new $className($rawArray);
Assert::isInstanceOf($segment, SegmentInterface::class);

return $segment;
}

private function classImplements(string $className, string $interface): bool
Expand Down
7 changes: 7 additions & 0 deletions tests/Unit/Segments/SegmentFactoryTest.php
Expand Up @@ -46,6 +46,13 @@ public function withCustomSegments(): void
self::assertInstanceOf(UnknownSegment::class, $factory->segmentFromArray(['DTM']));
}

/** @test */
public function exceptionWhenTagTooLarge(): void
{
$this->expectException(InvalidArgumentException::class);
SegmentFactory::withSegments(['TAG_TOO_LARGE' => UNHMessageHeader::class]);
}

/** @test */
public function exceptionWhenCreatingNonValidTag(): void
{
Expand Down

0 comments on commit 07273ec

Please sign in to comment.