testHashAlgorithmAssessorDefaultValue()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 1
b 0
f 1
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\Config;
22
23
use AmericanExpress\HyperledgerFabricClient\Config\ClientConfig;
24
use AmericanExpress\HyperledgerFabricClient\Exception\InvalidArgumentException;
25
use AmericanExpress\HyperledgerFabricClient\Organization\OrganizationOptionsInterface;
26
use org\bovigo\vfs\vfsStream;
27
use org\bovigo\vfs\vfsStreamDirectory;
28
use PHPUnit\Framework\TestCase;
29
30
/**
31
 * @covers \AmericanExpress\HyperledgerFabricClient\Config\ClientConfig
32
 */
33
class ClientConfigTest extends TestCase
34
{
35
    /**
36
     * @var vfsStreamDirectory
37
     */
38
    private $files;
39
40
    /**
41
     * @var ClientConfig
42
     */
43
    private $sut;
44
45
    protected function setUp()
46
    {
47
        $this->files = vfsStream::setup('test');
48
49
        $this->sut = new ClientConfig([
50
            'foo' => [
51
                'bar' => 'FizBuz',
52
            ],
53
        ]);
54
    }
55
56
    public function testGetIn()
57
    {
58
        self::assertSame('FizBuz', $this->sut->getIn(['foo', 'bar']));
59
        self::assertSame(['bar' => 'FizBuz'], $this->sut->getIn(['foo']));
60
        self::assertNull($this->sut->getIn(['Alice', 'Bob']));
61
        self::assertSame('FizBuz', $this->sut->getIn(['Alice', 'Bob'], 'FizBuz'));
62
    }
63
64
    public function testGetDefaults()
65
    {
66
        $sut = new ClientConfig([]);
67
68
        self::assertSame(5000, $sut->getIn(['timeout']));
69
        self::assertSame(0, $sut->getIn(['epoch']));
70
        self::assertSame('sha256', (string)$sut->getIn(['crypto-hash-algo']));
71
        self::assertSame(24, $sut->getIn(['nonce-size']));
72
    }
73
74
    public function testOverrideDefaults()
75
    {
76
        $sut = new ClientConfig([
77
            'timeout' => 10,
78
            'epoch' => -100,
79
            'crypto-hash-algo' => 'md5',
80
            'nonce-size' => 3,
81
        ]);
82
83
        self::assertSame(10, $sut->getIn(['timeout']));
84
        self::assertSame(-100, $sut->getIn(['epoch']));
85
        self::assertSame('md5', (string)$sut->getIn(['crypto-hash-algo']));
86
        self::assertSame(3, $sut->getIn(['nonce-size']));
87
    }
88
89
    public function testNonceSizeAssessor()
90
    {
91
        $sut = new ClientConfig([
92
            'nonce-size' => 234,
93
        ]);
94
95
        self::assertSame(234, $sut->getNonceSize());
96
    }
97
98
    public function testNonceSizeAssessorDefaultValue()
99
    {
100
        $sut = new ClientConfig([]);
101
102
        self::assertSame(24, $sut->getNonceSize());
103
    }
104
105
    public function testEpochAssessor()
106
    {
107
        $sut = new ClientConfig([
108
            'epoch' => 234,
109
        ]);
110
111
        self::assertSame(234, $sut->getEpoch());
112
    }
113
114
    public function testEpochAssessorDefaultValue()
115
    {
116
        $sut = new ClientConfig([]);
117
118
        self::assertSame(0, $sut->getEpoch());
119
    }
120
121
    public function testHashAlgorithmAssessor()
122
    {
123
        $sut = new ClientConfig([
124
            'crypto-hash-algo' => 'whirlpool',
125
        ]);
126
127
        self::assertSame('whirlpool', (string)$sut->getHashAlgorithm());
128
    }
129
130
    public function testHashAlgorithmAssessorDefaultValue()
131
    {
132
        $sut = new ClientConfig([]);
133
134
        self::assertSame('sha256', (string)$sut->getHashAlgorithm());
135
    }
136
137
    public function testTimeoutAssessor()
138
    {
139
        $sut = new ClientConfig([
140
            'timeout' => 234,
141
        ]);
142
143
        self::assertSame(234, $sut->getTimeout());
144
    }
145
146
    public function testTimeoutAssessorDefaultValue()
147
    {
148
        $sut = new ClientConfig([]);
149
150
        self::assertSame(5000, $sut->getTimeout());
151
    }
152
153
    public function testGetOrganizationOptionsByNetworkAndOrganizationNames()
154
    {
155
        $sut = new ClientConfig([
156
            'organizations' => [
157
                [
158
                    'name' => 'peerOrg1',
159
                    'mspid' => 'Org1MSP',
160
                    'admin_certs' => __FILE__,
161
                    'private_key' => __FILE__,
162
                ],
163
            ],
164
        ]);
165
166
        self::assertInstanceOf(
167
            OrganizationOptionsInterface::class,
168
            $sut->getOrganizationByName('peerOrg1')
169
        );
170
    }
171
172
    public function testGetOrganizationOptionsByInvalidNetworkAndOrganizationNames()
173
    {
174
        $sut = new ClientConfig([]);
175
176
        self::assertNull($sut->getOrganizationByName('FooBar'));
177
    }
178
179
    /**
180
     * @expectedException \AmericanExpress\HyperledgerFabricClient\Exception\InvalidArgumentException
181
     */
182
    public function testThrowsExceptionOnInvalidHashAlgoInConfigFile()
183
    {
184
        $config = new ClientConfig([
185
            'crypto-hash-algo' => 'DEFINITELY INVALID'
186
        ]);
187
188
        $config->getHashAlgorithm();
189
    }
190
191
    public function testGetDefaultOrganization()
192
    {
193
        $config = new ClientConfig([
194
            'organizations' => [
195
                [
196
                    'name' => 'peerOrg1',
197
                ],
198
            ],
199
        ]);
200
201
        $result = $config->getDefaultOrganization();
202
203
        self::assertInstanceOf(OrganizationOptionsInterface::class, $result);
204
        self::assertSame('peerOrg1', $result->getName());
205
    }
206
207
    /**
208
     * @expectedException InvalidArgumentException
209
     */
210
    public function testThrowsExceptionOnInvalidOrganizationConfig()
211
    {
212
        new ClientConfig([
213
            'organizations' => [
214
                [
215
                    'THIS' => 'IS NOT GOING TO WORK'
216
                ],
217
            ],
218
        ]);
219
    }
220
}
221