diff --git a/src/Methods/Shh/HasIdentity.php b/src/Methods/Shh/HasIdentity.php new file mode 100644 index 0000000..b166332 --- /dev/null +++ b/src/Methods/Shh/HasIdentity.php @@ -0,0 +1,61 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Shh; + +use InvalidArgumentException; +use Web3\Methods\EthMethod; +use Web3\Validators\IdentityValidator; + +class HasIdentity extends EthMethod +{ + /** + * validators + * + * @var array + */ + protected $validators = [ + IdentityValidator::class + ]; + + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = []; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = []; + + /** + * defaultValues + * + * @var array + */ + protected $defaultValues = []; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + // public function __construct($method='', $arguments=[]) + // { + // parent::__construct($method, $arguments); + // } +} \ No newline at end of file diff --git a/src/Shh.php b/src/Shh.php index 7e6c1f2..e1ccee2 100644 --- a/src/Shh.php +++ b/src/Shh.php @@ -38,7 +38,7 @@ class Shh * @var array */ private $allowedMethods = [ - 'shh_version' + 'shh_version', 'shh_newIdentity', 'shh_hasIdentity' ]; /** diff --git a/test/unit/ShhApiTest.php b/test/unit/ShhApiTest.php index 36d2445..9521b63 100644 --- a/test/unit/ShhApiTest.php +++ b/test/unit/ShhApiTest.php @@ -59,4 +59,59 @@ class ShhApiTest extends TestCase $this->assertEquals(mb_strlen($identity), 132); }); } + + /** + * testHasIdentity + * + * @return void + */ + public function testHasIdentity() + { + $shh = $this->shh; + $newIdentity = '0x' . implode('', array_fill(0, 120, '0')); + + $shh->hasIdentity($newIdentity, function ($err, $hasIdentity) { + if ($err !== null) { + return $this->fail($err->getMessage()); + } + $this->assertFalse($hasIdentity); + }); + + $shh->newIdentity(function ($err, $identity) use (&$newIdentity) { + if ($err !== null) { + return $this->fail($err->getMessage()); + } + $newIdentity = $identity; + + $this->assertEquals(mb_strlen($identity), 132); + }); + + $shh->hasIdentity($newIdentity, function ($err, $hasIdentity) { + if ($err !== null) { + return $this->fail($err->getMessage()); + } + $this->assertTrue($hasIdentity); + }); + } + + /** + * testWrongParam + * We transform data and throw invalid argument exception + * instead of runtime exception. + * + * @return void + */ + public function testWrongParam() + { + $this->expectException(RuntimeException::class); + + $shh = $this->shh; + + $shh->hasIdentity('0', function ($err, $hasIdentity) { + if ($err !== null) { + return $this->fail($err->getMessage()); + } + $this->assertTrue(true); + }); + } } \ No newline at end of file diff --git a/test/unit/ShhBatchTest.php b/test/unit/ShhBatchTest.php index 0af2786..02ad831 100644 --- a/test/unit/ShhBatchTest.php +++ b/test/unit/ShhBatchTest.php @@ -47,4 +47,28 @@ class ShhBatchTest extends TestCase $this->assertEquals(mb_strlen($data[1]), 132); }); } + + /** + * testWrongParam + * + * @return void + */ + public function testWrongParam() + { + $this->expectException(RuntimeException::class); + + $shh = $this->shh; + + $shh->batch(true); + $shh->version(); + $shh->hasIdentity('0'); + + $shh->provider->execute(function ($err, $data) { + if ($err !== null) { + return $this->fail('Got error!'); + } + $this->assertTrue(is_string($data[0])); + $this->assertFalse($data[1]); + }); + } } \ No newline at end of file