Update baustein fetch

This commit is contained in:
philipp lang 2022-03-04 01:56:03 +01:00
parent bf95d6f17e
commit 0816f1ad65
6 changed files with 859 additions and 859 deletions

1630
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -14,6 +14,7 @@ use Illuminate\Support\Str;
use Log; use Log;
use Zoomyboy\LaravelNami\Authentication\Authenticator; use Zoomyboy\LaravelNami\Authentication\Authenticator;
use Zoomyboy\LaravelNami\Concerns\IsNamiMember; use Zoomyboy\LaravelNami\Concerns\IsNamiMember;
use Zoomyboy\LaravelNami\Data\Baustein;
use Zoomyboy\LaravelNami\Data\Course; use Zoomyboy\LaravelNami\Data\Course;
use Zoomyboy\LaravelNami\Exceptions\NotAuthenticatedException; use Zoomyboy\LaravelNami\Exceptions\NotAuthenticatedException;
use Zoomyboy\LaravelNami\Exceptions\RightException; use Zoomyboy\LaravelNami\Exceptions\RightException;
@ -199,7 +200,7 @@ class Api {
$this->assertLoggedIn(); $this->assertLoggedIn();
return $this->fetchCollection('/ica/rest/module/baustein', 'Fetch courses failed') return $this->fetchCollection('/ica/rest/module/baustein', 'Fetch courses failed')
->map(fn ($course) => (object) ['id' => $course['id']]); ->map(fn ($course) => new Baustein($course));
} }
/** /**

5
src/Data/Baustein.php Normal file
View File

@ -0,0 +1,5 @@
<?php
namespace Zoomyboy\LaravelNami\Data;
class Baustein extends EnumData { }

15
src/Data/EnumData.php Normal file
View File

@ -0,0 +1,15 @@
<?php
namespace Zoomyboy\LaravelNami\Data;
use Spatie\DataTransferObject\Attributes\MapFrom;
use Spatie\DataTransferObject\DataTransferObject;
abstract class EnumData extends DataTransferObject {
public int $id;
#[MapFrom('descriptor')]
public string $name;
}

View File

@ -0,0 +1,27 @@
<?php
namespace Zoomyboy\LaravelNami\Fakes;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Http;
class BausteinFake extends Fake {
/**
* @param int $memberId
* @param array $courses
*
* @return self
*/
public function fetches(array $courses): self
{
Http::fake(function($request) use ($courses) {
if ($request->url() === "https://nami.dpsg.de/ica/rest/module/baustein") {
return $this->collection(collect($courses));
}
});
return $this;
}
}

View File

@ -0,0 +1,38 @@
<?php
namespace Zoomyboy\LaravelNami\Tests\Unit;
use Illuminate\Support\Facades\Http;
use Zoomyboy\LaravelNami\Authentication\Auth;
use Zoomyboy\LaravelNami\Exceptions\NotAuthenticatedException;
use Zoomyboy\LaravelNami\Fakes\BausteinFake;
use Zoomyboy\LaravelNami\Fakes\CourseFake;
use Zoomyboy\LaravelNami\LoginException;
use Zoomyboy\LaravelNami\Nami;
use Zoomyboy\LaravelNami\NamiException;
use Zoomyboy\LaravelNami\Tests\TestCase;
class BausteinTest extends TestCase
{
public function setUp(): void
{
parent::setUp();
Auth::fake();
}
public function test_get_all_courses(): void
{
Auth::success(12345, 'secret');
app(BausteinFake::class)->fetches([['id' => 788, 'descriptor' => 'abc']]);
$courses = Nami::login(12345, 'secret')->courses();
$this->assertCount(1, $courses);
$this->assertEquals(788, $courses->first()->id);
$this->assertEquals('abc', $courses->first()->name);
}
}