|
| 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