Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not call userLogin() at __construct() #53

Merged
merged 1 commit into from
May 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion build/templates/abstract.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ abstract class <CLASSNAME_ABSTRACT>
*/
private $defaultParams = array();

/**
* @var string
*/
private $user;

/**
* @var string
*/
private $password;

/**
* Auth string.
*
Expand Down Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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();
Expand Down
15 changes: 4 additions & 11 deletions tests/ZabbixApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down Expand Up @@ -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();
}

/**
Expand Down