ProposalFactoryTest::testCreate()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 33
Code Lines 24

Duplication

Lines 33
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 24
nc 1
nop 0
dl 33
loc 33
rs 8.8571
c 0
b 0
f 0
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\ProtoFactory;
22
23
use AmericanExpress\HyperledgerFabricClient\ProtoFactory\ChaincodeInvocationSpecFactory;
24
use AmericanExpress\HyperledgerFabricClient\ProtoFactory\ChaincodeProposalPayloadFactory;
25
use AmericanExpress\HyperledgerFabricClient\ProtoFactory\ChannelHeaderFactory;
26
use AmericanExpress\HyperledgerFabricClient\ProtoFactory\HeaderFactory;
27
use AmericanExpress\HyperledgerFabricClient\ProtoFactory\ProposalFactory;
28
use AmericanExpress\HyperledgerFabricClient\ProtoFactory\SerializedIdentityFactory;
29
use AmericanExpress\HyperledgerFabricClient\ProtoFactory\SignatureHeaderFactory;
30
use AmericanExpressTest\HyperledgerFabricClient\Chaincode\AbstractChaincodeTest;
31
use Hyperledger\Fabric\Protos\Peer\Proposal;
32
use PHPUnit\Framework\TestCase;
33
34
/**
35
 * @covers \AmericanExpress\HyperledgerFabricClient\ProtoFactory\ProposalFactory
36
 */
37
class ProposalFactoryTest extends AbstractChaincodeTest
38
{
39
    protected function setUp()
40
    {
41
        parent::setUp();
42
    }
43
44 View Code Duplication
    public function testCreate()
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...
45
    {
46
        $serializedIdentity = SerializedIdentityFactory::fromBytes('Alice', 'Bob');
47
        $nonce = 'u58920du89f';
48
        $txId = 'MyTransactionId';
49
50
        $channelHeader = ChannelHeaderFactory::create('MyChannelId');
51
        $channelHeader->setTxId($txId);
52
53
        $header = HeaderFactory::create(SignatureHeaderFactory::create(
54
            $serializedIdentity,
55
            $nonce
56
        ), $channelHeader);
57
58
        $chaincodeInvocationSpec = ChaincodeInvocationSpecFactory::fromArgs([
59
            'foo',
60
            'bar',
61
        ]);
62
63
        $chaincodeProposalPayload = ChaincodeProposalPayloadFactory::fromChaincodeInvocationSpec(
64
            $chaincodeInvocationSpec
65
        );
66
67
        $result = ProposalFactory::create($header, $chaincodeProposalPayload->serializeToString());
68
        self::assertInstanceOf(Proposal::class, $result);
69
        self::assertContains('Alice', $result->getHeader());
70
        self::assertContains('Bob', $result->getHeader());
71
        self::assertContains('MyChannelId', $result->getHeader());
72
        self::assertContains('MyTransactionId', $result->getHeader());
73
        self::assertContains('u58920du89f', $result->getHeader());
74
        self::assertContains('foo', $result->getPayload());
75
        self::assertContains('bar', $result->getPayload());
76
        self::assertSame('', $result->getExtension());
77
    }
78
79
    /**
80
     * @dataProvider getChainCodeProposalDataset
81
     * @param string $dateTime
82
     * @param string $proposalHeader
83
     * @param string $proposalPayload
84
     * @param string $proposalExtension
85
     */
86
    public function testCreateChaincodeProposal(
87
        string $dateTime,
88
        string $proposalHeader,
89
        string $proposalPayload,
90
        string $proposalExtension
91
    ) {
92
        $proposal = $this->createChaincodeProposal($dateTime, $this->getPrivateKeyFile());
93
94
        self::assertEquals(base64_decode($proposalHeader), $proposal->getHeader());
95
        self::assertEquals(base64_decode($proposalPayload), $proposal->getPayload());
96
        self::assertEquals(base64_decode($proposalExtension), $proposal->getExtension());
97
    }
98
}
99