Skip to content

Commit

Permalink
Merge branch 'apply-psalm-immutable'
Browse files Browse the repository at this point in the history
  • Loading branch information
Chemaclass committed May 31, 2020
2 parents 6a73f4b + a223f81 commit 298debd
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 5 deletions.
13 changes: 13 additions & 0 deletions src/Exception/MissingSubSegmentKey.php
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace EdifactParser\Exception;

final class MissingSubSegmentKey extends \Exception
{
public function __construct(string $missingKey, array $rawValues)
{
parent::__construct("Key '$missingKey' not found in " . json_encode($rawValues));
}
}
6 changes: 4 additions & 2 deletions src/Segments/CNTControl.php
Expand Up @@ -6,6 +6,8 @@

namespace EdifactParser\Segments;

use EdifactParser\Exception\MissingSubSegmentKey;

/** @psalm-immutable */
final class CNTControl implements SegmentInterface
{
Expand All @@ -24,10 +26,10 @@ public function name(): string
public function subSegmentKey(): string
{
if (!isset($this->rawValues[1][0])) {
throw new \Exception('missing sub segment key');
throw new MissingSubSegmentKey('[1][0]', $this->rawValues);
}

return (string) $this->rawValues[1][0];
return (string)$this->rawValues[1][0];
}

public function rawValues(): array
Expand Down
4 changes: 3 additions & 1 deletion src/Segments/DTMDateTimePeriod.php
Expand Up @@ -4,6 +4,8 @@

namespace EdifactParser\Segments;

use EdifactParser\Exception\MissingSubSegmentKey;

/** @psalm-immutable */
final class DTMDateTimePeriod implements SegmentInterface
{
Expand All @@ -22,7 +24,7 @@ public function name(): string
public function subSegmentKey(): string
{
if (!isset($this->rawValues[1][0])) {
throw new \Exception('missing sub segment key');
throw new MissingSubSegmentKey('[1][0]', $this->rawValues);
}

return (string) $this->rawValues[1][0];
Expand Down
4 changes: 3 additions & 1 deletion src/Segments/UNHMessageHeader.php
Expand Up @@ -4,6 +4,8 @@

namespace EdifactParser\Segments;

use EdifactParser\Exception\MissingSubSegmentKey;

/** @psalm-immutable */
final class UNHMessageHeader implements SegmentInterface
{
Expand All @@ -22,7 +24,7 @@ public function name(): string
public function subSegmentKey(): string
{
if (!isset($this->rawValues[1][0])) {
throw new \Exception('missing sub segment key');
throw new MissingSubSegmentKey('[1][0]', $this->rawValues);
}

return (string) $this->rawValues[1][0];
Expand Down
2 changes: 1 addition & 1 deletion src/TransactionMessage.php
Expand Up @@ -26,7 +26,7 @@ public static function groupSegmentsByMessage(SegmentInterface...$segments): arr
$groupedSegments = [];

foreach ($segments as $segment) {
if ($segment instanceof UNHMessageHeader && $groupedSegments) {
if ($segment instanceof UNHMessageHeader && !empty($groupedSegments)) {
$messages[] = self::groupSegmentsByName(...$groupedSegments);
$groupedSegments = [];
}
Expand Down
9 changes: 9 additions & 0 deletions tests/Unit/Segments/CNTControlTest.php
Expand Up @@ -4,6 +4,7 @@

namespace EdifactParser\Tests\Unit\Segments;

use EdifactParser\Exception\MissingSubSegmentKey;
use EdifactParser\Segments\CNTControl;
use PHPUnit\Framework\TestCase;

Expand All @@ -19,4 +20,12 @@ public function segmentValues(): void
self::assertEquals('7', $segment->subSegmentKey());
self::assertEquals($rawValues, $segment->rawValues());
}

/** @test */
public function missingSubSegmentKey(): void
{
$segment = new CNTControl(['CNT']);
self::expectException(MissingSubSegmentKey::class);
$segment->subSegmentKey();
}
}
10 changes: 10 additions & 0 deletions tests/Unit/Segments/DTMDateTimePeriodTest.php
Expand Up @@ -4,6 +4,8 @@

namespace EdifactParser\Tests\Unit\Segments;

use EdifactParser\Exception\MissingSubSegmentKey;
use EdifactParser\Segments\CNTControl;
use EdifactParser\Segments\DTMDateTimePeriod;
use PHPUnit\Framework\TestCase;

Expand All @@ -19,4 +21,12 @@ public function segmentValues(): void
self::assertEquals('10', $segment->subSegmentKey());
self::assertEquals($rawValues, $segment->rawValues());
}

/** @test */
public function missingSubSegmentKey(): void
{
$segment = new DTMDateTimePeriod(['DTM']);
self::expectException(MissingSubSegmentKey::class);
$segment->subSegmentKey();
}
}
9 changes: 9 additions & 0 deletions tests/Unit/Segments/UNHMessageHeaderTest.php
Expand Up @@ -4,6 +4,7 @@

namespace EdifactParser\Tests\Unit\Segments;

use EdifactParser\Exception\MissingSubSegmentKey;
use EdifactParser\Segments\UNHMessageHeader;
use PHPUnit\Framework\TestCase;

Expand All @@ -19,4 +20,12 @@ public function segmentValues(): void
self::assertEquals('1', $segment->subSegmentKey());
self::assertEquals($rawValues, $segment->rawValues());
}

/** @test */
public function missingSubSegmentKey(): void
{
$segment = new UNHMessageHeader(['UNH']);
self::expectException(MissingSubSegmentKey::class);
$segment->subSegmentKey();
}
}

0 comments on commit 298debd

Please sign in to comment.