Skip to content

Commit 7cea68c

Browse files
committed
Reviewed tests, detect missing terminators, fix warnings, add setXml to Analyser to allow usage of EDI\Mapping
1 parent 9afd224 commit 7cea68c

File tree

8 files changed

+69
-51
lines changed

8 files changed

+69
-51
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ codeCoverage/*
33
/build
44
composer.lock
55
.vscode
6-
.phan
6+
.phan
7+
.phpunit*

phpunit.xml

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,23 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
3-
<phpunit backupGlobals="false"
4-
backupStaticAttributes="false"
5-
colors="true"
6-
convertErrorsToExceptions="true"
7-
convertNoticesToExceptions="true"
8-
convertWarningsToExceptions="true"
9-
processIsolation="false"
10-
stopOnFailure="false"
11-
syntaxCheck="false"
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd"
124
bootstrap="tests/bootstrap.php"
13-
verbose="true"
14-
testdox="true"
15-
>
5+
cacheDirectory=".phpunit.cache"
6+
executionOrder="depends,defects"
7+
requireCoverageMetadata="false"
8+
beStrictAboutCoverageMetadata="false"
9+
beStrictAboutOutputDuringTests="true"
10+
failOnRisky="true"
11+
failOnWarning="true">
1612
<testsuites>
17-
<testsuite name="sabas/edifact">
18-
<directory>./tests/EDITest</directory>
13+
<testsuite name="default">
14+
<directory>tests</directory>
1915
</testsuite>
2016
</testsuites>
21-
<filter>
22-
<whitelist processUncoveredFilesFromWhitelist="true">
23-
<directory suffix=".php">./src/</directory>
24-
</whitelist>
25-
<blacklist>
26-
<directory suffix=".php">./vendor</directory>
27-
</blacklist>
28-
</filter>
29-
<logging>
30-
<log type="coverage-clover" target="build/logs/clover.xml"/>
31-
</logging>
17+
18+
<source restrictDeprecations="true" restrictNotices="true" restrictWarnings="true">
19+
<include>
20+
<directory>src</directory>
21+
</include>
22+
</source>
3223
</phpunit>

src/EDI/Analyser.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ class Analyser
2929
*/
3030
private $codes;
3131

32+
public function setXml($segments, $codes) {
33+
$this->segments = $segments;
34+
$this->codes = $codes;
35+
}
36+
3237
/**
3338
* @param string $message_xml_file
3439
*
@@ -92,6 +97,7 @@ public function loadCodesXml(string $codesXml)
9297
/**
9398
* convert segment definition from XML to array. Sequence of data_elements and
9499
* composite_data_element same as in XML
100+
* For single message, it's vailable also in (new EDI\Mapping\MappingProvider($version))->loadSegmentsXml()
95101
*
96102
* @param string $segmentXmlFile
97103
* @param bool $discardOldSegments
@@ -252,11 +258,11 @@ public function process(array $data, array $rawSegments = null): string
252258
$codeElementId = $d_desc_attr['id'];
253259
$line = ' [' . $n . '] ' . $detail;
254260
if(isset($this->codes[(int)$codeElementId][$detail])){
255-
/*
256-
* for retrieving code element description when first element of the segment
261+
/*
262+
* for retrieving code element description when first element of the segment
257263
* is a data element and not a composite one. Ex: NAD segment.
258-
* We rewrite also l1 line for adding 'id:' prefix before data element id.
259-
* It's just a cosmetic fix
264+
* We rewrite also l1 line for adding 'id:' prefix before data element id.
265+
* It's just a cosmetic fix
260266
*/
261267
$line .= ' - ' . \wordwrap($this->codes[$codeElementId][$detail], 71, \PHP_EOL . ' ');
262268
$l1 = ' id: ' . $d_desc_attr['id'] . ' - ' . $d_desc_attr['name'];
@@ -353,4 +359,4 @@ protected function readAttributesArray(\SimpleXMLElement $element): array
353359

354360
return $attributes;
355361
}
356-
}
362+
}

