RecordingDispatcherTest   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 2
c 1
b 0
f 1
lcom 1
cbo 4
dl 0
loc 39
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 7 1
A testDispatch() 0 12 1
1
<?php
2
3
namespace Werkspot\Bundle\StatsdBundle\Tests\Event;
4
5
use Mockery;
6
use Mockery\MockInterface;
7
use Symfony\Component\EventDispatcher\Event;
8
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
9
use Werkspot\Bundle\StatsdBundle\Client\StatsdClientInterface;
10
use Werkspot\Bundle\StatsdBundle\Event\RecordingDispatcher;
11
12
class RecordingDispatcherTest extends \PHPUnit_Framework_TestCase
13
{
14
    /**
15
     * @var EventDispatcherInterface|MockInterface
16
     */
17
    private $mockOriginalDispatcher;
18
19
    /**
20
     * @var StatsdClientInterface|MockInterface
21
     */
22
    private $mockStatsdClient;
23
24
    /**
25
     * @var RecordingDispatcher
26
     */
27
    private $dispatcher;
28
29
    protected function setUp()
30
    {
31
        $this->mockOriginalDispatcher = Mockery::mock(EventDispatcherInterface::class);
32
        $this->mockStatsdClient = Mockery::mock(StatsdClientInterface::class);
33
34
        $this->dispatcher = new RecordingDispatcher($this->mockOriginalDispatcher, $this->mockStatsdClient);
35
    }
36
37
    public function testDispatch()
38
    {
39
        $mockEventName = 'a.b-c.d-e';
40
        $mockEvent = Mockery::mock(Event::class);
41
42
        $this->mockOriginalDispatcher->shouldReceive('dispatch')->with($mockEventName, $mockEvent);
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, 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...
43
        $this->mockStatsdClient->shouldReceive('increment')->with('a-b-c-d-e');
44
45
        $this->dispatcher->dispatch($mockEventName, $mockEvent);
46
47
48
    }
49
50
}
51