diff --git a/build/templates/abstract.tpl.php b/build/templates/abstract.tpl.php index bf3983b..a603506 100644 --- a/build/templates/abstract.tpl.php +++ b/build/templates/abstract.tpl.php @@ -55,6 +55,16 @@ abstract class */ private $defaultParams = array(); + /** + * @var string + */ + private $user; + + /** + * @var string + */ + private $password; + /** * Auth string. * @@ -137,7 +147,8 @@ public function __construct($apiUrl = '', $user = '', $password = '', $httpUser if ($authToken) { $this->setAuthToken($authToken); } elseif ($user && $password) { - $this->userLogin(array('user' => $user, 'password' => $password)); + $this->user = $user; + $this->password = $password; } } @@ -270,6 +281,10 @@ public function printCommunication($print = true) */ public function request($method, $params = null, $resultArrayKey = '', $auth = true) { + if (!$this->authToken && $auth && $this->user && $this->password) { + $this->userLogin(array('user' => $this->user, 'password' => $this->password)); + } + // sanity check and conversion for params array if (!$params) { $params = array(); diff --git a/tests/ZabbixApiTest.php b/tests/ZabbixApiTest.php index e63226d..183cad1 100644 --- a/tests/ZabbixApiTest.php +++ b/tests/ZabbixApiTest.php @@ -41,7 +41,7 @@ public function testZabbixApiClass() $this->assertGreaterThanOrEqual(0, version_compare(ZabbixApi::ZABBIX_VERSION, '2.4')); - $zabbix = new ZabbixApi('http://localhost/json_rpc.php'); + $zabbix = new ZabbixApi('http://localhost/json_rpc.php', 'zabbix', 'very_secret'); $defaultParams = array( 'some_param' => array('one'), @@ -170,22 +170,15 @@ public function getAuthenticationRequired() ); } - public function testZabbixApiConnectionNotTriggered() - { - $zabbix = new ZabbixApi('http://localhost/json_rpc.php'); - $zabbix = new ZabbixApi('http://localhost/json_rpc.php', 'zabbix'); - $zabbix = new ZabbixApi('http://localhost/json_rpc.php', '', 'very_secret'); - - $this->assertSame('http://localhost/json_rpc.php', $zabbix->getApiUrl()); - } - /** * @expectedException \ZabbixApi\Exception * @expectedExceptionMessage Could not connect to "http://not.found.tld/json_rpc.php" */ public function testZabbixApiConnectionError() { - new ZabbixApi('http://not.found.tld/json_rpc.php', 'zabbix', 'very_secret_pass'); + $zabbix = new ZabbixApi('http://not.found.tld/json_rpc.php', 'zabbix', 'very_secret'); + + $zabbix->userGet(); } /**