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

InstanceStateManagerTest::testEventHandling()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 37
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 37
rs 8.8571
cc 1
eloc 26
nc 1
nop 0
1
<?php
2
3
namespace Jalle19\StatusManager\Test\Manager;
4
5
use Jalle19\StatusManager\Event\Events;
6
use Jalle19\StatusManager\Event\InstanceStateEvent;
7
use Jalle19\StatusManager\Event\InstanceStatusCollectionRequestEvent;
8
use Jalle19\StatusManager\Instance\InstanceState;
9
use Jalle19\StatusManager\Manager\InstanceStateManager;
10
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
11
12
/**
13
 * Class InstanceStateManagerTest
14
 * @package   Jalle19\StatusManager\Test\Manager
15
 * @copyright Copyright &copy; Sam Stenvall 2016-
16
 * @license   https://www.gnu.org/licenses/gpl.html The GNU General Public License v2.0
17
 */
18
class InstanceStateManagerTest extends AbstractManagerTest
19
{
20
21
	/**
22
	 * Mocks the instance state manager to test that events are actually handled
23
	 */
24
	public function testEventHandling()
25
	{
26
		/* @var EventSubscriberInterface|\PHPUnit_Framework_MockObject_MockObject|InstanceStateManager $instanceStateManager */
27
		$instanceStateManager = $this->getMockBuilder('\Jalle19\StatusManager\Manager\InstanceStateManager')
28
		                             ->setConstructorArgs([$this->configuration, $this->logger, $this->eventDispatcher])
29
		                             ->setMethods([
30
			                             'onInstanceStatusCollectionRequest',
31
			                             'onInstanceReachable',
32
			                             'onInstanceUnreachable',
33
			                             'onInstanceMaybeReachable',
34
		                             ])
35
		                             ->getMock();
36
37
		$instanceStateManager->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...
38
		                     ->method('onInstanceStatusCollectionRequest');
39
		$instanceStateManager->expects($this->once())
40
		                     ->method('onInstanceReachable');
41
		$instanceStateManager->expects($this->once())
42
		                     ->method('onInstanceUnreachable');
43
		$instanceStateManager->expects($this->once())
44
		                     ->method('onInstanceMaybeReachable');
45
46
		// Start triggering events and assert that the instance state is as expected
47
		$this->eventDispatcher->addSubscriber($instanceStateManager);
48
49
		$this->eventDispatcher->dispatch(Events::INSTANCE_STATUS_COLLECTION_REQUEST,
50
			new InstanceStatusCollectionRequestEvent());
51
52
		$this->eventDispatcher->dispatch(Events::INSTANCE_STATE_REACHABLE,
53
			new InstanceStateEvent($this->getTestInstance()));
54
55
		$this->eventDispatcher->dispatch(Events::INSTANCE_STATE_UNREACHABLE,
56
			new InstanceStateEvent($this->getTestInstance()));
57
58
		$this->eventDispatcher->dispatch(Events::INSTANCE_STATE_MAYBE_REACHABLE,
59
			new InstanceStateEvent($this->getTestInstance()));
60
	}
61
62
63
	/**
64
	 * Tests that instance state events are reflected properly in the actual instance stateF
65
	 */
66
	public function testInstanceStateHandling()
67
	{
68
		$instanceStateManager = new InstanceStateManager($this->configuration, $this->logger, $this->eventDispatcher);
69
		$this->eventDispatcher->addSubscriber($instanceStateManager);
70
71
		// Assert that triggered events actually trigger a change in state
72
		$this->eventDispatcher->dispatch(Events::INSTANCE_STATE_REACHABLE,
73
			new InstanceStateEvent($this->getTestInstance()));
74
75
		// Assert that the instance is now reachable
76
		$this->assertEquals(InstanceState::REACHABLE,
77
			$instanceStateManager->getInstanceState($this->getTestInstance())->getReachability());
78
79
		$this->eventDispatcher->dispatch(Events::INSTANCE_STATE_UNREACHABLE,
80
			new InstanceStateEvent($this->getTestInstance()));
81
82
		// Assert that the instance is now unreachable
83
		$this->assertEquals(InstanceState::UNREACHABLE,
84
			$instanceStateManager->getInstanceState($this->getTestInstance())->getReachability());
85
86
		// The instance should be maybe reachable after a full retry cycle
87
		for ($i = 0; $i < InstanceStateManager::UNREACHABLE_CYCLES_UNTIL_RETRY; $i++)
88
		{
89
			$this->eventDispatcher->dispatch(Events::INSTANCE_STATE_MAYBE_REACHABLE,
90
				new InstanceStateEvent($this->getTestInstance()));
91
		}
92
93
		$this->assertEquals(InstanceState::MAYBE_REACHABLE,
94
			$instanceStateManager->getInstanceState($this->getTestInstance())->getReachability());
95
96
		// And now if we mark it reachable it should be that
97
		$this->eventDispatcher->dispatch(Events::INSTANCE_STATE_REACHABLE,
98
			new InstanceStateEvent($this->getTestInstance()));
99
100
		$this->assertEquals(InstanceState::REACHABLE,
101
			$instanceStateManager->getInstanceState($this->getTestInstance())->getReachability());
102
	}
103
104
}
105