Skip to content

Commit

Permalink
issue-29 Allow all sex-values as per standard (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
senaranya authored Aug 1, 2020
1 parent fc7e6bd commit 7e11b08
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 41 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ $msg->addSegment($msh); // Message is: "MSH|^~\&|||||20171116140058|||2017111614
$abc = new Segment('ABC');
$abc->setField(1, 'xyz');
$abc->setField(4, ['']); // Set an empty field at 4th position. 2nd and 3rd positions will be automatically set to empty
$abc->clearField(2); // Clear the value from field 2
$msg->setSegment($abc, 1); // Message is now: "MSH|^~\&|||||20171116140058|||2017111614005840157||2.3|\nABC|xyz|\n"

// Create a defined segment (To know which segments are defined in this package, look into Segments/ directory)
Expand Down
11 changes: 11 additions & 0 deletions src/HL7/Segment.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,17 @@ public function setField(int $index, $value = ''): bool
return true;
}

/**
* Remove any existing value from the field
*
* @param int $index Field index
* @return void
*/
public function clearField(int $index): void
{
$this->fields[$index] = null;
}

/**
* Get the field at index.
*
Expand Down
8 changes: 5 additions & 3 deletions src/HL7/Segments/PID.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Aranyasen\HL7\Segments;

use Aranyasen\HL7\Segment;
use InvalidArgumentException;

/**
* PID segment class
Expand Down Expand Up @@ -83,10 +84,11 @@ public function setDateTimeOfBirth($value, int $position = 7)
return $this->setField($position, $value);
}

public function setSex($value, int $position = 8)
public function setSex(string $value, int $position = 8)
{
if ($value !== 'F' && $value !== 'M' && $value !== 'U') {
throw new \InvalidArgumentException("Sex should be either 'F', 'M' or 'U'. Given: '$value'");
// Ref: https://hl7-definition.caristix.com/v2/HL7v2.4/Tables/0001
if (!in_array($value, ['A', 'F', 'M', 'N', 'O', 'U'], true)) {
throw new InvalidArgumentException("Sex should one of 'A', 'F', 'M', 'N', 'O' or 'U'. Given: '$value'");
}
return $this->setField($position, $value);
}
Expand Down
54 changes: 28 additions & 26 deletions tests/SegmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,79 +8,81 @@
class SegmentTest extends TestCase
{
/** @test */
public function field_at_a_given_nonzero_index_can_be_set()
public function field_at_a_given_nonzero_index_can_be_set(): void
{
$seg = new Segment('XXX');
$seg->setField(1, 'YYY');
$this->assertSame('YYY', $seg->getField(1), 'Field 1 is YYY');
self::assertSame('YYY', $seg->getField(1), 'Field 1 is YYY');
}

/** @test */
public function field_at_index_0_can_not_be_changed()
public function field_at_index_0_can_not_be_changed(): void
{
$seg = new Segment('XXX');
$seg->setField(0, 'YYY');
$this->assertNotSame('YYY', $seg->getField(0), 'Field 0 has not been changed');
$this->assertSame('XXX', $seg->getField(0), 'Field 0 is still the same');
$this->assertSame('XXX', $seg->getName(), 'Segment name is still the same');
self::assertNotSame('YYY', $seg->getField(0), 'Field 0 has not been changed');
self::assertSame('XXX', $seg->getField(0), 'Field 0 is still the same');
self::assertSame('XXX', $seg->getName(), 'Segment name is still the same');
}

/** @test */
public function a_segment_can_be_contructed_from_an_array()
public function a_segment_can_be_constructed_from_an_array(): void
{
$seg = new Segment('XXX', ['a', 'b', 'c', ['p', 'q', 'r'], 'd']);
$this->assertSame('c', $seg->getField(3), 'Constructor with array');
$this->assertSame('r', $seg->getField(4)[2], 'Constructor with array for composed fields');
self::assertSame('c', $seg->getField(3), 'Constructor with array');
self::assertSame('r', $seg->getField(4)[2], 'Constructor with array for composed fields');
}

/** @test */
public function field_can_be_set_to_null()
public function field_can_be_cleared(): void
{
$seg = new Segment('XXX');
$seg->setField(1, null);
$this->assertNull($seg->getField(1), 'HL7 NULL value');
$segment = new Segment('XXX', ['a']);
self::assertSame('a', $segment->getField(1));
$segment->clearField(1);
self::assertNull($segment->getField(1), 'Field 1 should be NULL');
}

/** @test */
public function field_can_be_set_using_array()
public function field_can_be_set_using_array(): void
{
$seg = new Segment('XXX');
$seg->setField(3, ['1', '2', '3']);
$this->assertTrue(is_array($seg->getField(3)), 'Composed field 1^2^3');
$this->assertCount(3, $seg->getField(3), 'Getting composed fields as array');
$this->assertEquals(2, $seg->getField(3)[1], 'Getting single value from composed field');
self::assertIsArray($seg->getField(3), 'Composed field 1^2^3');
self::assertCount(3, $seg->getField(3), 'Getting composed fields as array');
self::assertEquals(2, $seg->getField(3)[1], 'Getting single value from composed field');
}

