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\Peer; |
22
|
|
|
|
23
|
|
|
use AmericanExpress\HyperledgerFabricClient\EndorserClient\EndorserClientManagerInterface; |
24
|
|
|
use AmericanExpress\HyperledgerFabricClient\Exception\InvalidArgumentException; |
25
|
|
|
use AmericanExpress\HyperledgerFabricClient\Peer\Peer; |
26
|
|
|
use AmericanExpress\HyperledgerFabricClient\Peer\PeerFactory; |
27
|
|
|
use AmericanExpress\HyperledgerFabricClient\Peer\PeerOptions; |
28
|
|
|
use Hyperledger\Fabric\Protos\Peer\EndorserClient; |
29
|
|
|
use PHPUnit\Framework\TestCase; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @covers \AmericanExpress\HyperledgerFabricClient\Peer\PeerFactory |
33
|
|
|
*/ |
34
|
|
|
class PeerFactoryTest extends TestCase |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* @var EndorserClient|\PHPUnit_Framework_MockObject_MockObject |
38
|
|
|
*/ |
39
|
|
|
private $endorserClient; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var EndorserClientManagerInterface|\PHPUnit_Framework_MockObject_MockObject |
43
|
|
|
*/ |
44
|
|
|
private $endorserClients; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var PeerFactory |
48
|
|
|
*/ |
49
|
|
|
private $sut; |
50
|
|
|
|
51
|
|
|
protected function setUp() |
52
|
|
|
{ |
53
|
|
|
$this->endorserClients = $this->getMockBuilder(EndorserClientManagerInterface::class) |
54
|
|
|
->getMock(); |
55
|
|
|
|
56
|
|
|
$this->endorserClient = $this->getMockBuilder(EndorserClient::class) |
57
|
|
|
->disableOriginalConstructor() |
58
|
|
|
->getMock(); |
59
|
|
|
|
60
|
|
|
$this->sut = new PeerFactory($this->endorserClients); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
View Code Duplication |
public function testFromPeerOptions() |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
$this->endorserClients->method('get') |
|
|
|
|
66
|
|
|
->with('localhost:8080') |
67
|
|
|
->willReturn($this->endorserClient); |
68
|
|
|
|
69
|
|
|
$result = $this->sut->fromPeerOptions(new PeerOptions([ |
70
|
|
|
'requests' => 'localhost:8080', |
71
|
|
|
])); |
72
|
|
|
|
73
|
|
|
self::assertInstanceOf(Peer::class, $result); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
View Code Duplication |
public function testFromArray() |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
$this->endorserClients->method('get') |
79
|
|
|
->with('localhost:8080') |
80
|
|
|
->willReturn($this->endorserClient); |
81
|
|
|
|
82
|
|
|
$result = $this->sut->fromArray([ |
83
|
|
|
'requests' => 'localhost:8080', |
84
|
|
|
]); |
85
|
|
|
|
86
|
|
|
self::assertInstanceOf(Peer::class, $result); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @expectedException InvalidArgumentException |
91
|
|
|
*/ |
92
|
|
|
public function testFromArrayThrowsExceptionOnInvalidConfig() |
93
|
|
|
{ |
94
|
|
|
$this->sut->fromArray([ |
95
|
|
|
'not' => 'useful', |
96
|
|
|
'configuration' => 'values' |
97
|
|
|
]); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.