forked from stomp-php/stomp-php
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ServerAliveObserverTest.php
101 lines (91 loc) · 3.33 KB
/
ServerAliveObserverTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
/*
* This file is part of the Stomp package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Stomp\Tests\Unit\Network\Observer;
use PHPUnit\Framework\TestCase;
use Stomp\Network\Observer\AbstractBeats;
use Stomp\Network\Observer\Exception\HeartbeatException;
use Stomp\Network\Observer\ServerAliveObserver;
use Stomp\Transport\Frame;
/**
* ServerAliveObserverTest
*
* @package Stomp\Tests\Unit\Network\Observer
* @author Jens Radtke <swefl.oss@fin-sn.de>
*/
class ServerAliveObserverTest extends TestCase
{
public function testHeartBeatFrameMapping()
{
$instance = new ServerAliveObserver();
$method = new \ReflectionMethod($instance, 'onHeartbeatFrame');
$method->setAccessible(true);
$method->invoke($instance, new Frame(AbstractBeats::FRAME_CLIENT_CONNECT), [10, 20]);
$clientProperty = new \ReflectionProperty($instance, 'intervalClient');
$clientProperty->setAccessible(true);
self::assertEquals(20, $clientProperty->getValue($instance));
$method->invoke($instance, new Frame(AbstractBeats::FRAME_SERVER_CONNECTED), [30, 40]);
$serverProperty = new \ReflectionProperty($instance, 'intervalServer');
$serverProperty->setAccessible(true);
self::assertEquals(30, $serverProperty->getValue($instance));
}
public function testCalculateInterval()
{
$instance = new ServerAliveObserver(5);
$method = new \ReflectionMethod($instance, 'calculateInterval');
$method->setAccessible(true);
self::assertEquals(20, $method->invoke($instance, 4));
}
public function testOnDelayThrowsException()
{
$this->expectException(HeartbeatException::class);
$this->expectExceptionMessage('The server failed to send expected heartbeats.');
$instance = new ServerAliveObserver();
$method = new \ReflectionMethod($instance, 'onDelay');
$method->setAccessible(true);
$method->invoke($instance);
}
/**
* Check that the given activity indicator raises a certain follow up task or not.
*
* @param string $activityMethod
* @param string|null $observerMethod
* @throws \ReflectionException
*
* @dataProvider observerMappingProvider
*/
public function testObserverMapping($activityMethod, $observerMethod = null)
{
$methods = ['rememberActivity', 'checkDelayed'];
$instance = $this->getMockBuilder(ServerAliveObserver::class)
->onlyMethods($methods)
->getMock();
foreach ($methods as $method) {
$instance->expects(($observerMethod === $method) ? $this->once() : $this->never())
->method($method);
}
$method = new \ReflectionMethod($instance, $activityMethod);
$method->setAccessible(true);
$method->invoke($instance);
}
public function observerMappingProvider()
{
return [
'Connection State Changes' => [
'onPotentialConnectionStateActivity',
'checkDelayed'
],
'Server Activity' => [
'onServerActivity',
'rememberActivity'
],
'Client Activity' => [
'onClientActivity'
]
];
}
}