Skip to content

Commit b50de13

Browse files
committed
implement mock methods for integration tests
1 parent 8297630 commit b50de13

File tree

2 files changed

+469
-0
lines changed

2 files changed

+469
-0
lines changed

tests/SapRfcIntegrationTest.php

Lines changed: 388 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,388 @@
1+
<?php
2+
3+
namespace tests\phpsap\saprfc;
4+
5+
use phpsap\IntegrationTests\AbstractSapRfcTestCase;
6+
use tests\phpsap\saprfc\Traits\TestCaseTrait;
7+
8+
/**
9+
* Class tests\phpsap\saprfc\SapRfcIntegrationTest
10+
*
11+
* Implement methods of the integration tests to mock SAP remote function
12+
* calls without an actual SAP system for testing.
13+
*
14+
* @package tests\phpsap\saprfc
15+
* @author Gregor J.
16+
* @license MIT
17+
*/
18+
class SapRfcIntegrationTest extends AbstractSapRfcTestCase
19+
{
20+
use TestCaseTrait;
21+
22+
/**
23+
* @var array raw API of RFC walk through test
24+
*/
25+
public static $rfcWalkThruTestApi = [
26+
'name' => 'RFC_WALK_THRU_TEST',
27+
'TEST_OUT' => [
28+
'type' => 'RFCTYPE_STRUCTURE',
29+
'direction' => 'RFC_EXPORT',
30+
'description' => 'ZZ8ruKPn6j',
31+
'optional' => false,
32+
'defaultValue' => '',
33+
],
34+
'TEST_IN' => [
35+
'type' => 'RFCTYPE_STRUCTURE',
36+
'direction' => 'RFC_IMPORT',
37+
'description' => 'p1f4ghivbE',
38+
'optional' => false,
39+
'defaultValue' => '',
40+
],
41+
'DESTINATIONS' => [
42+
'type' => 'RFCTYPE_TABLE',
43+
'direction' => 'RFC_TABLES',
44+
'description' => 'Xv7XloU3Jg',
45+
'optional' => false,
46+
'defaultValue' => '',
47+
],
48+
'LOG' => [
49+
'type' => 'RFCTYPE_TABLE',
50+
'direction' => 'RFC_TABLES',
51+
'description' => 'GriBlkJUOO',
52+
'optional' => false,
53+
'defaultValue' => '',
54+
]
55+
];
56+
57+
/**
58+
* @var array raw API of RFC read table
59+
*/
60+
public static $rfcReadTableApi = [
61+
'name' => 'RFC_READ_TABLE',
62+
'DELIMITER' => [
63+
'type' => 'RFCTYPE_CHAR',
64+
'direction' => 'RFC_IMPORT',
65+
'description' => 'Zeichen für Markierung von Feldgrenzen in DATA',
66+
'optional' => true,
67+
'defaultValue' => 'SPACE',
68+
],
69+
'NO_DATA' => [
70+
'type' => 'RFCTYPE_CHAR',
71+
'direction' => 'RFC_IMPORT',
72+
'description' => 'falls <> SPACE, wird nur FIELDS gefüllt',
73+
'optional' => true,
74+
'defaultValue' => 'SPACE',
75+
],
76+
'QUERY_TABLE' => [
77+
'type' => 'RFCTYPE_CHAR',
78+
'direction' => 'RFC_IMPORT',
79+
'description' => 'Tabelle, aus der gelesen wird',
80+
'optional' => false,
81+
'defaultValue' => '',
82+
],
83+
'ROWCOUNT' => [
84+
'type' => 'RFCTYPE_INT',
85+
'direction' => 'RFC_IMPORT',
86+
'description' => '',
87+
'optional' => true,
88+
'defaultValue' => '0',
89+
],
90+
'ROWSKIPS' => [
91+
'type' => 'RFCTYPE_INT',
92+
'direction' => 'RFC_IMPORT',
93+
'description' => '',
94+
'optional' => true,
95+
'defaultValue' => '0',
96+
],
97+
'DATA' => [
98+
'type' => 'RFCTYPE_TABLE',
99+
'direction' => 'RFC_TABLES',
100+
'description' => 'gelesene Daten (out)',
101+
'optional' => false,
102+
'defaultValue' => '',
103+
],
104+
'FIELDS' => [
105+
'type' => 'RFCTYPE_TABLE',
106+
'direction' => 'RFC_TABLES',
107+
'description' => 'Namen (in) und Struktur (out) gelesener Felder',
108+
'optional' => false,
109+
'defaultValue' => '',
110+
],
111+
'OPTIONS' => [
112+
'type' => 'RFCTYPE_TABLE',
113+
'direction' => 'RFC_TABLES',
114+
'description' => 'Selektionsangaben, "WHERE-Klauseln" (in)',
115+
'optional' => false,
116+
'defaultValue' => '',
117+
]
118+
];
119+
120+
/**
121+
* @inheritDoc
122+
*/
123+
protected function mockConnectionFailed()
124+
{
125+
static::mock('sapnwrfc::__construct', static function ($config) {
126+
unset($config);
127+
throw new \Exception('mock failed connection');
128+
});
129+
}
130+
131+
/**
132+
* @inheritDoc
133+
*/
134+
protected function mockSuccessfulRfcPing()
135+
{
136+
$flags = new \stdClass();
137+
$flags->conn = false;
138+
$flags->func = null;
139+
$expectedConfig = static::getSampleSapConfig();
140+
static::mock('sapnwrfc::__construct', static function ($config) use ($flags, $expectedConfig) {
141+
if (
142+
!is_array($config)
143+
|| !array_key_exists('ASHOST', $config)
144+
|| !array_key_exists('SYSNR', $config)
145+
|| !array_key_exists('CLIENT', $config)
146+
|| !array_key_exists('USER', $config)
147+
|| !array_key_exists('PASSWD', $config)
148+
|| $config['ASHOST'] !== $expectedConfig->getAshost()
149+
|| $config['SYSNR'] !== $expectedConfig->getSysnr()
150+
|| $config['CLIENT'] !== $expectedConfig->getClient()
151+
|| $config['USER'] !== $expectedConfig->getUser()
152+
|| $config['PASSWD'] !== $expectedConfig->getPasswd()
153+
) {
154+
throw new \Exception('mock received invalid config array!');
155+
}
156+
//set flag that a connection has been established
157+
$flags->conn = true;
158+
});
159+
static::mock('sapnwrfc::close', static function () use ($flags) {
160+
//calling sapnwrfc::close twice has to fail
161+
if ($flags->conn !== true) {
162+
throw new \Exception('mock connection already closed!');
163+
}
164+
$flags->conn = false;
165+
});
166+
static::mock('sapnwrfc::function_lookup', static function ($name) {
167+
return new \sapnwrfc_function($name);
168+
});
169+
static::mock('sapnwrfc_function::__construct', static function ($name) use ($flags) {
170+
if ($flags->conn !== true) {
171+
throw new \Exception('mock connection not open!');
172+
}
173+
if ($name !== 'RFC_PING') {
174+
throw new \Exception('expected RFC_PING as mock function name!');
175+
}
176+
$flags->func = $name;
177+
});
178+
static::mock('sapnwrfc_function::invoke', static function ($params) use ($flags) {
179+
if ($flags->conn !== true) {
180+
throw new \Exception('mock connection not open!');
181+
}
182+
if ($flags->func !== 'RFC_PING') {
183+
throw new \Exception('mock function not correctly constructed!');
184+
}
185+
if (!empty($params)) {
186+
throw new \Exception('mock RFC_PING received parameters! ' . json_encode($params));
187+
}
188+
return [];
189+
});
190+
}
191+
192+
/**
193+
* @inheritDoc
194+
*/
195+
protected function mockUnknownFunctionException()
196+
{
197+
$connFlag = false;
198+
$expectedConfig = static::getSampleSapConfig();
199+
static::mock('sapnwrfc::__construct', static function ($config) use (&$connFlag, $expectedConfig) {
200+
if (
201+
!is_array($config)
202+
|| !array_key_exists('ASHOST', $config)
203+
|| !array_key_exists('SYSNR', $config)
204+
|| !array_key_exists('CLIENT', $config)
205+
|| !array_key_exists('USER', $config)
206+
|| !array_key_exists('PASSWD', $config)
207+
|| $config['ASHOST'] !== $expectedConfig->getAshost()
208+
|| $config['SYSNR'] !== $expectedConfig->getSysnr()
209+
|| $config['CLIENT'] !== $expectedConfig->getClient()
210+
|| $config['USER'] !== $expectedConfig->getUser()
211+
|| $config['PASSWD'] !== $expectedConfig->getPasswd()
212+
) {
213+
throw new \Exception('mock received invalid config array!');
214+
}
215+
//set flag that a connection has been established
216+
$connFlag = true;
217+
});
218+
static::mock('sapnwrfc::close', static function () use (&$connFlag) {
219+
//calling sapnwrfc::close twice has to fail
220+
if ($connFlag !== true) {
221+
throw new \Exception('mock connection already closed!');
222+
}
223+
$connFlag = false;
224+
});
225+
static::mock('sapnwrfc::function_lookup', static function ($name) {
226+
throw new \Exception(sprintf('function %s not found', $name));
227+
});
228+
}
229+
230+
/**
231+
* @inheritDoc
232+
*/
233+
protected function mockRemoteFunctionCallWithParametersAndResults()
234+
{
235+
//Use an object for connection flag and function name.
236+
$flags = new \stdClass();
237+
$flags->conn = false;
238+
$flags->func = null;
239+
$flags->api = static::$rfcWalkThruTestApi;
240+
$expectedConfig = static::getSampleSapConfig();
241+
static::mock('sapnwrfc::__construct', static function ($config) use ($flags, $expectedConfig) {
242+
if (
243+
!is_array($config)
244+
|| !array_key_exists('ASHOST', $config)
245+
|| !array_key_exists('SYSNR', $config)
246+
|| !array_key_exists('CLIENT', $config)
247+
|| !array_key_exists('USER', $config)
248+
|| !array_key_exists('PASSWD', $config)
249+
|| $config['ASHOST'] !== $expectedConfig->getAshost()
250+
|| $config['SYSNR'] !== $expectedConfig->getSysnr()
251+
|| $config['CLIENT'] !== $expectedConfig->getClient()
252+
|| $config['USER'] !== $expectedConfig->getUser()
253+
|| $config['PASSWD'] !== $expectedConfig->getPasswd()
254+
) {
255+
throw new \Exception('mock received invalid config array!');
256+
}
257+
//set flag that a connection has been established
258+
$flags->conn = true;
259+
});
260+
static::mock('sapnwrfc::close', static function () use ($flags) {
261+
//calling sapnwrfc::close twice has to fail
262+
if ($flags->conn !== true) {
263+
throw new \Exception('mock connection already closed!');
264+
}
265+
$flags->conn = false;
266+
});
267+
static::mock('sapnwrfc_function::__construct', static function ($name) use ($flags) {
268+
if ($flags->conn !== true) {
269+
throw new \Exception('mock connection not open!');
270+
}
271+
if ($name !== 'RFC_WALK_THRU_TEST') {
272+
throw new \Exception('expected RFC_WALK_THRU_TEST as mock function name!');
273+
}
274+
$flags->func = $name;
275+
});
276+
static::mock('sapnwrfc::function_lookup', static function ($name) use ($flags) {
277+
if ($flags->conn !== true) {
278+
throw new \Exception('mock connection not open!');
279+
}
280+
if ($name !== 'RFC_WALK_THRU_TEST') {
281+
throw new \Exception('expected RFC_WALK_THRU_TEST as mock function name!');
282+
}
283+
$func = new \sapnwrfc_function($name);
284+
//Assigning all the API values that are later gathered by get_object_vars().
285+
foreach ($flags->api as $key => $value) {
286+
$func->$key = $value;
287+
}
288+
return $func;
289+
});
290+
static::mock('sapnwrfc_function::invoke', static function ($params) use ($flags) {
291+
if ($flags->conn !== true) {
292+
throw new \Exception('mock connection not open!');
293+
}
294+
if ($flags->func !== 'RFC_WALK_THRU_TEST') {
295+
throw new \Exception('function not correctly initialized!');
296+
}
297+
return [
298+
'TEST_OUT' => [
299+
'RFCFLOAT' => 70.109999999999999,
300+
'RFCCHAR1' => 'A',
301+
'RFCINT2' => 4095,
302+
'RFCINT1' => 163,
303+
'RFCCHAR4' => 'QqMh',
304+
'RFCINT4' => 416639,
305+
'RFCHEX3' => '53' . "\0" . '',
306+
'RFCCHAR2' => 'XC',
307+
'RFCTIME' => '102030',
308+
'RFCDATE' => '20191030',
309+
'RFCDATA1' => 'qKWjmNfad32rfS9Z ',
310+
'RFCDATA2' => 'xi82ph2zJ8BCVtlR '
311+
],
312+
'DESTINATIONS' => [],
313+
'LOG' => [
314+
[
315+
'RFCDEST' => 'AOP3 ',
316+
'RFCWHOAMI' => 'pzjti000 ',
317+
'RFCLOG' => 'FAP-RytEHBsRYKX AOP3 eumqvMJD ZLqovj. '
318+
]
319+
]
320+
];
321+
});
322+
}
323+
324+
/**
325+
* @inheritDoc
326+
*/
327+
protected function mockFailedRemoteFunctionCallWithParameters()
328+
{
329+
//Use an object for connection flag and function name.
330+
$flags = new \stdClass();
331+
$flags->conn = false;
332+
$flags->func = null;
333+
$flags->api = static::$rfcReadTableApi;
334+
$expectedConfig = static::getSampleSapConfig();
335+
static::mock('sapnwrfc::__construct', static function ($config) use ($flags, $expectedConfig) {
336+
if (
337+
!is_array($config)
338+
|| !array_key_exists('ASHOST', $config)
339+
|| !array_key_exists('SYSNR', $config)
340+
|| !array_key_exists('CLIENT', $config)
341+
|| !array_key_exists('USER', $config)
342+
|| !array_key_exists('PASSWD', $config)
343+
|| $config['ASHOST'] !== $expectedConfig->getAshost()
344+
|| $config['SYSNR'] !== $expectedConfig->getSysnr()
345+
|| $config['CLIENT'] !== $expectedConfig->getClient()
346+
|| $config['USER'] !== $expectedConfig->getUser()
347+
|| $config['PASSWD'] !== $expectedConfig->getPasswd()
348+
) {
349+
throw new \Exception('mock received invalid config array!');
350+
}
351+
//set flag that a connection has been established
352+
$flags->conn = true;
353+
});
354+
static::mock('sapnwrfc::close', static function () use ($flags) {
355+
//calling sapnwrfc::close twice has to fail
356+
if ($flags->conn !== true) {
357+
throw new \Exception('mock connection already closed!');
358+
}
359+
$flags->conn = false;
360+
});
361+
static::mock('sapnwrfc_function::__construct', static function ($name) use ($flags) {
362+
if ($flags->conn !== true) {
363+
throw new \Exception('mock connection not open!');
364+
}
365+
if ($name !== 'RFC_READ_TABLE') {
366+
throw new \Exception('expected RFC_READ_TABLE as mock function name!');
367+
}
368+
$flags->func = $name;
369+
});
370+
static::mock('sapnwrfc::function_lookup', static function ($name) use ($flags) {
371+
if ($flags->conn !== true) {
372+
throw new \Exception('mock connection not open!');
373+
}
374+
if ($name !== 'RFC_READ_TABLE') {
375+
throw new \Exception('expected RFC_READ_TABLE as mock function name!');
376+
}
377+
$func = new \sapnwrfc_function($name);
378+
//Assigning all the API values that are later gathered by get_object_vars().
379+
foreach ($flags->api as $key => $value) {
380+
$func->$key = $value;
381+
}
382+
return $func;
383+
});
384+
static::mock('sapnwrfc_function::invoke', static function ($params) use ($flags) {
385+
throw new \Exception('mock function call exception!');
386+
});
387+
}
388+
}

0 commit comments

Comments
 (0)