From b331abe3a13409b3ec3c24dc423446331426961c Mon Sep 17 00:00:00 2001 From: Aranya Sen Date: Sat, 14 Mar 2020 09:28:33 +0530 Subject: [PATCH] Add unit tests, readme for ACK update --- README.md | 12 +++++++- src/HL7/Segments/MSH.php | 61 ++++++++++++++++---------------------- tests/AckTest.php | 25 +++++++++++++++- tests/MessageTest.php | 10 +++++-- tests/Segments/MSHTest.php | 47 +++++++++++++++++++---------- 5 files changed, 101 insertions(+), 54 deletions(-) diff --git a/README.md b/README.md index 258847d..47cbd17 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/src/HL7/Segments/MSH.php b/src/HL7/Segments/MSH.php index 44f55c9..27e8421 100644 --- a/src/HL7/Segments/MSH.php +++ b/src/HL7/Segments/MSH.php @@ -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)); } /** @@ -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); diff --git a/tests/AckTest.php b/tests/AckTest.php index 4749a71..45177ed 100644 --- a/tests/AckTest.php +++ b/tests/AckTest.php @@ -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); @@ -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)); + } } diff --git a/tests/MessageTest.php b/tests/MessageTest.php index 8ca3d02..9d6338a 100644 --- a/tests/MessageTest.php +++ b/tests/MessageTest.php @@ -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"); @@ -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(); diff --git a/tests/Segments/MSHTest.php b/tests/Segments/MSHTest.php index 3e42ae8..00c65bd 100644 --- a/tests/Segments/MSHTest.php +++ b/tests/Segments/MSHTest.php @@ -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'); } -} \ No newline at end of file +}