Passed
Pull Request — master (#3)
by Timothy
11:06 queued 05:35
created

HeaderGeneratorTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 71
Duplicated Lines 26.76 %

Importance

Changes 0
Metric Value
dl 19
loc 71
rs 10
c 0
b 0
f 0
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testGeneratorPassesSerializedIdentifierToTransactionIdentifierGenerator() 0 8 1
A testGeneratorPassesConfiguredEpoch() 9 9 1
A testGeneratorAnnotatesChannelHeaderWithTransactionIdAndEpoch() 0 13 1
A setUp() 0 6 1
A testGeneratorPassesConfiguredDefaultEpoch() 9 9 1
A testGeneratorReturnsInstanceOfHeader() 0 4 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\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);
0 ignored issues
show
Bug introduced by
$identity of type Hyperledger\Fabric\Protos\MSP\SerializedIdentity 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

65
            ->with(/** @scrutinizer ignore-type */ $identity);
Loading history...
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()
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...
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()
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...
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