Skip to content

Commit 40b9ca3

Browse files
authored
Merge pull request #90 from voku/master
use "voku/arrayy" for the "Interpreter"-Results + tests
2 parents 7142bd7 + b3f5a63 commit 40b9ca3

File tree

6 files changed

+130
-17
lines changed

6 files changed

+130
-17
lines changed

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
"require": {
3333
"php": ">=7.0.0",
3434
"ext-simplexml": "*",
35-
"ext-json": "*"
35+
"ext-json": "*",
36+
"voku/arrayy": "5.5.*"
3637
},
3738
"require-dev": {
3839
"phpunit/phpunit": "~6.0",

src/EDI/Interpreter.php

+33-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace EDI;
66

7+
use Arrayy\Arrayy;
8+
79
/**
810
* EDIFACT Messages Interpreter
911
* (c) 2018 Stefano Sabatini
@@ -41,7 +43,7 @@ class Interpreter
4143
private $msgs;
4244

4345
/**
44-
* @var
46+
* @var array
4547
*/
4648
private $serviceSeg;
4749

@@ -551,7 +553,7 @@ private function processService(array &$segments): array
551553
}
552554

553555
/**
554-
* Get result as json
556+
* Get result as json.
555557
*
556558
* @param bool $pretty
557559
*
@@ -566,32 +568,48 @@ public function getJson(bool $pretty = false)
566568
return \json_encode($this->ediGroups);
567569
}
568570

571+
/**
572+
* Get result as Arrayy Object.
573+
*
574+
* @return Arrayy
575+
*/
576+
public function getArrayy()
577+
{
578+
return new Arrayy($this->ediGroups);
579+
}
580+
569581
/**
570582
* Get errors
583+
*
584+
* @return array
571585
*/
572586
public function getErrors(): array
573587
{
574588
return $this->errors;
575589
}
576590

577591
/**
578-
* Get splitted messages
592+
* Get splitted messages.
593+
*
594+
* @return array
579595
*/
580596
public function getMessages(): array
581597
{
582598
return $this->msgs;
583599
}
584600

585601
/**
586-
* Get service segments
602+
* Get service segments.
603+
*
604+
* @return array
587605
*/
588606
public function getServiceSegments()
589607
{
590608
return $this->serviceSeg;
591609
}
592610

593611
/**
594-
* Get json service segments
612+
* Get json service segments.
595613
*
596614
* @param bool $pretty
597615
*
@@ -605,4 +623,14 @@ public function getJsonServiceSegments(bool $pretty = false)
605623

606624
return \json_encode($this->serviceSeg);
607625
}
626+
627+
/**
628+
* Get service segments as Arrayy Object.
629+
*
630+
* @return Arrayy
631+
*/
632+
public function getArrayyServiceSegments()
633+
{
634+
return new Arrayy($this->serviceSeg);
635+
}
608636
}

tests/EDITest/InterpreterTest.php

+80
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,86 @@ public function testMissingUNTUNZ()
115115
$this->assertSame(['UNZ', 'UNT'], $segments);
116116
}
117117

118+
public function testOrderError()
119+
{
120+
$edi = file_get_contents(__DIR__ . '/../files/example_order_error.edi');
121+
$parser = new Parser($edi);
122+
$mapping = new \EDI\Mapping\MappingProvider('D95B');
123+
$analyser = new Analyser();
124+
$segs = $analyser->loadSegmentsXml($mapping->getSegments());
125+
$svc = $analyser->loadSegmentsXml($mapping->getServiceSegments(3));
126+
127+
$interpreter = new Interpreter($mapping->getMessage('ORDERS'), $segs, $svc);
128+
/** @noinspection UnusedFunctionResultInspection */
129+
$interpreter->prepare($parser->get());
130+
$this->assertSame(
131+
[
132+
0 => 'There\'s a character not escaped with ? in the data; string :::H-Vollmilch 3,5% ?*?*Marke?*?*:1l Tertra mit Drehverschluss',
133+
],
134+
$parser->errors()
135+
);
136+
137+
$errors = $interpreter->getErrors();
138+
$this->assertCount(3, $errors);
139+
$segments = [];
140+
foreach ($errors as $err) {
141+
$segments[] = $err['segmentId'];
142+
}
143+
$this->assertSame(['IMD', 'IMD', 'IMD'], $segments);
144+
145+
$this->assertCount(2, $interpreter->getMessages());
146+
147+
$this->assertContains('"messageHeader"', $interpreter->getJson(true));
148+
$this->assertContains('"interchangeHeader"', $interpreter->getJsonServiceSegments(true));
149+
150+
$arrayy = $interpreter->getArrayy();
151+
$this->assertSame(
152+
'Butter 40x250g Alu',
153+
$arrayy->get('0.SG25.0.itemDescription.itemDescription.itemDescription')
154+
);
155+
156+
$arrayy = $interpreter->getArrayyServiceSegments();
157+
$this->assertCount(
158+
13,
159+
$arrayy->get('interchangeHeader')
160+
);
161+
}
162+
163+
public function testOrderOk()
164+
{
165+
$edi = file_get_contents(__DIR__ . '/../files/example_order_ok.edi');
166+
$parser = new Parser($edi);
167+
$mapping = new \EDI\Mapping\MappingProvider('D95B');
168+
$analyser = new Analyser();
169+
$segs = $analyser->loadSegmentsXml($mapping->getSegments());
170+
$svc = $analyser->loadSegmentsXml($mapping->getServiceSegments(3));
171+
172+
$interpreter = new Interpreter($mapping->getMessage('ORDERS'), $segs, $svc);
173+
/** @noinspection UnusedFunctionResultInspection */
174+
$interpreter->prepare($parser->get());
175+
$this->assertSame([], $parser->errors());
176+
177+
$errors = $interpreter->getErrors();
178+
$this->assertCount(0, $errors);
179+
180+
$this->assertCount(2, $interpreter->getMessages());
181+
182+
$this->assertContains('"messageHeader"', $interpreter->getJson(true));
183+
$this->assertContains('"interchangeHeader"', $interpreter->getJsonServiceSegments(true));
184+
185+
$arrayy = $interpreter->getArrayy();
186+
$this->assertSame(
187+
'Butter 40x250g Alu',
188+
$arrayy->get('0.SG25.0.itemDescription.itemDescription.itemDescription')
189+
);
190+
191+
$arrayy = $interpreter->getArrayyServiceSegments();
192+
$this->assertCount(
193+
13,
194+
$arrayy->get('interchangeHeader')
195+
);
196+
}
197+
118198
public function testMissingUNBUNH()
119199
{
120200
$edi = "UNT+30+1907'UNZ+1+1865'";

tests/EDITest/ParserTest.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,12 @@ public function testFileOk()
7070
for ($i = 0; $i < 100; $i++) { // keep for simple performance tests
7171
$errors = (new Parser($string))->errors();
7272
}
73-
7473
$this->assertSame([], $errors);
74+
75+
$data = json_encode((new Parser($string))->get());
76+
$this->assertContains('Sup 1:10', $data);
77+
$this->assertNotContains('Konzentrat:o', $data);
78+
$this->assertContains('"Rindfleischsuppe Konzentrat","o', $data);
7579
}
7680

