Ejemplo n.º 1
0
 public function test()
 {
     # Simple constructor
     #
     $msg = new Net_HL7_Message();
     $seg1 = new Net_HL7_Segment("PID");
     $seg1->setField(2, "Foo");
     $msg->addSegment(new Net_HL7_Segments_MSH());
     $msg->addSegment($seg1);
     $seg0 = $msg->getSegmentByIndex(0);
     $seg1 = $msg->getSegmentByIndex(1);
     $seg0->setField(3, "XXX");
     $this->assertTrue($seg0->getName() == "MSH", "Segment 0 name MSH");
     $this->assertTrue($seg1->getName() == "PID", "Segment 1 name PID");
     $this->assertTrue($seg0->getField(3) == "XXX", "3d field of MSH");
     $this->assertTrue($seg1->getField(2) == "Foo", "2nd field of PID");
     // Check references
     $segX = $msg->getSegmentByIndex(0);
     $this->assertTrue($segX->getField(3) == "XXX", "3d field of MSH");
     $msg = new Net_HL7_Message("MSH|^~\\&|1|\rPID|||xxx|\r");
     $seg0 = $msg->getSegmentByIndex(0);
     $this->assertTrue($msg->toString() == "MSH|^~\\&|1|\rPID|||xxx|\r", "String representation of message");
     $this->assertTrue($msg->toString(1) == "MSH|^~\\&|1|\nPID|||xxx|\n", "Pretty print representation of message");
     $this->assertTrue($seg0->getField(2) == "^~\\&", "Encoding characters (MSH(2))");
     # Constructor with components and subcomponents
     #
     $msg = new Net_HL7_Message("MSH|^~\\&|1|\rPID|||xx^x&y&z^yy^zz|\r");
     $seg1 = $msg->getSegmentByIndex(1);
     $comps = $seg1->getField(3);
     $this->assertTrue($comps[0] == "xx", "Composed field");
     $this->assertTrue($comps[1][1] == "y", "Subcomposed field");
     # Trying different field separator
     #
     $msg = new Net_HL7_Message("MSH*^~\\&*1\rPID***xxx\r");
     $this->assertTrue($msg->toString() == "MSH*^~\\&*1*\rPID***xxx*\r", "String representation of message with * as field separator");
     $seg0 = $msg->getSegmentByIndex(0);
     $this->assertTrue($seg0->getField(3) == "1", "3d field of MSH");
     # Trying different field sep and control chars
     #
     $msg = new Net_HL7_Message("MSH*.%#@*1\rPID***x.x@y@z.z\r");
     $seg1 = $msg->getSegmentByIndex(1);
     $comps = $seg1->getField(3);
     $this->assertTrue($comps[0] == "x", "Composed field with . as separator");
     $this->assertTrue($comps[1][1] == "y", "Subcomposed field with @ as separator");
     # Faulty constuctor
     #
     //$this->assertTrue(! defined(new Net::HL7::Message("MSH|^~\\&*1\rPID|||xxx\r")), "Field separator not repeated");
     $seg2 = new Net_HL7_Segment("XXX");
     $msg->addSegment($seg2);
     $msg->removeSegmentByIndex(1);
     $seg1 = $msg->getSegmentByIndex(1);
     $this->assertTrue($seg1->getName() == $seg2->getName(), "Add/remove segment");
     $seg3 = new Net_HL7_Segment("YYY");
     $seg4 = new Net_HL7_Segment("ZZZ");
     $msg->insertSegment($seg3, 1);
     $msg->insertSegment($seg4, 1);
     $seg3 = $msg->getSegmentByIndex(3);
     $seg4 = $msg->getSegmentByIndex(4);
     $this->assertTrue($seg3->getName() == $seg2->getName(), "Insert segment");
     $msg->removeSegmentByIndex(1);
     $msg->removeSegmentByIndex(1);
     $msg->removeSegmentByIndex(6);
     $seg5 = new Net_HL7_Segment("ZZ1");
     # This shouldn't be possible
     @$msg->insertSegment($seg5, 3);
     $this->assertTrue(!$msg->getSegmentByIndex(3), "Erroneous insert");
     $msg->insertSegment($seg5, 2);
     $seg2 = $msg->getSegmentByIndex(2);
     $this->assertTrue($seg2->getName() == $seg5->getName(), "Insert segment");
     $msg->setSegment($seg3, 2);
     $seg2 = $msg->getSegmentByIndex(2);
     $this->assertTrue($seg2->getName() == $seg3->getName(), "Set segment");
     $this->assertTrue(count($msg->getSegmentsByName("MSH")) == 1, "Number of MSH segments");
     $msh2 = new Net_HL7_Segments_MSH();
     $msg->addSegment($msh2);
     $this->assertTrue(count($msg->getSegmentsByName("MSH")) == 2, "Added MSH segment, now two in message");
     # Fumble 'round with ctrl chars
     #
     $msg = new Net_HL7_Message();
     $msh = new Net_HL7_Segments_MSH(array());
     $msh->setField(1, "*");
     $msh->setField(2, "abcd");
     $msg->addSegment($msh);
     $this->assertTrue($msg->toString() == "MSH*abcd*\r", "Creating separate MSH");
     $msh->setField(1, "|");
     $msh->setField(2, "^~\\&");
     $this->assertTrue($msg->toString() == "MSH|^~\\&|\r", "Change MSH after add");
     $msh = new Net_HL7_Segments_MSH(array());
     $msh->setField(1, "*");
     $msh->setField(2, "abcd");
     $msg->setSegment($msh, 0);
     $this->assertTrue($msg->toString() == "MSH*abcd*\r", "New MSH with setSegment");
     $str = 'MSH|^~\\&|CodeRyte HL7|CodeRyte HQ|VISION|MISYS|200404061744||DFT^P03|TC-2743|P^T|2.3|||AL|NE||ASCII||| |';
     $msg = new Net_HL7_Message($str);
     $this->assertTrue($msg->toString(1) == "{$str}\n", "Message from string and to string with subcomponents");
     // Segment as string
     $msg = new Net_HL7_Message("MSH*^~\\&*1\rPID*a^b^c*a^b1&b2^c*xxx\r");
     $xxx = new Net_HL7_Segment("XXX");
     $xxx->setField(2, array("a", array("b1", "b2"), "c"));
     $msg->addSegment($xxx);
     $this->assertTrue($msg->getSegmentAsString(0) == "MSH*^~\\&*1*", "MSH segment as string");
     $this->assertTrue($msg->getSegmentAsString(1) == "PID*a^b^c*a^b1&b2^c*xxx*", "PID segment as string");
     $this->assertTrue($msg->getSegmentAsString(2) == "XXX**a^b1&b2^c*", "XXX segment as string");
     // Get segment field as string
     $this->assertTrue($msg->getSegmentFieldAsString(0, 3) == "1", "MSH(3) as string");
     $this->assertTrue($msg->getSegmentFieldAsString(1, 2) == "a^b1&b2^c", "PID(2) as string");
 }
