Advertisement
dkanavis

Untitled

Dec 25th, 2017
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.10 KB | None | 0 0
  1. #!/usr/bin/env php
  2. <?php
  3.  
  4. /*
  5. *   1C API test Kassa/GetBills
  6. *   usage: script
  7. *   usage: script <login1, [login2...]>
  8. */
  9.  
  10. class TestFailedInt extends Exception {}
  11. class TestFailed extends Exception {}
  12.  
  13. // Test logins
  14. $logins = [
  15.     '8P170737',
  16.     'm9-tts',
  17.     'master4-rn',
  18.     '8P#160409',
  19.     '1tvyam2-pm',
  20.     'khv11dc-mtel',
  21.     'm9-glavsv',
  22.     'tverb2-tass',
  23.     '8P120207',
  24.     '8P#120910',
  25.     '8P#160615',
  26.     '8P#161109',
  27.     '8P#161132',
  28.     '1gol35k3-olympia',
  29.     '1izmal8-grad',
  30.     '1izmal8-ip',
  31.     '1izmal8-knst',
  32.     '1izmal8-kut',
  33.     '1izmal8-qtaxi',
  34.     '1izmal8-sam',
  35.     '1izmal8-vvad',
  36.     '1kazach8s1-intg',
  37.     '1nkuznper10a-mteb',
  38.     '1pavl3-mar',
  39.     '1shemper15-atom',
  40.     '1shipk3-kbor',
  41.     '1tvyam2-pm',
  42.     '1volkper9k2-ol',
  43.     '2brest46k1-dr',
  44.     '2brest5-ran',
  45.     '2vmih5-isk'
  46. ];
  47. if (sizeof($argv) > 1) {
  48.     $logins = array_slice($argv, 1);
  49. }
  50.  
  51. // Server options
  52. $baseUrl = "http://ts.off.rinet.net:3380/test_restored/hs/Kassa/GetBills";
  53. $customResolve = ['ts.off.rinet.net:3380:195.54.192.62'];
  54. $userAgent = 'Rinet API';
  55. $username = 'online_kassa';
  56. $password = '1122334455';
  57. $connectTimeout = 10;
  58. $timeout = 10;
  59.  
  60. // Curl setopt wrapper
  61. function _curl_setopt($ch, $option, $value) {
  62.     if (!curl_setopt($ch, $option, $value))
  63.         throw new TestFailedInt('Curl setopt failed: '.curl_error($ch));
  64. }
  65.  
  66. // Expect functions
  67. function expect_fields($data, $fields, $msg = 'data') {
  68.     if (!is_array($fields)) $fields = [$fields];
  69.     foreach ($fields as $field)  {
  70.         if (!array_key_exists($field, $data))
  71.             throw new TestFailedInt('Cannot find field "'.$field.'" in '.$msg);
  72.     }
  73. }
  74. function expect_array($data, $fields = NULL, $msg = 'data') {
  75.     if (isset($fields)) {
  76.         if (!is_array($fields)) $fields = [$fields];
  77.         foreach ($fields as $field)  {
  78.             if (!array_key_exists($field, $data))
  79.                 throw new TestFailedInt('Missing field "'.$field.'" in '.$msg);
  80.             if (!is_array($data[$field]))
  81.                 throw new TestFailedInt('Field "'.$field.'" is not array in '.$msg);
  82.         }
  83.     } else {
  84.         if (!is_array($data))
  85.             throw new TestFailedInt($msg.' is not array');
  86.     }
  87. }
  88.  
  89. // Test
  90. $totalBills = 0;
  91. $garbageOut = 0;
  92. foreach ($logins as $login) {
  93.     try {
  94.         $url = $baseUrl.'?login='.urlencode($login);
  95.        
  96.         // cUrl init
  97.         $ch = curl_init();
  98.         if (!$ch) throw new Exception('Curl init failed');
  99.         _curl_setopt($ch, CURLOPT_URL, $url);
  100.         _curl_setopt($ch, CURLOPT_HTTPHEADER, [
  101.             'Accept: application/json',
  102.             'User-agent: '.$userAgent,
  103.             'Authorization: Basic '.base64_encode($username.':'.$password)
  104.         ]);
  105.         _curl_setopt($ch, CURLOPT_HEADER, 0);
  106.         _curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  107.         _curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $connectTimeout);
  108.         _curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  109.         if (!empty($customResolve)) {
  110.             _curl_setopt($ch, CURLOPT_RESOLVE, $customResolve);
  111.         }
  112.        
  113.         // cUrl exec
  114.         $data = curl_exec($ch);
  115.         if (!$data) {
  116.             throw new TestFailedInt('Curl request failed: '.curl_error($ch));
  117.         }
  118.        
  119.         // Clear garbage
  120.         $newData = preg_replace('/[\x00-\x1F\x7F]/u', '', $data);
  121.         $bom = pack('H*','EFBBBF');
  122.         $newData = preg_replace("/^$bom/", '', $newData);
  123.         if ($newData != $data) {
  124.             $garbageOut++;
  125.             $data = $newData;
  126.         }
  127.        
  128.        
  129.         // Decode
  130.         $decodedData = json_decode($data, true);
  131.         if ($decodedData == false) {
  132.             print "FAILED $login: cannot decode json: ".json_last_error()." data: \"$data\"\n";
  133.         }
  134.        
  135.         // Parse
  136.         expect_array($decodedData, NULL, 'API result');
  137.         expect_array($decodedData, 'result', 'API result');
  138.         foreach ($decodedData['result'] as $iRes => $result) {
  139.             $resName = 'result['.$iRes.']';
  140.             expect_fields($result, [
  141.                 "bill_login",
  142.                 "bill_contractName",
  143.                 "bill_inn",
  144.                 "bill_num",
  145.                 "bill_sum",
  146.                 "bill_pay",
  147.                 "bill_realiz"
  148.             ], $resName);
  149.             expect_array($result, "row_set", $resName);
  150.             foreach ($result['row_set'] as $iRow => $row) {
  151.                 $rowName = $resName.'["row_set"]['.$iRow.']';
  152.                 expect_array($row, NULL, $rowName);
  153.                 expect_fields($row, [
  154.                     "row_descr",
  155.                     "row_sum",
  156.                     "row_ndsrate"
  157.                 ], $rowName);
  158.                 $totalBills++;
  159.             }
  160.         }
  161.        
  162.        
  163.     } catch (TestFailedInt $e) {
  164.         throw new TestFailed("FAILED $login at line ".$e->getLine().": ".$e->getMessage());
  165.     }
  166. }
  167.  
  168. print "Test completed succesfully. ".sizeof($logins)." logins, $totalBills bills, "
  169.     .($garbageOut? "$garbageOut garbage bytes found and cleaned out" : "no garbage bytes found").".\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement