TerminalSynchronizerTest   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 1
lcom 1
cbo 6
dl 0
loc 85
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B testSyncAll() 0 82 1
1
<?php
2
3
namespace Loevgaard\DandomainAltapayBundle\Tests\Synchronizer;
4
5
use Loevgaard\AltaPay\Client\Client;
6
use Loevgaard\AltaPay\Response\GetTerminals;
7
use Loevgaard\DandomainAltapayBundle\Entity\TerminalRepository;
8
use Loevgaard\DandomainAltapayBundle\Synchronizer\TerminalSynchronizer;
9
use PHPUnit\Framework\TestCase;
10
use Psr\Http\Message\ResponseInterface;
11
12
class TerminalSynchronizerTest extends TestCase
13
{
14
    public function testSyncAll()
15
    {
16
        $xml = <<<XML
17
<?xml version="1.0"?>
18
<APIResponse version="20170228">
19
  <Header>
20
    <Date>2016-01-07T23:43:20+01:00</Date>
21
    <Path>API/getTerminals</Path>
22
    <ErrorCode>0</ErrorCode>
23
    <ErrorMessage></ErrorMessage>
24
  </Header>
25
  <Body>
26
    <Result>Success</Result>
27
    <Terminals>
28
      <Terminal>
29
        <Title>AltaPay Multi-Nature Terminal</Title>
30
        <Country>DK</Country>
31
        <Natures>
32
          <Nature>CreditCard</Nature>
33
          <Nature>EPayment</Nature>
34
          <Nature>IdealPayment</Nature>
35
          <Nature>Invoice</Nature>
36
        </Natures>
37
        <Currencies>
38
          <Currency>DKK</Currency>
39
          <Currency>EUR</Currency>
40
        </Currencies>
41
      </Terminal>
42
      <Terminal>
43
        <Title>AltaPay BankPayment Terminal</Title>
44
        <Country></Country>
45
        <Natures>
46
          <Nature>BankPayment</Nature>
47
        </Natures>
48
        <Currencies>
49
          <Currency>EUR</Currency>
50
        </Currencies>
51
      </Terminal>
52
    </Terminals>
53
  </Body>
54
</APIResponse>
55
XML;
56
57
        $psrResponse = $this->createMock(ResponseInterface::class);
58
        $psrResponse
59
            ->expects($this->any())
60
            ->method('getBody')
61
            ->willReturn($xml)
62
            ;
63
        $getTerminalsResponse = new GetTerminals($psrResponse);
0 ignored issues
show
Documentation introduced by
$psrResponse is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Psr\Http\Message\ResponseInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
64
65
        /** @var TerminalRepository|\PHPUnit_Framework_MockObject_MockObject $terminalRepository */
66
        $terminalRepository = $this->getMockBuilder(TerminalRepository::class)
67
            ->disableOriginalConstructor()
68
            ->setMethods(['save', 'findTerminalByTitle'])
69
            ->getMock()
70
        ;
71
72
        $terminalRepository
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Loevgaard\DandomainAltap...tity\TerminalRepository.

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...
73
            ->expects($this->any())
74
            ->method('save')
75
            ->willReturn(null)
76
        ;
77
78
        $terminalRepository
79
            ->expects($this->any())
80
            ->method('findTerminalByTitle')
81
            ->willReturn(null)
82
        ;
83
84
        $altapayClient = $this->createMock(Client::class);
85
        $altapayClient
86
            ->expects($this->any())
87
            ->method('getTerminals')
88
            ->willReturn($getTerminalsResponse)
89
        ;
90
91
        $terminalSynchronizer = new TerminalSynchronizer($terminalRepository, $altapayClient);
0 ignored issues
show
Bug introduced by
It seems like $terminalRepository defined by $this->getMockBuilder(\L...alByTitle'))->getMock() on line 66 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Loevgaard\DandomainAltap...hronizer::__construct() does only seem to accept object<Loevgaard\Dandoma...ity\TerminalRepository>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Documentation introduced by
$altapayClient is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Loevgaard\AltaPay\Client\Client>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
92
        $terminalSynchronizer->syncAll();
93
94
        $this->assertTrue(true);
95
    }
96
}
97