Skip to content

Commit 2f59f1e

Browse files
committed
Test issue API-225
1 parent 7d42ede commit 2f59f1e

1 file changed

Lines changed: 212 additions & 0 deletions

File tree

tests/Issues/API225Test.php

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
<?php
2+
3+
/**
4+
* Copyright (C) 2016-2020 Benjamin Heisig
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU Affero General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU Affero General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Affero General Public License
17+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*
19+
* @author Benjamin Heisig <https://benjamin.heisig.name/>
20+
* @copyright Copyright (C) 2016-2020 Benjamin Heisig
21+
* @license http://www.gnu.org/licenses/agpl-3.0 GNU Affero General Public License (AGPL)
22+
* @link https://github.com/bheisig/i-doit-api-client-php
23+
*/
24+
25+
declare(strict_types=1);
26+
27+
namespace bheisig\idoitapi\tests\Issues;
28+
29+
use bheisig\idoitapi\tests\Constants\Category;
30+
use bheisig\idoitapi\tests\Constants\ObjectType;
31+
use \Exception;
32+
use bheisig\idoitapi\tests\BaseTest;
33+
34+
/**
35+
* @group unreleased
36+
* @group issues
37+
* @group API-225
38+
* @see https://i-doit.atlassian.net/browse/API-225
39+
*/
40+
class API225Test extends BaseTest {
41+
42+
/**
43+
* @throws Exception on error
44+
*/
45+
public function testSave() {
46+
$adminRole = 1;
47+
$userRole = 2;
48+
49+
/**
50+
* Create test data:
51+
*/
52+
53+
$hostID = $this->createServer();
54+
$this->isID($hostID);
55+
56+
$personID = $this->cmdbObject->create(
57+
ObjectType::PERSON,
58+
$this->generateRandomString()
59+
);
60+
$this->isID($personID);
61+
62+
$entryID = $this->useCMDBCategory()->save(
63+
$hostID,
64+
Category::CATG__CONTACT,
65+
[
66+
'contact' => $personID,
67+
'role' => $adminRole,
68+
'description' => $this->generateDescription()
69+
]
70+
);
71+
72+
/**
73+
* Run tests:
74+
*/
75+
76+
$entries = $this->useCMDBCategory()->read(
77+
$hostID,
78+
Category::CATG__CONTACT
79+
);
80+
81+
$this->assertIsArray($entries);
82+
$this->validateEntries($entries, $hostID, $personID, $adminRole, $entryID);
83+
84+
/**
85+
* Update entry:
86+
*/
87+
88+
$sameEntryID = $this->useCMDBCategory()->save(
89+
$hostID,
90+
Category::CATG__CONTACT,
91+
[
92+
'contact' => $personID,
93+
'role' => $userRole
94+
],
95+
$entryID
96+
);
97+
98+
$this->assertSame($entryID, $sameEntryID);
99+
100+
/**
101+
* Run tests again:
102+
*/
103+
104+
$entries = $this->useCMDBCategory()->read(
105+
$hostID,
106+
Category::CATG__CONTACT
107+
);
108+
109+
$this->assertIsArray($entries);
110+
$this->validateEntries($entries, $hostID, $personID, $userRole, $entryID);
111+
}
112+
113+
/**
114+
* @throws Exception on error
115+
*/
116+
public function testCreateAndUpdate() {
117+
$adminRole = 1;
118+
$userRole = 2;
119+
120+
/**
121+
* Create test data:
122+
*/
123+
124+
$hostID = $this->createServer();
125+
$this->isID($hostID);
126+
127+
$personID = $this->cmdbObject->create(
128+
ObjectType::PERSON,
129+
$this->generateRandomString()
130+
);
131+
$this->isID($personID);
132+
133+
$entryID = $this->useCMDBCategory()->create(
134+
$hostID,
135+
Category::CATG__CONTACT,
136+
[
137+
'contact' => $personID,
138+
'role' => $adminRole,
139+
'description' => $this->generateDescription()
140+
]
141+
);
142+
143+
/**
144+
* Run tests:
145+
*/
146+
147+
$entries = $this->useCMDBCategory()->read(
148+
$hostID,
149+
Category::CATG__CONTACT
150+
);
151+
152+
$this->assertIsArray($entries);
153+
$this->validateEntries($entries, $hostID, $personID, $adminRole, $entryID);
154+
155+
/**
156+
* Update entry:
157+
*/
158+
159+
$this->useCMDBCategory()->update(
160+
$hostID,
161+
Category::CATG__CONTACT,
162+
[
163+
'contact' => $personID,
164+
'role' => $userRole
165+
],
166+
$entryID
167+
);
168+
169+
/**
170+
* Run tests again:
171+
*/
172+
173+
$entries = $this->useCMDBCategory()->read(
174+
$hostID,
175+
Category::CATG__CONTACT
176+
);
177+
178+
$this->assertIsArray($entries);
179+
$this->validateEntries($entries, $hostID, $personID, $userRole, $entryID);
180+
}
181+
182+
protected function validateEntries(array $entries, int $hostID, int $personID, int $roleID, int $entryID) {
183+
$this->assertCount(1, $entries);
184+
$this->assertArrayHasKey(0, $entries);
185+
$this->assertIsArray($entries[0]);
186+
$this->isCategoryEntry($entries[0]);
187+
188+
$this->assertSame($entryID, (int) $entries[0]['id']);
189+
190+
$this->assertSame($hostID, (int) $entries[0]['objID']);
191+
192+
$this->assertArrayHasKey('contact', $entries[0]);
193+
$this->assertIsArray($entries[0]['contact']);
194+
$this->assertArrayHasKey('id', $entries[0]['contact']);
195+
$this->assertSame($personID, (int) $entries[0]['contact']['id']);
196+
$this->assertArrayHasKey('type', $entries[0]['contact']);
197+
$this->assertSame(ObjectType::PERSON, $entries[0]['contact']['type']);
198+
199+
$this->assertArrayHasKey('contact_object', $entries[0]);
200+
$this->assertIsArray($entries[0]['contact_object']);
201+
$this->assertArrayHasKey('id', $entries[0]['contact_object']);
202+
$this->assertSame($personID, (int) $entries[0]['contact_object']['id']);
203+
$this->assertArrayHasKey('type', $entries[0]['contact_object']);
204+
$this->assertSame(ObjectType::PERSON, $entries[0]['contact_object']['type']);
205+
206+
$this->assertArrayHasKey('role', $entries[0]);
207+
$this->assertIsArray($entries[0]['role']);
208+
$this->assertArrayHasKey('id', $entries[0]['role']);
209+
$this->assertSame($roleID, (int) $entries[0]['role']['id']);
210+
}
211+
212+
}

0 commit comments

Comments
 (0)