Skip to content
This repository has been archived by the owner on Aug 9, 2021. It is now read-only.

Commit

Permalink
test: add test for MQTT and FCM services
Browse files Browse the repository at this point in the history
Signed-off-by: Domingo Oropeza <[email protected]>
  • Loading branch information
DIOHz0r committed Dec 21, 2018
1 parent c856c91 commit 5f688c2
Show file tree
Hide file tree
Showing 5 changed files with 292 additions and 2 deletions.
12 changes: 10 additions & 2 deletions inc/fcm/fcmenvelope.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,16 @@ final class FcmEnvelope implements BrokerEnvelopeItemInterface {
* @param array $context
*/
public function __construct(array $context) {
if (!isset($context['scope']) && !is_array($context['scope'])) {
throw new \InvalidArgumentException(__('The scope argument is needed (push type and token)', 'flyvemdm'));
if (!isset($context['scope']) || !is_array($context['scope'])) {
throw new \InvalidArgumentException(__('The scope argument is needed (push type and token)',
'flyvemdm'));
}

foreach ($context['scope'] as $index => $device) {
if (!isset($device['type']) || !isset($device['token'])) {
throw new \InvalidArgumentException(__('The scope argument is needed (push type and token)',
'flyvemdm'));
}
}

if (!isset($context['topic'])) {
Expand Down
93 changes: 93 additions & 0 deletions tests/suite-unit/fcm/FcmEnvelope.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php
/**
* LICENSE
*
* Copyright © 2016-2018 Teclib'
* Copyright © 2010-2018 by the FusionInventory Development Team.
*
* This file is part of Flyve MDM Plugin for GLPI.
*
* Flyve MDM Plugin for GLPI is a subproject of Flyve MDM. Flyve MDM is a mobile
* device management software.
*
* Flyve MDM Plugin for GLPI is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* Flyve MDM Plugin for GLPI is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
* You should have received a copy of the GNU Affero General Public License
* along with Flyve MDM Plugin for GLPI. If not, see http://www.gnu.org/licenses/.
* ------------------------------------------------------------------------------
* @author Domingo Oropeza <[email protected]>
* @copyright Copyright © 2018 Teclib
* @license http://www.gnu.org/licenses/agpl.txt AGPLv3+
* @link https://github.com/flyve-mdm/glpi-plugin
* @link https://flyve-mdm.com/
* ------------------------------------------------------------------------------
*/

namespace tests\units\GlpiPlugin\Flyvemdm\Fcm;


use Flyvemdm\Tests\CommonTestCase;

class FcmEnvelope extends CommonTestCase {

private $scope = [['type' => 'fcm', 'token' => 'Sup3rT0k3n']];
private $topic = ['lorem/ipsum/dolor'];

protected function providerContext() {
return [
'empty' => [
'context' => [[]],
'expected' => 'The scope argument is needed (push type and token)',
],
'invalid scope' => [
'context' => ['lorem' => ['key' => '']],
'expected' => 'The scope argument is needed (push type and token)',
],
'invalid type' => [
'context' => ['scope' => [['key' => '']]],
'expected' => 'The scope argument is needed (push type and token)',
],
'invalid token' => [
'context' => ['scope' => [['type' => 'fcm']]],
'expected' => 'The scope argument is needed (push type and token)',
],
'invalid topic' => [
'context' => ['scope' => $this->scope],
'expected' => 'A topic argument is needed',
],
];
}
/**
* @tags testException
* @dataProvider providerContext
* @param array $context
* @param string $expected
*/
public function testException($context, $expected) {
$this->exception(function () use ($context) {
$this->newTestedInstance($context);
})->hasMessage($expected);
}

/**
* @tags testEnvelope
*/
public function testEnvelope() {
$instance = $this->newTestedInstance(['scope' => $this->scope, 'topic' => $this->topic]);
$this->array($instance->getContext('scope'))->child[0](function ($child) {
$scope = $this->scope[0];
$child->hasKeys(['type', 'token'])->values
->string[0]->isEqualTo($scope['type'])
->string[1]->isEqualTo($scope['token']);
});
$context = $instance->getContext('topic');
$this->string($context[0])->isEqualTo('lorem-ipsum-dolor');
}

}
66 changes: 66 additions & 0 deletions tests/suite-unit/fcm/FcmSendMessageHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/**
* LICENSE
*
* Copyright © 2016-2018 Teclib'
* Copyright © 2010-2018 by the FusionInventory Development Team.
*
* This file is part of Flyve MDM Plugin for GLPI.
*
* Flyve MDM Plugin for GLPI is a subproject of Flyve MDM. Flyve MDM is a mobile
* device management software.
*
* Flyve MDM Plugin for GLPI is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* Flyve MDM Plugin for GLPI is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
* You should have received a copy of the GNU Affero General Public License
* along with Flyve MDM Plugin for GLPI. If not, see http://www.gnu.org/licenses/.
* ------------------------------------------------------------------------------
* @author Domingo Oropeza <[email protected]>
* @copyright Copyright © 2018 Teclib
* @license http://www.gnu.org/licenses/agpl.txt AGPLv3+
* @link https://github.com/flyve-mdm/glpi-plugin
* @link https://flyve-mdm.com/
* ------------------------------------------------------------------------------
*/

namespace tests\units\GlpiPlugin\Flyvemdm\Fcm;


use Flyvemdm\Tests\CommonTestCase;
use GlpiPlugin\Flyvemdm\Broker\BrokerMessage;
use GlpiPlugin\Flyvemdm\Fcm\FcmEnvelope;
use GlpiPlugin\Flyvemdm\Fcm\FcmSendMessageHandler as SendMessageHandler;
use Sly\NotificationPusher\Adapter\Gcm;
use Sly\NotificationPusher\PushManager;

class FcmSendMessageHandler extends CommonTestCase {

/**
* @tags testSendMessageHandler
*/
public function testSendMessageHandler() {
$pushManager = new PushManager();
$adapter = new Gcm(['apiKey' => 'ServerApikey']);
$toolbox = new \Toolbox();

$message = 'Hello world';
$topic = 'lorem/ipsum/dolor';
$scope = [['type' => 'fcm', 'token' => 'Sup3rT0k3n']];
$envelope = new FcmEnvelope(['scope' => $scope, 'topic' => $topic]);
$brokerMessage = new BrokerMessage($message);
$mockedConnection = $this->newMockInstance('\GlpiPlugin\Flyvemdm\Fcm\FcmConnection', null,
null, [$pushManager, $adapter, $toolbox]);
$mockedConnection->getMockController()->push = function () {};
$instance = new SendMessageHandler($envelope, $mockedConnection);
$this->object($instance)->isCallable();
$instance($brokerMessage);
$this->mock($mockedConnection)->call('push')->once();
}

}
64 changes: 64 additions & 0 deletions tests/suite-unit/mqtt/MqttEnvelope.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/**
* LICENSE
*
* Copyright © 2016-2018 Teclib'
* Copyright © 2010-2018 by the FusionInventory Development Team.
*
* This file is part of Flyve MDM Plugin for GLPI.
*
* Flyve MDM Plugin for GLPI is a subproject of Flyve MDM. Flyve MDM is a mobile
* device management software.
*
* Flyve MDM Plugin for GLPI is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* Flyve MDM Plugin for GLPI is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
* You should have received a copy of the GNU Affero General Public License
* along with Flyve MDM Plugin for GLPI. If not, see http://www.gnu.org/licenses/.
* ------------------------------------------------------------------------------
* @author Domingo Oropeza <[email protected]>
* @copyright Copyright © 2018 Teclib
* @license http://www.gnu.org/licenses/agpl.txt AGPLv3+
* @link https://github.com/flyve-mdm/glpi-plugin
* @link https://flyve-mdm.com/
* ------------------------------------------------------------------------------
*/

namespace tests\units\GlpiPlugin\Flyvemdm\Mqtt;


use Flyvemdm\Tests\CommonTestCase;

class MqttEnvelope extends CommonTestCase {

/**
* @tags testEnvelope
*/
public function testEnvelope() {
// try the exception
$this->exception(function () {
$this->newTestedInstance([]);
})->hasMessage('A topic argument is needed');

$topic = 'lorem';
$qos = 2;
$retain = 1;

// Defaut values
$instance = $this->newTestedInstance(['topic' => $topic]);
$this->string($instance->getContext('topic'))->isEqualTo($topic);
$this->integer($instance->getContext('qos'))->isEqualTo(0);
$this->integer($instance->getContext('retain'))->isEqualTo(0);

// set context values
$instance = $this->newTestedInstance(['topic' => $topic, 'qos' => $qos, 'retain' => $retain]);
$this->integer($instance->getContext('qos'))->isEqualTo($qos);
$this->integer($instance->getContext('retain'))->isEqualTo($retain);
}

}
59 changes: 59 additions & 0 deletions tests/suite-unit/mqtt/MqttSendMessageHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* LICENSE
*
* Copyright © 2016-2018 Teclib'
* Copyright © 2010-2018 by the FusionInventory Development Team.
*
* This file is part of Flyve MDM Plugin for GLPI.
*
* Flyve MDM Plugin for GLPI is a subproject of Flyve MDM. Flyve MDM is a mobile
* device management software.
*
* Flyve MDM Plugin for GLPI is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* Flyve MDM Plugin for GLPI is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
* You should have received a copy of the GNU Affero General Public License
* along with Flyve MDM Plugin for GLPI. If not, see http://www.gnu.org/licenses/.
* ------------------------------------------------------------------------------
* @author Domingo Oropeza <[email protected]>
* @copyright Copyright © 2018 Teclib
* @license http://www.gnu.org/licenses/agpl.txt AGPLv3+
* @link https://github.com/flyve-mdm/glpi-plugin
* @link https://flyve-mdm.com/
* ------------------------------------------------------------------------------
*/

namespace tests\units\GlpiPlugin\Flyvemdm\Mqtt;


use Flyvemdm\Tests\CommonTestCase;
use GlpiPlugin\Flyvemdm\Broker\BrokerMessage;
use GlpiPlugin\Flyvemdm\Mqtt\MqttEnvelope;
use GlpiPlugin\Flyvemdm\Mqtt\MqttSendMessageHandler as SendMessageHandler;

class MqttSendMessageHandler extends CommonTestCase {

/**
* @tags testSendMessageHandler
*/
public function testSendMessageHandler() {
$topic = 'lorem';
$message = 'Hello world';
$envelope = new MqttEnvelope(['topic' => $topic]);
$brokerMessage = new BrokerMessage($message);
$mockedConnection = $this->newMockInstance('\GlpiPlugin\Flyvemdm\Mqtt\MqttConnection');
$mockedConnection->getMockController()->publish = function () {};
$instance = new SendMessageHandler($mockedConnection, $envelope);
$this->object($instance)->isCallable();
$instance($brokerMessage);
$this->mock($mockedConnection)->call('publish')
->withIdenticalArguments($topic, $message, 0, 0)->once();
}

}

0 comments on commit 5f688c2

Please sign in to comment.