Ejemplo n.º 2
0
ok(count($msg->getSegmentsByName("MSH")) == 2, "Added MSH segment, now two in message<br>");
# Fumble 'round with ctrl chars
#
$msg = new Net_HL7_Message();
$msh = new Net_HL7_Segments_MSH(array());
$msh->setField(1, "*");
$msh->setField(2, "abcd");
$msg->addSegment($msh);
ok($msg->toString() == "MSH*abcd*\r", "Creating separate MSH<br>");
$msh->setField(1, "|");
$msh->setField(2, "^~\\&");
ok($msg->toString() == "MSH|^~\\&|\r", "Change MSH after add<br>");
$msh = new Net_HL7_Segments_MSH(array());
$msh->setField(1, "*");
$msh->setField(2, "abcd");
$msg->setSegment($msh, 0);
ok($msg->toString() == "MSH*abcd*\r", "New MSH with setSegment<br>");
$str = 'MSH|^~\\&|CodeRyte HL7|CodeRyte HQ|VISION|MISYS|200404061744||DFT^P03|TC-2743|P^T|2.3|||AL|NE||ASCII||| |';
$msg = new Net_HL7_Message($str);
ok($msg->toString(1) == "{$str}\n", "Message from string and to string with subcomponents<br>");
// Segment as string
$msg = new Net_HL7_Message("MSH*^~\\&*1\rPID*a^b^c*a^b1&b2^c*xxx\r");
$xxx = new Net_HL7_Segment("XXX");
$xxx->setField(2, array("a", array("b1", "b2"), "c"));
$msg->addSegment($xxx);
ok($msg->getSegmentAsString(0) == "MSH*^~\\&*1*", "MSH segment as string<br>");
ok($msg->getSegmentAsString(1) == "PID*a^b^c*a^b1&b2^c*xxx*", "PID segment as string<br>");
ok($msg->getSegmentAsString(2) == "XXX**a^b1&b2^c*", "XXX segment as string<br>");
// Get segment field as string
ok($msg->getSegmentFieldAsString(0, 3) == "1", "MSH(3) as string<br>");
ok($msg->getSegmentFieldAsString(1, 2) == "a^b1&b2^c", "PID(2) as string<br>");
Ejemplo n.º 3
0
    }

    if (false === ($buf = socket_read($msgsock, 8192, PHP_NORMAL_READ))) {
      echo "socket_read() failed: reason: " . socket_strerror($ret) . "\n";
      break 2;
    }

    echo "Incoming: $buf\n";

    $msg = new Net_HL7_Message($buf);

    $ack = new Net_HL7_Messages_ACK($msg);
    socket_write($msgsock, "\013" . $ack->toString() . "\034\015", strlen($ack->toString() + 3));
    echo "Said: " . $ack->toString(1) . "\n";

  } while (true);

  socket_close($msgsock);

  exit;
}
*/
$conn = new Net_HL7_Connection("localhost", 12002);
ok($conn, "Trying to connect");
$conn || exit - 1;
ok($conn, "Sending message\n" . $msg->toString(1));
$resp = $conn->send($msg);
$resp || exit - 1;
$msh = $resp->getSegmentByIndex(0);
ok($msh->getField(9) == "ACK", "Checking ACK field");
$conn->close();
Ejemplo n.º 4
0
 public function test()
 {
     $msg = new Net_HL7_Message();
     $msg->addSegment(new Net_HL7_Segments_MSH());
     $seg1 = new Net_HL7_Segment("PID");
     $seg1->setField(3, "XXX");
     $msg->addSegment($seg1);
     /**
     // If you have fork support, try this...
     
     $pid = pcntl_fork();
     
     if (! $pid) {
     
       // Server process
       set_time_limit(0);
     
       // Turn on implicit output flushing so we see what we're getting
       // as it comes in.
       ob_implicit_flush();
     
       if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) < 0) {
         echo "socket_create() failed: reason: " . socket_strerror($sock) . "\n";
       }
     
       if (($ret = socket_bind($sock, "localhost", 12011)) < 0) {
         echo "socket_bind() failed: reason: " . socket_strerror($ret) . "\n";
       }
     
       if (($ret = socket_listen($sock, 5)) < 0) {
         echo "socket_listen() failed: reason: " . socket_strerror($ret) . "\n";
       }
     
       do {
         if (($msgsock = socket_accept($sock)) < 0) {
           echo "socket_accept() failed: reason: " . socket_strerror($msgsock) . "\n";
           break;
         }
     
         if (false === ($buf = socket_read($msgsock, 8192, PHP_NORMAL_READ))) {
           echo "socket_read() failed: reason: " . socket_strerror($ret) . "\n";
           break 2;
         }
     
         echo "Incoming: $buf\n";
     
         $msg = new Net_HL7_Message($buf);
     
         $ack = new Net_HL7_Messages_ACK($msg);
         socket_write($msgsock, "\013" . $ack->toString() . "\034\015", strlen($ack->toString() + 3));
         echo "Said: " . $ack->toString(1) . "\n";
     
       } while (true);
     
       socket_close($msgsock);
     
       exit;
     }
     */
     $socket = $this->getMock('Net_Socket');
     $socket->expects($this->once())->method('write')->with("\v" . $msg->toString() . "\r");
     $socket->expects($this->once())->method('read')->will($this->returnValue("MSH*^~\\&*1\rPID***xxx\r" . "\r"));
     $conn = new Net_HL7_Connection($socket);
     $resp = $conn->send($msg);
     $this->assertTrue($resp instanceof Net_HL7_Message);
 }