Completed
Push — master ( 0ed61c...03ecff )
by Joachim
15:52
created

TransactionLoggerTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
lcom 1
cbo 5
dl 0
loc 46
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B testTransactionLog() 0 35 1
A createHttpTransaction() 0 4 1
1
<?php
2
3
namespace Loevgaard\DandomainAltapayBundle\Tests\Http;
4
5
use Loevgaard\DandomainAltapayBundle\Entity\HttpTransaction;
6
use Loevgaard\DandomainAltapayBundle\Http\TransactionLogger;
7
use Loevgaard\DandomainAltapayBundle\Manager\HttpTransactionManager;
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 HttpTransactionManager|\PHPUnit_Framework_MockObject_MockObject $httpTransactionManager */
20
        $httpTransactionManager = $this->getMockBuilder(HttpTransactionManager::class)
21
            ->disableOriginalConstructor()
22
            ->getMock()
23
        ;
24
25
        $httpTransactionManager
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Loevgaard\DandomainAltap...\HttpTransactionManager.

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...
26
            ->expects($this->any())
27
            ->method('create')
28
            ->willReturnCallback([$this, 'createHttpTransaction'])
29
        ;
30
31
        $transactionCounter = 0;
32
33
        $httpTransactionManager
34
            ->expects($this->any())
35
            ->method('update')
36
            ->willReturnCallback(function ($entity) use ($request, $response, &$transactionCounter) {
0 ignored issues
show
Unused Code introduced by
The parameter $entity is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
37
                /* @var HttpTransaction $entity */
38
                ++$transactionCounter;
39
            })
40
        ;
41
42
        $transactionLogger = new TransactionLogger($httpTransactionManager);
43
        $transactionLogger->setRequest($request);
44
        $transactionLogger->setResponse($request, $response);
45
        $transactionLogger->flush();
46
47
        $this->assertSame(1, $transactionCounter);
48
    }
49
50
    /**
51
     * @return HttpTransaction|\PHPUnit_Framework_MockObject_MockObject
52
     */
53
    public function createHttpTransaction()
54
    {
55
        return $this->getMockForAbstractClass(HttpTransaction::class);
56
    }
57
}
58