Completed
Push — master ( b123ad...2604a0 )
by Joachim
02:07
created

WebhookExchangeRepositoryTest::testReturnNull()   A

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\WebhookExchange;
6
use Loevgaard\DandomainAltapayBundle\Entity\WebhookExchangeRepository;
7
use PHPUnit\Framework\TestCase;
8
9
class WebhookExchangeRepositoryTest extends TestCase
10
{
11
    public function testReturnObject()
12
    {
13
        $webhookExchangeRepository = $this->getWebhookExchangeRepository();
14
15
        $url = 'https://www.example.com';
16
        $obj = new WebhookExchange($url);
17
        $webhookExchangeRepository
18
            ->method('findOneBy')
19
            ->willReturn($obj);
20
21
        $this->assertSame($obj, $webhookExchangeRepository->findByUrlOrCreate($url));
0 ignored issues
show
Bug introduced by
The method findByUrlOrCreate does only exist in Loevgaard\DandomainAltap...bhookExchangeRepository, 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...
22
    }
23
24
    /**
25
     * @return WebhookExchangeRepository|\PHPUnit_Framework_MockObject_MockObject
26
     */
27
    private function getWebhookExchangeRepository()
28
    {
29
        /** @var WebhookExchangeRepository|\PHPUnit_Framework_MockObject_MockObject $webhookExchangeRepository */
30
        $webhookExchangeRepository = $this->getMockBuilder(WebhookExchangeRepository::class)
31
            ->disableOriginalConstructor()
32
            ->setMethods(['findOneBy'])
33
            ->getMock();
34
35
        return $webhookExchangeRepository;
36
    }
37
}
38