From e6adbdd303281bfb5d6925464106c2a50ee9813e Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Fri, 12 Jan 2018 10:51:25 +0800 Subject: [PATCH] Web3 apis and test. --- src/Methods/Web3/ClientVersion.php | 7 +++++++ src/Methods/Web3/Sha3.php | 10 ++++++++++ src/Web3.php | 17 +++++++++++++---- test/unit/Web3ApiTest.php | 4 ++-- test/unit/Web3BatchTest.php | 24 ++++++++++++++++++++++++ 5 files changed, 56 insertions(+), 6 deletions(-) diff --git a/src/Methods/Web3/ClientVersion.php b/src/Methods/Web3/ClientVersion.php index 90f12fe..c119df1 100644 --- a/src/Methods/Web3/ClientVersion.php +++ b/src/Methods/Web3/ClientVersion.php @@ -16,6 +16,13 @@ use Web3\Methods\EthMethod; class ClientVersion extends EthMethod { + /** + * validators + * + * @var array + */ + protected $validators = []; + /** * inputFormatters * diff --git a/src/Methods/Web3/Sha3.php b/src/Methods/Web3/Sha3.php index c899f89..911aa26 100644 --- a/src/Methods/Web3/Sha3.php +++ b/src/Methods/Web3/Sha3.php @@ -14,9 +14,19 @@ namespace Web3\Methods\Web3; use InvalidArgumentException; use Web3\Methods\EthMethod; use Web3\Formatters\HexFormatter; +use Web3\Validators\StringValidator; class Sha3 extends EthMethod { + /** + * validators + * + * @var array + */ + protected $validators = [ + StringValidator::class + ]; + /** * inputFormatters * diff --git a/src/Web3.php b/src/Web3.php index d2b46fb..dcc792c 100644 --- a/src/Web3.php +++ b/src/Web3.php @@ -132,10 +132,19 @@ class Web3 } else { $methodObject = $this->methods[$method]; } - $inputs = $methodObject->transform($arguments, $methodObject->inputFormatters); - $methodObject->arguments = $inputs; - - $this->provider->send($methodObject, $callback); + try { + if ($methodObject->validate($arguments)) { + $inputs = $methodObject->transform($arguments, $methodObject->inputFormatters); + $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; + } + } } } diff --git a/test/unit/Web3ApiTest.php b/test/unit/Web3ApiTest.php index a46f504..c7c42fb 100644 --- a/test/unit/Web3ApiTest.php +++ b/test/unit/Web3ApiTest.php @@ -102,13 +102,13 @@ class Web3ApiTest extends TestCase */ public function testWrongParam() { - $this->expectException(RuntimeException::class); + // $this->expectException(RuntimeException::class); $web3 = $this->web3; $web3->sha3($web3, function ($err, $hash) { if ($err !== null) { - return $this->fail($err->getMessage()); + return $this->assertTrue($err instanceof RuntimeException); } $this->assertTrue(true); }); diff --git a/test/unit/Web3BatchTest.php b/test/unit/Web3BatchTest.php index c96e93b..5edaf54 100644 --- a/test/unit/Web3BatchTest.php +++ b/test/unit/Web3BatchTest.php @@ -54,4 +54,28 @@ class Web3BatchTest extends TestCase $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); + }); + } } \ No newline at end of file