1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2017 American Express Travel Related Services Company, Inc. |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
15
|
|
|
* or implied. See the License for the specific language governing |
16
|
|
|
* permissions and limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
declare(strict_types=1); |
20
|
|
|
|
21
|
|
|
namespace AmericanExpressTest\HyperledgerFabricClient\Chaincode; |
22
|
|
|
|
23
|
|
|
use AmericanExpress\HyperledgerFabricClient\Chaincode\Chaincode; |
24
|
|
|
use AmericanExpress\HyperledgerFabricClient\Chaincode\ChaincodeProposalProcessorInterface; |
25
|
|
|
use AmericanExpress\HyperledgerFabricClient\Peer\PeerInterface; |
26
|
|
|
use AmericanExpress\HyperledgerFabricClient\Proposal\ResponseCollection; |
27
|
|
|
use AmericanExpress\HyperledgerFabricClient\ProtoFactory\ChaincodeHeaderExtensionFactory; |
28
|
|
|
use AmericanExpress\HyperledgerFabricClient\ProtoFactory\ChaincodeProposalPayloadFactory; |
29
|
|
|
use AmericanExpress\HyperledgerFabricClient\Transaction\TransactionOptions; |
30
|
|
|
use Hyperledger\Fabric\Protos\Peer\ChaincodeID; |
31
|
|
|
use PHPUnit\Framework\TestCase; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @covers \AmericanExpress\HyperledgerFabricClient\Chaincode\Chaincode |
35
|
|
|
*/ |
36
|
|
|
class ChaincodeTest extends TestCase |
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* @var PeerInterface | \PHPUnit_Framework_MockObject_MockObject |
40
|
|
|
*/ |
41
|
|
|
private $peer; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var ChaincodeProposalProcessorInterface | \PHPUnit_Framework_MockObject_MockObject |
45
|
|
|
*/ |
46
|
|
|
private $channel; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var Chaincode |
50
|
|
|
*/ |
51
|
|
|
private $sut; |
52
|
|
|
|
53
|
|
|
protected function setUp() |
54
|
|
|
{ |
55
|
|
|
$this->channel = $this->getMockBuilder(ChaincodeProposalProcessorInterface::class) |
56
|
|
|
->getMock(); |
57
|
|
|
|
58
|
|
|
$this->peer = $this->getMockBuilder(PeerInterface::class) |
59
|
|
|
->getMock(); |
60
|
|
|
|
61
|
|
|
$this->sut = new Chaincode('foo', $this->channel); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testNameAccessorMethod() |
65
|
|
|
{ |
66
|
|
|
self::assertSame('foo', $this->sut->getName()); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testDefaultVersionAccessorMethod() |
70
|
|
|
{ |
71
|
|
|
self::assertSame('', $this->sut->getVersion()); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function testDefaultPathAccessorMethod() |
75
|
|
|
{ |
76
|
|
|
self::assertSame('', $this->sut->getPath()); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testFullSpecification() |
80
|
|
|
{ |
81
|
|
|
$this->sut = new Chaincode(['name' => 'foo', 'path' => 'bar', 'version' => '12.34'], $this->channel); |
82
|
|
|
self::assertSame('foo', $this->sut->getName()); |
83
|
|
|
self::assertSame('bar', $this->sut->getPath()); |
84
|
|
|
self::assertSame('12.34', $this->sut->getVersion()); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @expectedException \AmericanExpress\HyperledgerFabricClient\Exception\InvalidArgumentException |
89
|
|
|
*/ |
90
|
|
|
public function testNoNameSuppliedThrowsException() |
91
|
|
|
{ |
92
|
|
|
$this->sut = new Chaincode(['path' => 'bar', 'version' => '12.34'], $this->channel); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @expectedException \AmericanExpress\HyperledgerFabricClient\Exception\InvalidArgumentException |
97
|
|
|
*/ |
98
|
|
|
public function testEmptyNameSuppliedThrowsException() |
99
|
|
|
{ |
100
|
|
|
$this->sut = new Chaincode(['name' => '', 'path' => 'bar', 'version' => '12.34'], $this->channel); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @expectedException \AmericanExpress\HyperledgerFabricClient\Exception\InvalidArgumentException |
105
|
|
|
*/ |
106
|
|
|
public function testInvalidNameParameterSuppliedThrowsException() |
107
|
|
|
{ |
108
|
|
|
$this->sut = new Chaincode(new \stdClass(), $this->channel); |
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
111
|
|
View Code Duplication |
public function testInvokeReturnsResponseFromChannel() |
|
|
|
|
112
|
|
|
{ |
113
|
|
|
$this->channel->method('processChaincodeProposal') |
|
|
|
|
114
|
|
|
->willReturn($proposalResponse = new ResponseCollection()); |
115
|
|
|
|
116
|
|
|
$response = $this->sut->invoke('query', 'a'); |
|
|
|
|
117
|
|
|
|
118
|
|
|
self::assertSame($proposalResponse, $response); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
View Code Duplication |
public function testAnyUnspecifiedMethodReturnsResponseFromChannel() |
|
|
|
|
122
|
|
|
{ |
123
|
|
|
$this->channel->method('processChaincodeProposal') |
124
|
|
|
->willReturn($proposalResponse = new ResponseCollection()); |
125
|
|
|
|
126
|
|
|
$response = $this->sut->somethingElseEntirely('query', 'a'); |
|
|
|
|
127
|
|
|
|
128
|
|
|
self::assertSame($proposalResponse, $response); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function testCallSendsChainCodeProposalToChannel() |
132
|
|
|
{ |
133
|
|
|
$chaincodeHeaderExtension = ChaincodeHeaderExtensionFactory::fromChaincodeId( |
134
|
|
|
(new ChaincodeID())->setName($this->sut->getName()) |
135
|
|
|
); |
136
|
|
|
|
137
|
|
|
$chaincodeProposalPayload = ChaincodeProposalPayloadFactory::fromChaincodeInvocationSpecArgs( |
138
|
|
|
['invoke', 'query', 'a'] |
139
|
|
|
); |
140
|
|
|
|
141
|
|
|
$this->channel->expects(self::once())->method('processChaincodeProposal') |
142
|
|
|
->with($chaincodeProposalPayload, $chaincodeHeaderExtension, null); |
|
|
|
|
143
|
|
|
|
144
|
|
|
$this->sut->__call('invoke', ['query', 'a']); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function testCallSendsArgumentlessChainCodeProposalToChannel() |
148
|
|
|
{ |
149
|
|
|
$chaincodeHeaderExtension = ChaincodeHeaderExtensionFactory::fromChaincodeId( |
150
|
|
|
(new ChaincodeID())->setName($this->sut->getName()) |
151
|
|
|
); |
152
|
|
|
|
153
|
|
|
$chaincodeProposalPayload = ChaincodeProposalPayloadFactory::fromChaincodeInvocationSpecArgs( |
154
|
|
|
['invoke'] |
155
|
|
|
); |
156
|
|
|
|
157
|
|
|
$this->channel->expects(self::once())->method('processChaincodeProposal') |
158
|
|
|
->with($chaincodeProposalPayload, $chaincodeHeaderExtension, null); |
|
|
|
|
159
|
|
|
|
160
|
|
|
$this->sut->__call('invoke', []); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function testCallPassesTransactionRequestToChannel() |
164
|
|
|
{ |
165
|
|
|
$transactionRequest = new TransactionOptions([ |
166
|
|
|
'peers' => [$this->peer], |
167
|
|
|
]); |
168
|
|
|
|
169
|
|
|
$chaincodeHeaderExtension = ChaincodeHeaderExtensionFactory::fromChaincodeId( |
170
|
|
|
(new ChaincodeID())->setName($this->sut->getName()) |
171
|
|
|
); |
172
|
|
|
|
173
|
|
|
$chaincodeProposalPayload = ChaincodeProposalPayloadFactory::fromChaincodeInvocationSpecArgs( |
174
|
|
|
['invoke'] |
175
|
|
|
); |
176
|
|
|
|
177
|
|
|
$this->channel->expects(self::once())->method('processChaincodeProposal') |
178
|
|
|
->with($chaincodeProposalPayload, $chaincodeHeaderExtension, $transactionRequest); |
|
|
|
|
179
|
|
|
|
180
|
|
|
$this->sut->__call('invoke', [$transactionRequest]); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public function testCallPassesTransactionRequestToChannelAsFinalArgument() |
184
|
|
|
{ |
185
|
|
|
$transactionRequest = new TransactionOptions([ |
186
|
|
|
'peers' => [$this->peer], |
187
|
|
|
]); |
188
|
|
|
|
189
|
|
|
$chaincodeHeaderExtension = ChaincodeHeaderExtensionFactory::fromChaincodeId( |
190
|
|
|
(new ChaincodeID())->setName($this->sut->getName()) |
191
|
|
|
); |
192
|
|
|
|
193
|
|
|
$chaincodeProposalPayload = ChaincodeProposalPayloadFactory::fromChaincodeInvocationSpecArgs( |
194
|
|
|
['invoke', 'query', 'a'] |
195
|
|
|
); |
196
|
|
|
|
197
|
|
|
$this->channel->expects(self::once())->method('processChaincodeProposal') |
198
|
|
|
->with($chaincodeProposalPayload, $chaincodeHeaderExtension, $transactionRequest); |
|
|
|
|
199
|
|
|
|
200
|
|
|
$this->sut->__call('invoke', ['query', 'a', $transactionRequest]); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
public function testInvokePassesTransactionRequestToChannelAsFinalArgument() |
204
|
|
|
{ |
205
|
|
|
$transactionRequest = new TransactionOptions([ |
206
|
|
|
'peers' => [$this->peer], |
207
|
|
|
]); |
208
|
|
|
|
209
|
|
|
$chaincodeHeaderExtension = ChaincodeHeaderExtensionFactory::fromChaincodeId( |
210
|
|
|
(new ChaincodeID())->setName($this->sut->getName()) |
211
|
|
|
); |
212
|
|
|
|
213
|
|
|
$chaincodeProposalPayload = ChaincodeProposalPayloadFactory::fromChaincodeInvocationSpecArgs( |
214
|
|
|
['invoke', 'query', 'a'] |
215
|
|
|
); |
216
|
|
|
|
217
|
|
|
$this->channel->expects(self::once())->method('processChaincodeProposal') |
218
|
|
|
->with($chaincodeProposalPayload, $chaincodeHeaderExtension, $transactionRequest); |
|
|
|
|
219
|
|
|
|
220
|
|
|
$this->sut->invoke('query', 'a', $transactionRequest); |
|
|
|
|
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|