src/EDI/Interpreter.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class Interpreter
8181
* @var array
8282
*/
8383
private $codes;
84-
84+
8585
/**
8686
* @var callable
8787
*/
@@ -643,8 +643,8 @@ private function processSegment(array &$segment, array &$xmlMap, $segmentIdx, ar
643643

644644
$d_sub_desc_attr = $sub_details_desc[$d_n]['attributes'];
645645

646-
if ($this->codes !== null && $this->codes[$d_sub_desc_attr['id']] !== null) { //if codes is set enable translation of the value
647-
if ($this->codes[$d_sub_desc_attr['id']][$d_detail]) {
646+
if ($this->codes !== null && isset($this->codes[$d_sub_desc_attr['id']]) && is_array($this->codes[$d_sub_desc_attr['id']])) { //if codes is set enable translation of the value
647+
if (isset($this->codes[$d_sub_desc_attr['id']][$d_detail])) {
648648
$d_detail = $this->codes[$d_sub_desc_attr['id']][$d_detail];
649649
}
650650
}
@@ -663,16 +663,16 @@ private function processSegment(array &$segment, array &$xmlMap, $segmentIdx, ar
663663
} else {
664664
$d_sub_desc_attr = $sub_details_desc[0]['attributes'];
665665

666-
if ($this->codes !== null && $this->codes[$d_sub_desc_attr['id']] !== null) { //if codes is set enable translation of the value
667-
if ($this->codes[$d_sub_desc_attr['id']][$detail]) {
666+
if ($this->codes !== null && isset($this->codes[$d_sub_desc_attr['id']]) && is_array($this->codes[$d_sub_desc_attr['id']])) { //if codes is set enable translation of the value
667+
if (isset($this->codes[$d_sub_desc_attr['id']][$detail]) && $this->codes[$d_sub_desc_attr['id']][$detail]) {
668668
$detail = $this->codes[$d_sub_desc_attr['id']][$detail];
669669
}
670670
}
671671
$jsoncomposite[$d_sub_desc_attr[$this->outputKey]] = $detail;
672672
}
673673
} else {
674-
if ($this->codes !== null && $this->codes[$d_desc_attr['id']] !== null) { //if codes is set enable translation of the value
675-
if ($this->codes[$d_desc_attr['id']][$detail]) {
674+
if ($this->codes !== null && isset($this->codes[$d_desc_attr['id']]) && is_array($this->codes[$d_desc_attr['id']])) { //if codes is set enable translation of the value
675+
if (isset($this->codes[$d_desc_attr['id']][$detail]) && $this->codes[$d_desc_attr['id']][$detail]) {
676676
$detail = $this->codes[$d_desc_attr['id']][$detail];
677677
}
678678
}

src/EDI/Parser.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ class Parser
9595
*/
9696
private $messageDirectory;
9797

98+
/**
99+
* @var string : message number
100+
*/
101+
private $messageNumber;
102+
98103
/**
99104
* @var array<string,string>
100105
*/
@@ -398,6 +403,15 @@ public function getMessageDirectory()
398403
return $this->messageDirectory;
399404
}
400405

406+
/**
407+
* @return string|null
408+
*/
409+
public function getMessageNumber()
410+
{
411+
return $this->messageNumber;
412+
}
413+
414+
401415
/**
402416
* Reset UNA's characters definition
403417
*
@@ -461,8 +475,12 @@ private function unwrap(string &$string): array
461475
);
462476
}
463477

478+
if (preg_match_all("/[A-Z0-9]+[\r\n]+/i", $string, $matches, PREG_OFFSET_CAPTURE) > 0) {
479+
$this->errors[] = "This file contains some segments without terminators";
480+
}
481+
464482
$string = (string) \preg_replace(
465-
'/(([^' . $this->symbRel . ']' . $this->symbRel . '{2})+|[^' . $this->symbRel . '])' . $this->symbEnd . '/',
483+
'/(([^' . $this->symbRel . ']' . $this->symbRel . '{2})+|[^' . $this->symbRel . '])' . $this->symbEnd . '|[\r\n]+/',
466484
'$1' . $this->stringSafe,
467485
$string
468486
);

tests/EDITest/AnalyserTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ public function testProcess()
6868
$segments = $parser->getRawSegments();
6969
static::assertCount(15, $parsed);
7070
$result = (new Analyser())->process($parsed, $segments);
71-
static::assertSame(399, \strlen($result));
71+
72+
static::assertSame(228, \strlen($result));
7273
}
7374

7475
public function testProcessWrapped()

tests/EDITest/InterpreterTest.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use EDI\Parser;
66
use EDI\Analyser;
7-
use Arrayy\Arrayy;
87
use EDI\Interpreter;
98

109
/**
@@ -150,16 +149,17 @@ public function testOrderError()
150149
$this->assertStringContainsString('"messageHeader"', $interpreter->getJson(true));
151150
static::assertStringContainsString('"interchangeHeader"', $interpreter->getJsonServiceSegments(true));
152151

153-
$arrayy = new Arrayy($interpreter->getEdiGroups());
152+
$arrayy = $interpreter->getEdiGroups();
153+
$item = $arrayy[0]['SG25'][0]['itemDescription']['itemDescription']['itemDescription'];
154154
static::assertSame(
155155
'Butter 40x250g Alu',
156-
$arrayy->get('0.SG25.0.itemDescription.itemDescription.itemDescription')
156+
$item
157157
);
158158

159-
$arrayy = new Arrayy($interpreter->getServiceSegments());
159+
$arrayy = $interpreter->getServiceSegments();
160160
static::assertCount(
161161
14,
162-
$arrayy->get('interchangeHeader')
162+
$arrayy['interchangeHeader']
163163
);
164164
}
165165

@@ -185,16 +185,17 @@ public function testOrderOk()
185185
static::assertStringContainsString('"messageHeader"', $interpreter->getJson(true));
186186
static::assertStringContainsString('"interchangeHeader"', $interpreter->getJsonServiceSegments(true));
187187

188-
$arrayy = new Arrayy($interpreter->getEdiGroups());
188+
$arrayy = $interpreter->getEdiGroups();
189+
$item = $arrayy[0]['SG25'][0]['itemDescription']['itemDescription']['itemDescription'];
189190
static::assertSame(
190191
'Butter 40x250g Alu',
191-
$arrayy->get('0.SG25.0.itemDescription.itemDescription.itemDescription')
192+
$item
192193
);
193194

194-
$arrayy = new Arrayy($interpreter->getServiceSegments());
195+
$arrayy = $interpreter->getServiceSegments();
195196
static::assertCount(
196197
14,
197-
$arrayy->get('interchangeHeader')
198+
$arrayy['interchangeHeader']
198199
);
199200
}
200201

tests/EDITest/ParserTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function testMultipleEscapedSegments($string, $expected)
7272
static::assertSame($expected, $result);
7373
}
7474

75-
public function multipleEscapedSegmentsProvider()
75+
public static function multipleEscapedSegmentsProvider()
7676
{
7777
return [
7878
["EQD+CX??DU12?+3456+2?:0'", [['EQD', 'CX?DU12+3456', '2:0']]],

0 commit comments

Comments
 (0)