Fix json method to string and add tests.

This commit is contained in:
sc0Vu 2018-02-07 10:53:10 +08:00
parent b59a60f17e
commit 718a913902
2 changed files with 63 additions and 15 deletions

View File

@ -279,9 +279,9 @@ class Utils
*/ */
public static function toEther($number, $unit) public static function toEther($number, $unit)
{ {
if ($unit === 'ether') { // if ($unit === 'ether') {
throw new InvalidArgumentException('Please use another unit.'); // throw new InvalidArgumentException('Please use another unit.');
} // }
$wei = self::toWei($number, $unit); $wei = self::toWei($number, $unit);
$bnt = new BigNumber(self::UNITS['ether']); $bnt = new BigNumber(self::UNITS['ether']);
@ -347,7 +347,7 @@ class Utils
} elseif (!is_array($json)) { } elseif (!is_array($json)) {
throw new InvalidArgumentException('jsonMethodToString json must be array or stdClass.'); throw new InvalidArgumentException('jsonMethodToString json must be array or stdClass.');
} }
if (isset($json['name']) && (bool) strpos('(', $json['name']) === true) { if (isset($json['name']) && strpos($json['name'], '(') > 0) {
return $json['name']; return $json['name'];
} }
$typeName = []; $typeName = [];

View File

@ -198,7 +198,7 @@ class UtilsTest extends TestCase
$this->assertEquals($isAddressChecksum, false); $this->assertEquals($isAddressChecksum, false);
$this->expectException(InvalidArgumentException::class); $this->expectException(InvalidArgumentException::class);
$isAddressChecksum = Utils::isAddress(new stdClass); $isAddressChecksum = Utils::isAddressChecksum(new stdClass);
} }
/** /**
@ -225,12 +225,13 @@ class UtilsTest extends TestCase
public function testSha3() public function testSha3()
{ {
$str = Utils::sha3(''); $str = Utils::sha3('');
$this->assertNull($str); $this->assertNull($str);
$str = Utils::sha3('baz(uint32,bool)'); $str = Utils::sha3('baz(uint32,bool)');
$this->assertEquals(mb_substr($str, 0, 10), '0xcdcd77c0'); $this->assertEquals(mb_substr($str, 0, 10), '0xcdcd77c0');
$this->expectException(InvalidArgumentException::class);
$str = Utils::sha3(new stdClass);
} }
/** /**
@ -241,28 +242,34 @@ class UtilsTest extends TestCase
public function testToWei() public function testToWei()
{ {
$bn = Utils::toWei('0x1', 'wei'); $bn = Utils::toWei('0x1', 'wei');
$this->assertEquals($bn->toString(), '1'); $this->assertEquals($bn->toString(), '1');
$bn = Utils::toWei('18', 'wei'); $bn = Utils::toWei('18', 'wei');
$this->assertEquals($bn->toString(), '18'); $this->assertEquals($bn->toString(), '18');
$bn = Utils::toWei(1, 'wei'); $bn = Utils::toWei(1, 'wei');
$this->assertEquals($bn->toString(), '1'); $this->assertEquals($bn->toString(), '1');
$bn = Utils::toWei(0x11, 'wei'); $bn = Utils::toWei(0x11, 'wei');
$this->assertEquals($bn->toString(), '17'); $this->assertEquals($bn->toString(), '17');
$bn = Utils::toWei('1', 'ether'); $bn = Utils::toWei('1', 'ether');
$this->assertEquals($bn->toString(), '1000000000000000000'); $this->assertEquals($bn->toString(), '1000000000000000000');
$bn = Utils::toWei('0x5218', 'wei'); $bn = Utils::toWei('0x5218', 'wei');
$this->assertEquals($bn->toString(), '21016'); $this->assertEquals($bn->toString(), '21016');
try {
$toWei = Utils::toWei('0x5218', new stdClass);
} catch (InvalidArgumentException $e) {
$this->assertTrue($e !== null);
}
try {
$toWei = Utils::toWei('0x5218', 'test');
} catch (InvalidArgumentException $e) {
$this->assertTrue($e !== null);
}
} }
/** /**
@ -301,6 +308,11 @@ class UtilsTest extends TestCase
$this->assertEquals($bnq->toString(), '0'); $this->assertEquals($bnq->toString(), '0');
$this->assertEquals($bnr->toString(), '21016'); $this->assertEquals($bnr->toString(), '21016');
list($bnq, $bnr) = Utils::toEther('0x5218', 'ether');
$this->assertEquals($bnq->toString(), '21016');
$this->assertEquals($bnr->toString(), '0');
} }
/** /**
@ -334,6 +346,18 @@ class UtilsTest extends TestCase
$this->assertEquals($bnq->toString(), '21'); $this->assertEquals($bnq->toString(), '21');
$this->assertEquals($bnr->toString(), '16'); $this->assertEquals($bnr->toString(), '16');
try {
list($bnq, $bnr) = Utils::fromWei('0x5218', new stdClass);
} catch (InvalidArgumentException $e) {
$this->assertTrue($e !== null);
}
try {
list($bnq, $bnr) = Utils::fromWei('0x5218', 'test');
} catch (InvalidArgumentException $e) {
$this->assertTrue($e !== null);
}
} }
/** /**
@ -352,6 +376,14 @@ class UtilsTest extends TestCase
$methodString = Utils::jsonMethodToString($json); $methodString = Utils::jsonMethodToString($json);
$this->assertEquals($methodString, 'approve(address,uint256)'); $this->assertEquals($methodString, 'approve(address,uint256)');
$methodString = Utils::jsonMethodToString([
'name' => 'approve(address,uint256)'
]);
$this->assertEquals($methodString, 'approve(address,uint256)');
$this->expectException(InvalidArgumentException::class);
$methodString = Utils::jsonMethodToString('test');
} }
/** /**
@ -372,12 +404,28 @@ class UtilsTest extends TestCase
$this->assertEquals($jsonArrayDepth2, $jsonAssoc); $this->assertEquals($jsonArrayDepth2, $jsonAssoc);
$jsonArrayDepth2 = Utils::jsonToArray($jsonArrayDepth1, 2); $jsonArrayDepth2 = Utils::jsonToArray($jsonArrayDepth1, 2);
$this->assertEquals($jsonArrayDepth2, $jsonAssoc); $this->assertEquals($jsonArrayDepth2, $jsonAssoc);
$jsonArray = Utils::jsonToArray($this->testJsonMethodString); $jsonArray = Utils::jsonToArray($this->testJsonMethodString);
$this->assertEquals($jsonArray, $jsonAssoc); $this->assertEquals($jsonArray, $jsonAssoc);
try {
$jsonArray = Utils::jsonToArray($json, 0);
} catch (InvalidArgumentException $e) {
$this->assertTrue($e !== null);
}
try {
$jsonArray = Utils::jsonToArray(mb_substr($this->testJsonMethodString, 0, 50), 1);
} catch (InvalidArgumentException $e) {
$this->assertTrue($e !== null);
}
try {
$jsonArray = Utils::jsonToArray(0, 1);
} catch (InvalidArgumentException $e) {
$this->assertTrue($e !== null);
}
} }
/** /**