Issues (28)

tests/cli/Manager/ClientManagerTest.php (4 issues)

1
<?php
2
3
namespace Jalle19\StatusManager\Test\Manager;
4
5
use Jalle19\StatusManager\Event\Events;
6
use Jalle19\StatusManager\Event\InstanceStatusUpdatesEvent;
7
use Jalle19\StatusManager\Instance\InstanceStatusCollection;
8
use Jalle19\StatusManager\Message\Request\AuthenticationRequest;
9
use Jalle19\StatusManager\Message\Response\AuthenticationResponse;
10
use Ratchet\ConnectionInterface;
11
12
/**
13
 * Class ClientManagerTest
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 ClientManagerTest extends AbstractManagerTest
19
{
20
21
	/**
22
	 * @var DummyClientManager
23
	 */
24
	private $_manager;
25
26
	/**
27
	 * @var \PHPUnit_Framework_MockObject_MockObject|ConnectionInterface
28
	 */
29
	private $_clientMock;
30
31
	/**
32
	 * @var \PHPUnit_Framework_MockObject_MockObject|ConnectionInterface
33
	 */
34
	private $_anotherClientMock;
35
36
37
	/**
38
	 * @inheritdoc
39
	 */
40
	protected function setUp(): void
41
	{
42
		$this->_manager = new DummyClientManager($this->configuration, $this->logger, $this->eventDispatcher);
43
44
		// Create two clients and connect them
45
		$this->_clientMock = $this->getMockBuilder(ConnectionInterface::class)
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockBuilder(Ra...', 'close'))->getMock() of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type PHPUnit_Framework_MockOb...het\ConnectionInterface of property $_clientMock.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
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

45
		$this->_clientMock = /** @scrutinizer ignore-deprecated */ $this->getMockBuilder(ConnectionInterface::class)

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...
46
		                          ->setMethods(['send', 'close'])
47
		                          ->getMock();
48
49
		$this->_anotherClientMock = clone $this->_clientMock;
0 ignored issues
show
Documentation Bug introduced by
It seems like clone $this->_clientMock of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type PHPUnit_Framework_MockOb...het\ConnectionInterface of property $_anotherClientMock.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
50
51
		// Connect the clients
52
		$this->_manager->onOpen($this->_clientMock);
53
		$this->_manager->onOpen($this->_anotherClientMock);
54
55
		// Wire the event and message handling
56
		$this->eventDispatcher->addSubscriber($this->_manager);
57
		$this->_manager->registerMessageHandler($this->_manager);
58
	}
59
60
61
	/**
62
	 * Tests that clients are properly registered as connected
63
	 */
64
	public function testConnectedClients()
65
	{
66
		$this->assertEquals(2, $this->_manager->getConnectedClients()->count());
67
	}
68
69
70
	/**
71
	 * Tests that clients can only be authenticated with the correct access token
72
	 */
73
	public function testAuthentication()
74
	{
75
		// Authenticate the first client, check for success
76
		$response = $this->_manager->handleMessage(
77
			new AuthenticationRequest($this->configuration->getAccessToken()),
78
			$this->_clientMock);
79
80
		/* @var AuthenticationResponse $response */
81
		$this->assertEquals(AuthenticationResponse::STATUS_SUCCESS, $response->getStatus());
82
83
		// Authenticate the second client, check for failure
84
		$response = $this->_manager->handleMessage(
85
			new AuthenticationRequest('very invalid token'),
86
			$this->_anotherClientMock);
87
88
		/* @var AuthenticationResponse $response */
89
		$this->assertEquals(AuthenticationResponse::STATUS_FAILURE, $response->getStatus());
90
91
		$this->assertEquals(1, $this->_manager->getAuthenticatedClients()->count());
92
	}
93
94
95
	/**
96
	 * Tests that instance status updates are broadcast to all authenticated clients and not to those that are just
97
	 * connected but not authenticated
98
	 */
99
	public function testHandleStatusUpdates()
100
	{
101
		$this->_manager->handleMessage(
102
			new AuthenticationRequest($this->configuration->getAccessToken()),
103
			$this->_clientMock);
104
105
		$this->_clientMock->expects($this->once())->method('send');
0 ignored issues
show
The method expects() does not exist on Ratchet\ConnectionInterface. ( Ignorable by Annotation )

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

105
		$this->_clientMock->/** @scrutinizer ignore-call */ 
106
                      expects($this->once())->method('send');

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...
106
		$this->_anotherClientMock->expects($this->never())->method('send');
107
108
		$this->eventDispatcher->dispatch(Events::INSTANCE_STATUS_UPDATES, new InstanceStatusUpdatesEvent(
109
			new InstanceStatusCollection()
110
		));
111
	}
112
113
114
	/**
115
	 * Tests that requests from clients are sent back as responses
116
	 */
117
	public function testMessageHandling()
118
	{
119
		$authenticationRequest = new AuthenticationRequest($this->configuration->getAccessToken());
120
121
		// Authenticate the client
122
		$this->_manager->handleMessage($authenticationRequest, $this->_clientMock);
123
124
		$expectedResponse = new AuthenticationResponse(AuthenticationResponse::STATUS_SUCCESS);
125
		$this->_clientMock->expects($this->once())->method('send')->with(json_encode($expectedResponse));
126
127
		$this->_manager->onMessage($this->_clientMock, json_encode($authenticationRequest));
128
	}
129
130
}
131