Add factory for courses

This commit is contained in:
philipp lang 2023-02-07 02:30:00 +01:00
parent 5890d0dbe1
commit acaae53ac8
7 changed files with 113 additions and 16 deletions

View File

@ -2,8 +2,12 @@
namespace Zoomyboy\LaravelNami\Data; namespace Zoomyboy\LaravelNami\Data;
use Carbon\Carbon;
use Spatie\LaravelData\Attributes\MapInputName; use Spatie\LaravelData\Attributes\MapInputName;
use Spatie\LaravelData\Attributes\WithCast;
use Spatie\LaravelData\Data; use Spatie\LaravelData\Data;
use Zoomyboy\LaravelNami\Casters\CarbonCast;
use Zoomyboy\LaravelNami\Tests\Factories\CourseFactory;
class Course extends Data class Course extends Data
{ {
@ -20,7 +24,13 @@ class Course extends Data
public string $eventName, public string $eventName,
#[MapInputName('vstgTag')] #[MapInputName('vstgTag')]
public string $completedAt, #[WithCast(CarbonCast::class, format: 'Y-m-d H:i:s')]
public Carbon $completedAt,
) { ) {
} }
public static function factory(): CourseFactory
{
return CourseFactory::new();
}
} }

View File

@ -6,6 +6,7 @@ use Spatie\LaravelData\Attributes\MapInputName;
use Spatie\LaravelData\Attributes\WithCast; use Spatie\LaravelData\Attributes\WithCast;
use Spatie\LaravelData\Data; use Spatie\LaravelData\Data;
use Zoomyboy\LaravelNami\Casters\StringCast; use Zoomyboy\LaravelNami\Casters\StringCast;
use Zoomyboy\LaravelNami\Tests\Factories\MemberEntryRequestFactory;
class MemberEntry extends Data class MemberEntry extends Data
{ {
@ -24,4 +25,9 @@ class MemberEntry extends Data
public int $groupId, public int $groupId,
) { ) {
} }
public static function factory(): MemberEntryRequestFactory
{
return MemberEntryRequestFactory::new();
}
} }

View File

@ -3,14 +3,11 @@
namespace Zoomyboy\LaravelNami\Fakes; namespace Zoomyboy\LaravelNami\Fakes;
use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Http;
use Zoomyboy\LaravelNami\Data\Course;
class CourseFake extends Fake class CourseFake extends Fake
{ {
private array $defaults = [ private array $defaults = [
'bausteinId' => 506,
'veranstalter' => 'KJA',
'vstgName' => 'eventname',
'vstgTag' => '2021-11-12 00:00:00',
]; ];
/** /**
@ -38,14 +35,17 @@ class CourseFake extends Fake
return $this; return $this;
} }
/** public function shows(int $memberId, Course $data): self
* @param array<string, mixed> $data
*/
public function shows(int $memberId, array $data): self
{ {
Http::fake(function ($request) use ($memberId, $data) { Http::fake(function ($request) use ($memberId, $data) {
if ($request->url() === "https://nami.dpsg.de/ica/rest/nami/mitglied-ausbildung/filtered-for-navigation/mitglied/mitglied/{$memberId}/{$data['id']}") { if ($request->url() === "https://nami.dpsg.de/ica/rest/nami/mitglied-ausbildung/filtered-for-navigation/mitglied/mitglied/{$memberId}/{$data->id}") {
return $this->dataResponse(array_merge($this->defaults, $data)); return $this->dataResponse([
'bausteinId' => $data->courseId,
'veranstalter' => $data->organizer,
'vstgName' => $data->eventName,
'vstgTag' => $data->completedAt->format('Y-m-d').' 00:00:00',
'id' => $data->id,
]);
} }
}); });

View File

@ -3,6 +3,7 @@
namespace Zoomyboy\LaravelNami\Fakes; namespace Zoomyboy\LaravelNami\Fakes;
use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Http;
use Zoomyboy\LaravelNami\Data\MemberEntry;
class SearchFake extends Fake class SearchFake extends Fake
{ {
@ -14,4 +15,22 @@ class SearchFake extends Fake
} }
}); });
} }
/**
* @param array<int, MemberEntry> $data
*/
public function fetches(int $page, int $start, array $data): void
{
Http::fake(function ($request) use ($page, $start, $data) {
dump($request->url());
if ($request->url() === 'https://nami.dpsg.de/ica/rest/nami/search-multi/result-list?searchedValues='.rawurlencode('{}').'&page='.$page.'&start='.$start.'&limit=100') {
return $this->collection(collect($data)->map(fn ($member) => [
'id' => $member->id,
'entries_gruppierungId' => $member->groupId,
'entries_nachname' => $member->lastname,
'entries_vorname' => $member->firstname,
]));
}
});
}
} }

