MdanterEccSignatoryTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 108
Duplicated Lines 18.52 %

Importance

Changes 0
Metric Value
dl 20
loc 108
rs 10
c 0
b 0
f 0
wmc 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetSCharacterization() 0 16 1
A testSignProposal() 0 14 1
A dataGetS() 0 3 1
A testGetS() 0 8 1
A setUp() 0 4 1
A getProposalSignatureCharacterizationData() 18 18 1
A testSignEmptyProposal() 0 9 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\Signatory;
22
23
use AmericanExpress\HyperledgerFabricClient\Signatory\MdanterEccSignatory;
24
use AmericanExpressTest\HyperledgerFabricClient\Chaincode\AbstractChaincodeTest;
25
use Hyperledger\Fabric\Protos\Peer\Proposal;
26
use Hyperledger\Fabric\Protos\Peer\SignedProposal;
27
28
/**
29
 * @covers \AmericanExpress\HyperledgerFabricClient\Signatory\MdanterEccSignatory
30
 */
31
class MdanterEccSignatoryTest extends AbstractChaincodeTest
32
{
33
    /**
34
     * @var MdanterEccSignatory
35
     */
36
    private $sut;
37
38
    protected function setUp()
39
    {
40
        parent::setUp();
41
        $this->sut = new MdanterEccSignatory();
42
    }
43
44
    public function testSignEmptyProposal()
45
    {
46
        $result = $this->sut->signProposal(new Proposal(), $this->privateKeyFile);
47
48
        self::assertInstanceOf(SignedProposal::class, $result);
49
        self::assertInternalType('string', $result->getProposalBytes());
50
        self::assertEmpty($result->getProposalBytes());
51
        self::assertInternalType('string', $result->getSignature());
52
        self::assertNotEmpty($result->getSignature());
53
    }
54
55
    public function testSignProposal()
56
    {
57
        $proposal = new Proposal();
58
        $proposal->setHeader('HEADER-STRING');
59
        $proposal->setPayload('PAYLOAD-STRING');
60
        $result = $this->sut->signProposal($proposal, $this->privateKeyFile);
61
62
        self::assertEquals(
63
            'Cg1IRUFERVItU1RSSU5HEg5QQVlMT0FELVNUUklORw==',
64
            base64_encode($result->getProposalBytes())
65
        );
66
        self::assertEquals(
67
            'MEQCIEfgYNT2Rve6kGy7Ter1/77KcJin1MImCroLqIzdiLmtAiBRjOCkd7aW6KM+qRzDxWmC1+X9aP/tzWD6/Z5a2E9zOA==',
68
            base64_encode($result->getSignature())
69
        );
70
    }
71
72
    /**
73
     * @dataProvider getProposalSignatureCharacterizationData
74
     * @param string $encodedProposalBytes
75
     * @param string $encodedSignature
76
     * @param string $proposalHeader
77
     * @param string $proposalPayload
78
     * @param string $proposalExtension
79
     */
80
    public function testGetSCharacterization(
81
        string $encodedProposalBytes,
82
        string $encodedSignature,
83
        string $proposalHeader,
84
        string $proposalPayload,
85
        string $proposalExtension
86
    ) {
87
        $proposal = new Proposal();
88
        $proposal->setHeader(base64_decode($proposalHeader));
0 ignored issues
show
Bug introduced by
It seems like base64_decode($proposalHeader) can also be of type false; however, parameter $var of Hyperledger\Fabric\Proto...r\Proposal::setHeader() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

88
        $proposal->setHeader(/** @scrutinizer ignore-type */ base64_decode($proposalHeader));
Loading history...
89
        $proposal->setPayload(base64_decode($proposalPayload));
0 ignored issues
show
Bug introduced by
It seems like base64_decode($proposalPayload) can also be of type false; however, parameter $var of Hyperledger\Fabric\Proto...\Proposal::setPayload() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

89
        $proposal->setPayload(/** @scrutinizer ignore-type */ base64_decode($proposalPayload));
Loading history...
90
        $proposal->setExtension(base64_decode($proposalExtension));
0 ignored issues
show
Bug introduced by
It seems like base64_decode($proposalExtension) can also be of type false; however, parameter $var of Hyperledger\Fabric\Proto...roposal::setExtension() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

90
        $proposal->setExtension(/** @scrutinizer ignore-type */ base64_decode($proposalExtension));
Loading history...
91
        $result = $this->sut->signProposal($proposal, new \SplFileObject($this->privateKey->url()));
92
93
        self::assertInstanceOf(SignedProposal::class, $result);
94
        self::assertEquals($encodedProposalBytes, base64_encode($result->getProposalBytes()));
95
        self::assertEquals($encodedSignature, base64_encode($result->getSignature()));
96
    }
97
98
    /**
99
     * @covers       \AmericanExpress\HyperledgerFabricClient\Signatory\MdanterEccSignatory::getS
100
     * @dataProvider dataGetS
101
     * @param string $dateTime
102
     * @param string $encodedProposalBytes
103
     * @param string $encodedSignature
104
     */
105
    public function testGetS(string $dateTime, string $encodedProposalBytes, string $encodedSignature)
106
    {
107
        $proposal = $this->createChaincodeProposal($dateTime, $this->getPrivateKeyFile());
108
        $result = $this->sut->signProposal($proposal, new \SplFileObject($this->privateKey->url()));
109
110
        self::assertInstanceOf(SignedProposal::class, $result);
111
        self::assertEquals($encodedProposalBytes, base64_encode($result->getProposalBytes()));
112
        self::assertEquals($encodedSignature, base64_encode($result->getSignature()));
113
    }
114
115 View Code Duplication
    public function getProposalSignatureCharacterizationData()
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...
116
    {
117
        $data = $this->loadStaticData();
118
119
        return array_map(
120
            function ($value) {
121
                return array_intersect_key(
122
                    $value,
123
                    array_flip([
124
                        'encodedProposalBytes',
125
                        'encodedSignature',
126
                        'proposalHeader',
127
                        'proposalPayload',
128
                        'proposalExtension',
129
                    ])
130
                );
131
            },
132
            $data
133
        );
134
    }
135
136
    public function dataGetS()
137
    {
138
        return $this->loadStaticData();
139
    }
140
}
141