PaymentRepositoryTest::testReturnNull()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 10
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
namespace Loevgaard\DandomainAltapayBundle\Tests\Entity;
4
5
use Loevgaard\DandomainAltapayBundle\Entity\Payment;
6
use Loevgaard\DandomainAltapayBundle\Entity\PaymentRepository;
7
use PHPUnit\Framework\TestCase;
8
9
class PaymentRepositoryTest extends TestCase
10
{
11
    public function testReturnNull()
12
    {
13
        $paymentRepository = $this->getPaymentRepository();
14
15
        $paymentRepository
16
            ->method('findOneBy')
17
            ->willReturn(null);
18
19
        $this->assertSame(null, $paymentRepository->findByOrderIdOrAltapayId(1));
0 ignored issues
show
Bug introduced by
The method findByOrderIdOrAltapayId does only exist in Loevgaard\DandomainAltap...ntity\PaymentRepository, 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...
20
    }
21
22
    public function testFindBySetting()
23
    {
24
        $paymentRepository = $this->getPaymentRepository();
25
26
        $obj = new Payment();
27
        $paymentRepository
28
            ->method('findOneBy')
29
            ->willReturn($obj);
30
31
        $this->assertSame($obj, $paymentRepository->findByOrderIdOrAltapayId(1));
0 ignored issues
show
Bug introduced by
The method findByOrderIdOrAltapayId does only exist in Loevgaard\DandomainAltap...ntity\PaymentRepository, 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...
32
    }
33
34
    /**
35
     * @return PaymentRepository|\PHPUnit_Framework_MockObject_MockObject
36
     */
37
    private function getPaymentRepository()
38
    {
39
        /** @var PaymentRepository|\PHPUnit_Framework_MockObject_MockObject $paymentRepository */
40
        $paymentRepository = $this->getMockBuilder(PaymentRepository::class)
41
            ->disableOriginalConstructor()
42
            ->setMethods(['findOneBy'])
43
            ->getMock();
44
45
        return $paymentRepository;
46
    }
47
}
48