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

77 lines
3.7 KiB
PHP
Raw Normal View History

2020-06-28 00:12:36 +02:00
<?php
namespace Zoomyboy\LaravelNami\Tests\Unit;
use Illuminate\Support\Facades\Config;
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\LoginException;
use Zoomyboy\LaravelNami\Nami;
use Zoomyboy\LaravelNami\NamiServiceProvider;
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-02-18 18:29:02 +01:00
public function test_get_all_groups(): 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([
2020-07-04 17:50:55 +02:00
['id' => 100, 'name' => 'Group', 'parent_id' => null]
2022-02-18 18:29:02 +01:00
], $groups->toArray());
2020-06-28 00:12:36 +02:00
Http::assertSent(function($request) {
return $request->url() == 'https://nami.dpsg.de/ica/rest/nami/gruppierungen/filtered-for-navigation/gruppierung/node/root';
});
2022-02-18 18:29:02 +01:00
Http::assertSentCount(1);
2020-06-28 00:12:36 +02:00
}
2022-02-18 18:29:02 +01:00
public function test_has_group_access(): 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-02-18 18:29:02 +01:00
public function test_get_subgroups_for_a_group(): void
{
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),
'https://nami.dpsg.de/ica/rest/nami/gruppierungen/filtered-for-navigation/gruppierung/node/100' => Http::response($this->subgroupsResponse, 200)
]);
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'],
['id' => 101000, 'parent_id' => 100, 'name' => 'Voreifel']
2020-06-28 02:11:23 +02:00
], $subgroups->toArray());
$subgroups->each(function($group) {
$this->assertInstanceOf(Group::class, $group);
});
Http::assertSent(function($request) {
return $request->url() == 'https://nami.dpsg.de/ica/rest/nami/gruppierungen/filtered-for-navigation/gruppierung/node/root';
});
Http::assertSent(function($request) {
return $request->url() == 'https://nami.dpsg.de/ica/rest/nami/gruppierungen/filtered-for-navigation/gruppierung/node/100';
});
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
}