View File

@ -0,0 +1,34 @@
<?php
namespace Zoomyboy\LaravelNami\Tests\Factories;
use Carbon\Carbon;
use Worksome\RequestFactories\RequestFactory;
use Zoomyboy\LaravelNami\Data\Course;
class CourseFactory extends RequestFactory
{
public function definition(): array
{
return [
'courseId' => $this->faker->numberBetween(100, 200),
'id' => $this->faker->numberBetween(100, 200),
'organizer' => $this->faker->word(),
'eventName' => $this->faker->word(),
'completedAt' => Carbon::parse($this->faker->dateTime())->startOfDay(),
];
}
/**
* @param array<string, mixed> $attributes
*/
public function toModel(array $attributes = []): Course
{
return Course::from($this->create($attributes));
}
public function id(int $id): self
{
return $this->state(['id' => $id]);
}
}

View File

@ -0,0 +1,27 @@
<?php
namespace Zoomyboy\LaravelNami\Tests\Factories;
use Worksome\RequestFactories\RequestFactory;
use Zoomyboy\LaravelNami\Data\MemberEntry;
class MemberEntryRequestFactory extends RequestFactory
{
public function definition(): array
{
return [
'firstname' => $this->faker->firstName(),
'lastname' => $this->faker->lastName(),
'groupId' => $this->faker->numberBetween(100, 200),
'id' => $this->faker->numberBetween(100, 200),
];
}
/**
* @param array<string, mixed> $attributes
*/
public function toMember(array $attributes = []): MemberEntry
{
return MemberEntry::from($this->create($attributes));
}
}

View File

@ -2,6 +2,7 @@
namespace Zoomyboy\LaravelNami\Tests\Unit; namespace Zoomyboy\LaravelNami\Tests\Unit;
use Zoomyboy\LaravelNami\Data\Course;
use Zoomyboy\LaravelNami\Exceptions\NotAuthenticatedException; use Zoomyboy\LaravelNami\Exceptions\NotAuthenticatedException;
use Zoomyboy\LaravelNami\Fakes\CourseFake; use Zoomyboy\LaravelNami\Fakes\CourseFake;
use Zoomyboy\LaravelNami\LoginException; use Zoomyboy\LaravelNami\LoginException;
@ -15,13 +16,13 @@ class CourseTest extends TestCase
{ {
app(CourseFake::class) app(CourseFake::class)
->fetches(11111, [788]) ->fetches(11111, [788])
->shows(11111, [ ->shows(11111, Course::factory()->toModel([
'bausteinId' => 506, 'bausteinId' => 506,
'id' => 788, 'id' => 788,
'veranstalter' => 'KJA', 'veranstalter' => 'KJA',
'vstgName' => 'eventname', 'vstgName' => 'eventname',
'vstgTag' => '2021-11-12 00:00:00', 'vstgTag' => '2021-11-12 00:00:00',
]); ]));
$course = $this->login()->coursesFor(11111)->first(); $course = $this->login()->coursesFor(11111)->first();
@ -39,8 +40,8 @@ class CourseTest extends TestCase
{ {
app(CourseFake::class) app(CourseFake::class)
->fetches(11111, [788, 789]) ->fetches(11111, [788, 789])
->shows(11111, ['id' => 788]) ->shows(11111, Course::factory()->id(788)->toModel())
->shows(11111, ['id' => 789]); ->shows(11111, Course::factory()->id(789)->toModel());
$courses = $this->login()->coursesFor(11111); $courses = $this->login()->coursesFor(11111);
@ -52,7 +53,7 @@ class CourseTest extends TestCase
app(CourseFake::class) app(CourseFake::class)
->fetches(11111, [788, 789]) ->fetches(11111, [788, 789])
->failsShowingWithHtml(11111, 788) ->failsShowingWithHtml(11111, 788)
->shows(11111, ['id' => 789]); ->shows(11111, Course::factory()->id(789)->toModel());
$courses = $this->login()->coursesFor(11111); $courses = $this->login()->coursesFor(11111);