|
| 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-228 |
| 38 | + * @see https://i-doit.atlassian.net/browse/API-228 |
| 39 | + */ |
| 40 | +class API228Test extends BaseTest { |
| 41 | + |
| 42 | + /** |
| 43 | + * @throws Exception on error |
| 44 | + */ |
| 45 | + public function testSave() { |
| 46 | + /** |
| 47 | + * Create test data: |
| 48 | + */ |
| 49 | + |
| 50 | + $vhostID = $this->useCMDBObject()->create( |
| 51 | + ObjectType::VIRTUAL_HOST, |
| 52 | + $this->generateRandomString() |
| 53 | + ); |
| 54 | + $this->isID($vhostID); |
| 55 | + |
| 56 | + $licenseHostID = $this->useCMDBObject()->create( |
| 57 | + ObjectType::HOST, |
| 58 | + $this->generateRandomString() |
| 59 | + ); |
| 60 | + $this->isID($licenseHostID); |
| 61 | + |
| 62 | + $appID = $this->useCMDBObject()->create( |
| 63 | + ObjectType::APPLICATION, |
| 64 | + $this->generateRandomString() |
| 65 | + ); |
| 66 | + $this->isID($appID); |
| 67 | + |
| 68 | + $vmID = $this->useCMDBObject()->create( |
| 69 | + ObjectType::VIRTUAL_SERVER, |
| 70 | + $this->generateRandomString() |
| 71 | + ); |
| 72 | + $this->isID($vmID); |
| 73 | + |
| 74 | + $installationID = $this->useCMDBCategory()->save( |
| 75 | + $vmID, |
| 76 | + Category::CATG__APPLICATION, |
| 77 | + [ |
| 78 | + 'application' => $appID |
| 79 | + ] |
| 80 | + ); |
| 81 | + $this->isID($installationID); |
| 82 | + |
| 83 | + $relations = $this->useCMDBCategory()->read( |
| 84 | + $vmID, |
| 85 | + Category::CATG__RELATION |
| 86 | + ); |
| 87 | + |
| 88 | + $this->assertIsArray($relations); |
| 89 | + $this->assertCount(1, $relations); |
| 90 | + $this->assertArrayHasKey(0, $relations); |
| 91 | + $this->assertIsArray($relations[0]); |
| 92 | + |
| 93 | + $this->assertArrayHasKey('object1', $relations[0]); |
| 94 | + $this->assertIsArray($relations[0]['object1']); |
| 95 | + $this->assertArrayHasKey('id', $relations[0]['object1']); |
| 96 | + $this->assertSame($vmID, (int) $relations[0]['object1']['id']); |
| 97 | + $this->assertSame(ObjectType::VIRTUAL_SERVER, $relations[0]['object1']['type']); |
| 98 | + |
| 99 | + $this->assertArrayHasKey('object2', $relations[0]); |
| 100 | + $this->assertIsArray($relations[0]['object2']); |
| 101 | + $this->assertArrayHasKey('id', $relations[0]['object2']); |
| 102 | + $this->assertSame($appID, (int) $relations[0]['object2']['id']); |
| 103 | + $this->assertSame(ObjectType::APPLICATION, $relations[0]['object2']['type']); |
| 104 | + |
| 105 | + $this->assertArrayHasKey('relation_type', $relations[0]); |
| 106 | + $this->assertIsArray($relations[0]['relation_type']); |
| 107 | + $this->assertArrayHasKey('const', $relations[0]['relation_type']); |
| 108 | + $this->assertSame('C__RELATION_TYPE__SOFTWARE', $relations[0]['relation_type']['const']); |
| 109 | + |
| 110 | + $serviceID = (int) $relations[0]['objID']; |
| 111 | + |
| 112 | + $entryID = $this->useCMDBCategory()->save( |
| 113 | + $vhostID, |
| 114 | + Category::CATG__VIRTUAL_HOST, |
| 115 | + [ |
| 116 | + 'virtual_host' => 1, |
| 117 | + 'license_server' => $licenseHostID, |
| 118 | + 'administration_service' => $serviceID |
| 119 | + ] |
| 120 | + ); |
| 121 | + $this->isID($entryID); |
| 122 | + |
| 123 | + /** |
| 124 | + * Run tests: |
| 125 | + */ |
| 126 | + |
| 127 | + $entries = $this->useCMDBCategory()->read( |
| 128 | + $vhostID, |
| 129 | + Category::CATG__VIRTUAL_HOST |
| 130 | + ); |
| 131 | + |
| 132 | + $this->assertIsArray($entries); |
| 133 | + $this->validateEntries($entries, $vhostID, $licenseHostID, $serviceID); |
| 134 | + |
| 135 | + /** |
| 136 | + * Update entry: |
| 137 | + */ |
| 138 | + |
| 139 | + $sameEntryID = $this->useCMDBCategory()->save( |
| 140 | + $vhostID, |
| 141 | + Category::CATG__VIRTUAL_HOST, |
| 142 | + [ |
| 143 | + 'description' => $this->generateDescription() |
| 144 | + ] |
| 145 | + ); |
| 146 | + |
| 147 | + $this->assertSame($entryID, $sameEntryID); |
| 148 | + |
| 149 | + /** |
| 150 | + * Run tests again: |
| 151 | + */ |
| 152 | + |
| 153 | + $entries = $this->useCMDBCategory()->read( |
| 154 | + $vhostID, |
| 155 | + Category::CATG__VIRTUAL_HOST |
| 156 | + ); |
| 157 | + |
| 158 | + $this->assertIsArray($entries); |
| 159 | + $this->validateEntries($entries, $vhostID, $licenseHostID, $serviceID); |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * @throws Exception on error |
| 164 | + */ |
| 165 | + public function testUpdate() { |
| 166 | + /** |
| 167 | + * Create test data: |
| 168 | + */ |
| 169 | + |
| 170 | + $vhostID = $this->useCMDBObject()->create( |
| 171 | + ObjectType::VIRTUAL_HOST, |
| 172 | + $this->generateRandomString() |
| 173 | + ); |
| 174 | + $this->isID($vhostID); |
| 175 | + |
| 176 | + $licenseHostID = $this->useCMDBObject()->create( |
| 177 | + ObjectType::HOST, |
| 178 | + $this->generateRandomString() |
| 179 | + ); |
| 180 | + $this->isID($licenseHostID); |
| 181 | + |
| 182 | + $appID = $this->useCMDBObject()->create( |
| 183 | + ObjectType::APPLICATION, |
| 184 | + $this->generateRandomString() |
| 185 | + ); |
| 186 | + $this->isID($appID); |
| 187 | + |
| 188 | + $vmID = $this->useCMDBObject()->create( |
| 189 | + ObjectType::VIRTUAL_SERVER, |
| 190 | + $this->generateRandomString() |
| 191 | + ); |
| 192 | + $this->isID($vmID); |
| 193 | + |
| 194 | + $installationID = $this->useCMDBCategory()->create( |
| 195 | + $vmID, |
| 196 | + Category::CATG__APPLICATION, |
| 197 | + [ |
| 198 | + 'application' => $appID |
| 199 | + ] |
| 200 | + ); |
| 201 | + $this->isID($installationID); |
| 202 | + |
| 203 | + $relations = $this->useCMDBCategory()->read( |
| 204 | + $vmID, |
| 205 | + Category::CATG__RELATION |
| 206 | + ); |
| 207 | + |
| 208 | + $this->assertIsArray($relations); |
| 209 | + $this->assertCount(1, $relations); |
| 210 | + $this->assertArrayHasKey(0, $relations); |
| 211 | + $this->assertIsArray($relations[0]); |
| 212 | + |
| 213 | + $this->assertArrayHasKey('object1', $relations[0]); |
| 214 | + $this->assertIsArray($relations[0]['object1']); |
| 215 | + $this->assertArrayHasKey('id', $relations[0]['object1']); |
| 216 | + $this->assertSame($vmID, (int) $relations[0]['object1']['id']); |
| 217 | + $this->assertSame(ObjectType::VIRTUAL_SERVER, $relations[0]['object1']['type']); |
| 218 | + |
| 219 | + $this->assertArrayHasKey('object2', $relations[0]); |
| 220 | + $this->assertIsArray($relations[0]['object2']); |
| 221 | + $this->assertArrayHasKey('id', $relations[0]['object2']); |
| 222 | + $this->assertSame($appID, (int) $relations[0]['object2']['id']); |
| 223 | + $this->assertSame(ObjectType::APPLICATION, $relations[0]['object2']['type']); |
| 224 | + |
| 225 | + $this->assertArrayHasKey('relation_type', $relations[0]); |
| 226 | + $this->assertIsArray($relations[0]['relation_type']); |
| 227 | + $this->assertArrayHasKey('const', $relations[0]['relation_type']); |
| 228 | + $this->assertSame('C__RELATION_TYPE__SOFTWARE', $relations[0]['relation_type']['const']); |
| 229 | + |
| 230 | + $serviceID = (int) $relations[0]['objID']; |
| 231 | + |
| 232 | + $entryID = $this->useCMDBCategory()->create( |
| 233 | + $vhostID, |
| 234 | + Category::CATG__VIRTUAL_HOST, |
| 235 | + [ |
| 236 | + 'virtual_host' => 1, |
| 237 | + 'license_server' => $licenseHostID, |
| 238 | + 'administration_service' => $serviceID |
| 239 | + ] |
| 240 | + ); |
| 241 | + $this->isID($entryID); |
| 242 | + |
| 243 | + /** |
| 244 | + * Run tests: |
| 245 | + */ |
| 246 | + |
| 247 | + $entries = $this->useCMDBCategory()->read( |
| 248 | + $vhostID, |
| 249 | + Category::CATG__VIRTUAL_HOST |
| 250 | + ); |
| 251 | + |
| 252 | + $this->assertIsArray($entries); |
| 253 | + $this->validateEntries($entries, $vhostID, $licenseHostID, $serviceID); |
| 254 | + |
| 255 | + /** |
| 256 | + * Update entry: |
| 257 | + */ |
| 258 | + |
| 259 | + $this->useCMDBCategory()->update( |
| 260 | + $vhostID, |
| 261 | + Category::CATG__VIRTUAL_HOST, |
| 262 | + [ |
| 263 | + 'description' => $this->generateDescription() |
| 264 | + ] |
| 265 | + ); |
| 266 | + |
| 267 | + /** |
| 268 | + * Run tests again: |
| 269 | + */ |
| 270 | + |
| 271 | + $entries = $this->useCMDBCategory()->read( |
| 272 | + $vhostID, |
| 273 | + Category::CATG__VIRTUAL_HOST |
| 274 | + ); |
| 275 | + |
| 276 | + $this->assertIsArray($entries); |
| 277 | + $this->validateEntries($entries, $vhostID, $licenseHostID, $serviceID); |
| 278 | + } |
| 279 | + |
| 280 | + protected function validateEntries(array $entries, int $vhostID, int $licenseHostID, $serviceID) { |
| 281 | + $this->assertCount(1, $entries); |
| 282 | + $this->assertArrayHasKey(0, $entries); |
| 283 | + $this->assertIsArray($entries[0]); |
| 284 | + $this->isCategoryEntry($entries[0]); |
| 285 | + |
| 286 | + $this->assertArrayHasKey('objID', $entries[0]); |
| 287 | + $this->assertSame($vhostID, (int) $entries[0]['objID']); |
| 288 | + |
| 289 | + $this->assertArrayHasKey('virtual_host', $entries[0]); |
| 290 | + $this->assertIsArray($entries[0]['virtual_host']); |
| 291 | + $this->assertArrayHasKey('value', $entries[0]['virtual_host']); |
| 292 | + $this->assertSame('1', $entries[0]['virtual_host']['value']); |
| 293 | + |
| 294 | + $this->assertArrayHasKey('license_server', $entries[0]); |
| 295 | + $this->assertIsArray($entries[0]['license_server']); |
| 296 | + $this->assertArrayHasKey('id', $entries[0]['license_server']); |
| 297 | + $this->assertSame($licenseHostID, (int) $entries[0]['license_server']['id']); |
| 298 | + |
| 299 | + $this->assertArrayHasKey('administration_service', $entries[0]); |
| 300 | + $this->assertIsArray($entries[0]['administration_service']); |
| 301 | + |
| 302 | + // @todo Implement proper tests! |
| 303 | + $this->assertIsInt($serviceID); |
| 304 | + } |
| 305 | + |
| 306 | +} |
0 commit comments