diff --git a/inc/fcm/fcmenvelope.class.php b/inc/fcm/fcmenvelope.class.php index 4321062e..12a37f58 100644 --- a/inc/fcm/fcmenvelope.class.php +++ b/inc/fcm/fcmenvelope.class.php @@ -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'])) { diff --git a/tests/suite-unit/fcm/FcmEnvelope.php b/tests/suite-unit/fcm/FcmEnvelope.php new file mode 100644 index 00000000..726c28d0 --- /dev/null +++ b/tests/suite-unit/fcm/FcmEnvelope.php @@ -0,0 +1,93 @@ + + * @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'); + } + +} \ No newline at end of file diff --git a/tests/suite-unit/fcm/FcmSendMessageHandler.php b/tests/suite-unit/fcm/FcmSendMessageHandler.php new file mode 100644 index 00000000..1ddd38f9 --- /dev/null +++ b/tests/suite-unit/fcm/FcmSendMessageHandler.php @@ -0,0 +1,66 @@ + + * @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(); + } + +} \ No newline at end of file diff --git a/tests/suite-unit/mqtt/MqttEnvelope.php b/tests/suite-unit/mqtt/MqttEnvelope.php new file mode 100644 index 00000000..044e375b --- /dev/null +++ b/tests/suite-unit/mqtt/MqttEnvelope.php @@ -0,0 +1,64 @@ + + * @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); + } + +} \ No newline at end of file diff --git a/tests/suite-unit/mqtt/MqttSendMessageHandler.php b/tests/suite-unit/mqtt/MqttSendMessageHandler.php new file mode 100644 index 00000000..dc4d74f5 --- /dev/null +++ b/tests/suite-unit/mqtt/MqttSendMessageHandler.php @@ -0,0 +1,59 @@ + + * @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(); + } + +} \ No newline at end of file