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

65 lines
2.6 KiB
PHP
Raw Normal View History

2020-07-04 21:23:10 +02:00
<?php
namespace Zoomyboy\LaravelNami\Tests\Unit;
2022-02-18 18:29:02 +01:00
use Illuminate\Support\Facades\Http;
2023-02-17 14:54:17 +01:00
use Zoomyboy\LaravelNami\Data\Activity;
2023-02-08 01:26:01 +01:00
use Zoomyboy\LaravelNami\Exceptions\NoJsonReceivedException;
use Zoomyboy\LaravelNami\Exceptions\NotSuccessfulException;
2022-02-19 22:00:50 +01:00
use Zoomyboy\LaravelNami\Fakes\SubactivityFake;
2022-02-18 18:29:02 +01:00
use Zoomyboy\LaravelNami\Tests\TestCase;
2020-07-04 21:23:10 +02:00
class PullActivitiesTest 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":103}],"responseType":"OK"}';
2020-07-04 21:23:10 +02:00
2022-03-11 20:20:00 +01:00
public function testGetAllActivities(): void
2022-02-18 18:29:02 +01:00
{
Http::fake([
2020-07-04 21:23:10 +02:00
'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/taetigkeitaufgruppierung/filtered/gruppierung/gruppierung/103' => Http::response($this->fakeJson('activities.json'), 200),
2022-02-18 18:29:02 +01:00
]);
2020-07-04 21:23:10 +02:00
2022-02-18 18:29:02 +01:00
$activities = $this->login()->group(103)->activities();
2020-07-04 21:23:10 +02:00
$this->assertSame([
2023-02-17 14:54:17 +01:00
['id' => 4, 'name' => 'Ac1'],
['id' => 3, 'name' => 'Ac2'],
2022-02-18 18:29:02 +01:00
], $activities->toArray());
Http::assertSentCount(2);
2020-07-04 21:23:10 +02:00
}
2022-03-11 20:20:00 +01:00
public function testGetAllSubactivities(): void
2022-02-18 18:29:02 +01:00
{
Http::fake([
2020-07-04 21:23:10 +02:00
'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/taetigkeitaufgruppierung/filtered/gruppierung/gruppierung/103' => Http::response($this->fakeJson('activities.json'), 200),
2022-03-11 20:20:00 +01:00
'https://nami.dpsg.de/ica/rest/nami/untergliederungauftaetigkeit/filtered/untergliederung/taetigkeit/4' => Http::response($this->fakeJson('subactivities-4.json'), 200),
2022-02-18 18:29:02 +01:00
]);
2020-07-04 21:23:10 +02:00
2022-02-18 18:29:02 +01:00
$subactivities = $this->login()->group(103)->activities()->first()->subactivities();
2020-07-04 21:23:10 +02:00
$this->assertSame([
2023-02-17 14:54:17 +01:00
['id' => 40, 'name' => 'Biber'],
['id' => 30, 'name' => 'Wölfling'],
2022-02-18 18:29:02 +01:00
], $subactivities->toArray());
Http::assertSentCount(3);
2020-07-04 21:23:10 +02:00
}
2022-03-11 20:20:00 +01:00
public function testThrowErrorWhenSubactivitiesRequestFails(): void
2022-02-19 22:00:50 +01:00
{
2023-02-08 01:26:01 +01:00
$this->expectException(NotSuccessfulException::class);
app(SubactivityFake::class)->fetchFails(4, 'sorry dude');
2022-02-19 22:00:50 +01:00
2023-02-17 14:54:17 +01:00
$this->login()->subactivitiesOf(Activity::from(['id' => 4]));
}
2022-02-19 22:00:50 +01:00
2022-03-11 20:20:00 +01:00
public function testContinueIfSubactivitiesRequestReturnsHtml(): void
{
2023-02-08 01:26:01 +01:00
$this->expectException(NoJsonReceivedException::class);
app(SubactivityFake::class)->fetchFailsWithoutJson(4);
2023-02-17 14:54:17 +01:00
$this->login()->subactivitiesOf(Activity::from(['id' => 4]));
2022-02-19 22:00:50 +01:00
}
2020-07-04 21:23:10 +02:00
}