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 © 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()) |
|
|
|
|
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
|
|
|
|
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:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: