diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 30d7d85..0faa04d 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,9 +1,6 @@ name: Test -on: - push: - branches-ignore: - - 'master' +on: [push, pull_request] jobs: phpunit: @@ -11,19 +8,22 @@ jobs: strategy: matrix: - php-version: [7.4, 8.0, 8.1] + php-version: ['8.1', '8.2', '8.3'] steps: - uses: actions/checkout@v3 - uses: php-actions/composer@v5 with: php_version: ${{ matrix.php-version }} - php_extensions: bcmath gmp xdebug + args: --ignore-platform-reqs - uses: php-actions/phpunit@v3 with: configuration: phpunit.xml php_version: ${{ matrix.php-version }} php_extensions: bcmath gmp xdebug + version: 10 + env: + XDEBUG_MODE: coverage phpcs: runs-on: ubuntu-latest diff --git a/composer.json b/composer.json index c2c98c0..bbb81ce 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,13 @@ "": "src/" } }, + "autoload-dev": { + "psr-4": { + "BitPayKeyUtils\\UnitTest\\": "test/unit/BitPayKeyUtils" + } + }, "require": { + "php": "^8.1 || ^8.2 || ^8.3", "ext-bcmath": "*", "ext-openssl": "*", "ext-curl": "*", @@ -22,7 +28,7 @@ "ext-iconv": "*" }, "require-dev": { - "phpunit/phpunit": "^7.5 || ^9.0" + "phpunit/phpunit": "^10.0" }, "suggest": { "ext-gmp": "Required to use this package with GMP instead of BCMath" diff --git a/test/unit/BitPayKeyUtils/KeyHelper/KeyTest.php b/test/unit/BitPayKeyUtils/KeyHelper/KeyTest.php index 501dbb9..81d3fcd 100644 --- a/test/unit/BitPayKeyUtils/KeyHelper/KeyTest.php +++ b/test/unit/BitPayKeyUtils/KeyHelper/KeyTest.php @@ -1,121 +1,110 @@ -getTestedClass(); - $result = $testedObject::create(); - $this->assertInstanceOf(Key::class, $result); - } - - public function testGetIdWhenIdNotSet() - { - $testedObject = $this->getTestedClass(); - $result = $testedObject->getId(); - $this->assertEmpty($result); - } - - public function testGetIdWhenIdSet() - { - $id = 'test'; - $testedObject = $this->getTestedClass($id); - $result = $testedObject->getId(); - $this->assertEquals($id, $result); - } - - /** - * @throws ReflectionException - */ - public function testGetHex() - { - $testedObject = $this->getTestedClass(); - $exampleValue = 'test'; - - $this->setProtectedPropertyValue($testedObject, 'hex', $exampleValue); - $result = $testedObject->getHex(); - $this->assertEquals($exampleValue, $result); - } - - /** - * @throws ReflectionException - */ - public function testGetDec() - { - $testedObject = $this->getTestedClass(); - $exampleValue = 'test'; - - $this->setProtectedPropertyValue($testedObject, 'dec', $exampleValue); - $result = $testedObject->getDec(); - $this->assertEquals($exampleValue, $result); - } - - /** - * @throws Exception - */ - public function testSerialize() - { - $exampleId = 'test'; - - $testedObject = $this->getTestedClass($exampleId); - $result = $testedObject->serialize(); - $this->assertIsString($result); - $this->assertStringContainsString($exampleId, $result); - } - - public function testUnserialize() - { - $data = serialize(['id', 'x', 'y', 'hex', 'dec']); - - $testedObject = $this->getTestedClass(); - $this->assertEmpty($testedObject->getId()); - - $testedObject->unserialize($data); - - $this->assertEquals('id', $testedObject->getId()); - $this->assertEquals('x', $testedObject->getX()); - $this->assertEquals('y', $testedObject->getY()); - $this->assertEquals('hex', $testedObject->getHex()); - $this->assertEquals('dec', $testedObject->getDec()); - } - - /** - * @throws ReflectionException - */ - public function testIsGenerated() - { - $testedObject = $this->getTestedClass(); - $this->assertIsBool($testedObject->isGenerated()); - - $this->setProtectedPropertyValue($testedObject, 'hex', 'test'); - $this->assertTrue($testedObject->isGenerated()); - } - - /** - * @throws ReflectionException - */ - private function setProtectedPropertyValue(&$instance, $propertyName, $propertyValue) - { - $reflection = new \ReflectionProperty(get_class($instance), $propertyName); - $reflection->setAccessible(true); - $reflection->setValue($instance, $propertyValue); - } - - public function getTestedClass($id = null): Key - { - return new class($id) extends Key { - public function generate(): bool - { - return true; - } - - public function isValid(): bool - { - return true; - } - }; - } +getTestedClass(); + $result = $testedObject::create(); + self::assertInstanceOf(Key::class, $result); + } + + public function testGetIdWhenIdNotSet(): void + { + $testedObject = $this->getTestedClass(); + $result = $testedObject->getId(); + self::assertEmpty($result); + } + + public function testGetIdWhenIdSet(): void + { + $id = 'test'; + $testedObject = $this->getTestedClass($id); + $result = $testedObject->getId(); + self::assertSame($id, $result); + } + + public function testGetHex(): void + { + $testedObject = $this->getTestedClass(); + $exampleValue = 'test'; + + $this->setProtectedPropertyValue($testedObject, 'hex', $exampleValue); + $result = $testedObject->getHex(); + self::assertSame($exampleValue, $result); + } + + public function testGetDec(): void + { + $testedObject = $this->getTestedClass(); + $exampleValue = 'test'; + + $this->setProtectedPropertyValue($testedObject, 'dec', $exampleValue); + $result = $testedObject->getDec(); + self::assertSame($exampleValue, $result); + } + + public function testSerialize(): void + { + $exampleId = 'test'; + + $testedObject = $this->getTestedClass($exampleId); + $result = $testedObject->serialize(); + self::assertIsString($result); + self::assertStringContainsString($exampleId, $result); + } + + public function testUnserialize(): void + { + $data = serialize(['id', 'x', 'y', 'hex', 'dec']); + + $testedObject = $this->getTestedClass(); + self::assertEmpty($testedObject->getId()); + + $testedObject->unserialize($data); + + self::assertSame('id', $testedObject->getId()); + self::assertSame('x', $testedObject->getX()); + self::assertSame('y', $testedObject->getY()); + self::assertSame('hex', $testedObject->getHex()); + self::assertSame('dec', $testedObject->getDec()); + } + + public function testIsGenerated(): void + { + $testedObject = $this->getTestedClass(); + self::assertIsBool($testedObject->isGenerated()); + + $this->setProtectedPropertyValue($testedObject, 'hex', 'test'); + self::assertTrue($testedObject->isGenerated()); + } + + private function setProtectedPropertyValue(&$instance, $propertyName, $propertyValue): void + { + $reflection = new \ReflectionProperty(get_class($instance), $propertyName); + $reflection->setAccessible(true); + $reflection->setValue($instance, $propertyValue); + } + + private function getTestedClass($id = null): Key + { + return new class($id) extends Key { + public function generate(): bool + { + return true; + } + + public function isValid(): bool + { + return true; + } + }; + } } \ No newline at end of file diff --git a/test/unit/BitPayKeyUtils/KeyHelper/PrivateKeyTest.php b/test/unit/BitPayKeyUtils/KeyHelper/PrivateKeyTest.php index 76776b1..b082960 100644 --- a/test/unit/BitPayKeyUtils/KeyHelper/PrivateKeyTest.php +++ b/test/unit/BitPayKeyUtils/KeyHelper/PrivateKeyTest.php @@ -1,214 +1,209 @@ -createClassObject(); - $exampleValue = 'test'; - - $testedObject->setHex( $exampleValue ); - $realValue = $this->accessProtected( $testedObject, 'hex' ); - $this->assertEquals( $exampleValue, $realValue ); - } - - /** - * @throws Exception - */ - public function testGenerateReturnsObjectWithRandomHexIfHexIsNotSet() - { - $testedObject = $this->createClassObject(); - $result = $testedObject->generate(); - $realValue = $this->accessProtected( $result, 'hex' ); - $this->assertIsString( $realValue ); - } - - /** - * @throws Exception - */ - public function testGenerateReturnsObjectWithHexIfHexIsSet() - { - $testedObject = $this->createClassObject(); - $expectedValue = 'test'; - - $testedObject->setHex( $expectedValue ); - $result = $testedObject->generate(); - $realValue = $this->accessProtected( $result, 'hex' ); - $this->assertEquals( $expectedValue, $realValue ); - } - - public function testIsValidIfKeyNotSet() - { - $testedObject = $this->createClassObject(); - $this->assertFalse( $testedObject->isValid() ); - } - - public function testHasValidHexIfHexNotSet() - { - $testedObject = $this->createClassObject(); - $this->assertFalse( $testedObject->hasValidHex() ); - } - - public function testIsValid() - { - $testedObject = $this->createClassObject(); - $testedObject->setHex( self::EXAMPLE_HEX_STRING ); - $this->assertTrue( $testedObject->isValid() ); - } - - public function testSign() - { - $testedObject = $this->createClassObject(); - $testedObject->setHex( self::EXAMPLE_HEX_STRING ); - - try - { - $result = $testedObject->sign( 'test' ); - } - catch ( Exception $exception ) - { - $this->fail(); - } - - $this->assertIsString( $result ); - } - - public function testSignWhenNoHex() - { - $testedObject = $this->createClassObject(); - - $this->expectException( Exception::class ); - $this->expectExceptionMessage( 'The private key must be in hex format.' ); - - $testedObject->sign( 'test' ); - } - - public function testSignWhenDataEmpty() - { - $testedObject = $this->createClassObject(); - $testedObject->setHex( self::EXAMPLE_HEX_STRING ); - - $this->expectException( Exception::class ); - $this->expectExceptionMessage( 'You did not provide any data to sign' ); - - $testedObject->sign( [] ); - } - - public function testPemDecode() - { - $testedObject = $this->createClassObject(); - - $result = $testedObject->pemDecode( self::EXAMPLE_ECDSA_KEY ); - - $this->assertIsArray( $result ); - $this->assertEquals( '52f7b0b9e2a06703bc758cfc13e5370f7a04ec7528a728daab2ae14c21414a2c', $result[ 'private_key' ] ); - $this->assertEquals( - '045ecd459aae1074f01b9f737271d114f873a3737916f5e7dbb28da423fbdd0d359c482324df5d2ce771b7b510a50c13cfbae240b0a8216cf7da4da52999714234', - $result[ 'public_key' ] ); - } - - public function testPemDecodeTooShortPemException() - { - $testedObject = $this->createClassObject(); - - $this->expectException( Exception::class ); - $this->expectExceptionMessage( 'Invalid or corrupt secp256k1 key provided. Cannot decode the supplied PEM data.' ); - $testedObject->pemDecode( 'test' ); - } - - public function testPemDecodePemShouldThrowExceptionWhenCorruptKeyProvided() - { - $testedObject = $this->createClassObject(); - - $this->expectException( Exception::class ); - $this->expectExceptionMessage( 'Invalid or corrupt secp256k1 key provided. Cannot decode the supplied PEM data.' ); - $testedObject->pemDecode( - 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' - ); - } - - public function testPemEncode() - { - $testedObject = $this->createClassObject(); - - $beg_ec_text = '-----BEGIN EC PRIVATE KEY-----'; - $end_ec_text = '-----END EC PRIVATE KEY-----'; - - $exampleKeyPair = [ '52f7b0b9e2a06703bc758cfc13e5370f7a04ec7528a728daab2ae14c21414a2c', - '045ecd459aae1074f01b9f737271d114f873a3737916f5e7dbb28da423fbdd0d359c482324df5d2ce771b7b510a50c13cfbae240b0a8216cf7da4da529997142', - ]; - - $result = $testedObject->pemEncode( $exampleKeyPair ); - $this->assertIsString( $result ); - $this->assertStringContainsString( $beg_ec_text, $result ); - $this->assertStringContainsString( $end_ec_text, $result ); - } - - public function testPemEncodeCorruptSecp() - { - $testedObject = $this->createClassObject(); - - $exampleCorruptKeyPair = [ '52f7b0b9e2a06703bc758cfc13e5370f7a04ec7528a728daab2ae14c21414a2c', - '7dbb28da423fbdd0d359c482324df5d2ce771b7b510a50c13cfbae240b0a8216cf7da4da52999714234', - ]; - - $this->expectExceptionMessage( 'Invalid or corrupt secp256k1 keypair provided. Cannot decode the supplied PEM data.' ); - $this->expectException( Exception::class ); - $testedObject->pemEncode( $exampleCorruptKeyPair ); - } - - public function test__toString() - { - $testedObject = $this->createClassObject(); - - $this->assertEmpty( (string) $testedObject ); - } - - public function test__toStringWhenHexIsSet() - { - $testedObject = $this->createClassObject(); - $testedObject->setHex( self::EXAMPLE_HEX_STRING ); - - $this->assertEquals( self::EXAMPLE_HEX_STRING, (string) $testedObject ); - } - - public function testGetPublicKeyIfKeyIsNull() - { - $testedObject = $this->createClassObject(); - $result = $testedObject->getPublicKey(); - $this->assertInstanceOf( PublicKey::class, $result ); - } - - public function testGetPublicKeyIfKeyIsNotNull() - { - $testedObject = $this->createClassObject(); - - $firstResult = $testedObject->getPublicKey(); - $secondResult = $testedObject->getPublicKey(); - - $this->assertInstanceOf( PublicKey::class, $firstResult ); - $this->assertEquals( $firstResult, $secondResult ); - } - - private function createClassObject(): PrivateKey - { - return new PrivateKey(); - } - - private function accessProtected( $obj, $prop ) - { - $reflection = new ReflectionClass( $obj ); - $property = $reflection->getProperty( $prop ); - $property->setAccessible( true ); - - return $property->getValue( $obj ); - } +createClassObject(); + $exampleValue = 'test'; + + $testedObject->setHex($exampleValue); + $realValue = $this->accessProtected($testedObject, 'hex'); + self::assertSame($exampleValue, $realValue); + } + + public function testGenerateReturnsObjectWithRandomHexIfHexIsNotSet(): void + { + $testedObject = $this->createClassObject(); + $result = $testedObject->generate(); + $realValue = $this->accessProtected($result, 'hex'); + self::assertIsString($realValue); + } + + public function testGenerateReturnsObjectWithHexIfHexIsSet(): void + { + $testedObject = $this->createClassObject(); + $expectedValue = 'test'; + + $testedObject->setHex($expectedValue); + $result = $testedObject->generate(); + $realValue = $this->accessProtected($result, 'hex'); + self::assertSame($expectedValue, $realValue); + } + + public function testIsValidIfKeyNotSet(): void + { + $testedObject = $this->createClassObject(); + self::assertFalse($testedObject->isValid()); + } + + public function testHasValidHexIfHexNotSet(): void + { + $testedObject = $this->createClassObject(); + self::assertFalse($testedObject->hasValidHex()); + } + + public function testIsValid(): void + { + $testedObject = $this->createClassObject(); + $testedObject->setHex(self::EXAMPLE_HEX_STRING); + self::assertTrue($testedObject->isValid()); + } + + public function testSign(): void + { + $testedObject = $this->createClassObject(); + $testedObject->setHex(self::EXAMPLE_HEX_STRING); + + try { + $result = $testedObject->sign('test'); + } catch (\Exception $exception) { + self::fail(); + } + + self::assertIsString($result); + } + + public function testSignWhenNoHex(): void + { + $testedObject = $this->createClassObject(); + + $this->expectException(\Exception::class); + $this->expectExceptionMessage('The private key must be in hex format.'); + + $testedObject->sign('test'); + } + + public function testSignWhenDataEmpty(): void + { + $testedObject = $this->createClassObject(); + $testedObject->setHex(self::EXAMPLE_HEX_STRING); + + $this->expectException(\Exception::class); + $this->expectExceptionMessage('You did not provide any data to sign'); + + $testedObject->sign([]); + } + + public function testPemDecode(): void + { + $testedObject = $this->createClassObject(); + + $result = $testedObject->pemDecode(self::EXAMPLE_ECDSA_KEY); + + self::assertIsArray($result); + self::assertSame('52f7b0b9e2a06703bc758cfc13e5370f7a04ec7528a728daab2ae14c21414a2c', $result['private_key']); + self::assertSame( + '045ecd459aae1074f01b9f737271d114f873a3737916f5e7dbb28da423fbdd0d359c482324df5d2ce771b7b510a50c13cfbae240b0a8216cf7da4da52999714234', + $result['public_key']); + } + + public function testPemDecodeTooShortPemException(): void + { + $testedObject = $this->createClassObject(); + + $this->expectException(\Exception::class); + $this->expectExceptionMessage('Invalid or corrupt secp256k1 key provided. Cannot decode the supplied PEM data.'); + $testedObject->pemDecode('test'); + } + + public function testPemDecodePemShouldThrowExceptionWhenCorruptKeyProvided(): void + { + $testedObject = $this->createClassObject(); + + $this->expectException(\Exception::class); + $this->expectExceptionMessage('Invalid or corrupt secp256k1 key provided. Cannot decode the supplied PEM data.'); + $testedObject->pemDecode( + 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' + ); + } + + public function testPemEncode(): void + { + $testedObject = $this->createClassObject(); + + $beg_ec_text = '-----BEGIN EC PRIVATE KEY-----'; + $end_ec_text = '-----END EC PRIVATE KEY-----'; + + $exampleKeyPair = ['52f7b0b9e2a06703bc758cfc13e5370f7a04ec7528a728daab2ae14c21414a2c', + '045ecd459aae1074f01b9f737271d114f873a3737916f5e7dbb28da423fbdd0d359c482324df5d2ce771b7b510a50c13cfbae240b0a8216cf7da4da529997142', + ]; + + $result = $testedObject->pemEncode($exampleKeyPair); + self::assertIsString($result); + self::assertStringContainsString($beg_ec_text, $result); + self::assertStringContainsString($end_ec_text, $result); + } + + public function testPemEncodeCorruptSecp(): void + { + $testedObject = $this->createClassObject(); + + $exampleCorruptKeyPair = ['52f7b0b9e2a06703bc758cfc13e5370f7a04ec7528a728daab2ae14c21414a2c', + '7dbb28da423fbdd0d359c482324df5d2ce771b7b510a50c13cfbae240b0a8216cf7da4da52999714234', + ]; + + $this->expectExceptionMessage('Invalid or corrupt secp256k1 keypair provided. Cannot decode the supplied PEM data.'); + $this->expectException(\Exception::class); + $testedObject->pemEncode($exampleCorruptKeyPair); + } + + public function test__toString(): void + { + $testedObject = $this->createClassObject(); + + self::assertEmpty((string)$testedObject); + } + + public function test__toStringWhenHexIsSet(): void + { + $testedObject = $this->createClassObject(); + $testedObject->setHex(self::EXAMPLE_HEX_STRING); + + self::assertSame(self::EXAMPLE_HEX_STRING, (string)$testedObject); + } + + public function testGetPublicKeyIfKeyIsNull(): void + { + $testedObject = $this->createClassObject(); + $result = $testedObject->getPublicKey(); + self::assertInstanceOf(PublicKey::class, $result); + } + + public function testGetPublicKeyIfKeyIsNotNull(): void + { + $testedObject = $this->createClassObject(); + + $firstResult = $testedObject->getPublicKey(); + $secondResult = $testedObject->getPublicKey(); + + self::assertInstanceOf(PublicKey::class, $firstResult); + self::assertSame($firstResult, $secondResult); + } + + private function createClassObject(): PrivateKey + { + return new PrivateKey(); + } + + private function accessProtected($obj, $prop) + { + $reflection = new \ReflectionClass($obj); + $property = $reflection->getProperty($prop); + $property->setAccessible(true); + + return $property->getValue($obj); + } } \ No newline at end of file diff --git a/test/unit/BitPayKeyUtils/KeyHelper/PublicKeyTest.php b/test/unit/BitPayKeyUtils/KeyHelper/PublicKeyTest.php index ca97fdb..c5f4959 100644 --- a/test/unit/BitPayKeyUtils/KeyHelper/PublicKeyTest.php +++ b/test/unit/BitPayKeyUtils/KeyHelper/PublicKeyTest.php @@ -1,5 +1,9 @@ createMock(PrivateKey::class); $result = PublicKey::createFromPrivateKey($privateKeyMock); - $this->assertInstanceOf(PublicKey::class, $result); + self::assertInstanceOf(PublicKey::class, $result); } - /** - * @throws ReflectionException - */ - public function testSetPrivateKey() + public function testSetPrivateKey(): void { $privateKeyMock = $this->createMock(PrivateKey::class); $testedObject = $this->getTestedClassObject(); $testedObject->setPrivateKey($privateKeyMock); - $this->assertEquals($privateKeyMock, $this->accessProtected($testedObject, 'privateKey')); + self::assertSame($privateKeyMock, $this->accessProtected($testedObject, 'privateKey')); } - public function testToStringWhenXIsNull() + public function testToStringWhenXIsNull(): void { $testedObject = $this->getTestedClassObject(); - $this->assertEquals('', (string)$testedObject); + self::assertSame('', (string)$testedObject); } - /** - * @throws ReflectionException - */ - public function testToStringWhenXIsNotNullAndMathModIs1() + public function testToStringWhenXIsNotNullAndMathModIs1(): void { $testedObject = $this->getTestedClassObject(); @@ -47,13 +45,10 @@ public function testToStringWhenXIsNotNullAndMathModIs1() $this->setProtectedPropertyValue($testedObject, 'y', 1); $toStringCompressionResult = '031'; - $this->assertEquals($toStringCompressionResult, (string)$testedObject); + self::assertSame($toStringCompressionResult, (string)$testedObject); } - /** - * @throws ReflectionException - */ - public function testToStringWhenXIsNotNullAndMathModIsNot1() + public function testToStringWhenXIsNotNullAndMathModIsNot1(): void { $testedObject = $this->getTestedClassObject(); @@ -61,127 +56,97 @@ public function testToStringWhenXIsNotNullAndMathModIsNot1() $this->setProtectedPropertyValue($testedObject, 'y', 4); $toStringCompressionResult = '021'; - $this->assertEquals($toStringCompressionResult, (string)$testedObject); + self::assertSame($toStringCompressionResult, (string)$testedObject); } - public function testIsValidWhenPublicKeyIsBlank() + public function testIsValidWhenPublicKeyIsBlank(): void { $testedObject = $this->getTestedClassObject(); $result = $testedObject->isValid(); - $this->assertFalse($result); + self::assertFalse($result); } - /** - * @throws ReflectionException - */ - public function testIsValidWhenIsValid() + public function testIsValidWhenIsValid(): void { $testedObject = $this->getTestedClassObject(); $this->setProtectedPropertyValue($testedObject, 'dec', self::EXAMPLE_DEC); $this->setProtectedPropertyValue($testedObject, 'hex', self::EXAMPLE_HEX); $result = $testedObject->isValid(); - $this->assertTrue($result); + self::assertTrue($result); } - /** - * @throws ReflectionException - */ - public function testIsValidWhenIsInvalid() + public function testIsValidWhenIsInvalid(): void { $testedObject = $this->getTestedClassObject(); $this->setProtectedPropertyValue($testedObject, 'dec', null); $this->setProtectedPropertyValue($testedObject, 'hex', 'FF5733'); $result = $testedObject->isValid(); - $this->assertFalse($result); + self::assertFalse($result); } - /** - * @throws ReflectionException - */ - public function testGetSinWhenKeyNotGenerated() + public function testGetSinWhenKeyNotGenerated(): void { $testedObject = $this->getTestedClassObject(); $this->setProtectedPropertyValue($testedObject, 'hex', null); - $this->expectException(Exception::class); + $this->expectException(\Exception::class); $testedObject->getSin(); } - /** - * @throws ReflectionException - * @throws Exception - */ - public function testGetSin() + public function testGetSin(): void { $testedObject = $this->getTestedClassObject(); $privateKeyMock = $this->getValidPrivateKeyMock(); $testedObject->setPrivateKey($privateKeyMock); $result = $testedObject->getSin(); - $this->assertInstanceOf(SinKey::class, $result); + self::assertInstanceOf(SinKey::class, $result); } - /** - * @throws ReflectionException - * @throws Exception - */ - public function testGenerate() + public function testGenerate(): void { $testedObject = $this->getTestedClassObject(); $privateKeyMock = $this->getValidPrivateKeyMock(); $result = $testedObject->generate($privateKeyMock); - $this->assertInstanceOf(PublicKey::class, $result); + self::assertInstanceOf(PublicKey::class, $result); } - /** - * @throws ReflectionException - * @throws Exception - */ - public function testGenerateWhenHexNotEmpty() + public function testGenerateWhenHexNotEmpty(): void { $testedObject = $this->getTestedClassObject(); $privateKeyMock = $this->createMock(PrivateKey::class); $this->setProtectedPropertyValue($testedObject, 'hex', self::EXAMPLE_HEX); $result = $testedObject->generate($privateKeyMock); - $this->assertInstanceOf(PublicKey::class, $result); + self::assertInstanceOf(PublicKey::class, $result); } - /** - * @throws Exception - */ - public function testGenerateWhenPrivateKeyInvalid() + public function testGenerateWhenPrivateKeyInvalid(): void { $testedObject = $this->getTestedClassObject(); $privateKeyMock = $this->createMock(PrivateKey::class); $privateKeyMock->expects($this->any())->method('isValid')->willReturn(false); - $this->expectException(Exception::class); + $this->expectException(\Exception::class); $result = $testedObject->generate($privateKeyMock); - $this->assertInstanceOf(PublicKey::class, $result); + self::assertInstanceOf(PublicKey::class, $result); } - /** - * @throws ReflectionException - */ private function accessProtected($obj, $prop) { - $reflection = new ReflectionClass($obj); + $reflection = new \ReflectionClass($obj); $property = $reflection->getProperty($prop); $property->setAccessible(true); return $property->getValue($obj); } - /** - * @throws ReflectionException - */ - private function setProtectedPropertyValue(&$instance, $propertyName, $propertyValue) + private function setProtectedPropertyValue(&$instance, $propertyName, $propertyValue): void { $reflection = new \ReflectionProperty(get_class($instance), $propertyName); $reflection->setAccessible(true); @@ -193,9 +158,6 @@ private function getTestedClassObject(): PublicKey return new PublicKey(); } - /** - * @throws ReflectionException - */ private function getValidPrivateKeyMock() { $privateKeyMock = $this->createMock(PrivateKey::class); diff --git a/test/unit/BitPayKeyUtils/KeyHelper/SinKeyTest.php b/test/unit/BitPayKeyUtils/KeyHelper/SinKeyTest.php index 5ad2975..dea9903 100644 --- a/test/unit/BitPayKeyUtils/KeyHelper/SinKeyTest.php +++ b/test/unit/BitPayKeyUtils/KeyHelper/SinKeyTest.php @@ -1,6 +1,9 @@ assertEmpty($this->sinKey->__toString()); + self::assertEmpty($this->sinKey->__toString()); } public function test__toStringValue(): void @@ -33,19 +33,19 @@ public function test__toStringValue(): void $property = $this->getAccessibleProperty(SinKey::class, 'value'); $value = $property->getValue($this->sinKey); - $this->assertEquals($value, $this->sinKey->__toString()); + self::assertSame($value, $this->sinKey->__toString()); } public function testSetPublicKey(): void { $publicKey = $this->getMockBuilder(PublicKey::class)->getMock(); - $this->assertEquals($this->sinKey, $this->sinKey->setPublicKey($publicKey)); + self::assertSame($this->sinKey, $this->sinKey->setPublicKey($publicKey)); } public function testGenerateWithoutPublicKey(): void { - $this->expectException(Exception::class); + $this->expectException(\Exception::class); $this->sinKey->generate(); } @@ -53,7 +53,7 @@ public function testPublicKeyGenerateException(): void { $property = $this->getAccessibleProperty(SinKey::class, 'publicKey'); $property->setValue($this->sinKey, ''); - $this->expectException(Exception::class); + $this->expectException(\Exception::class); $this->sinKey->generate(); } @@ -65,17 +65,17 @@ public function testIsValid(): void $this->sinKey->setPublicKey($publicKey); $this->sinKey->generate(); - $this->assertEquals(true, $this->sinKey->isValid()); + self::assertSame(true, $this->sinKey->isValid()); } public function testIsValidFalse(): void { - $this->assertEquals(false, $this->sinKey->isValid()); + self::assertSame(false, $this->sinKey->isValid()); } - private function getAccessibleProperty(string $class, string $property): ReflectionProperty + private function getAccessibleProperty(string $class, string $property): \ReflectionProperty { - $reflection = new ReflectionClass($class); + $reflection = new \ReflectionClass($class); $property = $reflection->getProperty($property); $property->setAccessible(true); diff --git a/test/unit/BitPayKeyUtils/Math/BcEngineTest.php b/test/unit/BitPayKeyUtils/Math/BcEngineTest.php index 0f5da68..32a2d0f 100644 --- a/test/unit/BitPayKeyUtils/Math/BcEngineTest.php +++ b/test/unit/BitPayKeyUtils/Math/BcEngineTest.php @@ -1,174 +1,178 @@ createClassObject(); - $this->assertInstanceOf(BcEngine::class, $bcEngine); + self::assertInstanceOf(BcEngine::class, $bcEngine); } - public function testAdd() + public function testAdd(): void { $expectedValue = '12'; $bcEngine = $this->createClassObject(); - $this->assertEquals($expectedValue, $bcEngine->add('5', '7')); + self::assertSame($expectedValue, $bcEngine->add('5', '7')); } - public function testInput() + public function testInput(): void { $expectedValue = '5'; $bcEngine = $this->createClassObject(); - $this->assertEquals($expectedValue, $bcEngine->input($expectedValue)); + self::assertSame($expectedValue, $bcEngine->input($expectedValue)); } - public function testInputNull() + public function testInputNull(): void { $expectedValue = '0'; $bcEngine = $this->createClassObject(); - $this->assertEquals($expectedValue, $bcEngine->input(null)); + self::assertSame($expectedValue, $bcEngine->input(null)); } - public function testInputHex() + public function testInputHex(): void { $expectedValue = '86'; $bcEngine = $this->createClassObject(); - $this->assertEquals($expectedValue, $bcEngine->input('0x56')); + self::assertSame($expectedValue, $bcEngine->input('0x56')); } - public function testInputException() + public function testInputException(): void { - $this->expectException(Exception::class); + $this->expectException(\Exception::class); $bcEngine = $this->createClassObject(); $bcEngine->input('Teg4ew'); } - public function testCmpGreaterThan() + public function testCmpGreaterThan(): void { - $expectedValue = '1'; + $expectedValue = 1; $bcEngine = $this->createClassObject(); - $this->assertEquals($expectedValue, $bcEngine->cmp('9', '7')); + self::assertSame($expectedValue, $bcEngine->cmp('9', '7')); } - public function testCmpLessThan() + public function testCmpLessThan(): void { - $expectedValue = '-1'; + $expectedValue = -1; $bcEngine = $this->createClassObject(); - $this->assertEquals($expectedValue, $bcEngine->cmp('7', '9')); + self::assertSame($expectedValue, $bcEngine->cmp('7', '9')); } - public function testCmpEqualsTo() + public function testCmpEqualsTo(): void { - $expectedValue = '0'; + $expectedValue = 0; $bcEngine = $this->createClassObject(); - $this->assertEquals($expectedValue, $bcEngine->cmp('7', '7')); + self::assertSame($expectedValue, $bcEngine->cmp('7', '7')); } - public function testDiv() + public function testDiv(): void { $expectedValue = '3'; $bcEngine = $this->createClassObject(); - $this->assertEquals($expectedValue, $bcEngine->div('6', '2')); + self::assertSame($expectedValue, $bcEngine->div('6', '2')); } - public function testInvertm() + public function testInvertm(): void { $expectedValue = '0'; $bcEngine = $this->createClassObject(); - $this->assertEquals($expectedValue, $bcEngine->invertm('6', '2')); + self::assertSame($expectedValue, $bcEngine->invertm('6', '2')); } - public function testInvertm2() + public function testInvertm2(): void { $expectedValue = '1'; $bcEngine = $this->createClassObject(); - $this->assertEquals($expectedValue, $bcEngine->invertm('-1', '2')); + self::assertSame($expectedValue, $bcEngine->invertm('-1', '2')); } - public function testMod() + public function testMod(): void { $expectedValue = '0'; $bcEngine = $this->createClassObject(); - $this->assertEquals($expectedValue, $bcEngine->mod('6', '2')); + self::assertSame($expectedValue, $bcEngine->mod('6', '2')); } - public function testMod2() + public function testMod2(): void { $expectedValue = '2'; $bcEngine = $this->createClassObject(); - $this->assertEquals($expectedValue, $bcEngine->mod('-6', '2')); + self::assertSame($expectedValue, $bcEngine->mod('-6', '2')); } - public function testMul() + public function testMul(): void { $expectedValue = '21'; $bcEngine = $this->createClassObject(); - $this->assertEquals($expectedValue, $bcEngine->mul('3', '7')); + self::assertSame($expectedValue, $bcEngine->mul('3', '7')); } - public function testPow() + public function testPow(): void { $expectedValue = '64'; $bcEngine = $this->createClassObject(); - $this->assertEquals($expectedValue, $bcEngine->pow('4', '3')); + self::assertSame($expectedValue, $bcEngine->pow('4', '3')); } - public function testSub() + public function testSub(): void { $expectedValue = '18'; $bcEngine = $this->createClassObject(); - $this->assertEquals($expectedValue, $bcEngine->sub('20', '2')); + self::assertSame($expectedValue, $bcEngine->sub('20', '2')); } - public function testCoprime() + public function testCoprime(): void { $expectedValue = false; $bcEngine = $this->createClassObject(); - $this->assertEquals($expectedValue, $bcEngine->coprime('20', '2')); + self::assertSame($expectedValue, $bcEngine->coprime('20', '2')); } - public function testCoprime2() + public function testCoprime2(): void { $expectedValue = true; $bcEngine = $this->createClassObject(); - $this->assertEquals($expectedValue, $bcEngine->coprime('5', '3')); + self::assertSame($expectedValue, $bcEngine->coprime('5', '3')); } - public function testCoprime3() + public function testCoprime3(): void { $expectedValue = true; $bcEngine = $this->createClassObject(); - $this->assertEquals($expectedValue, $bcEngine->coprime('3', '5')); + self::assertSame($expectedValue, $bcEngine->coprime('3', '5')); } - public function testCoprime4() + public function testCoprime4(): void { $expectedValue = false; $bcEngine = $this->createClassObject(); - $this->assertEquals($expectedValue, $bcEngine->coprime('3', '3')); + self::assertSame($expectedValue, $bcEngine->coprime('3', '3')); } private function createClassObject() diff --git a/test/unit/BitPayKeyUtils/Math/GmpEngineTest.php b/test/unit/BitPayKeyUtils/Math/GmpEngineTest.php index 06a2ce4..01eb757 100644 --- a/test/unit/BitPayKeyUtils/Math/GmpEngineTest.php +++ b/test/unit/BitPayKeyUtils/Math/GmpEngineTest.php @@ -1,111 +1,115 @@ createClassObject(); $actualResult = $gmp->add('2', '3'); - $this->assertIsString($actualResult); - $this->assertEquals($expectedResult, $actualResult); + self::assertIsString($actualResult); + self::assertSame($expectedResult, $actualResult); } - public function testCmpGreaterThan() + public function testCmpGreaterThan(): void { $expectedResult = '1'; $gmp = $this->createClassObject(); $actualResult = $gmp->cmp('1234', '1000'); - $this->assertIsString($actualResult); - $this->assertEquals($expectedResult, $actualResult); + self::assertIsString($actualResult); + self::assertSame($expectedResult, $actualResult); } - public function testCmpLessThan() + public function testCmpLessThan(): void { $expectedResult = '-1'; $gmp = $this->createClassObject(); $actualResult = $gmp->cmp('1000', '1234'); - $this->assertIsString($actualResult); - $this->assertEquals($expectedResult, $actualResult); + self::assertIsString($actualResult); + self::assertSame($expectedResult, $actualResult); } - public function testCmpEqualTo() + public function testCmpEqualTo(): void { $expectedResult = '0'; $gmp = $this->createClassObject(); $actualResult = $gmp->cmp('1000', '1000'); - $this->assertIsString($actualResult); - $this->assertEquals($expectedResult, $actualResult); + self::assertIsString($actualResult); + self::assertSame($expectedResult, $actualResult); } - public function testDiv() + public function testDiv(): void { $expectedResult = '2'; $gmp = $this->createClassObject(); $actualResult = $gmp->div('10', '5'); - $this->assertIsString($actualResult); - $this->assertEquals($expectedResult, $actualResult); + self::assertIsString($actualResult); + self::assertSame($expectedResult, $actualResult); } - public function testInvertm() + public function testInvertm(): void { $expectedResult = '9'; $gmp = $this->createClassObject(); $actualResult = $gmp->invertm('5', '11'); - $this->assertIsString($actualResult); - $this->assertEquals($expectedResult, $actualResult); + self::assertIsString($actualResult); + self::assertSame($expectedResult, $actualResult); } - public function testMod() + public function testMod(): void { $expectedResult = '3'; $gmp = $this->createClassObject(); $actualResult = $gmp->mod('7', '4'); - $this->assertIsString($actualResult); - $this->assertEquals($expectedResult, $actualResult); + self::assertIsString($actualResult); + self::assertSame($expectedResult, $actualResult); } - public function testMul() + public function testMul(): void { $expectedResult = '16'; $gmp = $this->createClassObject(); $actualResult = $gmp->mul('8', '2'); - $this->assertIsString($actualResult); - $this->assertEquals($expectedResult, $actualResult); + self::assertIsString($actualResult); + self::assertSame($expectedResult, $actualResult); } - public function testPow() + public function testPow(): void { $expectedResult = '27'; $gmp = $this->createClassObject(); $actualResult = $gmp->pow('3', '3'); - $this->assertIsString($actualResult); - $this->assertEquals($expectedResult, $actualResult); + self::assertIsString($actualResult); + self::assertSame($expectedResult, $actualResult); } - public function testSub() + public function testSub(): void { $expectedResult = '42'; $gmp = $this->createClassObject(); $actualResult = $gmp->sub('64', '22'); - $this->assertIsString($actualResult); - $this->assertEquals($expectedResult, $actualResult); + self::assertIsString($actualResult); + self::assertSame($expectedResult, $actualResult); } - private function createClassObject() + private function createClassObject(): GmpEngine { return new GmpEngine(); } diff --git a/test/unit/BitPayKeyUtils/Math/MathTest.php b/test/unit/BitPayKeyUtils/Math/MathTest.php index 3b48c2c..56774cf 100644 --- a/test/unit/BitPayKeyUtils/Math/MathTest.php +++ b/test/unit/BitPayKeyUtils/Math/MathTest.php @@ -1,36 +1,40 @@ createClassObject(); - $this->assertInstanceOf(Math::class, $math); + self::assertInstanceOf(Math::class, $math); } - public function testGetEngine() + public function testGetEngine(): void { $expectedEngine = new GmpEngine(); $math = $this->createClassObject(); $math::setEngine($expectedEngine); - $this->assertEquals($expectedEngine, $math::getEngine()); + self::assertSame($expectedEngine, $math::getEngine()); } - public function testGetEngineName() + public function testGetEngineName(): void { $expectedEngineName = 'Test engine name'; $math = $this->createClassObject(); $math::setEngineName($expectedEngineName); - $this->assertEquals($expectedEngineName, $math::getEngineName()); + self::assertSame($expectedEngineName, $math::getEngineName()); } - private function createClassObject() + private function createClassObject(): Math { return new Math(); } diff --git a/test/unit/BitPayKeyUtils/Storage/EncryptedFilesystemStorageTest.php b/test/unit/BitPayKeyUtils/Storage/EncryptedFilesystemStorageTest.php index 8547e4e..c03f316 100644 --- a/test/unit/BitPayKeyUtils/Storage/EncryptedFilesystemStorageTest.php +++ b/test/unit/BitPayKeyUtils/Storage/EncryptedFilesystemStorageTest.php @@ -1,30 +1,33 @@ createClassObject(); - $this->assertInstanceOf(EncryptedFilesystemStorage::class, $encryptedFilesystemStorage); + self::assertInstanceOf(EncryptedFilesystemStorage::class, $encryptedFilesystemStorage); } - public function testPersist() + public function testPersist(): void { $encryptedFilesystemStorage = $this->createClassObject(); - $keyInterface = $this->getMockBuilder(Key::class)->getMock(); + $keyInterface = $this->getMockBuilder(Key::class)->setMockClassName('KeyMock')->getMock(); $keyInterface->method('getId')->willReturn(__DIR__ . '/test11.txt'); - $this->assertFileExists(__DIR__ . '/test11.txt'); - $this->assertEquals(null, $encryptedFilesystemStorage->persist($keyInterface)); - + self::assertFileExists(__DIR__ . '/test11.txt'); + self::assertSame(null, $encryptedFilesystemStorage->persist($keyInterface)); } - public function testLoadNotFindException() + public function testLoadNotFindException(): void { - $this->expectException(Exception::class); + $this->expectException(\Exception::class); $this->expectExceptionMessage('Could not find "'.__DIR__.'/test2.txt"'); $encryptedFilesystemStorage = $this->createClassObject(); @@ -42,13 +45,13 @@ public function testLoadNotPermissionException() } **/ - public function testLoad() + public function testLoad(): void { $encryptedFilesystemStorage = $this->createClassObject(); - $this->assertIsObject($encryptedFilesystemStorage->load(__DIR__ . '/test11.txt')); + self::assertIsObject($encryptedFilesystemStorage->load(__DIR__ . '/test11.txt')); } - private function createClassObject() + private function createClassObject(): EncryptedFilesystemStorage { return new EncryptedFilesystemStorage('test'); } diff --git a/test/unit/BitPayKeyUtils/Storage/FilesystemStorageTest.php b/test/unit/BitPayKeyUtils/Storage/FilesystemStorageTest.php index 7aec66b..489ce42 100644 --- a/test/unit/BitPayKeyUtils/Storage/FilesystemStorageTest.php +++ b/test/unit/BitPayKeyUtils/Storage/FilesystemStorageTest.php @@ -1,31 +1,35 @@ createClassObject(); - $this->assertInstanceOf(FilesystemStorage::class, $filesystemStorage); + self::assertInstanceOf(FilesystemStorage::class, $filesystemStorage); } - public function testPersist() + public function testPersist(): void { $filesystemStorage = $this->createClassObject(); - $keyInterface = $this->getMockBuilder(Key::class)->getMock(); + $keyInterface = $this->getMockBuilder(Key::class)->setMockClassName('KeyMock')->getMock(); $keyInterface->method('getId')->willReturn(__DIR__ . '/test1.txt'); - $this->assertFileExists(__DIR__ . '/test1.txt'); + self::assertFileExists(__DIR__ . '/test1.txt'); @chmod(__DIR__ . 'test1.txt', 0777); - $this->assertEquals(null, $filesystemStorage->persist($keyInterface)); + $filesystemStorage->persist($keyInterface); } - public function testLoadNotFindException() + public function testLoadNotFindException(): void { - $this->expectException(Exception::class); + $this->expectException(\Exception::class); $this->expectExceptionMessage('Could not find "'.__DIR__.'/test2.txt"'); $filesystemStorage = $this->createClassObject(); @@ -44,15 +48,15 @@ public function testLoadNotPermissionException() } **/ - public function testLoad() + public function testLoad(): void { $expectedArray = ['Red', 'Green', 'Blue']; $filesystemStorage = $this->createClassObject(); - $this->assertEquals($expectedArray, $filesystemStorage->load(__DIR__ . '/test4.txt')); + self::assertSame($expectedArray, $filesystemStorage->load(__DIR__ . '/test4.txt')); } - private function createClassObject() + private function createClassObject(): FilesystemStorage { return new FilesystemStorage(); } diff --git a/test/unit/BitPayKeyUtils/Storage/test1.txt b/test/unit/BitPayKeyUtils/Storage/test1.txt index 46f07be..ae24bcd 100755 --- a/test/unit/BitPayKeyUtils/Storage/test1.txt +++ b/test/unit/BitPayKeyUtils/Storage/test1.txt @@ -1 +1 @@ -O:17:"Mock_Key_c1e4b2bf":0:{} \ No newline at end of file +O:7:"KeyMock":0:{} \ No newline at end of file diff --git a/test/unit/BitPayKeyUtils/Storage/test11.txt b/test/unit/BitPayKeyUtils/Storage/test11.txt index 92c1ed0..e7b67c5 100644 --- a/test/unit/BitPayKeyUtils/Storage/test11.txt +++ b/test/unit/BitPayKeyUtils/Storage/test11.txt @@ -1 +1 @@ -a2c2ae8c5cd35cc02dfeef6309593cfe0ddcd995e6de235a033c889bc2c23299 \ No newline at end of file +7ebba060008a3f86d972e0d679fffaec7d0e99d02fa1ed9bf330dc26befd4ff8 \ No newline at end of file diff --git a/test/unit/BitPayKeyUtils/Util/Base58Test.php b/test/unit/BitPayKeyUtils/Util/Base58Test.php index 9df0604..f6f22a0 100644 --- a/test/unit/BitPayKeyUtils/Util/Base58Test.php +++ b/test/unit/BitPayKeyUtils/Util/Base58Test.php @@ -1,56 +1,60 @@ createClassObject(); - $this->assertInstanceOf(Base58::class, $base58); + self::assertInstanceOf(Base58::class, $base58); } - public function testEncodeException() + public function testEncodeException(): void { - $this->expectException(Exception::class); + $this->expectException(\Exception::class); $this->expectExceptionMessage('Invalid Length'); $base58 = $this->createClassObject(); $base58->encode('0x16p4t'); } - public function testEncode() + public function testEncode(): void { $base58 = $this->createClassObject(); - $this->assertEquals('P', $base58->encode('0x16')); + self::assertSame('P', $base58->encode('0x16')); } - public function testEncode2() + public function testEncode2(): void { $base58 = $this->createClassObject(); - $this->assertEquals('1', $base58->encode('00')); + self::assertSame('1', $base58->encode('00')); } - public function testDecode() + public function testDecode(): void { $base58 = $this->createClassObject(); - $this->assertEquals('4f59cb', $base58->decode('Test')); + self::assertSame('4f59cb', $base58->decode('Test')); } - public function testDecode2() + public function testDecode2(): void { $base58 = $this->createClassObject(); - $this->assertEquals('02bf547c6d249ea9', $base58->decode('Test 5 T 2')); + self::assertSame('02bf547c6d249ea9', $base58->decode('Test 5 T 2')); } - public function testDecode3() + public function testDecode3(): void { $base58 = $this->createClassObject(); - $this->assertEquals('00', $base58->decode('1')); + self::assertSame('00', $base58->decode('1')); } - private function createClassObject() + private function createClassObject(): Base58 { return new Base58(); } diff --git a/test/unit/BitPayKeyUtils/Util/ErrorTest.php b/test/unit/BitPayKeyUtils/Util/ErrorTest.php index ae3ddb9..788aca8 100644 --- a/test/unit/BitPayKeyUtils/Util/ErrorTest.php +++ b/test/unit/BitPayKeyUtils/Util/ErrorTest.php @@ -1,36 +1,39 @@ getTestedClassObject(); $result = $testedObject->backtrace(); - $this->assertEquals('backtrace', $result[0]['function']); + self::assertSame('backtrace', $result[0]['function']); } - public function testBacktraceWhenPrintIsTrue() + public function testBacktraceWhenPrintIsTrue(): void { $testedObject = $this->getTestedClassObject(); ob_start(); $testedObject->backtrace(true); $result = ob_get_clean(); - $this->assertIsString($result); + self::assertIsString($result); } - public function testLastWhenNoErrorOccured() + public function testLastWhenNoErrorOccured(): void { error_clear_last(); $testedObject = $this->getTestedClassObject(); $result = $testedObject->last(); - $this->assertNull($result); + self::assertNull($result); } - public function testLog() + public function testLog(): void { $exampleLogMessage = 'test'; $testedObject = $this->getTestedClassObject(); @@ -40,80 +43,88 @@ public function testLog() ini_set('error_log', $errorLogLocationBackup); $result = stream_get_contents($errorLogTemporaryFile); - $this->assertStringContainsString($exampleLogMessage, $result); + self::assertStringContainsString($exampleLogMessage, $result); } - public function testReportingWithNoParam() + public function testReportingWithNoParam(): void { $testedObject = $this->getTestedClassObject(); $result = $testedObject->reporting(); - $this->assertEquals(error_reporting(), $result); + self::assertSame(error_reporting(), $result); } - public function testReportingWithLevel() + public function testReportingWithLevel(): void { $testedObject = $this->getTestedClassObject(); $exampleReportingLevel = 32767; $testedObject->reporting($exampleReportingLevel); $currentLevel = error_reporting(); - $this->assertEquals($exampleReportingLevel, $currentLevel); + self::assertSame($exampleReportingLevel, $currentLevel); } - public function testHandlerWithNoParams() + public function testHandlerWithNoParams(): void { $testedObject = $this->getTestedClassObject(); $result = $testedObject->handler(); - $this->assertTrue($result); + self::assertTrue($result); } - public function testHandlerWithActionSet() + public function testHandlerWithActionSet(): void { $testedObject = $this->getTestedClassObject(); $result = $testedObject->handler('error', 'set', null); - $this->assertInstanceOf(ErrorHandler::class, $result); + self::assertInstanceOf(\PHPUnit\Runner\ErrorHandler::class, $result); } - public function testHandlerWithActionFalse() + public function testHandlerWithActionFalse(): void { $testedObject = $this->getTestedClassObject(); $result = $testedObject->handler('error', false); - $this->assertFalse($result); + self::assertFalse($result); } - public function testHandlerWithTypeExceptionAndNoAction() + public function testHandlerWithTypeExceptionAndNoAction(): void { $testedObject = $this->getTestedClassObject(); $result = $testedObject->handler('exception', false); - $this->assertFalse($result); + self::assertFalse($result); } - public function testHandlerWithTypeExceptionAndActionSet() + public function testHandlerWithTypeExceptionAndActionSet(): void { $testedObject = $this->getTestedClassObject(); $result = $testedObject->handler('exception', 'set', null); - $this->assertNull($result); + self::assertNull($result); } - public function testHandlerWithTypeExceptionAndActionRestore() + public function testHandlerWithTypeExceptionAndActionRestore(): void { $testedObject = $this->getTestedClassObject(); $result = $testedObject->handler('exception', 'restore', null); - $this->assertTrue($result); + self::assertTrue($result); } - public function testHandlerWithUnhandledType() + public function testHandlerWithUnhandledType(): void { $testedObject = $this->getTestedClassObject(); $result = $testedObject->handler('asd', 'restore', null); - $this->assertFalse($result); + self::assertFalse($result); } - public function testRaise() + public function testRaise(): void { + set_error_handler( + static function ( $errno, $errstr ) { + restore_error_handler(); + throw new \Exception( $errstr, $errno ); + }, + E_ALL + ); $testedObject = $this->getTestedClassObject(); - $this->expectError(); + $this->expectExceptionMessage('error'); + $result = $testedObject->raise('error'); - $this->assertTrue($result); + self::assertTrue($result); } private function getTestedClassObject(): Error diff --git a/test/unit/BitPayKeyUtils/Util/FingerprintTest.php b/test/unit/BitPayKeyUtils/Util/FingerprintTest.php index 71ac4cc..e03b49b 100644 --- a/test/unit/BitPayKeyUtils/Util/FingerprintTest.php +++ b/test/unit/BitPayKeyUtils/Util/FingerprintTest.php @@ -1,17 +1,21 @@ assertIsString($fingerprint::generate()); + self::assertIsString($fingerprint::generate()); } - public function testGenerateIssetFinHash() + public function testGenerateIssetFinHash(): void { $expectedValue = 'ce9c26116feb916c356b5313226ff177bf30f819'; @@ -22,7 +26,7 @@ public function testGenerateIssetFinHash() $fingerprint = new Fingerprint(); $actualValue = $fingerprint::generate(); - $this->assertIsString($actualValue); - $this->assertEquals($expectedValue, $actualValue); + self::assertIsString($actualValue); + self::assertSame($expectedValue, $actualValue); } } diff --git a/test/unit/BitPayKeyUtils/Util/PointTest.php b/test/unit/BitPayKeyUtils/Util/PointTest.php index c5ebf02..66c06f1 100644 --- a/test/unit/BitPayKeyUtils/Util/PointTest.php +++ b/test/unit/BitPayKeyUtils/Util/PointTest.php @@ -1,85 +1,90 @@ createClassObject(); - $this->assertInstanceOf(Point::class, $point); + self::assertInstanceOf(Point::class, $point); } - public function test__toString() + public function test__toString(): void { $point = new Point('3', '2'); - $this->assertEquals('(3, 2)', $point->__toString()); + self::assertSame('(3, 2)', $point->__toString()); } - public function test__toStringInfinite() + public function test__toStringInfinite(): void { $point = new Point('inf', '2'); - $this->assertEquals('inf', $point->__toString()); + self::assertSame('inf', $point->__toString()); } - public function testIsInfinityFalse() + public function testIsInfinityFalse(): void { $point = $this->createClassObject(); - $this->assertFalse($point->isInfinity()); + self::assertFalse($point->isInfinity()); } - public function testIsInfinityTrue() + public function testIsInfinityTrue(): void { $point = new Point('inf', '4'); - $this->assertTrue($point->isInfinity()); + self::assertTrue($point->isInfinity()); } - public function testGetX() + public function testGetX(): void { $point = $this->createClassObject(); - $this->assertEquals('-2', $point->getX()); + self::assertSame('-2', $point->getX()); } - public function testGetY() + public function testGetY(): void { $point = $this->createClassObject(); - $this->assertEquals('3', $point->getY()); + self::assertSame('3', $point->getY()); } - public function testSerialize() + public function testSerialize(): void { $expectedValue = 'a:2:{i:0;s:2:"-2";i:1;s:1:"3";}'; $point = $this->createClassObject(); - $this->assertEquals($expectedValue, $point->serialize()); + self::assertSame($expectedValue, $point->serialize()); } - public function testUnserialize() + public function testUnserialize(): void { $expectedValue = '[-2, 3]'; $testedData = 'a:2:{i:0;s:2:"-2";i:1;s:1:"3";}'; $point = $this->createClassObject(); - $this->assertEquals(null, $point->unserialize($testedData)); + self::assertSame(null, $point->unserialize($testedData)); } - public function test__serialize() + public function test__serialize(): void { $expectedValue = ['-2', '3']; $point = $this->createClassObject(); - $this->assertEquals($expectedValue, $point->__serialize()); + self::assertSame($expectedValue, $point->__serialize()); } - public function test__unserialize() + public function test__unserialize(): void { $expectedValue = ['-2', '3']; $point = $this->createClassObject(); - $this->assertEquals(null, $point->__unserialize(['-2', '3'])); + self::assertSame(null, $point->__unserialize(['-2', '3'])); } - private function createClassObject() + private function createClassObject(): Point { return new Point('-2', '3'); } diff --git a/test/unit/BitPayKeyUtils/Util/Secp256k1Test.php b/test/unit/BitPayKeyUtils/Util/Secp256k1Test.php index 68de014..fb90a63 100644 --- a/test/unit/BitPayKeyUtils/Util/Secp256k1Test.php +++ b/test/unit/BitPayKeyUtils/Util/Secp256k1Test.php @@ -1,81 +1,85 @@ createClassObject(); - $this->assertInstanceOf(Secp256k1::class, $secp256k1); + self::assertInstanceOf(Secp256k1::class, $secp256k1); } - public function testAHex() + public function testAHex(): void { $expectedValue = '0x00'; $secp256k1 = $this->createClassObject(); - $this->assertEquals($expectedValue, $secp256k1->aHex()); + self::assertSame($expectedValue, $secp256k1->aHex()); } - public function testBHex() + public function testBHex(): void { $expectedValue = '0x07'; $secp256k1 = $this->createClassObject(); - $this->assertEquals($expectedValue, $secp256k1->bHex()); + self::assertSame($expectedValue, $secp256k1->bHex()); } - public function testGHex() + public function testGHex(): void { $expectedValue = '0x0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8'; $secp256k1 = $this->createClassObject(); - $this->assertEquals($expectedValue, $secp256k1->gHex()); + self::assertSame($expectedValue, $secp256k1->gHex()); } - public function testGxHex() + public function testGxHex(): void { $expectedValue = '0x0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f817'; $secp256k1 = $this->createClassObject(); - $this->assertEquals($expectedValue, $secp256k1->gxHex()); + self::assertSame($expectedValue, $secp256k1->gxHex()); } - public function testGyHex() + public function testGyHex(): void { $expectedValue = '0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8'; $secp256k1 = $this->createClassObject(); - $this->assertEquals($expectedValue, $secp256k1->gyHex()); + self::assertSame($expectedValue, $secp256k1->gyHex()); } - public function testHHex() + public function testHHex(): void { $expectedValue = '0x01'; $secp256k1 = $this->createClassObject(); - $this->assertEquals($expectedValue, $secp256k1->hHex()); + self::assertSame($expectedValue, $secp256k1->hHex()); } - public function testNHex() + public function testNHex(): void { $expectedValue = '0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141'; $secp256k1 = $this->createClassObject(); - $this->assertEquals($expectedValue, $secp256k1->nHex()); + self::assertSame($expectedValue, $secp256k1->nHex()); } - public function testPHex() + public function testPHex(): void { $expectedValue = '0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f'; $secp256k1 = $this->createClassObject(); - $this->assertEquals($expectedValue, $secp256k1->pHex()); + self::assertSame($expectedValue, $secp256k1->pHex()); } - private function createClassObject() + private function createClassObject(): Secp256k1 { return new Secp256k1(); } diff --git a/test/unit/BitPayKeyUtils/Util/SecureRandomTest.php b/test/unit/BitPayKeyUtils/Util/SecureRandomTest.php index c459ae8..03e5db9 100644 --- a/test/unit/BitPayKeyUtils/Util/SecureRandomTest.php +++ b/test/unit/BitPayKeyUtils/Util/SecureRandomTest.php @@ -1,38 +1,45 @@ createClassObject(); - $this->assertInstanceOf(SecureRandom::class, $secureRandom); + self::assertInstanceOf(SecureRandom::class, $secureRandom); } - public function testHasOpenSSL() + /** + * @throws \ReflectionException + */ + public function testHasOpenSSL(): void { $secureRandom = $this->createClassObject(); $secureRandom::hasOpenSSL(); - $reflection = new ReflectionProperty($secureRandom, 'hasOpenSSL'); + $reflection = new \ReflectionProperty($secureRandom, 'hasOpenSSL'); $reflection->setAccessible(true); - $this->assertTrue($reflection->getValue()); - $this->assertObjectHasAttribute('hasOpenSSL', $secureRandom); + self::assertTrue($reflection->getValue()); + self::assertTrue(property_exists($secureRandom, 'hasOpenSSL')); } - public function testGenerateRandom() + public function testGenerateRandom(): void { $secureRandom = $this->createClassObject(); $secureRandom::generateRandom(); - $this->assertIsString($secureRandom::generateRandom()); + self::assertIsString($secureRandom::generateRandom()); } - public function testGenerateRandomException() + public function testGenerateRandomException(): void { - $this->expectException(Exception::class); + $this->expectException(\Exception::class); $reflection = new \ReflectionProperty(SecureRandom::class, 'hasOpenSSL'); $reflection->setAccessible(true); diff --git a/test/unit/BitPayKeyUtils/Util/UtilTest.php b/test/unit/BitPayKeyUtils/Util/UtilTest.php index 6f526e3..96eccb1 100644 --- a/test/unit/BitPayKeyUtils/Util/UtilTest.php +++ b/test/unit/BitPayKeyUtils/Util/UtilTest.php @@ -1,118 +1,122 @@ createClassObject(); - $this->assertInstanceOf(Util::class, $util); + self::assertInstanceOf(Util::class, $util); } - public function testSha512() + public function testSha512(): void { $expectedValue = 'ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff'; $util = $this->createClassObject(); - $this->assertEquals($expectedValue, $util::sha512('test')); + self::assertSame($expectedValue, $util::sha512('test')); } - public function testSha512hmac() + public function testSha512hmac(): void { $expectedValue = '287a0fb89a7fbdfa5b5538636918e537a5b83065e4ff331268b7aaa115dde047a9b0f4fb5b828608fc0b6327f10055f7637b058e9e0dbb9e698901a3e6dd461c'; $util = $this->createClassObject(); - $this->assertEquals($expectedValue, $util::sha512hmac('test', 'key')); + self::assertSame($expectedValue, $util::sha512hmac('test', 'key')); } - public function testSha256ripe160() + public function testSha256ripe160(): void { $expectedValue = 'cebaa98c19807134434d107b0d3e5692a516ea66'; $util = $this->createClassObject(); - $this->assertEquals($expectedValue, $util::sha256ripe160('test')); + self::assertSame($expectedValue, $util::sha256ripe160('test')); } - public function testRipe160() + public function testRipe160(): void { $expectedValue = '5e52fee47e6b070565f74372468cdc699de89107'; $util = $this->createClassObject(); - $this->assertEquals($expectedValue, $util::ripe160('test')); + self::assertSame($expectedValue, $util::ripe160('test')); } - public function testSha256() + public function testSha256(): void { $expectedValue = '9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08'; $util = $this->createClassObject(); - $this->assertEquals($expectedValue, $util::sha256('test')); + self::assertSame($expectedValue, $util::sha256('test')); } - public function testTwoSha256() + public function testTwoSha256(): void { $expectedValue = '7b3d979ca8330a94fa7e9e1b466d8b99e0bcdea1ec90596c0dcc8d7ef6b4300c'; $util = $this->createClassObject(); - $this->assertEquals($expectedValue, $util::twoSha256('test')); + self::assertSame($expectedValue, $util::twoSha256('test')); } - public function testNonce() + public function testNonce(): void { $util = $this->createClassObject(); - $this->assertIsFloat($util::nonce()); + self::assertIsFloat($util::nonce()); } - public function testGuid() + public function testGuid(): void { $util = $this->createClassObject(); - $this->assertIsString($util::guid()); + self::assertIsString($util::guid()); } - public function testEncodeHexException() + public function testEncodeHexException(): void { - $this->expectException(Exception::class); + $this->expectException(\Exception::class); $util = $this->createClassObject(); $util::encodeHex(null); } - public function testEncodeHex1() + public function testEncodeHex1(): void { $expectedValue = '7b'; $util = $this->createClassObject(); - $this->assertEquals($expectedValue, $util::encodeHex('-123')); + self::assertSame($expectedValue, $util::encodeHex('-123')); } - public function testEncodeHex2() + public function testEncodeHex2(): void { $expectedValue = '1c2'; $util = $this->createClassObject(); - $this->assertEquals($expectedValue, $util::encodeHex('450')); + self::assertSame($expectedValue, $util::encodeHex('450')); } - public function testDecToBin() + public function testDecToBin(): void { $expectedValue = '0011111'; $util = $this->createClassObject(); - $this->assertEquals($expectedValue, $util::decToBin('124')); + self::assertSame($expectedValue, $util::decToBin('124')); } - public function testDecodeHex() + public function testDecodeHex(): void { $expectedValue = '257'; $util = $this->createClassObject(); - $this->assertEquals($expectedValue, $util::decodeHex('00101')); + self::assertSame($expectedValue, $util::decodeHex('00101')); } - public function testPointDouble() + public function testPointDouble(): void { $pointInterface = $this->getMockBuilder(Point::class)->disableOriginalConstructor()->getMock(); $pointInterface->method('getX')->willReturn('5'); @@ -120,12 +124,12 @@ public function testPointDouble() $util = $this->createClassObject(); $point = $util::pointDouble($pointInterface); - $this->assertIsObject($point); - $this->assertEquals('28948022309329048855892746252171976963317496166410141009864396001977208668062', $point->getX()); - $this->assertEquals('43422033463993573283839119378257965444976244249615211514796594002965813000105', $point->getY()); + self::assertIsObject($point); + self::assertSame('28948022309329048855892746252171976963317496166410141009864396001977208668062', $point->getX()); + self::assertSame('43422033463993573283839119378257965444976244249615211514796594002965813000105', $point->getY()); } - public function testPointAdd() + public function testPointAdd(): void { $pointInterfaceP = $this->getMockBuilder(Point::class)->disableOriginalConstructor()->getMock(); $pointInterfaceP->method('getX')->willReturn('6'); @@ -137,12 +141,12 @@ public function testPointAdd() $util = $this->createClassObject(); $point = $util::pointAdd($pointInterfaceP, $pointInterfaceQ); - $this->assertIsObject($point); - $this->assertEquals('28948022309329048855892746252171976963317496166410141009864396001977208667914', $point->getX()); - $this->assertEquals('101318078082651670995624611882601919371611236582435493534525386006920230337669', $point->getY()); + self::assertIsObject($point); + self::assertSame('28948022309329048855892746252171976963317496166410141009864396001977208667914', $point->getX()); + self::assertSame('101318078082651670995624611882601919371611236582435493534525386006920230337669', $point->getY()); } - public function testPointAddSameValues() + public function testPointAddSameValues(): void { $pointInterfaceP = $this->getMockBuilder(Point::class)->disableOriginalConstructor()->getMock(); $pointInterfaceP->method('getX')->willReturn('5'); @@ -154,12 +158,12 @@ public function testPointAddSameValues() $util = $this->createClassObject(); $point = $util::pointAdd($pointInterfaceP, $pointInterfaceQ); - $this->assertIsObject($point); - $this->assertEquals('28948022309329048855892746252171976963317496166410141009864396001977208668062', $point->getX()); - $this->assertEquals('43422033463993573283839119378257965444976244249615211514796594002965813000105', $point->getY()); + self::assertIsObject($point); + self::assertSame('28948022309329048855892746252171976963317496166410141009864396001977208668062', $point->getX()); + self::assertSame('43422033463993573283839119378257965444976244249615211514796594002965813000105', $point->getY()); } - public function testPointAddInfinity() + public function testPointAddInfinity(): void { $pointInterfaceP = $this->getMockBuilder(Point::class)->disableOriginalConstructor()->getMock(); $pointInterfaceP->method('isInfinity')->willReturn(false); @@ -171,24 +175,24 @@ public function testPointAddInfinity() $util = $this->createClassObject(); $point = $util::pointAdd($pointInterfaceP, $pointInterfaceQ); - $this->assertIsObject($point); - $this->assertEquals('1', $point->getX()); - $this->assertEquals('2', $point->getY()); + self::assertIsObject($point); + self::assertSame('1', $point->getX()); + self::assertSame('2', $point->getY()); } - public function testBinConv() + public function testBinConv(): void { $util = $this->createClassObject(); - $this->assertIsString($util::binConv('FF')); + self::assertIsString($util::binConv('FF')); } - public function testCheckRequirements() + public function testCheckRequirements(): void { $util = $this->createClassObject(); - $this->assertIsArray($util::checkRequirements()); + self::assertIsArray($util::checkRequirements()); } - private function createClassObject() + private function createClassObject(): Util { return new Util(); }