Completed
Push — master ( 7c1eaa...80d841 )
by Joachim
15:53
created

TerminalSynchronizerTest::testSyncAll()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 90
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 90
c 0
b 0
f 0
rs 8.5454
cc 1
eloc 35
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Loevgaard\DandomainAltapayBundle\Tests\Synchronizer;
4
5
use Loevgaard\AltaPay\Client;
6
use Loevgaard\AltaPay\Response\GetTerminals;
7
use Loevgaard\DandomainAltapayBundle\Entity\Terminal;
8
use Loevgaard\DandomainAltapayBundle\Manager\TerminalManager;
9
use Loevgaard\DandomainAltapayBundle\Synchronizer\TerminalSynchronizer;
10
use PHPUnit\Framework\TestCase;
11
use Psr\Http\Message\ResponseInterface;
12
13
class TerminalSynchronizerTest extends TestCase
14
{
15
    public function testSyncAll()
16
    {
17
        $xml = <<<XML
18
<?xml version="1.0"?>
19
<APIResponse version="20170228">
20
  <Header>
21
    <Date>2016-01-07T23:43:20+01:00</Date>
22
    <Path>API/getTerminals</Path>
23
    <ErrorCode>0</ErrorCode>
24
    <ErrorMessage></ErrorMessage>
25
  </Header>
26
  <Body>
27
    <Result>Success</Result>
28
    <Terminals>
29
      <Terminal>
30
        <Title>AltaPay Multi-Nature Terminal</Title>
31
        <Country>DK</Country>
32
        <Natures>
33
          <Nature>CreditCard</Nature>
34
          <Nature>EPayment</Nature>
35
          <Nature>IdealPayment</Nature>
36
          <Nature>Invoice</Nature>
37
        </Natures>
38
        <Currencies>
39
          <Currency>DKK</Currency>
40
          <Currency>EUR</Currency>
41
        </Currencies>
42
      </Terminal>
43
      <Terminal>
44
        <Title>AltaPay BankPayment Terminal</Title>
45
        <Country></Country>
46
        <Natures>
47
          <Nature>BankPayment</Nature>
48
        </Natures>
49
        <Currencies>
50
          <Currency>EUR</Currency>
51
        </Currencies>
52
      </Terminal>
53
    </Terminals>
54
  </Body>
55
</APIResponse>
56
XML;
57
58
        $terminal = $this->createMock(Terminal::class);
59
60
        $psrResponse = $this->createMock(ResponseInterface::class);
61
        $psrResponse
62
            ->expects($this->any())
63
            ->method('getBody')
64
            ->willReturn($xml)
65
            ;
66
        $getTerminalsResponse = new GetTerminals($psrResponse);
67
68
        /** @var TerminalManager|\PHPUnit_Framework_MockObject_MockObject $terminalManager */
69
        $terminalManager = $this->getMockBuilder(TerminalManager::class)
70
            ->disableOriginalConstructor()
71
            ->setMethods(['create', 'update', 'findTerminalByTitle'])
72
            ->getMock()
73
        ;
74
75
        $terminalManager
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Loevgaard\DandomainAltap...Manager\TerminalManager.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
76
            ->expects($this->any())
77
            ->method('create')
78
            ->willReturn($terminal)
79
        ;
80
81
        $terminalManager
82
            ->expects($this->any())
83
            ->method('update')
84
            ->willReturn(null)
85
        ;
86
87
        $terminalManager
88
            ->expects($this->any())
89
            ->method('findTerminalByTitle')
90
            ->willReturn(null)
91
        ;
92
93
        $altapayClient = $this->createMock(Client::class);
94
        $altapayClient
95
            ->expects($this->any())
96
            ->method('getTerminals')
97
            ->willReturn($getTerminalsResponse)
98
        ;
99
100
        $terminalSynchronizer = new TerminalSynchronizer($terminalManager, $altapayClient);
101
        $terminalSynchronizer->syncAll();
102
103
        $this->assertTrue(true);
104
    }
105
}
106