[your code here]

allnewso nattawatll9525 at gmail.com
Fri Dec 15 23:29:25 UTC 2017


namespace  Ethplorer \ UnitTest ;
ใช้ ข้อยกเว้น ;
ใช้ PHPUnit_Framework_TestCase ;
/ **
  * ทดสอบ Ethplorer API
  * /
class  Api_Test  ขยาย PHPUnit_Framework_TestCase {
     protected  $ url  =  ' https://api.ethplorer.io/ ' ;
     / **
      * @return void
      * /
     การ ตั้งค่าฟังก์ชัน สาธารณะ () {
         parent :: setUp ();
     }
     / **
      * @ปก
      * /
      ฟังก์ชัน สาธารณะtestGetTokenInfo_OK () {
         $ cmd  =  ' getTokenInfo ' ;
         $ testAddress  =  ' '
ETH: 0

Transactions out: 0

This is a contract address

  ;
         $ aResult  =  $ this -> rq ( $ cmd , $ testAddress , 
array ( ' apiKey '  =>  ' freekey ' ));
         $ this -> assertTrue (
             is_array ( $ aResult ),
             sprintf (
                 "การตอบสนองที่ไม่ถูกต้องที่ได้รับ: \ n % s " ,
                 var_export ( $ aResult , TRUE )
             )
         );
         $ aMandatoryFields  =  array ( " address " , " name " , " 
decimals " , "สัญลักษณ์" , " totalSupply " , " owner " , " totalIn " 
, " totalOut " , " holdersCount " , " issuancesCount " , " 
countOps " );
         $ this -> checkArray ( $ aResult , $ aMandatoryFields );
         $ this -> assertEquals ( strtolower ( $ testAddress ), $ 
aResult [ ' address ' ]);
         $ this -> assertEquals ( ' THBEX ' , $ aResult [ ' name ' 
]);
         $ this -> assertEquals ( " 4 " , $ aResult [ ' decimals ' 
]);
         $ this -> assertEquals ( ' THBEX ' , $ aResult [ ' symbol 
' ]);
     }
     / **
      * @ปก
      * /
     ประชาชน ฟังก์ชั่น testGetTokenInfo_Errors () {
         $ cmd  =  ' getTokenInfo ' ;
         $ testAddress  =  ' ' 
0xFf71Cb760666Ab06aa73f34995b42dd4b85ea07b ;
         $ testAddresses  =  array (
             // address => รหัสข้อผิดพลาด
             ' '
ETH: 0

Transactions out: 0

This is a contract address

      =>  150 , //สัญญา แต่ไม่ใช่โทเค็น
             ' ' 0xf3763c30dd6986b53402d41a8552b8f7f6a6089c     => 
  150 , //ไม่ใช่สัญญา
             ' '0xff3763c30dd6986b53402d41a8552b8f7f6a6089c    =>  
104 , //รูปแบบที่อยู่ไม่ถูกต้อง
             ' f3763c30dd6986b53402d41a8552b8f7f6a6089c '       => 
  104 , //รูปแบบที่อยู่ไม่ถูกต้อง
         );
         foreach ( $ testAddresses  เป็น $ address  =>  $ code ) {
             $ aResult  =  $ this -> rq ( $ cmd , $ address , 
array ( ' apiKey '  =>  ' freekey ' ));
             $ this -> assertTrue (
                 is_array ( $ aResult ) &&  isset ( $ aResult [ ' 
error ' ]) &&  is_array ( $ aResult [ ' error ' ]),
                 sprintf (
                     "การตอบสนองที่ไม่ถูกต้องที่ได้รับ: \ n % s " ,
                     var_export ( $ aResult , TRUE )
                 )
             );
             $ this -> assertEquals ( $ code , $ aResult [ ' error 
' ] [ ' code ' ]);
         }
         //ไม่มีคีย์ API
         $ aResult  =  $ this -> rq ( $ cmd , $ testAddress );
         $ this -> assertEquals ( 1 , $ aResult [ ' error ' ] [ ' 
code ' ]);
         //คีย์ API ไม่ถูกต้อง
         $ aResult  =  $ this -> rq ( $ cmd , $ testAddress , 
array ( ' apiKey '  =>  ' freekey1 ' ));
         $ this -> assertEquals ( 1 , $ aResult [ ' error ' ] [ ' 
code ' ]);
     }
     / **
      * @ปก
      * /
     ประชาชน ฟังก์ชั่น testGetAddressInfo_OK () {
         $ cmd  =  ' getAddressInfo ' ;
         //ไม่โทเค็นไม่ได้ทำสัญญา
         $ testAddress  =  ' ' 
0x01763c30dd6986b53402d41a8552b8f7f6a6089b ;
         $ aResult  =  $ this -> rq ( $ cmd , $ testAddress , 
array ( ' apiKey '  =>  ' freekey ' ));
         $ aMandatoryFields  =  array (
             " ETH " , " ETH.balance " , " ETH.totalIn " , " 
ETH.totalOut " ,
             "ที่อยู่" , " countTxs " ,
         );
         $ this -> checkArray ( $ aResult , $ aMandatoryFields );
         $ this -> assertEquals ( strtolower ( $ testAddress ), $ 
aResult [ ' address ' ]);
         //ไม่โทเค็น แต่สัญญา
         $ testAddress  =  ' ' 
0xf3763c30dd6986b53402d41a8552b8f7f6a6089b ;
         $ aResult  =  $ this -> rq ( $ cmd , $ testAddress , 
array ( ' apiKey '  =>  ' freekey ' ));
         $ aMandatoryFields  =  array (
             " ETH " , " ETH.balance " , " ETH.totalIn " , " 
ETH.totalOut " ,
             "ที่อยู่" , " countTxs " ,
             " contractInfo " , " contractInfo.creatorAddress " , 
" contractInfo.transactionHash " , " contractInfo.timestamp "
         );
         $ this -> checkArray ( $ aResult , $ aMandatoryFields );
         $ this -> assertEquals ( strtolower ( $ testAddress ), $ 
aResult [ ' address ' ]);
         $ this -> assertEquals ( " " 
0xc2be1c765d622bcfa3ab30bedb508b633ab79217 , $ aResult [ ' 
contractInfo ' ] [ ' creatorAddress ' ]);
         $ this -> assertEquals ( " 
"0xbccde49492ff509f9a162de06574e63bc4d2038b96a13ca5935c9c1e97a4278d , $ aResult [ ' contractInfo ' ] [ ' transactionHash ' ]);
         $ this -> assertEquals ( 1474030125 , $ aResult [ ' 
contractInfo ' ] [ ' timestamp ' ]);
         // Token
         $ testAddress  =  ' ' 
0xFf71Cb760666Ab06aa73f34995b42dd4b85ea07b ;
         $ aResult  =  $ this -> rq ( $ cmd , $ testAddress , 
array ( ' apiKey '  =>  ' freekey ' ));
         $ aMandatoryFields  =  array (
             " ETH " , " ETH.balance " , " ETH.totalIn " , " 
ETH.totalOut " ,
             "ที่อยู่" , " contractInfo " , " countTxs " ,
             " tokenInfo " , " tokenInfo.address " , " 
tokenInfo.name " , " tokenInfo.decimals " , " 
tokenInfo.totalSupply "
         );
         $ this -> checkArray ( $ aResult , $ aMandatoryFields );
         $ this -> assertEquals ( strtolower ( $ testAddress ), $ 
aResult [ ' address ' ]);
         $ this -> assertEquals ( ' THBEX ' , $ aResult [ ' 
tokenInfo ' ] [ ' name ' ]);
         $ this -> assertEquals ( " 4 " , $ aResult [ ' tokenInfo 
' ] [ ' decimals ' ]);
         $ this -> assertEquals ( ' THBEX ' , $ aResult [ ' 
tokenInfo ' ] [ 'สัญลักษณ์' ]);
     }
     / **
      * @ปก
      * /
     ประชาชน ฟังก์ชั่น testGetAddressInfo_Errors () {
         $ cmd  =  ' getAddressInfo ' ;
         $ testAddress  =  ' ' 
0xFf71Cb760666Ab06aa73f34995b42dd4b85ea07b ;
         //รูปแบบที่อยู่ไม่ถูกต้อง
         $ aResult  =  $ this -> rq ( $ cmd , str_replace ( ' 0x ' 
, ' xx ' , $ testAddress ));
         $ this -> assertEquals ( 1 , $ aResult [ ' error ' ] [ ' 
code ' ]);
         //ไม่มีคีย์ API
         $ aResult  =  $ this -> rq ( $ cmd , $ testAddress );
         $ this -> assertEquals ( 1 , $ aResult [ ' error ' ] [ ' 
code ' ]);
         //คีย์ API ไม่ถูกต้อง
         $ aResult  =  $ this -> rq ( $ cmd , $ testAddress , 
array ( ' apiKey '  =>  ' freekey1 ' ));
         $ this -> assertEquals ( 1 , $ aResult [ ' error ' ] [ ' 
code ' ]);
     }
     / **
      * @ปก
      * /
     ประชาชน ฟังก์ชั่น testGetTxInfo_OK () {
         $ cmd  =  ' getTxInfo ' ;
         //ไม่เป็นรายการโทเค็น
         $ tx  =  ' 
'0xd98FF0f44e4700c26bcf00413bf2d3aaaf1d4e8d306b68dea4bcbd940b9063a0 ;
         $ aResult  =  $ this -> rq ( $ cmd , $ tx , array ( ' 
apiKey '  =>  ' freekey ' ));
         $ aMandatoryFields  =  array ( " hash " , " blockNumber " 
, " confirmations " , " success " , " from " , " to " , " value " 
, " input " , " gasLimit " , " gasUsed " , " logs " );
         $ this -> checkArray ( $ aResult , $ aMandatoryFields );
         $ this -> assertEquals ( strtolower ( $ tx ), $ aResult [ 
' hash ' ]);
         // $ this-> assertEquals (strtolower ($ tx), $ aResult 
['timestamp']);
         $ this -> assertEquals ( 759019 , $ aResult [ ' 
blockNumber ' ]);
         $ this -> assertEquals ( true , $ aResult [ ' success ' 
]);
         $ this -> assertEquals ( " " 
0x4bb96091ee9d802ed039c4d1a5f6216f90f81b01 , $ aResult [ ' from ' 
]);
         $ this -> assertEquals ( " " 
0xc1c9c914552dc4e110c57e6d946870c5752c7aac , $ aResult [ ' to ' 
]);
         $ this -> assertEquals ( 0 , $ aResult [ ' value ' ]);
         $ this -> assertEquals ( " 0x " , $ aResult [ ' input ' 
]);
         $ this -> assertEquals ( 41000 , $ aResult [ ' gasLimit ' 
]);
         $ this -> assertEquals ( 0 , $ aResult [ ' gasUsed ' ]);
         $ this -> assertEquals ( 0 , count ( $ aResult [ ' logs ' 
]));
         // การทำธุรกรรมโทเค็นด้วยการดำเนินการครั้งเดียว
         $ tx  =  ' 
'0x91D6024517497c5740e1d8a4786779c257caa6d852b7c7365a0368e0ab2ebf86 ;
         $ aResult  =  $ this -> rq ( $ cmd , $ tx , array ( ' 
apiKey '  =>  ' freekey ' ));
         $ aMandatoryFields  =  array ( " hash " , / * 
"timestamp", * /  " blockNumber " , " confirmations " , " success 
" , " from " , " to " , " value " , " input " , " gasLimit " , " 
gasUsed " , " logs ", "การดำเนินงาน" );
         $ this -> checkArray ( $ aResult , $ aMandatoryFields );
         $ this -> assertEquals ( strtolower ( $ tx ), $ aResult [ 
' hash ' ]);
         // $ this-> assertEquals (strtolower ($ tx), $ aResult 
['timestamp']);
         $ this -> assertEquals ( 2817933 , $ aResult [ ' 
blockNumber ' ]);
         $ this -> assertEquals ( true , $ aResult [ ' success ' 
]);
         $ this -> assertEquals ( " "
ETH: 0.260857041

Transactions out: 1,085

  , $ aResult [ ' from ' ]);
         $ this -> assertEquals ( " " 
0x48c80f1f4d53d5951e5d5438b54cba84f29f32a5 , $ aResult [ ' to ' 
]);
         $ this -> assertEquals ( 0 , $ aResult [ ' value ' ]);
         $ this -> assertEquals ( 63000 , $ aResult [ ' gasLimit ' 
]);
         $ this -> assertEquals ( 37274 , $ aResult [ ' gasUsed ' 
]);
         $ this -> assertEquals ( 1 , count ( $ aResult [ ' logs ' 
]));
         $ this -> assertEquals ( " " 
0x48c80f1f4d53d5951e5d5438b54cba84f29f32a5 , $ aResult [ ' logs ' 
] [ 0 ] [ ' address ' ]);
         $ this -> assertEquals ( " 
"0x000000000000000000000000000000000000000000000005e750ea1d10a28000 , $ aResult [ 'บันทึก' ] [ 0 ] [ 'ข้อมูล' ]);
         $ this -> assertEquals ( 3 , count ( $ aResult [ ' logs ' 
] [ 0 ] [ ' topics ' ])));
         $ this -> assertEquals ( " 
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef , $ aResult [ 'ล็อก' ] [ 0 ] [ 'หัวข้อ' ] [ 0 ]);
         $ this -> assertEquals ( 1 , count ( $ aResult [ ' 
operations ' ]));
         $ this -> assertEquals ( " " 
0xcab65db421ec1afdfb3a18fedb82ba24142e5e45 , $ aResult [ ' 
operations ' ] [ 0 ] [ ' from ' ]);
         $ this -> assertEquals ( " " 
0x6fc51e87f55bd76ef83d6dc622ea9ad4ba72a056 , $ aResult [ ' 
operations ' ] [ 0 ] [ ' to ' ]);
         $ this -> assertEquals ( 1481877751 , $ aResult [ ' 
operations ' ] [ 0 ] [ ' timestamp ' ]);
         $ this -> assertEquals ( strtolower ( $ tx ), $ aResult [ 
' operations ' ] [ 0 ] [ ' transactionHash ' ]);
         $ this -> assertEquals ( " transfer " , $ aResult [ ' 
operations ' ] [ 0 ] [ ' type ' ]);
         $ this -> assertEquals ( " 108901800000000000000 " , $ 
aResult [ ' operations ' ] [ 0 ] [ ' value ' ]);
         $ this -> assertEquals ( " REP " , $ aResult [ ' 
operations ' ] [ 0 ] [ ' tokenInfo ' ] [ 'สัญลักษณ์' ]);
         // การทำธุรกรรมโทเค็นกับการดำเนินการหลายรายการ: 
เราจะตรวจสอบใบสั่งงานเท่านั้น
         $ tx  =  ' 
'0xe1a33ccdf62f9b4d0dd6f369a61e01120be151275794163c38fb7e890e428dc7 ;
         $ aResult  =  $ this -> rq ( $ cmd , $ tx , array ( ' 
apiKey '  =>  ' freekey ' ));
         $ this -> assertEquals ( 2 , count ( $ aResult [ ' 
operations ' ]));
         $ this -> assertEquals ( " " 
0x96477a1c968a0e64e53b7ed01d0d6e4a311945c2 , $ aResult [ ' 
operations ' ] [ 0 ] [ ' tokenInfo ' ] [ ' address ' ]);
         $ this -> assertEquals ( " " 
0xc66ea802717bfb9833400264dd12c2bceaa34a6d , $ aResult [ ' 
operations ' ] [ 1 ] [ ' tokenInfo ' ] [ ' address ' ]);
     }
     / **
      * @ปก
      * /
     ประชาชน ฟังก์ชั่น testGetTxInfo_Error () {
         $ cmd  =  ' getTxInfo ' ;
         $ tx  =  ' 
'0xd98FF0f44e4700c26bcf00413bf2d3aaaf1d4e8d306b68dea4bcbd940b9063a1 ;
         //ไม่รู้จัก hash
         $ aResult  =  $ this -> rq ( $ cmd , $ tx , array ( ' 
apiKey '  =>  ' freekey ' ));
         $ this -> assertEquals ( 404 , $ aResult [ ' error ' ] [ 
' code ' ]);
         //รูปแบบแฮชไม่ถูกต้อง
         $ aResult  =  $ this -> rq ( $ cmd , str_replace ( ' 0x ' 
, ' xx ' , $ tx ), array ( ' apiKey '  =>  ' freekey ' ));
         $ this -> assertEquals ( 102 , $ aResult [ ' error ' ] [ 
' code ' ]);
         //ไม่มีคีย์ API
         $ aResult  =  $ this -> rq ( $ cmd , $ tx );
         $ this -> assertEquals ( 1 , $ aResult [ ' error ' ] [ ' 
code ' ]);
         //คีย์ API ไม่ถูกต้อง
         $ aResult  =  $ this -> rq ( $ cmd , $ tx , array ( ' 
apiKey '  =>  ' freekey1 ' ));
         $ this -> assertEquals ( 1 , $ aResult [ ' error ' ] [ ' 
code ' ]);
     }
      ฟังก์ชัน สาธารณะtestGetTokenHistory_OK () {
         $ cmd  =  ' getTokenHistory ' ;
         $ testAddress  =  FALSE ;
         $ aResult  =  $ this -> rq ( $ cmd , $ testAddress , 
array ( ' apiKey '  =>  ' freekey ' ));
         $ aMandatoryFields  =  array ( ' operations ' );
         $ this -> checkArray ( $ aResult , $ aMandatoryFields );
         $ this -> assertEquals ( 10 , count ( $ aResult [ ' 
operations ' ]));
         $ aOperation  =  $ aResult [ ' operations ' ] [ 0 ];
         $ aMandatoryFields  =  array ( 'จาก' , 'ถึง' , ' timestamp 
' , ' tokenInfo ' , ' tokenInfo.address ' , ' 
tokenInfo.totalSupply ' , ' transactionHash ' , ' type ' , ' 
value ' );
         $ this -> checkArray ( $ aOperation , $ aMandatoryFields 
);
         $ testAddress  =  ' ' 
0xFf71Cb760666Ab06aa73f34995b42dd4b85ea07b ;
         $ aResult  =  $ this -> rq ( $ cmd , $ testAddress , 
array ( ' apiKey '  =>  ' freekey ' ));
         $ aMandatoryFields  =  array ( ' operations ' );
         $ this -> checkArray ( $ aResult , $ aMandatoryFields );
         $ this -> assertEquals ( 10 , count ( $ aResult [ ' 
operations ' ]));
         $ aOperation  =  $ aResult [ ' operations ' ] [ 0 ];
         $ aMandatoryFields  =  array ( 'จาก' , 'ถึง' , ' timestamp 
' , ' tokenInfo ' , ' tokenInfo.address ' , ' 
tokenInfo.totalSupply ' , ' transactionHash ' , ' type ' , ' 
value ' );
         $ this -> checkArray ( $ aOperation , $ aMandatoryFields 
);
         สำหรับ ( $ i = 0 ; $ i < 9 ; $ i ++ ) {
             $ this -> assertEquals ( ' ' 
0xff71Cb760666ab06aa73f34995b42dd4b85ea07b , $ aResult [ ' 
operations ' ] [ $ i ] [ ' tokenInfo ' ] [ ' address ' ]);
         }
         // Check Type
         $ testAddress  =  ' '
ETH: 0

Transactions out: 0

This is a contract address

  ;
         $ aResult  =  $ this -> rq ( $ cmd , $ testAddress , 
array ( ' apiKey '  =>  ' freekey ' , ' type '  =>  ' issuance ' 
));
         $ aMandatoryFields  =  array ( ' operations ' );
         $ this -> checkArray ( $ aResult , $ aMandatoryFields );
         $ this -> assertEquals ( 3 , count ( $ aResult [ ' 
operations ' ]));
         สำหรับ ( $ i = 0 ; $ i < 2 ; $ i ++ ) {
             $ this -> assertEquals ( ' issuance ' , $ aResult [ ' 
operations ' ] [ $ i ] [ ' type ' ]);
         }
         //ไม่ใช่โทเค็นการดำเนินการใด ๆ
         $ testAddress  =  ' '
ETH: 0

Transactions out: 0

  ;
         $ aResult  =  $ this -> rq ( $ cmd , $ testAddress , 
array ( ' apiKey '  =>  ' freekey ' , ' type '  =>  ' issuance ' 
));
         $ aMandatoryFields  =  array ( ' operations ' );
         $ this -> checkArray ( $ aResult , $ aMandatoryFields );
         $ this -> assertEquals ( 0 , count ( $ aResult [ ' 
operations ' ]));
         // Check Limit
         $ testAddress  =  ' '
ETH: 0

Transactions out: 0

This is a contract address

  ;
         $ aResult  =  $ this -> rq ( $ cmd , $ testAddress , 
array ( ' apiKey '  =>  ' freekey ' , ' limit '  =>  5 ));
         $ aMandatoryFields  =  array ( ' operations ' );
         $ this -> checkArray ( $ aResult , $ aMandatoryFields );
         $ this -> assertEquals ( 5 , count ( $ aResult [ ' 
operations ' ]));
         //ตรวจสอบขีด จำกัด สูงสุด
         $ aResult  =  $ this -> rq ( $ cmd , $ testAddress , 
array ( ' apiKey '  =>  ' freekey ' , ' limit '  =>  500 ));
         $ aMandatoryFields  =  array ( ' operations ' );
         $ this -> checkArray ( $ aResult , $ aMandatoryFields );
         $ this -> assertEquals ( 10 , count ( $ aResult [ ' 
operations ' ]));
     }
     / **
      * @ปก
      * /
      ฟังก์ชัน สาธารณะtestGetTokenHistory_Error () {
         $ cmd  =  ' getTokenHistory ' ;
         $ testAddress  =  '
ETH: 0

Transactions out: 0

This is a contract address

' ;
         //รูปแบบที่อยู่ไม่ถูกต้อง
         $ aResult  =  $ this -> rq ( $ cmd , str_replace ( ' 0x ' 
, ' xx ' , $ testAddress ), อาร์เรย์ ( ' apiKey '  =>  ' freekey ' 
));
         $ this -> assertEquals ( 104 , $ aResult [ ' error ' ] [ 
' code ' ]);
         //ไม่มีคีย์ API
         $ aResult  =  $ this -> rq ( $ cmd , $ testAddress );
         $ this -> assertEquals ( 1 , $ aResult [ ' error ' ] [ ' 
code ' ]);
         //คีย์ API ไม่ถูกต้อง
         $ aResult  =  $ this -> rq ( $ cmd , $ testAddress , 
array ( ' apiKey '  =>  ' freekey1 ' ));
         $ this -> assertEquals ( 1 , $ aResult [ ' error ' ] [ ' 
code ' ]);
     }
     / **
      * คำขอ API
      * * * *
      * @param type $ cmd
      * @param type $ subject
      * @ พระรามอาร์เรย์ $ aParameters
      * @return type
      * /
     การคุ้มครอง การทำงาน RQ ( $ cmd , $ เรื่อง =  ' ' , อาร์เรย์ $ 
aParameters  =  อาร์เรย์ ()) {
         $ URL  =  $ นี้-> URL  $ cmd ;
         if ( $ subject ) {
             $ URL  =  $ URL  ' / ' $ subject ;
         }
         if ( ! empty ( $ aParameters )) {
             $ URL  =  $ URL  ' ? ' . http_build_query ( $ 
aParameters );
         }
         $ json  =  file_get_contents ( $ url );
         $ aResult  =  json_decode ( $ json , TRUE );
         return  $ aResult ;
     }
     / **
      * ตรวจสอบว่าอาร์เรย์มีฟิลด์ระบุทั้งหมดหรือไม่
      * * * *
      * @ พระรามอาร์เรย์ $ aResult
      * @ พระรามอาร์เรย์ $ aMandatoryFields
      * /
      ฟังก์ชันที่ ได้รับการป้องกันcheckArray ( array  $ aResult , array  $ 
aMandatoryFields ) {
         foreach ( $ aMandatoryFields  as  $ field ) {
             $ fieldOriginal  =  $ field ;
             if ( FALSE  ! ==  strpos ( $ field , ' . ' )) {
                 $ field  =  explode ( ' . ' , $ field );
             }
             $ this -> assertTrue (
                 is_array ( ฟิลด์ $ ) หรือไม่? isset ( $ aResult [ $ 
field [ 0 ]] [ $ field [ 1 ]]): isset ( $ aResult [ $ field ]),
                 sprintf (
                     " Field { $ fieldOriginal } ไม่อยู่ในการตอบสนอง: 
\ n % s " ,
                     var_export ( $ aResult , TRUE )
                 )
             );
         }
     }
}



More information about the Digitalmars-d mailing list