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

testCreateCallbackFromRequest()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
c 0
b 0
f 0
rs 8.8571
cc 1
eloc 28
nc 1
nop 0
1
<?php
2
3
namespace Loevgaard\DandomainAltapayBundle\Tests\Manager;
4
5
use Doctrine\Common\Persistence\ManagerRegistry;
6
use Doctrine\Common\Persistence\ObjectManager;
7
use Doctrine\Common\Persistence\ObjectRepository;
8
use Loevgaard\DandomainAltapayBundle\Entity\CallbackInterface;
9
use Loevgaard\DandomainAltapayBundle\Manager\CallbackManager;
10
use PHPUnit\Framework\TestCase;
11
use Symfony\Component\HttpFoundation\Request;
12
13
class CallbackManagerTest extends TestCase
14
{
15
    /**
16
     * @var \PHPUnit_Framework_MockObject_MockObject|CallbackManager
17
     */
18
    protected $manager;
19
20
    /**
21
     * @var \PHPUnit_Framework_MockObject_MockObject|ObjectRepository
22
     */
23
    protected $objectRepository;
24
25 View Code Duplication
    protected function setUp()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
26
    {
27
        $objectManager = $this->getMockBuilder(ObjectManager::class)
28
            ->disableOriginalConstructor()
29
            ->getMock();
30
31
        /** @var ManagerRegistry|\PHPUnit_Framework_MockObject_MockObject $managerRegistry */
32
        $managerRegistry = $this->getMockBuilder(ManagerRegistry::class)
33
            ->disableOriginalConstructor()
34
            ->getMock();
35
36
        $managerRegistry
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\Common\Persistence\ManagerRegistry.

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...
37
            ->expects($this->any())
38
            ->method('getManagerForClass')
39
            ->willReturn($objectManager);
40
41
        $this->objectRepository = $this->getMockBuilder(ObjectRepository::class)
42
            ->disableOriginalConstructor()
43
            ->getMock();
44
45
        $objectManager
46
            ->expects($this->any())
47
            ->method('getRepository')
48
            ->willReturn($this->objectRepository);
49
50
        $this->manager = new CallbackManager($managerRegistry, 'Loevgaard\DandomainAltapayBundle\Tests\TestCallback');
51
    }
52
53
    public function testCreateCallbackFromRequest()
54
    {
55
        $request = new Request([], [
56
            'shop_orderid' => 123,
57
            'amount' => 100,
58
            'currency' => 208,
59
            'language' => 'language',
60
            'transaction_info' => ['transaction info'],
61
            'status' => 'status',
62
            'error_message' => 'errorMessage',
63
            'merchant_error_message' => 'merchantErrorMessage',
64
            'cardholder_message_must_be_shown' => 'cardholderMessageMustBeShown',
65
            'transaction_id' => 'transactionId',
66
            'type' => 'type',
67
            'payment_status' => 'paymentStatus',
68
            'masked_credit_card' => 'maskedCreditCard',
69
            'blacklist_token' => 'blacklistToken',
70
            'credit_card_token' => 'creditCardToken',
71
            'nature' => 'nature',
72
            'require_capture' => 'requireCapture',
73
            'xml' => 'xml',
74
            'checksum' => 'checksum',
75
            'fraud_risk_score' => 9.5,
76
            'fraud_explanation' => 'fraudExplanation',
77
            'fraud_recommendation' => 'fraudRecommendation',
78
            'avs_code' => 'avsCode',
79
            'avs_text' => 'avsText',
80
        ]);
81
        $callback = $this->manager->createCallbackFromRequest($request);
0 ignored issues
show
Bug introduced by
The method createCallbackFromRequest does only exist in Loevgaard\DandomainAltap...Manager\CallbackManager, but not in PHPUnit_Framework_MockObject_MockObject.

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...
82
83
        $this->assertInstanceOf(CallbackInterface::class, $callback);
84
    }
85
}
86