Skip to content

Commit

Permalink
Fix linting errors (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
senaranya authored Feb 13, 2022
1 parent fa79f7e commit 28f60da
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 21 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
},
"scripts": {
"test": "vendor/bin/phpunit tests/ --coverage-text",
"lint": "vendor/bin/phpcs --standard=psr12 src/"
"lint": "vendor/bin/phpcs"
},
"extra": {
"branch-alias": {
Expand Down
14 changes: 14 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0"?>

<ruleset name="PHP_CodeSniffer">
<description>PHPCS configuration file.</description>

<rule ref="PSR12"/>

<file>src</file>
<file>tests</file>

<rule ref="PSR1.Methods.CamelCapsMethodName.NotCamelCaps">
<exclude-pattern>*/tests/*</exclude-pattern>
</rule>
</ruleset>
8 changes: 7 additions & 1 deletion tests/AckTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);

namespace Aranyasen\HL7\Tests;
Expand Down Expand Up @@ -87,7 +88,12 @@ public function test()
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']);
$msh = new MSH(
[
'MSH', '^~\&', 'HL7 Corp', 'HL7 HQ', 'VISION', 'MISYS', '200404061744', '', ['DFT', 'P03'], 'TC-22222',
'T', '2.3',
]
);
$ack = new ACK($msg, $msh);
self::assertSame("MSH|^~\&|VISION|MISYS|HL7 Corp|HL7 HQ|||ACK|\nMSA|AA|TC-22222|\n", $ack->toString(true));
}
Expand Down
3 changes: 2 additions & 1 deletion tests/ConnectionTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);

namespace Aranyasen\HL7\Tests;
Expand Down Expand Up @@ -72,7 +73,7 @@ public function do_not_wait_for_ack_after_sending_if_corresponding_parameter_is_

$connection = new Connection('localhost', $this->port);
$msg = new Message("MSH|^~\\&|1|\rPV1|1|O|^AAAA1^^^BB|", null, true, true);
self::assertNull($connection->send($msg,' UTF-8', true));
self::assertNull($connection->send($msg, ' UTF-8', true));

$this->closeTcpSocket($connection->getSocket()); // Clean up listener
pcntl_wait($status); // Wait till child is closed
Expand Down
12 changes: 8 additions & 4 deletions tests/Hl7ListenerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Aranyasen\Exceptions\HL7Exception;
use Aranyasen\HL7\Message;
use Aranyasen\HL7\Messages\ACK;
use ReflectionException;
use RuntimeException;

/**
* Trait Hl7ListenerTrait
Expand All @@ -25,13 +27,13 @@ trait Hl7ListenerTrait

public function writeToPipe(string $value): void
{
$pipe = fopen($this->pipeName,'wb');
$pipe = fopen($this->pipeName, 'wb');
fwrite($pipe, $value);
}

public function readFromPipe(): string
{
$pipe = fopen($this->pipeName,'rb');
$pipe = fopen($this->pipeName, 'rb');
return fread($pipe, 1024);
}

Expand All @@ -44,12 +46,14 @@ public function getWhatServerGot(): string
* @param int $port
* @param int $totalClientsToConnect How many clients are expected to connect to this server, once it's up
* @throws HL7Exception
* @throws \ReflectionException
* @throws ReflectionException
*/
public function createTcpServer(int $port, int $totalClientsToConnect): void
{
if (($socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
throw new \RuntimeException('socket_create() failed: reason: ' . socket_strerror(socket_last_error()) . "\n");
throw new RuntimeException(
'socket_create() failed: reason: ' . socket_strerror(socket_last_error()) . "\n"
);
}

// This is to avoid "address already in use" error while doing ->bind()
Expand Down
39 changes: 29 additions & 10 deletions tests/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ public function fields_can_be_separated_by_custom_character(): void
$msg = new Message("MSH*^~\\&*1\rABC***xxx\r"); // Use * as a field separator

self::assertSame(
'MSH*^~\&*1*\nABC***xxx*\n', $msg->toString(),
'MSH*^~\&*1*\nABC***xxx*\n',
$msg->toString(),
'String representation of message with * as field separator'
);
self::assertSame('1', $msg->getSegmentByIndex(0)->getField(3), '3d field of MSH');
Expand Down Expand Up @@ -162,7 +163,11 @@ public function segment_can_be_removed_from_message(): void
$msg = new Message("MSH|^~\\&|1|\nAAA|1||xxx|\nBBB|1|a|\nBBB|2|b|\nBBB|3|c|");
$segment = $msg->getSegmentsByName('BBB')[1];
$msg->removeSegment($segment, true);
self::assertSame("MSH|^~\\&|1|\nAAA|1||xxx|\nBBB|1|a|\nBBB|2|c|\n", $msg->toString(true), 'Should reset index of subsequent segments');
self::assertSame(
"MSH|^~\\&|1|\nAAA|1||xxx|\nBBB|1|a|\nBBB|2|c|\n",
$msg->toString(true),
'Should reset index of subsequent segments'
);
}

/** @test */
Expand Down Expand Up @@ -299,7 +304,7 @@ public function an_exception_will_be_throw_in_invalid_string(): void
/** @test */
public function segment_ending_bar_can_be_omitted(): void
{
$msg = new Message("MSH|^~\\&|1|\nABC|||xxx|\n", ['SEGMENT_ENDING_BAR' => false]);
$msg = new Message("MSH|^~\\&|1|\nABC|||xxx|\n", ['SEGMENT_ENDING_BAR' => false]);
self::assertSame("MSH|^~\\&|1\nABC|||xxx\n", $msg->toString(true), 'No ending bar on each segment');

$msg = new Message("MSH|^~\\&|1|\nABC|||xxx|\n");
Expand Down Expand Up @@ -341,11 +346,13 @@ public function message_type_can_be_checked(): void

/**
* The segment classes use static properties to maintain segment-index. This is required as one can add new segments
* to a given message object and expect the index to auto-increment. The side-effect to this is, if you create a
* second message, adding the same segments will now start indexing from their index in the last message instead of 1!
* This happens because static properties are class properties, not object ones, and thus shared across all objects.
* to a given message object and expect the index to auto-increment. The side effect to this is, if you create a
* second message, adding the same segments will now start indexing from their index in the last message instead of
* 1! This happens because static properties are class properties, not object ones, and thus shared across all
* objects.
*
* To counter this, use 'true' in the 4th argument while creating a message, or call resetSegmentIndices() explicitly
* To counter this, use 'true' in the 4th argument while creating a message, or call resetSegmentIndices()
* explicitly.
*
* This test verifies both.
*
Expand All @@ -357,12 +364,20 @@ public function segment_id_can_be_reset_on_demand(): void
// Create a message with a PID segment
$msg1 = new Message("MSH|^~\&|||||||ORM^O01||P|2.3.1|");
$msg1->addSegment(new PID());
self::assertSame("MSH|^~\&|||||||ORM^O01||P|2.3.1|\nPID|1|\n", $msg1->toString(true), 'PID index in first message is 1');
self::assertSame(
"MSH|^~\&|||||||ORM^O01||P|2.3.1|\nPID|1|\n",
$msg1->toString(true),
'PID index in first message is 1'
);

// Create another message with a PID segment
$msg2 = new Message("MSH|^~\&|||||||ORM^O01||P|2.3.1|");
$msg2->addSegment(new PID());
self::assertSame("MSH|^~\&|||||||ORM^O01||P|2.3.1|\nPID|2|\n", $msg2->toString(true), 'PID index gets incremented');
self::assertSame(
"MSH|^~\&|||||||ORM^O01||P|2.3.1|\nPID|2|\n",
$msg2->toString(true),
'PID index gets incremented'
);

// Create another message with a PID segment, this time 4th argument to message as true
$msg3 = new Message("MSH|^~\&|||||||ORM^O01||P|2.3.1|", null, true, true);
Expand All @@ -372,7 +387,11 @@ public function segment_id_can_be_reset_on_demand(): void
// Create a message with a PID segment
$msg4 = new Message("MSH|^~\&|||||||ORM^O01||P|2.3.1|");
$msg4->addSegment(new PID());
self::assertSame("MSH|^~\&|||||||ORM^O01||P|2.3.1|\nPID|2|\n", $msg4->toString(true), 'PID index gets incremented');
self::assertSame(
"MSH|^~\&|||||||ORM^O01||P|2.3.1|\nPID|2|\n",
$msg4->toString(true),
'PID index gets incremented'
);

$msg5 = new Message("MSH|^~\&|||||||ORM^O01||P|2.3.1|");
$msg5->resetSegmentIndices();
Expand Down
15 changes: 12 additions & 3 deletions tests/Segments/MSHTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Aranyasen\HL7\Segments\MSH;
use Aranyasen\HL7\Tests\TestCase;
use Exception;

class MSHTest extends TestCase
{
Expand All @@ -21,13 +22,21 @@ public function MSH_formed_without_any_arguments_should_have_mandatory_fields_se
}

/** @test
* @throws \Exception
* @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']);
$msh = new MSH(
[
'MSH', '^~\&', 'HL7 Corp', 'HL7 HQ', 'VISION', 'MISYS', '200404061744', '', ['DFT', 'P03'], 'TC-22222',
'T', '2.3',
]
);
self::assertSame(
['MSH', '', '^~\&', 'HL7 Corp', 'HL7 HQ', 'VISION', 'MISYS', '200404061744', '', ['DFT', 'P03'], 'TC-22222', 'T', '2.3'],
[
'MSH', '', '^~\&', 'HL7 Corp', 'HL7 HQ', 'VISION', 'MISYS', '200404061744', '', ['DFT', 'P03'],
'TC-22222', 'T', '2.3',
],
$msh->getFields()
);
}
Expand Down
3 changes: 2 additions & 1 deletion tests/Segments/PIDTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ public function PID_5_should_parse_properly(): void
$msg = new Message($messageString, null, false, true, true, true);

self::assertSame(
'MSH|^~\&|1|\nPID|1||||Test &""&firstname &""&""^F^N N^F^""~Test &""&Test^Test^""^B~""&""&""^P~^Lastname^N|\n',
'MSH|^~\&|1|\nPID|1||||Test &""&firstname &""&""^F^N N^F^""~Test &""&Test^Test^""^B~""&""&""^P~^' .
'Lastname^N|\n',
$msg->toString()
);
}
Expand Down

0 comments on commit 28f60da

Please sign in to comment.