Skip to content

Commit

Permalink
Add unit tests, readme for ACK update
Browse files Browse the repository at this point in the history
  • Loading branch information
senaranya committed Mar 14, 2020
1 parent 7a33161 commit b331abe
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 54 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,19 @@ if ($ackCode[1] === 'A') {
}
else {
echo "Recieved NACK from remote\n";
echo "Error text: " . $msa->getTextMessage()
echo "Error text: " . $msa->getTextMessage();
}
```
Create an ACK response from a given HL7 message:
```php
$msg = new Message("MSH|^~\\&|1|\rABC|1||^AAAA1^^^BB|", null, true);
$ackResponse = new ACK($msg);
```
Options can be passed while creating ACK object:
```php
$msg = new Message("MSH|^~\\&|1|\rABC|1||^AAAA1^^^BB|", null, true);
$ackResponse = new ACK($msg, ['SEGMENT_SEPARATOR' => '\r\n', 'HL7_VERSION' => '2.5']);
```

## APIs
This package exposes a number of public methods for convenient HL7 handling. Some examples are:
Expand Down
61 changes: 26 additions & 35 deletions src/HL7/Segments/MSH.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,34 +41,29 @@ public function __construct(array $fields = null, array $hl7Globals = null)
{
parent::__construct('MSH', $fields);

// Only fill default fields if no fields array is given
//
if (!isset($fields)) {
if (!\is_array($hl7Globals)) {
$this->setField(1, '|');
$this->setField(2, '^~\\&');
$this->setField(7, strftime('%Y%m%d%H%M%S'));

// Set ID field
$this->setField(10, $this->getField(7) . random_int(10000, 99999));
$this->setField(12, '2.3');
}
else {
$this->setField(1, $hl7Globals['FIELD_SEPARATOR']);
$this->setField(
2,
$hl7Globals['COMPONENT_SEPARATOR'] .
$hl7Globals['REPETITION_SEPARATOR'] .
$hl7Globals['ESCAPE_CHARACTER'] .
$hl7Globals['SUBCOMPONENT_SEPARATOR']
);
$this->setField(7, strftime('%Y%m%d%H%M%S'));

// Set ID field
$this->setField(10, $this->getField(7) . random_int(10000, 99999));
$this->setField(12, $hl7Globals['HL7_VERSION']);
}
if (isset($fields)) { // We're done if MSH fields were provided
return;
}

// Fill mandatory fields if no fields array is given
if (is_array($hl7Globals)) {
$this->setField(1, $hl7Globals['FIELD_SEPARATOR']);
$this->setField(
2,
$hl7Globals['COMPONENT_SEPARATOR'] .
$hl7Globals['REPETITION_SEPARATOR'] .
$hl7Globals['ESCAPE_CHARACTER'] .
$hl7Globals['SUBCOMPONENT_SEPARATOR']
);
$this->setVersionId($hl7Globals['HL7_VERSION']);
}
else {
$this->setField(1, '|');
$this->setField(2, '^~\\&');
$this->setVersionId('2.3');
}
$this->setDateTimeOfMessage(strftime('%Y%m%d%H%M%S'));
$this->setMessageControlId($this->getDateTimeOfMessage() . random_int(10000, 99999));
}

/**
Expand All @@ -86,16 +81,12 @@ public function __construct(array $fields = null, array $hl7Globals = null)
*/
public function setField(int $index, $value = ''): bool
{
if ($index === 1) {
if (\strlen($value) !== 1) {
return false;
}
if (($index === 1) && strlen($value) !== 1) {
return false;
}

if ($index === 2) {
if (\strlen($value) !== 4) {
return false;
}
if (($index === 2) && strlen($value) !== 4) {
return false;
}

return parent::setField($index, $value);
Expand Down
25 changes: 24 additions & 1 deletion tests/AckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ public function test()
$ack->setAckCode('CR', 'XX');
$this->assertSame('XX', $seg1->getField(3), 'Set message and code');


$msg = new Message();
$msg->addSegment(new MSH());
$msh = $msg->getSegmentByIndex(0);
Expand All @@ -77,4 +76,28 @@ public function test()
$this->assertSame('Some error', $seg1->getField(3), 'Setting error message');
$this->assertSame('CE', $seg1->getField(1), 'Code CE after setting message');
}

/** @test
* @throws \Aranyasen\Exceptions\HL7Exception
* @throws \ReflectionException
* @throws \Exception
*/
public function a_MSH_can_be_provided_to_get_the_fields_from(): void
{
$msg = new Message("MSH|^~\\&|1|\rPV1|1|O|^AAAA1^^^BB|");
$msh = new MSH(['MSH', '^~\&', 'HL7 Corp', 'HL7 HQ', 'VISION', 'MISYS', '200404061744', '', ['DFT', 'P03'], 'TC-22222', 'T', '2.3']);
$ack = new ACK($msg, $msh);
$this->assertSame("MSH|^~\&|VISION|MISYS|HL7 Corp|HL7 HQ|||ACK|\nMSA|AA|TC-22222|\n", $ack->toString(true));
}

/** @test
* @throws \Aranyasen\Exceptions\HL7Exception
* @throws \ReflectionException
*/
public function globals_can_be_passed_to_constructor(): void
{
$msg = new Message("MSH|^~\\&|1|\rPV1|1|O|^AAAA1^^^BB|");
$ack = new ACK($msg, null, ['HL7_VERSION' => '2.5']);
$this->assertSame("MSH|^~\&|1||||||ACK|\nMSA|AA|\n", $ack->toString(true));
}
}
10 changes: 8 additions & 2 deletions tests/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ public function control_characters_can_be_customized_using_second_argument(): vo
'toString() respects custom line-endings');
}

/** @test */
/** @test
* @throws HL7Exception
* @throws \ReflectionException
*/
public function message_can_be_converted_to_string(): void
{
$msg = new Message("MSH|^~\\&|1|\rABC|||xxx|\r");
Expand All @@ -78,7 +81,10 @@ public function message_can_be_converted_to_string(): void
'Pretty print representation of message');
}

/** @test */
/** @test
* @throws HL7Exception
* @throws \ReflectionException
*/
public function toString_method_throws_exception_when_message_empty(): void
{
$msg = new Message();
Expand Down
47 changes: 32 additions & 15 deletions tests/Segments/MSHTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,51 @@

class MSHTest extends TestCase
{
private $msh;
/** @test */
public function MSH_formed_without_any_arguments_should_have_mandatory_fields_set_automatically(): void
{
$msh = new MSH();
$this->assertSame('|', $msh->getField(1));
$this->assertSame('^~\\&', $msh->getField(2));
$this->assertSame('2.3', $msh->getVersionId());
$this->assertNotEmpty($msh->getDateTimeOfMessage());
$this->assertNotEmpty($msh->getMessageControlId());
}

public function setUp(): void
/** @test */
public function an_array_of_fields_can_be_passed_to_constructor_to_construct_MSH(): void
{
$this->msh = new MSH();
$msh = new MSH(['MSH', '^~\&', 'HL7 Corp', 'HL7 HQ', 'VISION', 'MISYS', '200404061744', '', ['DFT', 'P03'], 'TC-22222', 'T', '2.3']);
$this->assertSame(
['MSH', '', '^~\&', 'HL7 Corp', 'HL7 HQ', 'VISION', 'MISYS', '200404061744', '', ['DFT', 'P03'], 'TC-22222', 'T', '2.3'],
$msh->getFields()
);
}

/** @test */
public function field_separator_can_be_set()
public function field_separator_can_be_set(): void
{
$this->msh->setField(1, '*');
$this->assertSame('*', $this->msh->getField(1), 'MSH Field sep field (MSH(1))');
$msh = new MSH();
$msh->setField(1, '*');
$this->assertSame('*', $msh->getField(1), 'MSH Field sep field (MSH(1))');
}

/** @test */
public function more_than_one_character_as_field_separator_is_not_accepted()
public function more_than_one_character_as_field_separator_is_not_accepted(): void
{
$this->msh->setField(1, 'xx');
$this->assertSame('|', $this->msh->getField(1), 'MSH Field sep field (MSH(1))');
$msh = new MSH();
$msh->setField(1, 'xx');
$this->assertSame('|', $msh->getField(1), 'MSH Field sep field (MSH(1))');
}

/** @test */
public function index_2_in_MSH_accepts_only_4_character_strings()
public function index_2_in_MSH_accepts_only_4_character_strings(): void
{
$this->msh->setField(2, 'xxxx');
$this->assertSame('xxxx', $this->msh->getField(2), 'Special fields not changed');
$msh = new MSH();
$msh->setField(2, 'xxxx');
$this->assertSame('xxxx', $msh->getField(2), 'Special fields not changed');

$this->msh->setField(2, 'yyyyy');
$this->assertSame('xxxx', $this->msh->getField(2), 'Special fields not changed');
$msh->setField(2, 'yyyyy');
$this->assertSame('xxxx', $msh->getField(2), 'Special fields not changed');
}
}
}

0 comments on commit b331abe

Please sign in to comment.