Skip to content

Commit

Permalink
Merge pull request #31 from mwarzybok-sumoheavy/feature/SP-771
Browse files Browse the repository at this point in the history
SP-771 PHP Key Utils 2.0.0
  • Loading branch information
bobbrodie authored Jan 22, 2024
2 parents 4428301 + f3aa962 commit c958595
Show file tree
Hide file tree
Showing 20 changed files with 695 additions and 685 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
name: Test

on:
push:
branches-ignore:
- 'master'
on: [push, pull_request]

jobs:
phpunit:
runs-on: ubuntu-latest

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

Expand Down
8 changes: 7 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,21 @@
"": "src/"
}
},
"autoload-dev": {
"psr-4": {
"BitPayKeyUtils\\UnitTest\\": "test/unit/BitPayKeyUtils"
}
},
"require": {
"php": "^8.1 || ^8.2 || ^8.3",
"ext-bcmath": "*",
"ext-openssl": "*",
"ext-curl": "*",
"ext-json": "*",
"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"
Expand Down
229 changes: 109 additions & 120 deletions test/unit/BitPayKeyUtils/KeyHelper/KeyTest.php
Original file line number Diff line number Diff line change
@@ -1,121 +1,110 @@
<?php

use BitPayKeyUtils\KeyHelper\Key;
use PHPUnit\Framework\TestCase;

class KeyTest extends TestCase
{
public function testCreate()
{
$testedObject = $this->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;
}
};
}
<?php

declare(strict_types=1);

namespace BitPayKeyUtils\UnitTest\KeyHelper;

use BitPayKeyUtils\KeyHelper\Key;
use PHPUnit\Framework\TestCase;

class KeyTest extends TestCase
{
public function testCreate(): void
{
$testedObject = $this->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;
}
};
}
}
Loading

0 comments on commit c958595

Please sign in to comment.