forked from bitpay/bitpay-php-keyutils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyTest.php
More file actions
109 lines (88 loc) · 3.03 KB
/
KeyTest.php
File metadata and controls
109 lines (88 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?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->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;
}
};
}
}