ChaincodeTest   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 185
Duplicated Lines 9.73 %

Importance

Changes 0
Metric Value
dl 18
loc 185
rs 10
c 0
b 0
f 0
wmc 15

15 Methods

Rating   Name   Duplication   Size   Complexity  
A testCallPassesTransactionRequestToChannel() 0 18 1
A testInvalidNameParameterSuppliedThrowsException() 0 3 1
A testAnyUnspecifiedMethodReturnsResponseFromChannel() 8 8 1
A testFullSpecification() 0 6 1
A testDefaultPathAccessorMethod() 0 3 1
A testEmptyNameSuppliedThrowsException() 0 3 1
A testCallSendsChainCodeProposalToChannel() 0 14 1
A testCallPassesTransactionRequestToChannelAsFinalArgument() 0 18 1
A testNameAccessorMethod() 0 3 1
A testInvokeReturnsResponseFromChannel() 8 8 1
A testNoNameSuppliedThrowsException() 0 3 1
A testCallSendsArgumentlessChainCodeProposalToChannel() 0 14 1
A setUp() 0 9 1
A testInvokePassesTransactionRequestToChannelAsFinalArgument() 0 18 1
A testDefaultVersionAccessorMethod() 0 3 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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);
0 ignored issues
show
Bug introduced by
new stdClass() of type stdClass is incompatible with the type string|array expected by parameter $nameOrDetails of AmericanExpress\Hyperled...haincode::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

108
        $this->sut = new Chaincode(/** @scrutinizer ignore-type */ new \stdClass(), $this->channel);
Loading history...
109
    }
110
111 View Code Duplication
    public function testInvokeReturnsResponseFromChannel()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
