Web3 apis and test.

This commit is contained in:
sc0Vu 2018-01-12 10:51:25 +08:00
parent 2eefb14b64
commit e6adbdd303
5 changed files with 56 additions and 6 deletions

View File

@ -16,6 +16,13 @@ use Web3\Methods\EthMethod;
class ClientVersion extends EthMethod class ClientVersion extends EthMethod
{ {
/**
* validators
*
* @var array
*/
protected $validators = [];
/** /**
* inputFormatters * inputFormatters
* *

View File

@ -14,9 +14,19 @@ namespace Web3\Methods\Web3;
use InvalidArgumentException; use InvalidArgumentException;
use Web3\Methods\EthMethod; use Web3\Methods\EthMethod;
use Web3\Formatters\HexFormatter; use Web3\Formatters\HexFormatter;
use Web3\Validators\StringValidator;
class Sha3 extends EthMethod class Sha3 extends EthMethod
{ {
/**
* validators
*
* @var array
*/
protected $validators = [
StringValidator::class
];
/** /**
* inputFormatters * inputFormatters
* *

View File

@ -132,10 +132,19 @@ class Web3
} else { } else {
$methodObject = $this->methods[$method]; $methodObject = $this->methods[$method];
} }
$inputs = $methodObject->transform($arguments, $methodObject->inputFormatters); try {
$methodObject->arguments = $inputs; if ($methodObject->validate($arguments)) {
$inputs = $methodObject->transform($arguments, $methodObject->inputFormatters);
$this->provider->send($methodObject, $callback); $methodObject->arguments = $inputs;
$this->provider->send($methodObject, $callback);
}
} catch (\Exception $e) {
if (is_callable($callback) === true) {
call_user_func($callback, $e, null);
} else {
throw $e;
}
}
} }
} }

View File

@ -102,13 +102,13 @@ class Web3ApiTest extends TestCase
*/ */
public function testWrongParam() public function testWrongParam()
{ {
$this->expectException(RuntimeException::class); // $this->expectException(RuntimeException::class);
$web3 = $this->web3; $web3 = $this->web3;
$web3->sha3($web3, function ($err, $hash) { $web3->sha3($web3, function ($err, $hash) {
if ($err !== null) { if ($err !== null) {
return $this->fail($err->getMessage()); return $this->assertTrue($err instanceof RuntimeException);
} }
$this->assertTrue(true); $this->assertTrue(true);
}); });

View File

@ -54,4 +54,28 @@ class Web3BatchTest extends TestCase
$this->assertEquals($data[1], $this->testHash); $this->assertEquals($data[1], $this->testHash);
}); });
} }
/**
* testWrongParam
*
* @return void
*/
public function testWrongParam()
{
$this->expectException(RuntimeException::class);
$web3 = $this->web3;
$web3->batch(true);
$web3->clientVersion();
$web3->sha3($web3);
$web3->provider->execute(function ($err, $data) {
if ($err !== null) {
return $this->fail('Got error!');
}
$this->assertTrue(is_string($data[0]));
$this->assertEquals($data[1], $this->testHash);
});
}
} }