7781
public function testFileError()

tests/files/example_order_error.edi

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ BGM+220+2019265563+9'
55
DTM+137:20181205:102'
66
DTM+2:20181206:102'
77
RFF+ON:2019265563'
8-
NAD+BY+4250159300001::9++A?+K K�lte-Team GmbH+Uferstra�e 73a+Gelsenkirchen++45881+DE'
9-
CTA+PD+Herr Scheewe'
8+
NAD+BY+4250159300001::9++A?+A K�lte GmbH+Teststra�e 16a+TestCity++45881+DE'
9+
CTA+PD+Herr Foo'
1010
COM+020994125-32:TE'
1111
COM+020994125-44:FX'
12-
NAD+SU+4399901760448::9++BLF Logi-Food GmbH & Co. KG+Milchstrasse 15+Velbert++42553+DE'
13-
NAD+DP+4250159399005::9++A?+K K�lte-Team GmbH+Emschertalstra�e 5+Oberhausen++46149+DE'
12+
NAD+SU+4399901760448::9++FooBar GmbH & Co. KG+Teststrasse 15+TestCity++42553+DE'
13+
NAD+DP+4250159399005::9++A?+A K�lte GmbH+Teststra�e 7+TestCity++46149+DE'
1414
RFF+IT:9949940'
1515
LIN+1++1001:SA'
1616
PIA+5+6001001:IN::92'

tests/files/example_order_ok.edi

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ BGM+220+2019265563+9'
55
DTM+137:20181205:102'
66
DTM+2:20181206:102'
77
RFF+ON:2019265563'
8-
NAD+BY+4250159300001::9++A?+K K�lte-Team GmbH+Uferstra�e 73a+Gelsenkirchen++45881+DE'
9-
CTA+PD+Herr Scheewe'
8+
NAD+BY+4250159300001::9++A?+A K�lte GmbH+Teststra�e 16a+TestCity++45881+DE'
9+
CTA+PD+Herr Foo'
1010
COM+020994125-32:TE'
1111
COM+020994125-44:FX'
12-
NAD+SU+4399901760448::9++BLF Logi-Food GmbH & Co. KG+Milchstrasse 15+Velbert++42553+DE'
13-
NAD+DP+4250159399005::9++A?+K K�lte-Team GmbH+Emschertalstra�e 5+Oberhausen++46149+DE'
12+
NAD+SU+4399901760448::9++FooBar GmbH & Co. KG+Teststrasse 15+TestCity++42553+DE'
13+
NAD+DP+4250159399005::9++A?+A K�lte GmbH+Teststra�e 7+TestCity++46149+DE'
1414
RFF+IT:9949940'
1515
LIN+1++1001:SA'
1616
PIA+5+6001001:IN::92'
@@ -358,11 +358,11 @@ IMD+F++:::Apfelmus gezuckert 5/1 Dose:FM 4350g Adrian'
358358
QTY+21:34:STK'
359359
LIN+87++7288:SA'
360360
PIA+5+6007288:IN::92'
361-
IMD+F++:::H�hnersuppe Konzentrat ohne:Einlage f Sup 1:10 und So� 1:6'
361+
IMD+F++:::H�hnersuppe Konzentrat ohne:Einlage f Sup 1?:10 und So� 1?:6'
362362
QTY+21:7:STK'
363363
LIN+88++7289:SA'
364364
PIA+5+6007289:IN::92'
365-
IMD+F++:::Rindfleischsuppe Konzentrat:o Einlage f�r Suppen 1:10'
365+
IMD+F++:::Rindfleischsuppe Konzentrat:o Einlage f�r Suppen 1?:10'
366366
QTY+21:8:STK'
367367
LIN+89++7295:SA'
368368
PIA+5+6007295:IN::92'

0 commit comments

Comments
 (0)