TransactionOptionsTest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 95
Duplicated Lines 31.58 %

Importance

Changes 0
Metric Value
dl 30
loc 95
rs 10
c 0
b 0
f 0
wmc 8

8 Methods

Rating   Name   Duplication   Size   Complexity  
A testAddManyPeers() 10 10 1
A testFromMultiDimensionalArray() 8 8 1
A setUp() 0 6 1
A testDefaultPeers() 0 4 1
A testAddOnePeer() 0 10 1
A testAddPeersImmutable() 0 16 1
A testFromArray() 9 9 1
A testSetPeers() 0 6 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\Transaction;
22
23
use AmericanExpress\HyperledgerFabricClient\Peer\PeerInterface;
24
use AmericanExpress\HyperledgerFabricClient\Peer\PeerOptions;
25
use AmericanExpress\HyperledgerFabricClient\Transaction\TransactionOptions;
26
use PHPUnit\Framework\TestCase;
27
28
/**
29
 * @covers \AmericanExpress\HyperledgerFabricClient\Transaction\TransactionOptions
30
 */
31
class TransactionOptionsTest extends TestCase
32
{
33
    /**
34
     * @var PeerInterface|\PHPUnit_Framework_MockObject_MockObject
35
     */
36
    private $peer;
37
38
    /**
39
     * @var TransactionOptions
40
     */
41
    private $sut;
42
43
    protected function setUp()
44
    {
45
        $this->peer = $this->getMockBuilder(PeerInterface::class)
46
            ->getMock();
47
48
        $this->sut = new TransactionOptions();
49
    }
50
51
    public function testDefaultPeers()
52
    {
53
        self::assertFalse($this->sut->hasPeers());
54
        self::assertCount(0, $this->sut->getPeers());
55
    }
56
57
    public function testAddOnePeer()
58
    {
59
        self::assertFalse($this->sut->hasPeers());
60
        self::assertCount(0, $this->sut->getPeers());
61
62
        $this->sut->addPeers($this->peer);
63
64
        self::assertTrue($this->sut->hasPeers());
65
        self::assertCount(1, $this->sut->getPeers());
66
        self::assertContains($this->peer, $this->sut->getPeers());
67
    }
68
69 View Code Duplication
    public function testFromArray()
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...
70
    {
71
        $sut = new TransactionOptions([
72
            'peers' => [$this->peer],
73
        ]);
74
75
        self::assertTrue($sut->hasPeers());
76
        self::assertCount(1, $sut->getPeers());
77
        self::assertContains($this->peer, $sut->getPeers());
78
    }
79
80 View Code Duplication
    public function testFromMultiDimensionalArray()
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...
81
    {
82
        $sut = new TransactionOptions([
83
            'peers' => [$this->peer],
84
        ]);
85
86
        self::assertCount(1, $sut->getPeers());
87
        self::assertContains($this->peer, $sut->getPeers());
88
    }
89
90
    public function testSetPeers()
91
    {
92
        $this->sut->setPeers([$this->peer]);
93
94
        self::assertCount(1, $this->sut->getPeers());
95
        self::assertContains($this->peer, $this->sut->getPeers());
96
    }
97
98 View Code Duplication
    public function testAddManyPeers()
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...
99
    {
100
        $peer = $this->getMockBuilder(PeerInterface::class)
101
            ->getMock();
102
103
        $this->sut->addPeers($this->peer, $peer);
104
105
        self::assertCount(2, $this->sut->getPeers());
106
        self::assertContains($this->peer, $this->sut->getPeers());
107
        self::assertContains($peer, $this->sut->getPeers());
108
    }
109
110
    public function testAddPeersImmutable()
111
    {
112
        $this->sut->addPeers($this->peer);
113
114
        $peer = $this->getMockBuilder(PeerInterface::class)
115
            ->getMock();
116
117
        $result = $this->sut->withPeers($peer);
118
119
        self::assertCount(1, $result->getPeers());
120
        self::assertNotContains($this->peer, $result->getPeers());
121
        self::assertContains($peer, $result->getPeers());
122
123
        self::assertCount(1, $this->sut->getPeers());
124
        self::assertContains($this->peer, $this->sut->getPeers());
125
        self::assertNotContains($peer, $this->sut->getPeers());
126
    }
127
}
128