Skip to content

Commit

Permalink
Add remaining tests to ensure 100% coverage for Message class (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
senaranya authored Sep 17, 2023
1 parent 2609721 commit f31ff81
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/HL7/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ public function removeSegmentsByName(string $segmentName): int
*/
public function setSegment(Segment $segment, int $index): bool
{
if (!isset($index) || $index > count($this->segments)) {
if ($index > count($this->segments)) {
throw new InvalidArgumentException('Index out of range');
}

Expand Down
110 changes: 110 additions & 0 deletions tests/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,27 @@

class MessageTest extends TestCase
{
/** @test */
public function segments_can_be_retrieved_using_index(): void
{
$msg = new Message("MSH|^~\\&|1|\rABC|\r");
self::assertSame('ABC', $msg->getSegmentByIndex(1)->getName());
}

/** @test */
public function segmentByIndex_returns_null_when_target_index_is_beyond_the_total_number_of_segments(): void
{
$msg = new Message("MSH|^~\\&|1|\rABC|\r");
self::assertNull($msg->getSegmentByIndex(2));
}

/** @test */
public function it_throws_error_when_field_separator_control_char_doesnt_match_the_actual_field_separators(): void
{
$this->expectException(HL7Exception::class);
$msg = new Message("MSH|^~\\&#\r");
}

/**
* @test
* @throws Exception
Expand Down Expand Up @@ -209,6 +230,28 @@ public function a_new_segment_can_be_inserted_between_two_existing_segments(): v
self::assertSame('BBB', $msg->getSegmentByIndex(2)->getName(), 'Existing segment should shift');
}

/** @test */
public function MSH_segment_can_be_inserted_with_new_control_characters(): void
{
$msg = new Message();
$msh = new MSH(['MSH', '#~\&', '', '', '', '', '', '', ['AAA', 'BBB'], '', '', '555']);
$msg->insertSegment($msh, 0);
self::assertSame('MSH', $msg->getSegmentByIndex(0)->getName(), 'Inserted MSH');
self::assertSame(
'MSH|#~\&|||||||AAA#BBB|||555|\n',
$msg->toString(),
'Component separator should be # and version should be 555'
);
}

/** @test */
public function insertSegment_appends_when_the_target_index_is_same_as_the_count_of_total_segments(): void
{
$msg = new Message("MSH|^~\\&|1|\rAAA|1", null, true);
$msg->insertSegment(new Segment('XXX'), 2);
self::assertSame('MSH|^~\&|1|\nAAA|1|\nXXX|\n', $msg->toString());
}

/** @test */
public function it_should_not_be_possible_to_insert_segment_beyond_last_index(): void
{
Expand All @@ -228,6 +271,27 @@ public function a_segment_can_be_overwritten(): void
self::assertSame('BBB', $msg->getSegmentByIndex(0)->getName(), 'BBB should have replaced AAA');
}

/** @test */
public function setSegment_throws_exception_when_the_target_index_is_beyond_total_count_of_segments(): void
{
$this->expectException(InvalidArgumentException::class);
$msg = new Message("MSH|^~\\&|1|\nAAA|1|\n");
$msg->setSegment(new Segment('BBB'), 3);
}

/** @test */
public function setSegment_resets_control_characters_when_adding_MSH_segment_at_0th_index(): void
{
$msg = new Message();
$msh = new MSH(['MSH', '#~\&', '', '', '', '', '', '', ['AAA', 'BBB'], '', '', '555']);
$msg->setSegment($msh, 0);
self::assertSame(
'MSH|#~\&|||||||AAA#BBB|||555|\n',
$msg->toString(),
'Component separator should be # and version should be 555'
);
}

/** @test */
public function same_segment_type_can_be_added_multiple_times(): void
{
Expand Down Expand Up @@ -291,6 +355,27 @@ public function a_segment_can_be_retrieved_as_a_string(): void
self::assertSame('a^b1&b2^c', $msg->getSegmentFieldAsString(1, 2), 'XXX(2) as string');
}

/** @test */
public function getSegmentAsString_returns_null_if_the_target_segment_doesnt_exist(): void
{
$msg = new Message("MSH|^~\\&|1|\nABC|");
self::assertNull($msg->getSegmentAsString(5));
}

/** @test */
public function getSegmentFieldAsString_returns_null_if_the_target_segment_doesnt_exist(): void
{
$msg = new Message("MSH|^~\\&|1|\nABC|");
self::assertNull($msg->getSegmentFieldAsString(5, 1));
}

/** @test */
public function getSegmentFieldAsString_returns_null_if_the_target_field_doesnt_exist(): void
{
$msg = new Message("MSH|^~\\&|1|\nABC|");
self::assertNull($msg->getSegmentFieldAsString(1, 5));
}

/**
* @test
*/
Expand Down Expand Up @@ -325,6 +410,13 @@ public function segment_index_can_be_retrieved_from_a_message(): void
self::assertSame(2, $message->getSegmentIndex($pid));
}

/** @test */
public function segmentIndex_returns_null_if_the_target_segment_is_not_found(): void
{
$message = new Message("MSH|^~\\&|1|\nABC|\n");
self::assertNull($message->getSegmentIndex(new Segment('XXX')));
}

/** @test */
public function message_type_can_be_checked(): void
{
Expand All @@ -342,6 +434,11 @@ public function message_type_can_be_checked(): void
self::assertTrue($msg->isSiu());
self::assertFalse($msg->isOrm());
self::assertFalse($msg->isOru());

$msg = new Message("MSH|^~\&|||||||ADT^A01|");
self::assertTrue($msg->isAdt());
self::assertFalse($msg->isOrm());
self::assertFalse($msg->isOru());
}

/**
Expand Down Expand Up @@ -493,4 +590,17 @@ public function repetition_separation_character_can_be_ignored(): void
self::assertIsArray($patientIdentifierList);
self::assertSame(['3', '0~4', '1'], $patientIdentifierList);
}

/** @test */
public function a_message_can_be_saved_in_a_file(): void
{
$hl7File = __DIR__ . DIRECTORY_SEPARATOR . 'hl7_test_' . random_int(100, 1000) . '.hl7';
$message = new Message("MSH|^~\&|");
$message->toFile($hl7File);
self::assertFileExists($hl7File);
self::assertSame($message->toString(true), file_get_contents($hl7File));
if (file_exists($hl7File)) {
unlink($hl7File);
}
}
}
22 changes: 22 additions & 0 deletions tests/SegmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,31 @@
namespace Aranyasen\HL7\Tests;

use Aranyasen\HL7\Segment;
use InvalidArgumentException;

class SegmentTest extends TestCase
{
/** @test */
public function it_throws_error_when_segment_name_has_less_than_3_characters(): void
{
$this->expectException(InvalidArgumentException::class);
new Segment('XX');
}

/** @test */
public function it_throws_exception_when_a_blank_string_is_passed_as_segment_name(): void
{
$this->expectException(InvalidArgumentException::class);
new Segment('');
}

/** @test */
public function it_throws_exception_when_segment_name_is_not_in_upper_case(): void
{
$this->expectException(InvalidArgumentException::class);
new Segment('xxx');
}

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

0 comments on commit f31ff81

Please sign in to comment.