Issues (28)

tests/cli/Manager/InstanceStateManagerTest.php (3 issues)

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')
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\MockOb...ckBuilder::setMethods() has been deprecated: https://github.com/sebastianbergmann/phpunit/pull/3687 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

27
		$instanceStateManager = /** @scrutinizer ignore-deprecated */ $this->getMockBuilder('\Jalle19\StatusManager\Manager\InstanceStateManager')

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
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
The method expects() does not exist on Jalle19\StatusManager\Manager\InstanceStateManager. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

37
		$instanceStateManager->/** @scrutinizer ignore-call */ 
38
                         expects($this->once())

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
The method expects() does not exist on Symfony\Component\EventD...ventSubscriberInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

37
		$instanceStateManager->/** @scrutinizer ignore-call */ 
38
                         expects($this->once())

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

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->assertTrue($instanceStateManager->getInstanceState($this->getTestInstance())->isReachable());
80
81
		$this->eventDispatcher->dispatch(Events::INSTANCE_STATE_UNREACHABLE,
82
			new InstanceStateEvent($this->getTestInstance()));
83
84
		// Assert that the instance is now unreachable
85
		$this->assertEquals(InstanceState::UNREACHABLE,
86
			$instanceStateManager->getInstanceState($this->getTestInstance())->getReachability());
87
88
		$this->assertFalse($instanceStateManager->getInstanceState($this->getTestInstance())->isReachable());
89
90
		// The instance should be maybe reachable after a full retry cycle
91
		for ($i = 0; $i < InstanceStateManager::UNREACHABLE_CYCLES_UNTIL_RETRY; $i++)
92
		{
93
			$this->eventDispatcher->dispatch(Events::INSTANCE_STATE_MAYBE_REACHABLE,
94
				new InstanceStateEvent($this->getTestInstance()));
95
		}
96
97
		$this->assertEquals(InstanceState::MAYBE_REACHABLE,
98
			$instanceStateManager->getInstanceState($this->getTestInstance())->getReachability());
99
100
		$this->assertTrue($instanceStateManager->getInstanceState($this->getTestInstance())->isReachable());
101
102
		// And now if we mark it reachable it should be that
103
		$this->eventDispatcher->dispatch(Events::INSTANCE_STATE_REACHABLE,
104
			new InstanceStateEvent($this->getTestInstance()));
105
106
		$this->assertEquals(InstanceState::REACHABLE,
107
			$instanceStateManager->getInstanceState($this->getTestInstance())->getReachability());
108
109
		$this->assertTrue($instanceStateManager->getInstanceState($this->getTestInstance())->isReachable());
110
	}
111
112
}
113