From f231b23bb9507ebbb498d0a21000d68fc178d05e Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Tue, 6 Feb 2018 18:05:55 +0800 Subject: [PATCH] Fix toBn bug Fix $bn not define when call toBn with BigNumber instance. --- src/Utils.php | 11 +++++------ test/unit/UtilsTest.php | 11 +++++++---- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/Utils.php b/src/Utils.php index c1cd35d..2d88ce0 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -235,11 +235,8 @@ class Utils */ public static function toString($value) { - try { - $value = (string) $value; - } catch (\Exception $e) { - throw new RuntimeException('Cannot transform value to string!'); - } + $value = (string) $value; + return $value; } @@ -436,7 +433,9 @@ class Utils } else { $bn = new BigNumber($number); } - } elseif (!$number instanceof BigNumber){ + } elseif ($number instanceof BigNumber){ + $bn = $number; + } else { throw new InvalidArgumentException('toBn number must be BigNumber, string or int.'); } return $bn; diff --git a/test/unit/UtilsTest.php b/test/unit/UtilsTest.php index 3fb7d8b..6e98a62 100644 --- a/test/unit/UtilsTest.php +++ b/test/unit/UtilsTest.php @@ -3,6 +3,7 @@ namespace Test\Unit; use InvalidArgumentException; +use stdClass; use Test\TestCase; use phpseclib\Math\BigInteger as BigNumber; use Web3\Utils; @@ -410,19 +411,21 @@ class UtilsTest extends TestCase public function testToBn() { $bn = Utils::toBn(11); - $this->assertEquals($bn->toString(), '11'); $bn = Utils::toBn('0x12'); - $this->assertEquals($bn->toString(), '18'); $bn = Utils::toBn(0x12); - $this->assertEquals($bn->toString(), '18'); $bn = Utils::toBn('ae'); - $this->assertEquals($bn->toString(), '174'); + + $bn = Utils::toBn(new BigNumber(1)); + $this->assertEquals($bn->toString(), '1'); + + $this->expectException(InvalidArgumentException::class); + $bn = Utils::toBn(new stdClass); } } \ No newline at end of file