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\Header; |
22
|
|
|
|
23
|
|
|
use AmericanExpress\HyperledgerFabricClient\Header\HeaderGenerator; |
24
|
|
|
use AmericanExpress\HyperledgerFabricClient\Transaction\TransactionIdentifier; |
25
|
|
|
use AmericanExpress\HyperledgerFabricClient\Transaction\TransactionIdentifierGeneratorInterface; |
26
|
|
|
use Hyperledger\Fabric\Protos\Common\ChannelHeader; |
27
|
|
|
use Hyperledger\Fabric\Protos\Common\Header; |
28
|
|
|
use Hyperledger\Fabric\Protos\MSP\SerializedIdentity; |
29
|
|
|
use PHPUnit\Framework\TestCase; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @covers \AmericanExpress\HyperledgerFabricClient\Header\HeaderGenerator |
33
|
|
|
*/ |
34
|
|
|
class HeaderGeneratorTest extends TestCase |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* @var TransactionIdentifierGeneratorInterface|\PHPUnit_Framework_MockObject_MockObject $headerGenerator |
38
|
|
|
*/ |
39
|
|
|
private $idGenerator; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var HeaderGenerator $sut |
43
|
|
|
*/ |
44
|
|
|
private $sut; |
45
|
|
|
|
46
|
|
|
protected function setUp() |
47
|
|
|
{ |
48
|
|
|
|
49
|
|
|
$this->idGenerator = $this->getMockBuilder(TransactionIdentifierGeneratorInterface::class) |
50
|
|
|
->getMock(); |
51
|
|
|
$this->sut = new HeaderGenerator($this->idGenerator, 0); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testGeneratorReturnsInstanceOfHeader() |
55
|
|
|
{ |
56
|
|
|
$result = $this->sut->fromChannelHeader(new SerializedIdentity(), new ChannelHeader()); |
57
|
|
|
self::assertInstanceOf(Header::class, $result); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function testGeneratorPassesSerializedIdentifierToTransactionIdentifierGenerator() |
61
|
|
|
{ |
62
|
|
|
$identity = new SerializedIdentity(); |
63
|
|
|
$this->idGenerator->expects(self::once()) |
64
|
|
|
->method('fromSerializedIdentity') |
65
|
|
|
->with($identity); |
|
|
|
|
66
|
|
|
|
67
|
|
|
$this->sut->fromChannelHeader($identity, new ChannelHeader()); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testGeneratorAnnotatesChannelHeaderWithTransactionIdAndEpoch() |
71
|
|
|
{ |
72
|
|
|
$identity = new SerializedIdentity(); |
73
|
|
|
$this->idGenerator->expects(self::once()) |
74
|
|
|
->method('fromSerializedIdentity') |
75
|
|
|
->willReturn($txId = new TransactionIdentifier('12345', '54321')); |
76
|
|
|
|
77
|
|
|
$channelHeader = new ChannelHeader(); |
78
|
|
|
|
79
|
|
|
$this->sut->fromChannelHeader($identity, $channelHeader); |
80
|
|
|
|
81
|
|
|
self::assertEquals('12345', $channelHeader->getTxId()); |
82
|
|
|
self::assertEquals(0, $channelHeader->getEpoch()); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
View Code Duplication |
public function testGeneratorPassesConfiguredEpoch() |
|
|
|
|
86
|
|
|
{ |
87
|
|
|
$this->sut = new HeaderGenerator($this->idGenerator, 42); |
88
|
|
|
|
89
|
|
|
$channelHeader = new ChannelHeader(); |
90
|
|
|
|
91
|
|
|
$this->sut->fromChannelHeader(new SerializedIdentity(), $channelHeader); |
92
|
|
|
|
93
|
|
|
self::assertEquals(42, $channelHeader->getEpoch()); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
View Code Duplication |
public function testGeneratorPassesConfiguredDefaultEpoch() |
|
|
|
|
97
|
|
|
{ |
98
|
|
|
$this->sut = new HeaderGenerator($this->idGenerator); |
99
|
|
|
|
100
|
|
|
$channelHeader = new ChannelHeader(); |
101
|
|
|
|
102
|
|
|
$this->sut->fromChannelHeader(new SerializedIdentity(), $channelHeader); |
103
|
|
|
|
104
|
|
|
self::assertEquals(0, $channelHeader->getEpoch()); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|