Welcome to mirror list, hosted at ThFree Co, Russian Federation.

CAPITest.php « include « tests « php « frontends - github.com/zabbix/zabbix.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 56fdd6715ecf9c6f3d4d5d71ea21733a2e49a40d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
/*
** Zabbix
** Copyright (C) 2001-2020 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program 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 General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
**/

require_once 'vendor/autoload.php';

require_once dirname(__FILE__).'/CTest.php';

/**
 * Base class for Zabbix API tests.
 */
class CAPITest extends CTest {

	/**
	 * Check API response.
	 *
	 * @param array $response  response to be checked
	 * @param mixed $error     expected error:
	 *
	 * Possible $error types:
	 *     null/false - there should not be an error
	 *     array      - exact match
	 *     string     - error message should match
	 *     int        - error code should match
	 */
	public function checkResult($response, $error = null) {
		// Check response data.
		if ($error === null || $error === false) {
			$this->assertArrayHasKey('result', $response);
			$this->assertArrayNotHasKey('error', $response);
		}
		else {
			$this->assertArrayNotHasKey('result', $response);
			$this->assertArrayHasKey('error', $response);

			if (is_array($error)) {
				$this->assertSame($error, $response['error']);
			}
			elseif (is_string($error)) {
				$this->assertSame($error, $response['error']['data']);
			}
			elseif (is_numeric($error)) {
				$this->assertSame($error, $response['error']['code']);
			}
		}
	}

	/**
	 * Make API call.
	 *
	 * @param mixed $data     string containing request data as json.
	 *
	 * @return array
	 *
	 * @throws Exception      if API call fails.
	 */
	public function callRaw($data) {
		return CAPIHelper::callRaw($data);
	}

	/**
	 * Prepare request for API call and make API call (@see callRaw).
	 *
	 * @param string $method    API method to be called.
	 * @param mixed $params     API call params.
	 * @param mixed $error      expected error if any or null/false if successful result is expected.
	 *
	 * @return array
	 */
	public function call($method, $params, $error = null) {
		if (CAPIHelper::getSessionId() === null) {
			$this->authorize(PHPUNIT_LOGIN_NAME, PHPUNIT_LOGIN_PWD);
		}

		$response = CAPIHelper::call($method, $params);
		$this->checkResult($response, $error);

		return $response;
	}

	/**
	 * Enable authorization/session for the following API calls.
	 */
	public function enableAuthorization() {
		CAPIHelper::setSessionId(null);
	}

	/**
	 * Disable authorization/session for the following API calls.
	 */
	public function disableAuthorization() {
		CAPIHelper::setSessionId(false);
	}

	/**
	 * Authorize as user.
	 *
	 * @param string $user        username to be used for authorization.
	 * @param string $password    password.
	 */
	public function authorize($user, $password) {
		CAPIHelper::authorize($user, $password);
	}

	/**
	 * @inheritdoc
	 */
	protected function onNotSuccessfulTest($e) {
		if ($e instanceof Exception && CAPIHelper::getDebugInfo()) {
			CExceptionHelper::setMessage($e, $e->getMessage()."\n\nAPI calls:\n".CAPIHelper::getDebugInfoAsString());
		}

		parent::onNotSuccessfulTest($e);
	}

	/**
	 * Callback executed before every test case.
	 *
	 * @before
	 */
	public function onBeforeTestCase() {
		global $URL;
		$URL = PHPUNIT_URL.'api_jsonrpc.php';
		CAPIHelper::reset();

		parent::onBeforeTestCase();
	}

	/**
	 * Callback executed after every test case.
	 *
	 * @after
	 */
	public function onAfterTestCase() {
		parent::onAfterTestCase();

		CAPIHelper::clearDebugInfo();
	}
}