112
    {
113
        $this->channel->method('processChaincodeProposal')
0 ignored issues
show
Bug introduced by
The method method() does not exist on AmericanExpress\Hyperled...posalProcessorInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

113
        $this->channel->/** @scrutinizer ignore-call */ 
114
                        method('processChaincodeProposal')

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
114
            ->willReturn($proposalResponse = new ResponseCollection());
115
116
        $response = $this->sut->invoke('query', 'a');
0 ignored issues
show
Bug introduced by
'query' of type string is incompatible with the type array expected by parameter $args of AmericanExpress\Hyperled...ode\Chaincode::invoke(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

116
        $response = $this->sut->invoke(/** @scrutinizer ignore-type */ 'query', 'a');
Loading history...
117
118
        self::assertSame($proposalResponse, $response);
119
    }
120
121 View Code Duplication
    public function testAnyUnspecifiedMethodReturnsResponseFromChannel()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
122
    {
123
        $this->channel->method('processChaincodeProposal')
124
            ->willReturn($proposalResponse = new ResponseCollection());
125
126
        $response = $this->sut->somethingElseEntirely('query', 'a');
0 ignored issues
show
Bug introduced by
The method somethingElseEntirely() does not exist on AmericanExpress\Hyperled...ent\Chaincode\Chaincode. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

126
        /** @scrutinizer ignore-call */ 
127
        $response = $this->sut->somethingElseEntirely('query', 'a');
Loading history...
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);
0 ignored issues
show
Bug introduced by
$chaincodeHeaderExtension of type Hyperledger\Fabric\Proto...haincodeHeaderExtension is incompatible with the type array expected by parameter $arguments of PHPUnit\Framework\MockOb...nvocationMocker::with(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

142
            ->with($chaincodeProposalPayload, /** @scrutinizer ignore-type */ $chaincodeHeaderExtension, null);
Loading history...
Bug introduced by
$chaincodeProposalPayload of type Hyperledger\Fabric\Proto...haincodeProposalPayload is incompatible with the type array expected by parameter $arguments of PHPUnit\Framework\MockOb...nvocationMocker::with(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

142
            ->with(/** @scrutinizer ignore-type */ $chaincodeProposalPayload, $chaincodeHeaderExtension, null);
Loading history...
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);
0 ignored issues
show
Bug introduced by
$chaincodeHeaderExtension of type Hyperledger\Fabric\Proto...haincodeHeaderExtension is incompatible with the type array expected by parameter $arguments of PHPUnit\Framework\MockOb...nvocationMocker::with(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

158
            ->with($chaincodeProposalPayload, /** @scrutinizer ignore-type */ $chaincodeHeaderExtension, null);
Loading history...
Bug introduced by
$chaincodeProposalPayload of type Hyperledger\Fabric\Proto...haincodeProposalPayload is incompatible with the type array expected by parameter $arguments of PHPUnit\Framework\MockOb...nvocationMocker::with(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

158
            ->with(/** @scrutinizer ignore-type */ $chaincodeProposalPayload, $chaincodeHeaderExtension, null);
Loading history...
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);
0 ignored issues
show
Bug introduced by
$chaincodeProposalPayload of type Hyperledger\Fabric\Proto...haincodeProposalPayload is incompatible with the type array expected by parameter $arguments of PHPUnit\Framework\MockOb...nvocationMocker::with(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

178
            ->with(/** @scrutinizer ignore-type */ $chaincodeProposalPayload, $chaincodeHeaderExtension, $transactionRequest);
Loading history...
Bug introduced by
$chaincodeHeaderExtension of type Hyperledger\Fabric\Proto...haincodeHeaderExtension is incompatible with the type array expected by parameter $arguments of PHPUnit\Framework\MockOb...nvocationMocker::with(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

178
            ->with($chaincodeProposalPayload, /** @scrutinizer ignore-type */ $chaincodeHeaderExtension, $transactionRequest);
Loading history...
Bug introduced by
$transactionRequest of type AmericanExpress\Hyperled...tion\TransactionOptions is incompatible with the type array expected by parameter $arguments of PHPUnit\Framework\MockOb...nvocationMocker::with(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

178
            ->with($chaincodeProposalPayload, $chaincodeHeaderExtension, /** @scrutinizer ignore-type */ $transactionRequest);
Loading history...
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);
0 ignored issues
show
Bug introduced by
$chaincodeHeaderExtension of type Hyperledger\Fabric\Proto...haincodeHeaderExtension is incompatible with the type array expected by parameter $arguments of PHPUnit\Framework\MockOb...nvocationMocker::with(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

198
            ->with($chaincodeProposalPayload, /** @scrutinizer ignore-type */ $chaincodeHeaderExtension, $transactionRequest);
Loading history...
Bug introduced by
$transactionRequest of type AmericanExpress\Hyperled...tion\TransactionOptions is incompatible with the type array expected by parameter $arguments of PHPUnit\Framework\MockOb...nvocationMocker::with(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

198
            ->with($chaincodeProposalPayload, $chaincodeHeaderExtension, /** @scrutinizer ignore-type */ $transactionRequest);
Loading history...
Bug introduced by
$chaincodeProposalPayload of type Hyperledger\Fabric\Proto...haincodeProposalPayload is incompatible with the type array expected by parameter $arguments of PHPUnit\Framework\MockOb...nvocationMocker::with(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

198
            ->with(/** @scrutinizer ignore-type */ $chaincodeProposalPayload, $chaincodeHeaderExtension, $transactionRequest);
Loading history...
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);
0 ignored issues
show
Bug introduced by
$transactionRequest of type AmericanExpress\Hyperled...tion\TransactionOptions is incompatible with the type array expected by parameter $arguments of PHPUnit\Framework\MockOb...nvocationMocker::with(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

218
            ->with($chaincodeProposalPayload, $chaincodeHeaderExtension, /** @scrutinizer ignore-type */ $transactionRequest);
Loading history...
Bug introduced by
$chaincodeHeaderExtension of type Hyperledger\Fabric\Proto...haincodeHeaderExtension is incompatible with the type array expected by parameter $arguments of PHPUnit\Framework\MockOb...nvocationMocker::with(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

218
            ->with($chaincodeProposalPayload, /** @scrutinizer ignore-type */ $chaincodeHeaderExtension, $transactionRequest);
Loading history...
Bug introduced by
$chaincodeProposalPayload of type Hyperledger\Fabric\Proto...haincodeProposalPayload is incompatible with the type array expected by parameter $arguments of PHPUnit\Framework\MockOb...nvocationMocker::with(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

218
            ->with(/** @scrutinizer ignore-type */ $chaincodeProposalPayload, $chaincodeHeaderExtension, $transactionRequest);
Loading history...
219
220
        $this->sut->invoke('query', 'a', $transactionRequest);
0 ignored issues
show
Bug introduced by
'query' of type string is incompatible with the type array expected by parameter $args of AmericanExpress\Hyperled...ode\Chaincode::invoke(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

220
        $this->sut->invoke(/** @scrutinizer ignore-type */ 'query', 'a', $transactionRequest);
Loading history...
Bug introduced by
$transactionRequest of type AmericanExpress\Hyperled...tion\TransactionOptions is incompatible with the type array expected by parameter $args of AmericanExpress\Hyperled...ode\Chaincode::invoke(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

220
        $this->sut->invoke('query', 'a', /** @scrutinizer ignore-type */ $transactionRequest);
Loading history...
221
    }
222
}
223