Completed
Push — master ( 2abbc3...c001e8 )
by Sam
04:41
created

StatusManagerTest   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 1
c 1
b 0
f 1
lcom 1
cbo 3
dl 0
loc 25
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A testEventHandling() 0 17 1
1
<?php
2
3
namespace Jalle19\StatusManager\Test\Manager;
4
5
use Jalle19\StatusManager\Event\Events;
6
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
7
8
/**
9
 * Class StatusManagerTest
10
 * @package   Jalle19\StatusManager\Test\Manager
11
 * @copyright Copyright &copy; Sam Stenvall 2016-
12
 * @license   https://www.gnu.org/licenses/gpl.html The GNU General Public License v2.0
13
 */
14
class StatusManagerTest extends AbstractManagerTest
15
{
16
17
	/**
18
	 *
19
	 */
20
	public function testEventHandling()
21
	{
22
		/* @var EventSubscriberInterface|\PHPUnit_Framework_MockObject_MockObject $statusManager */
23
		$statusManager = $this->getMockBuilder('\Jalle19\StatusManager\Manager\StatusManager')
24
		                      ->setConstructorArgs([$this->configuration, $this->logger, $this->eventDispatcher])
25
		                      ->setMethods(['onMainLoopStarted', 'onMainLoopTick'])
26
		                      ->getMock();
27
28
		$statusManager->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...ventSubscriberInterface.

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...
29
		              ->method('onMainLoopStarted');
30
		$statusManager->expects($this->once())
31
		              ->method('onMainLoopTick');
32
33
		$this->eventDispatcher->addSubscriber($statusManager);
34
		$this->eventDispatcher->dispatch(Events::MAIN_LOOP_STARTING);
35
		$this->eventDispatcher->dispatch(Events::MAIN_LOOP_TICK);
36
	}
37
38
}
39