Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env php
- <?php
- /*
- * 1C API test Kassa/GetBills
- * usage: script
- * usage: script <login1, [login2...]>
- */
- class TestFailedInt extends Exception {}
- class TestFailed extends Exception {}
- // Test logins
- $logins = [
- '8P170737',
- 'm9-tts',
- 'master4-rn',
- '8P#160409',
- '1tvyam2-pm',
- 'khv11dc-mtel',
- 'm9-glavsv',
- 'tverb2-tass',
- '8P120207',
- '8P#120910',
- '8P#160615',
- '8P#161109',
- '8P#161132',
- '1gol35k3-olympia',
- '1izmal8-grad',
- '1izmal8-ip',
- '1izmal8-knst',
- '1izmal8-kut',
- '1izmal8-qtaxi',
- '1izmal8-sam',
- '1izmal8-vvad',
- '1kazach8s1-intg',
- '1nkuznper10a-mteb',
- '1pavl3-mar',
- '1shemper15-atom',
- '1shipk3-kbor',
- '1tvyam2-pm',
- '1volkper9k2-ol',
- '2brest46k1-dr',
- '2brest5-ran',
- '2vmih5-isk'
- ];
- if (sizeof($argv) > 1) {
- $logins = array_slice($argv, 1);
- }
- // Server options
- $baseUrl = "http://ts.off.rinet.net:3380/test_restored/hs/Kassa/GetBills";
- $customResolve = ['ts.off.rinet.net:3380:195.54.192.62'];
- $userAgent = 'Rinet API';
- $username = 'online_kassa';
- $password = '1122334455';
- $connectTimeout = 10;
- $timeout = 10;
- // Curl setopt wrapper
- function _curl_setopt($ch, $option, $value) {
- if (!curl_setopt($ch, $option, $value))
- throw new TestFailedInt('Curl setopt failed: '.curl_error($ch));
- }
- // Expect functions
- function expect_fields($data, $fields, $msg = 'data') {
- if (!is_array($fields)) $fields = [$fields];
- foreach ($fields as $field) {
- if (!array_key_exists($field, $data))
- throw new TestFailedInt('Cannot find field "'.$field.'" in '.$msg);
- }
- }
- function expect_array($data, $fields = NULL, $msg = 'data') {
- if (isset($fields)) {
- if (!is_array($fields)) $fields = [$fields];
- foreach ($fields as $field) {
- if (!array_key_exists($field, $data))
- throw new TestFailedInt('Missing field "'.$field.'" in '.$msg);
- if (!is_array($data[$field]))
- throw new TestFailedInt('Field "'.$field.'" is not array in '.$msg);
- }
- } else {
- if (!is_array($data))
- throw new TestFailedInt($msg.' is not array');
- }
- }
- // Test
- $totalBills = 0;
- $garbageOut = 0;
- foreach ($logins as $login) {
- try {
- $url = $baseUrl.'?login='.urlencode($login);
- // cUrl init
- $ch = curl_init();
- if (!$ch) throw new Exception('Curl init failed');
- _curl_setopt($ch, CURLOPT_URL, $url);
- _curl_setopt($ch, CURLOPT_HTTPHEADER, [
- 'Accept: application/json',
- 'User-agent: '.$userAgent,
- 'Authorization: Basic '.base64_encode($username.':'.$password)
- ]);
- _curl_setopt($ch, CURLOPT_HEADER, 0);
- _curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
- _curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $connectTimeout);
- _curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- if (!empty($customResolve)) {
- _curl_setopt($ch, CURLOPT_RESOLVE, $customResolve);
- }
- // cUrl exec
- $data = curl_exec($ch);
- if (!$data) {
- throw new TestFailedInt('Curl request failed: '.curl_error($ch));
- }
- // Clear garbage
- $newData = preg_replace('/[\x00-\x1F\x7F]/u', '', $data);
- $bom = pack('H*','EFBBBF');
- $newData = preg_replace("/^$bom/", '', $newData);
- if ($newData != $data) {
- $garbageOut++;
- $data = $newData;
- }
- // Decode
- $decodedData = json_decode($data, true);
- if ($decodedData == false) {
- print "FAILED $login: cannot decode json: ".json_last_error()." data: \"$data\"\n";
- }
- // Parse
- expect_array($decodedData, NULL, 'API result');
- expect_array($decodedData, 'result', 'API result');
- foreach ($decodedData['result'] as $iRes => $result) {
- $resName = 'result['.$iRes.']';
- expect_fields($result, [
- "bill_login",
- "bill_contractName",
- "bill_inn",
- "bill_num",
- "bill_sum",
- "bill_pay",
- "bill_realiz"
- ], $resName);
- expect_array($result, "row_set", $resName);
- foreach ($result['row_set'] as $iRow => $row) {
- $rowName = $resName.'["row_set"]['.$iRow.']';
- expect_array($row, NULL, $rowName);
- expect_fields($row, [
- "row_descr",
- "row_sum",
- "row_ndsrate"
- ], $rowName);
- $totalBills++;
- }
- }
- } catch (TestFailedInt $e) {
- throw new TestFailed("FAILED $login at line ".$e->getLine().": ".$e->getMessage());
- }
- }
- print "Test completed succesfully. ".sizeof($logins)." logins, $totalBills bills, "
- .($garbageOut? "$garbageOut garbage bytes found and cleaned out" : "no garbage bytes found").".\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement