Completed
Push — master ( 6895de...8b3e50 )
by Artem
02:07
created

SymfonyEventDispatcherTest::testDispatchMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 23
rs 9.0856
cc 1
eloc 15
nc 1
nop 0
1
<?php
2
/*
3
 * This file is part of the FirebaseCloudMessagingBundle
4
 *
5
 * (c) Artem Genvald <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Fresh\FirebaseCloudMessagingBundle\Tests\Event;
12
13
use Fresh\FirebaseCloudMessaging\Event\FirebaseEvents;
14
use Fresh\FirebaseCloudMessaging\Event\MulticastMessageResponseEvent;
15
use Fresh\FirebaseCloudMessaging\Response\MessageResult\Collection\CanonicalTokenMessageResultCollection;
16
use Fresh\FirebaseCloudMessaging\Response\MessageResult\Collection\FailedMessageResultCollection;
17
use Fresh\FirebaseCloudMessaging\Response\MessageResult\Collection\SuccessfulMessageResultCollection;
18
use Fresh\FirebaseCloudMessagingBundle\Event\SymfonyEventDispatcher;
19
use Symfony\Component\EventDispatcher\EventDispatcherInterface as SymfonyEventDispatcherInterface;
20
21
/**
22
 * SymfonyEventDispatcherTest.
23
 *
24
 * @author Artem Genvald <[email protected]>
25
 */
26
class SymfonyEventDispatcherTest extends \PHPUnit_Framework_TestCase
27
{
28
    public function testDispatchMethod()
29
    {
30
        /** @var SymfonyEventDispatcherInterface|\PHPUnit_Framework_MockObject_MockObject $eventDispatcher */
31
        $eventDispatcher = $this->getMockBuilder(SymfonyEventDispatcherInterface::class)
32
                                ->disableOriginalConstructor()
33
                                ->getMock();
34
35
        $eventDispatcher->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Symfony\Component\EventD...ventDispatcherInterface.

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...
36
                        ->method('dispatch');
37
38
        $eventData = new MulticastMessageResponseEvent(
39
            1234567890,
40
            new SuccessfulMessageResultCollection(),
41
            new FailedMessageResultCollection(),
42
            new CanonicalTokenMessageResultCollection()
43
        );
44
45
        $symfonyEventDispatcher = new SymfonyEventDispatcher($eventDispatcher);
46
        $symfonyEventDispatcher->dispatch(
47
            FirebaseEvents::MULTICAST_MESSAGE_RESPONSE_EVENT,
48
            $eventData
49
        );
50
    }
51
}
52