TransactionLoggerTest::testTransactionLog()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 28
rs 8.8571
cc 1
eloc 17
nc 1
nop 0
1
<?php
2
3
namespace Loevgaard\DandomainAltapayBundle\Tests\Http;
4
5
use Loevgaard\DandomainAltapayBundle\Entity\HttpTransaction;
6
use Loevgaard\DandomainAltapayBundle\Entity\HttpTransactionRepository;
7
use Loevgaard\DandomainAltapayBundle\Http\TransactionLogger;
8
use PHPUnit\Framework\TestCase;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpFoundation\Response;
11
12
final class TransactionLoggerTest extends TestCase
13
{
14
    public function testTransactionLog()
15
    {
16
        $request = new Request();
17
        $response = new Response();
18
19
        /** @var HttpTransactionRepository|\PHPUnit_Framework_MockObject_MockObject $httpTransactionRepository */
20
        $httpTransactionRepository = $this->getMockBuilder(HttpTransactionRepository::class)
21
            ->disableOriginalConstructor()
22
            ->getMock()
23
        ;
24
25
        $transactionCounter = 0;
26
27
        $httpTransactionRepository
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Loevgaard\DandomainAltap...tpTransactionRepository.

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...
28
            ->expects($this->any())
29
            ->method('save')
30
            ->willReturnCallback(function () use (&$transactionCounter) {
31
                ++$transactionCounter;
32
            })
33
        ;
34
35
        $transactionLogger = new TransactionLogger($httpTransactionRepository);
0 ignored issues
show
Bug introduced by
It seems like $httpTransactionRepository defined by $this->getMockBuilder(\L...onstructor()->getMock() on line 20 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Loevgaard\DandomainAltap...onLogger::__construct() does only seem to accept object<Loevgaard\Dandoma...pTransactionRepository>, 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...
36
        $transactionLogger->setRequest($request);
37
        $transactionLogger->setResponse($request, $response);
38
        $transactionLogger->flush();
39
40
        $this->assertSame(1, $transactionCounter);
41
    }
42
43
    /**
44
     * @return HttpTransaction|\PHPUnit_Framework_MockObject_MockObject
45
     */
46
    public function createHttpTransaction()
47
    {
48
        return new HttpTransaction();
49
    }
50
}
51