From 61d6697b86df7d60e8c38079939acf526a255626 Mon Sep 17 00:00:00 2001 From: Domingo Oropeza Date: Tue, 12 Jun 2018 15:31:02 -0400 Subject: [PATCH] test(policy): add unit tests for APN policy Signed-off-by: Domingo Oropeza --- tests/suite-unit/PluginFlyvemdmPolicyApn.php | 135 +++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 tests/suite-unit/PluginFlyvemdmPolicyApn.php diff --git a/tests/suite-unit/PluginFlyvemdmPolicyApn.php b/tests/suite-unit/PluginFlyvemdmPolicyApn.php new file mode 100644 index 00000000..4a54b0be --- /dev/null +++ b/tests/suite-unit/PluginFlyvemdmPolicyApn.php @@ -0,0 +1,135 @@ + 'connectivity', + 'symbol' => 'apnConfiguration', + 'type' => 'apn', + 'type_data' => '', + 'unicity' => '0', + ]; + + protected function validationProvider() { + return [ + 'Check apn_name is not set' => [ + 'data' => [[], null, null], + 'expected' => [false, 'APN name is mandatory'], + ], + 'Check apn_name is not empty' => [ + 'data' => [['apn_name' => ''], null, null], + 'expected' => [false, 'APN name is mandatory'], + ], + 'Check apn_fqn is not set' => [ + 'data' => [['apn_name' => 'lorem'], null, null], + 'expected' => [false, 'APN value is mandatory'], + ], + 'Check apn_fqn is not empty' => [ + 'data' => [['apn_name' => 'lorem', 'apn_fqn' => ''], null, null], + 'expected' => [false, 'APN value is mandatory'], + ], + 'Valid check 1' => [ + 'data' => [['apn_name' => 'lorem', 'apn_fqn' => 'ipsum'], null, null], + 'expected' => [true], + ], + ]; + } + + /** + * @dataProvider validationProvider + * @tags testCreatePolicy + * @param array $data + * @param array $expected + */ + public function testCreatePolicy($data, $expected) { + list($policy) = $this->createNewPolicyInstance(); + $success = $policy->integrityCheck($data[0], $data[1], $data[2]); + $this->boolean($success)->isEqualTo($expected[0]); + if (!$expected[0]) { + $this->string($_SESSION["MESSAGE_AFTER_REDIRECT"][0][0])->isEqualTo($expected[1]); + unset($_SESSION["MESSAGE_AFTER_REDIRECT"]); // to clear the buffer + } + } + + private function createNewPolicyInstance() { + $policyData = new \PluginFlyvemdmPolicy(); + $policyData->fields = $this->dataField; + $policy = $this->newTestedInstance($policyData); + return [$policy, $policyData]; + } + + /** + * @tags testGetMqttMessage + */ + public function testGetMqttMessage() { + list($policy) = $this->createNewPolicyInstance(); + + $this->boolean($policy->getMqttMessage(null, null, null))->isFalse(); + $value = '{"apn_name":"lorem","apn_fqn":"ipsum","apn_auth_type":"0","apn_type":"0"}'; + $result = $policy->getMqttMessage($value, null, null); + $this->array($result)->hasKeys([$this->dataField['symbol']]) + ->string($result[$this->dataField['symbol']])->contains('"apn_name":"lorem","apn_fqn":"ipsum"'); + } + + /** + * @tags testShowValueInput + */ + public function testShowValueInput() { + list($policy) = $this->createNewPolicyInstance(); + $value = $policy->showValueInput(); + $this->string($value) + ->contains('input type="text" name="value[apn_name]" value=""') + ->contains('input type="text" name="value[apn_fqn]" value=""'); + + $dropdowns = ['apn_auth_type', 'apn_type']; + foreach ($dropdowns as $inputName) { + $matches = null; + preg_match('/.*]*name=\'value\[' . $inputName . '\]\'[^>]*>.*/', + $value, $matches); + $this->array($matches)->hasSize(1); + } + } + + /** + * @tags testShowValue + */ + public function testShowValue() { + list($policy) = $this->createNewPolicyInstance(); + $mockInstance = $this->newMockInstance('\PluginFlyvemdmTask'); + $mockInstance->getMockController()->getField = '{"apn_name":"lorem","apn_fqn":"ipsum","apn_auth_type":"0","apn_type":"0"}'; + $this->string($policy->showValue($mockInstance))->isEqualTo('lorem'); + } +} \ No newline at end of file