PaymentRequestTest::testPkiType()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 9.504
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Bip70\Test\Protobuf\Proto;
6
7
use Bip70\Protobuf\Codec\NonDiscardingBinaryCodec;
8
use Bip70\Protobuf\Proto\PaymentDetails;
9
use Bip70\Protobuf\Proto\PaymentRequest;
10
use Bip70\X509\PKIType;
11
use PHPUnit\Framework\TestCase;
12
13
class PaymentRequestTest extends TestCase
14
{
15
    public function testSerializedPaymentDetails()
16
    {
17
        $now = time();
18
        $details = new PaymentDetails();
19
        $details->setTime($now);
20
21
        $request = new PaymentRequest();
22
        $this->assertFalse($request->hasSerializedPaymentDetails());
23
        $request->setSerializedPaymentDetails($details->serialize());
24
        $this->assertTrue($request->hasSerializedPaymentDetails());
25
26
        $codec = new NonDiscardingBinaryCodec();
27
        $parsed = new PaymentRequest();
28
        $parsed->parse($request->serialize($codec), $codec);
29
30
        $this->assertEquals($request->hasSerializedPaymentDetails(), $parsed->hasSerializedPaymentDetails());
31
        $this->assertEquals($request->getSerializedPaymentDetails(), $parsed->getSerializedPaymentDetails());
32
33
        $request->clearSerializedPaymentDetails();
34
        $this->assertFalse($request->hasSerializedPaymentDetails());
35
    }
36
37
    public function testDetailsVersion()
38
    {
39
        $now = time();
40
        $details = new PaymentDetails();
41
        $details->setTime($now);
42
43
        $request = new PaymentRequest();
44
        $request->setSerializedPaymentDetails($details->serialize());
45
46
        $this->assertTrue($request->hasPaymentDetailsVersion());
47
        $this->assertEquals(1, $request->getPaymentDetailsVersion());
48
49
        $request->clearPaymentDetailsVersion();
50
        $this->assertFalse($request->hasPaymentDetailsVersion());
51
52
        $request->setPaymentDetailsVersion(2);
53
        $this->assertTrue($request->hasPaymentDetailsVersion());
54
        $this->assertEquals(2, $request->getPaymentDetailsVersion());
55
56
        $codec = new NonDiscardingBinaryCodec();
57
        $parsed = new PaymentRequest();
58
        $parsed->parse($request->serialize($codec), $codec);
59
60
        $this->assertEquals($request->hasPaymentDetailsVersion(), $parsed->hasPaymentDetailsVersion());
61
        $this->assertEquals($request->getPaymentDetailsVersion(), $parsed->getPaymentDetailsVersion());
62
    }
63
64
    public function testPkiType()
65
    {
66
        $now = time();
67
        $details = new PaymentDetails();
68
        $details->setTime($now);
69
70
        $request = new PaymentRequest();
71
        $request->setSerializedPaymentDetails($details->serialize());
72
73
        $this->assertTrue($request->hasPkiType());
74
        $this->assertEquals(PKIType::NONE, $request->getPkiType());
75
76
        $request->clearPkiType();
77
        $this->assertFalse($request->hasPkiType());
78
79
        $request->setPkiType(PKIType::X509_SHA1);
80
        $this->assertTrue($request->hasPkiType());
81
        $this->assertEquals(PKIType::X509_SHA1, $request->getPkiType());
82
83
        $codec = new NonDiscardingBinaryCodec();
84
        $parsed = new PaymentRequest();
85
        $parsed->parse($request->serialize($codec), $codec);
86
87
        $this->assertEquals($request->hasPkiType(), $parsed->hasPkiType());
88
        $this->assertEquals($request->getPkiType(), $parsed->getPkiType());
89
    }
90
91
    public function testPkiData()
92
    {
93
        $now = time();
94
        $details = new PaymentDetails();
95
        $details->setTime($now);
96
97
        $x509data = 'serialized.x509..';
98
99
        $request = new PaymentRequest();
100
        $request->setSerializedPaymentDetails($details->serialize());
101
102
        $this->assertFalse($request->hasPkiData());
103
        $request->setPkiData($x509data);
104
        $this->assertTrue($request->hasPkiData());
105
        $this->assertEquals($x509data, $request->getPkiData());
106
107
        $codec = new NonDiscardingBinaryCodec();
108
        $parsed = new PaymentRequest();
109
        $parsed->parse($request->serialize($codec), $codec);
110
111
        $this->assertEquals($request->hasPkiData(), $parsed->hasPkiData());
112
        $this->assertEquals($request->getPkiData(), $parsed->getPkiData());
113
114
        $request->clearPkiData();
115
        $this->assertFalse($request->hasPkiData());
116
    }
117
118
    public function testSignature()
119
    {
120
        $now = time();
121
        $details = new PaymentDetails();
122
        $details->setTime($now);
123
124
        $signature = 'serialized.sig..';
125
126
        $request = new PaymentRequest();
127
        $request->setSerializedPaymentDetails($details->serialize());
128
129
        $this->assertFalse($request->hasSignature());
130
        $request->setSignature($signature);
131
        $this->assertTrue($request->hasSignature());
132
        $this->assertEquals($signature, $request->getSignature());
133
134
        $codec = new NonDiscardingBinaryCodec();
135
        $parsed = new PaymentRequest();
136
        $parsed->parse($request->serialize($codec), $codec);
137
138
        $this->assertEquals($request->hasSignature(), $parsed->hasSignature());
139
        $this->assertEquals($request->getSignature(), $parsed->getSignature());
140
141
        $request->clearSignature();
142
        $this->assertFalse($request->hasSignature());
143
    }
144
}
145