Skip to content

Commit f3ec2be

Browse files
authored
Merge pull request #26 from rakoitde/main
Add method `cmdb.condition.read`
2 parents 8656d41 + bd50efe commit f3ec2be

4 files changed

Lines changed: 158 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ For almost every case there is a remote procedure you may call to read from or m
192192
| | `cmdb.category.delete` | | `delete()` |
193193
| | `cmdb.category.purge` | | `purge()` |
194194
| `cmdb.category_info` | `cmdb.category_info.read` | `CMDBCategoryInfo` | `read()` |
195+
| `cmdb.condition` | `cmdb.condition.read` | `CMDBCondition` | `read()` |
195196
| `cmdb.dialog` | `cmdb.dialog.create` | `CMDBDialog` | `create()` |
196197
| | `cmdb.dialog.read` | | `read()` |
197198
| | `cmdb.dialog.delete` | | `delete()` |

src/CMDBCondition.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
/**
4+
* Copyright (C) 2022 synetics GmbH
5+
* Copyright (C) 2016-2022 Benjamin Heisig
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Affero General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU Affero General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Affero General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*
20+
* @author Benjamin Heisig <https://benjamin.heisig.name/>
21+
* @copyright Copyright (C) 2022 synetics GmbH
22+
* @copyright Copyright (C) 2016-2022 Benjamin Heisig
23+
* @license http://www.gnu.org/licenses/agpl-3.0 GNU Affero General Public License (AGPL)
24+
* @link https://github.com/i-doit/api-client-php
25+
*/
26+
27+
declare(strict_types=1);
28+
29+
namespace Idoit\APIClient;
30+
31+
use \Exception;
32+
33+
/**
34+
* Requests for API namespace 'cmdb.condition'
35+
*/
36+
class CMDBCondition extends Request {
37+
38+
const ATTRIBUTE_CONDITIONS = 'conditions';
39+
40+
/**
41+
* Get list of objects filtered by conditions
42+
*
43+
* @param array $conditions array of conditions
44+
*
45+
* @return array Indexed array of associative arrays
46+
*
47+
* @throws Exception on error
48+
*/
49+
public function read($conditions): array {
50+
return $this->api->request(
51+
'cmdb.condition.read',
52+
[
53+
'conditions' => $conditions
54+
]
55+
);
56+
}
57+
58+
}

tests/Idoit/APIClient/BaseTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ abstract class BaseTest extends TestCase {
5757
*/
5858
protected $cmdbCategory;
5959

60+
/**
61+
* @var CMDBCondition|null
62+
*/
63+
protected $cmdbCondition;
64+
6065
/**
6166
* @var CMDBDialog|null
6267
*/
@@ -182,6 +187,14 @@ public function useCMDBCategory(): CMDBCategory {
182187
return $this->cmdbCategory;
183188
}
184189

190+
public function useCMDBCondition(): CMDBCondition {
191+
if (!isset($this->cmdbCondition)) {
192+
$this->cmdbCondition = new CMDBCondition($this->api);
193+
}
194+
195+
return $this->cmdbCondition;
196+
}
197+
185198
public function useCMDBDialog(): CMDBDialog {
186199
if (!isset($this->cmdbDialog)) {
187200
$this->cmdbDialog = new CMDBDialog($this->api);
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
/**
4+
* Copyright (C) 2022 synetics GmbH
5+
* Copyright (C) 2016-2022 Benjamin Heisig
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Affero General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU Affero General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Affero General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*
20+
* @author Benjamin Heisig <https://benjamin.heisig.name/>
21+
* @copyright Copyright (C) 2022 synetics GmbH
22+
* @copyright Copyright (C) 2016-2022 Benjamin Heisig
23+
* @license http://www.gnu.org/licenses/agpl-3.0 GNU Affero General Public License (AGPL)
24+
* @link https://github.com/i-doit/api-client-php
25+
*/
26+
27+
declare(strict_types=1);
28+
29+
namespace Idoit\APIClient;
30+
31+
use \Exception;
32+
use Idoit\APIClient\Constants\Category;
33+
34+
class CMDBConditionTest extends BaseTest {
35+
36+
/**
37+
* @throws Exception on error
38+
*/
39+
public function testReadObjectByCondition() {
40+
$objectID = $this->createServer();
41+
42+
$attributes = [
43+
'inventory_no' => $this->generateRandomString(),
44+
'order_no' => $this->generateRandomString(),
45+
'invoice_no' => $this->generateRandomString()
46+
];
47+
48+
$entryID = $this->useCMDBCategory()->save(
49+
$objectID,
50+
Category::CATG__ACCOUNTING,
51+
$attributes
52+
);
53+
54+
$this->assertIsInt($entryID);
55+
$this->isID($entryID);
56+
57+
$entries = $this->useCMDBCategory()->read(
58+
$objectID,
59+
Category::CATG__ACCOUNTING
60+
);
61+
62+
$this->assertIsArray($entries);
63+
$this->assertCount(1, $entries);
64+
$this->assertArrayHasKey(0, $entries);
65+
66+
$entry = $entries[0];
67+
68+
// Check attributes:
69+
foreach ($attributes as $attribute => $value) {
70+
$this->assertArrayHasKey($attribute, $entry);
71+
$this->assertIsString($entry[$attribute]);
72+
$this->assertSame($value, $entry[$attribute]);
73+
}
74+
75+
$cmdbCondition = $this->useCMDBCondition();
76+
foreach ($attributes as $attribute => $value) {
77+
$conditions = [['property' => "C__CATG__ACCOUNTING-".$attribute,
78+
'comparison' => "=",
79+
'value' => $value,
80+
]];
81+
$objects = $cmdbCondition->read($conditions);
82+
$this->assertSame($objectID, intval($objects[0]['id']));
83+
}
84+
}
85+
86+
}

0 commit comments

Comments
 (0)