Completed
Push — master ( f019ba...e16295 )
by Joachim
03:16
created

WebhookExchangeRepositoryTest::testReturnObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
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 testReturnNull()
12
    {
13
        $webhookExchangeRepository = $this->getWebhookExchangeRepository();
14
15
        $webhookExchangeRepository
16
            ->method('findOneBy')
17
            ->willReturn(null);
18
19
        $this->assertSame(null, $webhookExchangeRepository->findByUrl('https://www.example.com'));
0 ignored issues
show
Bug introduced by
The method findByUrl 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...
20
    }
21
22
    public function testReturnObject()
23
    {
24
        $webhookExchangeRepository = $this->getWebhookExchangeRepository();
25
26
        $url = 'https://www.example.com';
27
        $obj = new WebhookExchange($url);
28
        $webhookExchangeRepository
29
            ->method('findOneBy')
30
            ->willReturn($obj);
31
32
        $this->assertSame($obj, $webhookExchangeRepository->findByUrl($url));
0 ignored issues
show
Bug introduced by
The method findByUrl 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...
33
    }
34
35
    /**
36
     * @return WebhookExchangeRepository|\PHPUnit_Framework_MockObject_MockObject
37
     */
38
    private function getWebhookExchangeRepository()
39
    {
40
        /** @var WebhookExchangeRepository|\PHPUnit_Framework_MockObject_MockObject $webhookExchangeRepository */
41
        $webhookExchangeRepository = $this->getMockBuilder(WebhookExchangeRepository::class)
42
            ->disableOriginalConstructor()
43
            ->setMethods(['findOneBy'])
44
            ->getMock();
45
46
        return $webhookExchangeRepository;
47
    }
48
}
49