Skip to content
This repository was archived by the owner on Aug 9, 2021. It is now read-only.
/ glpi-plugin Public archive
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 65d5761

Browse files
committedDec 19, 2018
test: add test for MQTT and FCM services
Signed-off-by: Domingo Oropeza <doropeza@teclib.com>
1 parent 8b54dc4 commit 65d5761

File tree

5 files changed

+292
-2
lines changed

5 files changed

+292
-2
lines changed
 

‎inc/fcm/fcmenvelope.class.php

+10-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,16 @@ final class FcmEnvelope implements BrokerEnvelopeItemInterface {
4646
* @param array $context
4747
*/
4848
public function __construct(array $context) {
49-
if (!isset($context['scope']) && !is_array($context['scope'])) {
50-
throw new \InvalidArgumentException(__('The scope argument is needed (push type and token)', 'flyvemdm'));
49+
if (!isset($context['scope']) || !is_array($context['scope'])) {
50+
throw new \InvalidArgumentException(__('The scope argument is needed (push type and token)',
51+
'flyvemdm'));
52+
}
53+
54+
foreach ($context['scope'] as $index => $device) {
55+
if (!isset($device['type']) || !isset($device['token'])) {
56+
throw new \InvalidArgumentException(__('The scope argument is needed (push type and token)',
57+
'flyvemdm'));
58+
}
5159
}
5260

5361
if (!isset($context['topic'])) {

‎tests/suite-unit/fcm/FcmEnvelope.php

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
/**
3+
* LICENSE
4+
*
5+
* Copyright © 2016-2018 Teclib'
6+
* Copyright © 2010-2018 by the FusionInventory Development Team.
7+
*
8+
* This file is part of Flyve MDM Plugin for GLPI.
9+
*
10+
* Flyve MDM Plugin for GLPI is a subproject of Flyve MDM. Flyve MDM is a mobile
11+
* device management software.
12+
*
13+
* Flyve MDM Plugin for GLPI is free software: you can redistribute it and/or
14+
* modify it under the terms of the GNU Affero General Public License as published
15+
* by the Free Software Foundation, either version 3 of the License, or
16+
* (at your option) any later version.
17+
* Flyve MDM Plugin for GLPI is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU Affero General Public License for more details.
21+
* You should have received a copy of the GNU Affero General Public License
22+
* along with Flyve MDM Plugin for GLPI. If not, see http://www.gnu.org/licenses/.
23+
* ------------------------------------------------------------------------------
24+
* @author Thierry Bugier
25+
* @copyright Copyright © 2018 Teclib
26+
* @license AGPLv3+ http://www.gnu.org/licenses/agpl.txt
27+
* @link https://github.com/flyve-mdm/glpi-plugin
28+
* @link https://flyve-mdm.com/
29+
* ------------------------------------------------------------------------------
30+
*/
31+
32+
namespace tests\units\GlpiPlugin\Flyvemdm\Fcm;
33+
34+
35+
use Flyvemdm\Tests\CommonTestCase;
36+
37+
class FcmEnvelope extends CommonTestCase {
38+
39+
private $scope = [['type' => 'fcm', 'token' => 'Sup3rT0k3n']];
40+
private $topic = ['lorem/ipsum/dolor'];
41+
42+
protected function providerContext() {
43+
return [
44+
'empty' => [
45+
'context' => [[]],
46+
'expected' => 'The scope argument is needed (push type and token)',
47+
],
48+
'invalid scope' => [
49+
'context' => ['lorem' => ['key' => '']],
50+
'expected' => 'The scope argument is needed (push type and token)',
51+
],
52+
'invalid type' => [
53+
'context' => ['scope' => [['key' => '']]],
54+
'expected' => 'The scope argument is needed (push type and token)',
55+
],
56+
'invalid token' => [
57+
'context' => ['scope' => [['type' => 'fcm']]],
58+
'expected' => 'The scope argument is needed (push type and token)',
59+
],
60+
'invalid topic' => [
61+
'context' => ['scope' => $this->scope],
62+
'expected' => 'A topic argument is needed',
63+
],
64+
];
65+
}
66+
/**
67+
* @tags testException
68+
* @dataProvider providerContext
69+
* @param array $context
70+
* @param string $expected
71+
*/
72+
public function testException($context, $expected) {
73+
$this->exception(function () use ($context) {
74+
$this->newTestedInstance($context);
75+
})->hasMessage($expected);
76+
}
77+
78+
/**
79+
* @tags testEnvelope
80+
*/
81+
public function testEnvelope() {
82+
$instance = $this->newTestedInstance(['scope' => $this->scope, 'topic' => $this->topic]);
83+
$this->array($instance->getContext('scope'))->child[0](function ($child) {
84+
$scope = $this->scope[0];
85+
$child->hasKeys(['type', 'token'])->values
86+
->string[0]->isEqualTo($scope['type'])
87+
->string[1]->isEqualTo($scope['token']);
88+
});
89+
$context = $instance->getContext('topic');
90+
$this->string($context[0])->isEqualTo('lorem-ipsum-dolor');
91+
}
92+
93+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
/**
3+
* LICENSE
4+
*
5+
* Copyright © 2016-2018 Teclib'
6+
* Copyright © 2010-2018 by the FusionInventory Development Team.
7+
*
8+
* This file is part of Flyve MDM Plugin for GLPI.
9+
*
10+
* Flyve MDM Plugin for GLPI is a subproject of Flyve MDM. Flyve MDM is a mobile
11+
* device management software.
12+
*
13+
* Flyve MDM Plugin for GLPI is free software: you can redistribute it and/or
14+
* modify it under the terms of the GNU Affero General Public License as published
15+
* by the Free Software Foundation, either version 3 of the License, or
16+
* (at your option) any later version.
17+
* Flyve MDM Plugin for GLPI is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU Affero General Public License for more details.
21+
* You should have received a copy of the GNU Affero General Public License
22+
* along with Flyve MDM Plugin for GLPI. If not, see http://www.gnu.org/licenses/.
23+
* ------------------------------------------------------------------------------
24+
* @author Thierry Bugier
25+
* @copyright Copyright © 2018 Teclib
26+
* @license AGPLv3+ http://www.gnu.org/licenses/agpl.txt
27+
* @link https://github.com/flyve-mdm/glpi-plugin
28+
* @link https://flyve-mdm.com/
29+
* ------------------------------------------------------------------------------
30+
*/
31+
32+
namespace tests\units\GlpiPlugin\Flyvemdm\Fcm;
33+
34+
35+
use Flyvemdm\Tests\CommonTestCase;
36+
use GlpiPlugin\Flyvemdm\Broker\BrokerMessage;
37+
use GlpiPlugin\Flyvemdm\Fcm\FcmEnvelope;
38+
use GlpiPlugin\Flyvemdm\Fcm\FcmSendMessageHandler as SendMessageHandler;
39+
use Sly\NotificationPusher\Adapter\Gcm;
40+
use Sly\NotificationPusher\PushManager;
41+
42+
class FcmSendMessageHandler extends CommonTestCase {
43+
44+
/**
45+
* @tags testSendMessageHandler
46+
*/
47+
public function testSendMessageHandler() {
48+
$pushManager = new PushManager();
49+
$adapter = new Gcm(['apiKey' => 'ServerApikey']);
50+
$toolbox = new \Toolbox();
51+
52+
$message = 'Hello world';
53+
$topic = 'lorem/ipsum/dolor';
54+
$scope = [['type' => 'fcm', 'token' => 'Sup3rT0k3n']];
55+
$envelope = new FcmEnvelope(['scope' => $scope, 'topic' => $topic]);
56+
$brokerMessage = new BrokerMessage($message);
57+
$mockedConnection = $this->newMockInstance('\GlpiPlugin\Flyvemdm\Fcm\FcmConnection', null,
58+
null, [$pushManager, $adapter, $toolbox]);
59+
$mockedConnection->getMockController()->push = function () {};
60+
$instance = new SendMessageHandler($envelope, $mockedConnection);
61+
$this->object($instance)->isCallable();
62+
$instance($brokerMessage);
63+
$this->mock($mockedConnection)->call('push')->once();
64+
}
65+
66+
}
+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
/**
3+
* LICENSE
4+
*
5+
* Copyright © 2016-2018 Teclib'
6+
* Copyright © 2010-2018 by the FusionInventory Development Team.
7+
*
8+
* This file is part of Flyve MDM Plugin for GLPI.
9+
*
10+
* Flyve MDM Plugin for GLPI is a subproject of Flyve MDM. Flyve MDM is a mobile
11+
* device management software.
12+
*
13+
* Flyve MDM Plugin for GLPI is free software: you can redistribute it and/or
14+
* modify it under the terms of the GNU Affero General Public License as published
15+
* by the Free Software Foundation, either version 3 of the License, or
16+
* (at your option) any later version.
17+
* Flyve MDM Plugin for GLPI is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU Affero General Public License for more details.
21+
* You should have received a copy of the GNU Affero General Public License
22+
* along with Flyve MDM Plugin for GLPI. If not, see http://www.gnu.org/licenses/.
23+
* ------------------------------------------------------------------------------
24+
* @author Domingo Oropeza <doropeza@teclib.com>
25+
* @copyright Copyright © 2018 Teclib
26+
* @license AGPLv3+ http://www.gnu.org/licenses/agpl.txt
27+
* @link https://github.com/flyve-mdm/glpi-plugin
28+
* @link https://flyve-mdm.com/
29+
* ------------------------------------------------------------------------------
30+
*/
31+
32+
namespace tests\units\GlpiPlugin\Flyvemdm\Mqtt;
33+
34+
35+
use Flyvemdm\Tests\CommonTestCase;
36+
37+
class MqttEnvelope extends CommonTestCase {
38+
39+
/**
40+
* @tags testEnvelope
41+
*/
42+
public function testEnvelope() {
43+
// try the exception
44+
$this->exception(function () {
45+
$this->newTestedInstance([]);
46+
})->hasMessage('A topic argument is needed');
47+
48+
$topic = 'lorem';
49+
$qos = 2;
50+
$retain = 1;
51+
52+
// Defaut values
53+
$instance = $this->newTestedInstance(['topic' => $topic]);
54+
$this->string($instance->getContext('topic'))->isEqualTo($topic);
55+
$this->integer($instance->getContext('qos'))->isEqualTo(0);
56+
$this->integer($instance->getContext('retain'))->isEqualTo(0);
57+
58+
// set context values
59+
$instance = $this->newTestedInstance(['topic' => $topic, 'qos' => $qos, 'retain' => $retain]);
60+
$this->integer($instance->getContext('qos'))->isEqualTo($qos);
61+
$this->integer($instance->getContext('retain'))->isEqualTo($retain);
62+
}
63+
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
/**
3+
* LICENSE
4+
*
5+
* Copyright © 2016-2018 Teclib'
6+
* Copyright © 2010-2018 by the FusionInventory Development Team.
7+
*
8+
* This file is part of Flyve MDM Plugin for GLPI.
9+
*
10+
* Flyve MDM Plugin for GLPI is a subproject of Flyve MDM. Flyve MDM is a mobile
11+
* device management software.
12+
*
13+
* Flyve MDM Plugin for GLPI is free software: you can redistribute it and/or
14+
* modify it under the terms of the GNU Affero General Public License as published
15+
* by the Free Software Foundation, either version 3 of the License, or
16+
* (at your option) any later version.
17+
* Flyve MDM Plugin for GLPI is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU Affero General Public License for more details.
21+
* You should have received a copy of the GNU Affero General Public License
22+
* along with Flyve MDM Plugin for GLPI. If not, see http://www.gnu.org/licenses/.
23+
* ------------------------------------------------------------------------------
24+
* @author Thierry Bugier
25+
* @copyright Copyright © 2018 Teclib
26+
* @license AGPLv3+ http://www.gnu.org/licenses/agpl.txt
27+
* @link https://github.com/flyve-mdm/glpi-plugin
28+
* @link https://flyve-mdm.com/
29+
* ------------------------------------------------------------------------------
30+
*/
31+
32+
namespace tests\units\GlpiPlugin\Flyvemdm\Mqtt;
33+
34+
35+
use Flyvemdm\Tests\CommonTestCase;
36+
use GlpiPlugin\Flyvemdm\Broker\BrokerMessage;
37+
use GlpiPlugin\Flyvemdm\Mqtt\MqttEnvelope;
38+
use GlpiPlugin\Flyvemdm\Mqtt\MqttSendMessageHandler as SendMessageHandler;
39+
40+
class MqttSendMessageHandler extends CommonTestCase {
41+
42+
/**
43+
* @tags testSendMessageHandler
44+
*/
45+
public function testSendMessageHandler() {
46+
$topic = 'lorem';
47+
$message = 'Hello world';
48+
$envelope = new MqttEnvelope(['topic' => $topic]);
49+
$brokerMessage = new BrokerMessage($message);
50+
$mockedConnection = $this->newMockInstance('\GlpiPlugin\Flyvemdm\Mqtt\MqttConnection');
51+
$mockedConnection->getMockController()->publish = function () {};
52+
$instance = new SendMessageHandler($mockedConnection, $envelope);
53+
$this->object($instance)->isCallable();
54+
$instance($brokerMessage);
55+
$this->mock($mockedConnection)->call('publish')
56+
->withIdenticalArguments($topic, $message, 0, 0)->once();
57+
}
58+
59+
}

0 commit comments

Comments
 (0)
This repository has been archived.