Skip to content

Commit 59e89ef

Browse files
committed
add CMDBConditionTest.php
1 parent 6bd3c45 commit 59e89ef

3 files changed

Lines changed: 96 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()` |

tests/Idoit/APIClient/BaseTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,14 @@ public function useCMDBCategory(): CMDBCategory {
182182
return $this->cmdbCategory;
183183
}
184184

185+
public function useCMDBCondition(): CMDBCondition {
186+
if (!isset($this->cmdbCondition)) {
187+
$this->cmdbCondition = new CMDBCondition($this->api);
188+
}
189+
190+
return $this->cmdbCondition;
191+
}
192+
185193
public function useCMDBDialog(): CMDBDialog {
186194
if (!isset($this->cmdbDialog)) {
187195
$this->cmdbDialog = new CMDBDialog($this->api);
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
87+
}

0 commit comments

Comments
 (0)