Completed
Push — master ( a29d2d...4681dd )
by Hannes
10:53 queued 08:08
created

AccountProcessorSpec   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 59
Duplicated Lines 23.73 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 3
dl 14
loc 59
rs 10
c 0
b 0
f 0

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
declare(strict_types = 1);
4
5
namespace spec\byrokrat\autogiro\Processor;
6
7
use byrokrat\autogiro\Processor\AccountProcessor;
8
use byrokrat\autogiro\Tree\AccountNode;
9
use byrokrat\autogiro\Tree\BankgiroNode;
10
use byrokrat\banking\AccountFactory;
11
use byrokrat\banking\AccountNumber;
12
use byrokrat\banking\Exception\InvalidAccountNumberException as BankingException;
13
use PhpSpec\ObjectBehavior;
14
15
class AccountProcessorSpec extends ObjectBehavior
16
{
17
    function let(
18
        AccountFactory $accountFactory,
19
        AccountFactory $bankgiroFactory,
20
        AccountNode $accountNode,
21
        BankgiroNode $bankgiroNode,
22
        AccountNumber $accountNumber
23
    ) {
24
        $accountFactory->createAccount('not-valid')->willThrow(BankingException::CLASS);
25
        $accountFactory->createAccount('valid')->willReturn($accountNumber);
26
27
        $bankgiroFactory->createAccount('not-valid')->willThrow(BankingException::CLASS);
28
        $bankgiroFactory->createAccount('valid')->willReturn($accountNumber);
29
30
        $accountNode->getLineNr()->willReturn(1);
31
        $accountNode->getType()->willReturn('AccountNode');
32
33
        $bankgiroNode->getLineNr()->willReturn(1);
34
        $bankgiroNode->getType()->willReturn('BankgiroNode');
35
36
        $this->beConstructedWith($accountFactory, $bankgiroFactory);
37
    }
38
39
    function it_is_initializable()
40
    {
41
        $this->shouldHaveType(AccountProcessor::CLASS);
42
    }
43
44
    function it_fails_on_unvalid_account_number($accountNode)
45
    {
46
        $accountNode->getValue()->willReturn('not-valid');
47
        $this->visitBefore($accountNode);
48
        $this->getErrors()->shouldHaveCount(1);
49
    }
50
51
    function it_creates_valid_account_numbers($accountNode, $accountNumber)
52
    {
53
        $accountNode->getValue()->willReturn('valid');
54
        $accountNode->setAttribute('account', $accountNumber)->shouldBeCalled();
55
        $this->visitBefore($accountNode);
56
        $this->getErrors()->shouldHaveCount(0);
57
    }
58
59
    function it_fails_on_unvalid_bankgiro_number($bankgiroNode)
60
    {
61
        $bankgiroNode->getValue()->willReturn('not-valid');
62
        $this->visitBefore($bankgiroNode);
63
        $this->getErrors()->shouldHaveCount(1);
64
    }
65
66
    function it_creates_valid_bankgiro_numbers($bankgiroNode, $accountNumber)
67
    {
68
        $bankgiroNode->getValue()->willReturn('valid');
69
        $bankgiroNode->setAttribute('account', $accountNumber)->shouldBeCalled();
70
        $this->visitBefore($bankgiroNode);
71
        $this->getErrors()->shouldHaveCount(0);
72
    }
73
}
74