/** @test */
public function fields_from_a_given_position_to_end_can_be_retrieved_in_an_array()
public function fields_from_a_given_position_to_end_can_be_retrieved_in_an_array(): void
{
$seg = new Segment('XXX');
$seg->setField(8, 'aaa');
$this->assertCount(7, $seg->getFields(2), 'Getting all fields from 2nd index');
self::assertCount(7, $seg->getFields(2), 'Getting all fields from 2nd index');
}

/** @test */
public function a_chunk_of_fields_can_be_retrieved_from_a_segment()
public function a_chunk_of_fields_can_be_retrieved_from_a_segment(): void
{
$seg = new Segment('XXX');
$seg->setField(8, 'aaa');
$this->assertCount(3, $seg->getFields(2, 4), 'Getting fields from 2 till 4');
self::assertCount(3, $seg->getFields(2, 4), 'Getting fields from 2 till 4');
}

/** @test */
public function setting_field_beyond_last_index_creates_empty_fields_in_between()
public function setting_field_beyond_last_index_creates_empty_fields_in_between(): void
{
$seg = new Segment('XXX');
$seg->setField(8, 'aaa');
$this->assertCount(9, $seg->getFields(), 'Number of fields in segment');
self::assertCount(9, $seg->getFields(), 'Number of fields in segment');
}

/** @test */
public function total_size_of_a_segment_can_be_obtained()
public function total_size_of_a_segment_can_be_obtained(): void
{
$seg = new Segment('XXX');
$seg->setField(8, ['']);
$this->assertEquals(8, $seg->size(), 'Size operator');
self::assertEquals(8, $seg->size(), 'Size operator');
$seg->setField(12, 'x');
$this->assertEquals(12, $seg->size(), 'Size operator');
self::assertEquals(12, $seg->size(), 'Size operator');
}
}
26 changes: 14 additions & 12 deletions tests/Segments/MSHTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
declare(strict_types=1);

namespace Aranyasen\HL7\Segments\Tests;
namespace Aranyasen\HL7\Tests\Segments;

use Aranyasen\HL7\Segments\MSH;
use Aranyasen\HL7\Tests\TestCase;
Expand All @@ -12,18 +12,20 @@ class MSHTest extends TestCase
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());
self::assertSame('|', $msh->getField(1));
self::assertSame('^~\\&', $msh->getField(2));
self::assertSame('2.3', $msh->getVersionId());
self::assertNotEmpty($msh->getDateTimeOfMessage());
self::assertNotEmpty($msh->getMessageControlId());
}

/** @test */
/** @test
* @throws \Exception
*/
public function an_array_of_fields_can_be_passed_to_constructor_to_construct_MSH(): void
{
$msh = new MSH(['MSH', '^~\&', 'HL7 Corp', 'HL7 HQ', 'VISION', 'MISYS', '200404061744', '', ['DFT', 'P03'], 'TC-22222', 'T', '2.3']);
$this->assertSame(
self::assertSame(
['MSH', '', '^~\&', 'HL7 Corp', 'HL7 HQ', 'VISION', 'MISYS', '200404061744', '', ['DFT', 'P03'], 'TC-22222', 'T', '2.3'],
$msh->getFields()
);
Expand All @@ -34,25 +36,25 @@ public function field_separator_can_be_set(): void
{
$msh = new MSH();
$msh->setField(1, '*');
$this->assertSame('*', $msh->getField(1), 'MSH Field sep field (MSH(1))');
self::assertSame('*', $msh->getField(1), 'MSH Field sep field (MSH(1))');
}

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

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

$msh->setField(2, 'yyyyy');
$this->assertSame('xxxx', $msh->getField(2), 'Special fields not changed');
self::assertSame('xxxx', $msh->getField(2), 'Special fields not changed');
}
}
51 changes: 51 additions & 0 deletions tests/Segments/PIDTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Aranyasen\HL7\Tests\Segments;

use Aranyasen\HL7\Segments\PID;
use Aranyasen\HL7\Tests\TestCase;
use InvalidArgumentException;

class PIDTest extends TestCase
{
/**
* @dataProvider validSexValues
* @test
*/
public function PID_8_should_accept_value_for_sex_as_per_hl7_standard(string $validSexValue): void
{
$pidSegment = (new PID());
$pidSegment->setSex($validSexValue);
self::assertSame($validSexValue, $pidSegment->getSex(), "Sex should have been set with '$validSexValue'");
self::assertSame($validSexValue, $pidSegment->getField(8), "Sex should have been set with '$validSexValue'");
}

/**
* @dataProvider invalidSexValues
* @test
* @param $invalidSexValue
*/
public function PID_8_should_not_accept_non_standard_values_for_sex(string $invalidSexValue): void
{
$pidSegment = (new PID());
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage("Sex should one of 'A', 'F', 'M', 'N', 'O' or 'U'. Given: '$invalidSexValue'");
$pidSegment->setSex($invalidSexValue);
self::assertEmpty($pidSegment->getSex(), "Sex should not have been set with value: $invalidSexValue");
self::assertEmpty($pidSegment->getField(8), "Sex should not have been set with value: $invalidSexValue");
}

public function validSexValues(): array
{
return [
['A'], ['F'], ['M'], ['N'], ['O'], ['U']
];
}

public function invalidSexValues(): array
{
return [
['B'], ['Z'], ['z'], ['a']
];
}
}

0 comments on commit 7e11b08

Please sign in to comment.