-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SP-771 PHP Key Utils 2.0.0 - improve tests
- Loading branch information
1 parent
f731b33
commit f3aa962
Showing
18 changed files
with
681 additions
and
679 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; | ||
} | ||
} |
Oops, something went wrong.