laravel-nami-api/tests/Unit/GetGroupsTest.php

72 lines
3.5 KiB
PHP
Raw Normal View History

2020-06-28 00:12:36 +02:00
<?php
namespace Zoomyboy\LaravelNami\Tests\Unit;
2022-02-18 18:29:02 +01:00
use Illuminate\Support\Facades\Http;
2020-06-28 02:11:23 +02:00
use Zoomyboy\LaravelNami\Group;
2022-02-18 18:29:02 +01:00
use Zoomyboy\LaravelNami\Tests\TestCase;
2020-06-28 00:12:36 +02:00
class GetGroupsTest extends TestCase
{
2022-02-18 18:29:02 +01:00
public string $groupsResponse = '{"success":true,"data":[{"descriptor":"Group","name":"","representedClass":"de.iconcept.nami.entity.org.Gruppierung","id":100}],"responseType":"OK"}';
public string $subgroupsResponse = '{ "success": true, "data": [ { "descriptor": "Siebengebirge", "name": "", "representedClass": "de.iconcept.nami.entity.org.Gruppierung", "id": 101300 }, { "descriptor": "Sieg", "name": "", "representedClass": "de.iconcept.nami.entity.org.Gruppierung", "id": 100900 }, { "descriptor": "Sieg", "name": "", "representedClass": "de.iconcept.nami.entity.org.Gruppierung", "id": 100900 }, { "descriptor": "Voreifel", "name": "", "representedClass": "de.iconcept.nami.entity.org.Gruppierung", "id": 101000 } ], "responseType": "OK" }';
public string $subsubgroupsResponse = '{ "success": true, "data": [ { "descriptor": "Silva", "name": "", "representedClass": "de.iconcept.nami.entity.org.Gruppierung", "id": 100105 } ], "responseType": "OK" }';
2020-06-28 00:12:36 +02:00
2022-03-11 20:20:00 +01:00
public function testGetAllGroups(): void
2020-06-28 00:12:36 +02:00
{
Http::fake([
2020-06-28 00:25:41 +02:00
'https://nami.dpsg.de/ica/rest/nami/gruppierungen/filtered-for-navigation/gruppierung/node/root' => Http::response($this->groupsResponse, 200),
2020-06-28 00:12:36 +02:00
]);
2022-02-18 18:29:02 +01:00
$groups = $this->login()->groups();
2020-06-28 00:12:36 +02:00
$this->assertEquals([
2022-03-11 20:20:00 +01:00
['id' => 100, 'name' => 'Group', 'parent_id' => null],
2022-02-18 18:29:02 +01:00
], $groups->toArray());
2022-03-11 20:20:00 +01:00
Http::assertSent(function ($request) {
return 'https://nami.dpsg.de/ica/rest/nami/gruppierungen/filtered-for-navigation/gruppierung/node/root' == $request->url();
2020-06-28 00:12:36 +02:00
});
2022-02-18 18:29:02 +01:00
Http::assertSentCount(1);
2020-06-28 00:12:36 +02:00
}
2022-03-11 20:20:00 +01:00
public function testHasGroupAccess(): void
2020-06-28 00:12:36 +02:00
{
Http::fake([
2020-06-28 00:25:41 +02:00
'https://nami.dpsg.de/ica/rest/nami/gruppierungen/filtered-for-navigation/gruppierung/node/root' => Http::response($this->groupsResponse, 200),
2020-06-28 00:12:36 +02:00
]);
2022-02-18 18:29:02 +01:00
$api = $this->login();
2020-06-28 00:12:36 +02:00
2022-02-18 18:29:02 +01:00
$this->assertTrue($api->hasGroup(100));
$this->assertFalse($api->hasGroup(10101));
2020-06-28 00:12:36 +02:00
2022-02-18 18:29:02 +01:00
Http::assertSentCount(2);
2020-06-28 00:12:36 +02:00
}
2020-06-28 02:11:23 +02:00
2022-03-11 20:20:00 +01:00
public function testGetSubgroupsForAGroup(): void
2022-02-18 18:29:02 +01:00
{
2020-06-28 02:11:23 +02:00
Http::fake([
'https://nami.dpsg.de/ica/rest/nami/gruppierungen/filtered-for-navigation/gruppierung/node/root' => Http::response($this->groupsResponse, 200),
2022-03-11 20:20:00 +01:00
'https://nami.dpsg.de/ica/rest/nami/gruppierungen/filtered-for-navigation/gruppierung/node/100' => Http::response($this->subgroupsResponse, 200),
2020-06-28 02:11:23 +02:00
]);
2022-02-18 18:29:02 +01:00
$subgroups = $this->login()->group(100)->subgroups();
2020-06-28 02:11:23 +02:00
$this->assertEquals([
2020-07-04 17:50:55 +02:00
['id' => 101300, 'parent_id' => 100, 'name' => 'Siebengebirge'],
['id' => 100900, 'parent_id' => 100, 'name' => 'Sieg'],
['id' => 100900, 'parent_id' => 100, 'name' => 'Sieg'],
2022-03-11 20:20:00 +01:00
['id' => 101000, 'parent_id' => 100, 'name' => 'Voreifel'],
2020-06-28 02:11:23 +02:00
], $subgroups->toArray());
2022-03-11 20:20:00 +01:00
$subgroups->each(function ($group) {
2020-06-28 02:11:23 +02:00
$this->assertInstanceOf(Group::class, $group);
});
2022-03-11 20:20:00 +01:00
Http::assertSent(function ($request) {
return 'https://nami.dpsg.de/ica/rest/nami/gruppierungen/filtered-for-navigation/gruppierung/node/root' == $request->url();
2020-06-28 02:11:23 +02:00
});
2022-03-11 20:20:00 +01:00
Http::assertSent(function ($request) {
return 'https://nami.dpsg.de/ica/rest/nami/gruppierungen/filtered-for-navigation/gruppierung/node/100' == $request->url();
2020-06-28 02:11:23 +02:00
});
2022-02-18 18:29:02 +01:00
Http::assertSentCount(2);
2020-06-28 02:11:23 +02:00
}
2020-06-28 00:12:36 +02:00
}