Add json test for course and membership

This commit is contained in:
philipp lang 2023-02-13 01:01:15 +01:00
parent e09a5eade5
commit 927bf7a09c
7 changed files with 118 additions and 5 deletions

View File

@ -5,9 +5,11 @@ namespace Zoomyboy\LaravelNami\Data;
use Carbon\Carbon;
use Spatie\LaravelData\Attributes\MapInputName;
use Spatie\LaravelData\Attributes\WithCast;
use Spatie\LaravelData\Attributes\WithTransformer;
use Spatie\LaravelData\Data;
use Zoomyboy\LaravelNami\Casters\CarbonCast;
use Zoomyboy\LaravelNami\Tests\Factories\CourseFactory;
use Zoomyboy\LaravelNami\Transformers\CarbonTransformer;
class Course extends Data
{
@ -25,6 +27,7 @@ class Course extends Data
#[MapInputName('vstgTag')]
#[WithCast(CarbonCast::class, format: 'Y-m-d H:i:s')]
#[WithTransformer(CarbonTransformer::class, format: 'Y-m-d H:i:s')]
public Carbon $completedAt,
) {
}

View File

@ -5,9 +5,12 @@ namespace Zoomyboy\LaravelNami\Data;
use Carbon\Carbon;
use Spatie\LaravelData\Attributes\MapInputName;
use Spatie\LaravelData\Attributes\WithCast;
use Spatie\LaravelData\Attributes\WithTransformer;
use Spatie\LaravelData\Data;
use Zoomyboy\LaravelNami\Casters\CarbonCast;
use Zoomyboy\LaravelNami\Casters\StringCast;
use Zoomyboy\LaravelNami\Tests\Factories\MembershipEntryFactory;
use Zoomyboy\LaravelNami\Transformers\CarbonTransformer;
class MembershipEntry extends Data
{
@ -19,10 +22,12 @@ class MembershipEntry extends Data
#[MapInputName('entries_aktivVon')]
#[WithCast(CarbonCast::class, format: 'Y-m-d H:i:s')]
#[WithTransformer(CarbonTransformer::class, format: 'Y-m-d H:i:s')]
public Carbon $startsAt,
#[MapInputName('entries_aktivBis')]
#[WithCast(CarbonCast::class, format: 'Y-m-d H:i:s')]
#[WithTransformer(CarbonTransformer::class, format: 'Y-m-d H:i:s')]
public ?Carbon $endsAt,
#[MapInputName('entries_taetigkeit')]
@ -33,4 +38,9 @@ class MembershipEntry extends Data
public ?string $subactivity,
) {
}
public static function factory(): MembershipEntryFactory
{
return MembershipEntryFactory::new();
}
}

View File

@ -22,7 +22,7 @@ class CourseFactory extends RequestFactory
/**
* @param array<string, mixed> $attributes
*/
public function toModel(array $attributes = []): Course
public function toCourse(array $attributes = []): Course
{
return Course::from($this->create($attributes));
}

View File

@ -0,0 +1,34 @@
<?php
namespace Zoomyboy\LaravelNami\Tests\Factories;
use Worksome\RequestFactories\RequestFactory;
use Zoomyboy\LaravelNami\Data\MembershipEntry;
class MembershipEntryFactory extends RequestFactory
{
public function definition(): array
{
return [
'id' => $this->faker->numberBetween(100, 200),
'group' => $this->faker->word(),
'startsAt' => $this->faker->dateTime()->format('Y-m-d').' 00:00:00',
'endsAt' => $this->faker->dateTime()->format('Y-m-d').' 00:00:00',
'activity' => $this->faker->word(),
'subactivity' => $this->faker->word(),
];
}
/**
* @param array<string, mixed> $attributes
*/
public function toMembership(array $attributes = []): MembershipEntry
{
return MembershipEntry::from($this->create($attributes));
}
public function id(int $id): self
{
return $this->state(['id' => $id]);
}
}

View File

@ -0,0 +1,33 @@
<?php
namespace Zoomyboy\LaravelNami\Tests\Unit;
use Carbon\Carbon;
use ReflectionClass;
use Zoomyboy\LaravelNami\Data\Course;
use Zoomyboy\LaravelNami\Tests\TestCase;
class CourseFactoryTest extends TestCase
{
public function testItCanRenderACourseAsJson(): void
{
$course = Course::factory()->toCourse();
$json = $course->toArray();
$data = json_decode(json_encode($json));
$newCourse = Course::from($data);
foreach ((new ReflectionClass(Course::class))->getMethod('__construct')->getParameters() as $parameter) {
$name = $parameter->getName();
if (!is_object($course->{$name})) {
$this->assertSame($newCourse->{$name}, $course->{$name});
}
if (is_a($course->{$name}, Carbon::class)) {
$this->assertSame($course->{$name}->toDateTimeString(), $newCourse->{$name}->toDateTimeString());
}
}
}
}

View File

@ -17,7 +17,7 @@ class CourseTest extends TestCase
{
app(CourseFake::class)
->fetches(11111, [788])
->shows(11111, Course::factory()->toModel([
->shows(11111, Course::factory()->toCourse([
'bausteinId' => 506,
'id' => 788,
'veranstalter' => 'KJA',
@ -41,8 +41,8 @@ class CourseTest extends TestCase
{
app(CourseFake::class)
->fetches(11111, [788, 789])
->shows(11111, Course::factory()->id(788)->toModel())
->shows(11111, Course::factory()->id(789)->toModel());
->shows(11111, Course::factory()->id(788)->toCourse())
->shows(11111, Course::factory()->id(789)->toCourse());
$courses = $this->login()->coursesOf(11111);
@ -54,7 +54,7 @@ class CourseTest extends TestCase
app(CourseFake::class)
->fetches(11111, [788, 789])
->failsShowingWithHtml(11111, 788)
->shows(11111, Course::factory()->id(789)->toModel());
->shows(11111, Course::factory()->id(789)->toCourse());
$courses = $this->login()->coursesOf(11111);

View File

@ -0,0 +1,33 @@
<?php
namespace Zoomyboy\LaravelNami\Tests\Unit;
use Carbon\Carbon;
use ReflectionClass;
use Zoomyboy\LaravelNami\Data\MembershipEntry;
use Zoomyboy\LaravelNami\Tests\TestCase;
class MembershipEntryFactoryTest extends TestCase
{
public function testItCanRenderACourseAsJson(): void
{
$membership = MembershipEntry::factory()->toMembership();
$json = $membership->toArray();
$data = json_decode(json_encode($json));
$newMembership = MembershipEntry::from($data);
foreach ((new ReflectionClass(MembershipEntry::class))->getMethod('__construct')->getParameters() as $parameter) {
$name = $parameter->getName();
if (!is_object($membership->{$name})) {
$this->assertSame($newMembership->{$name}, $membership->{$name});
}
if (is_a($membership->{$name}, Carbon::class)) {
$this->assertSame($membership->{$name}->toDateTimeString(), $newMembership->{$name}->toDateTimeString());
}
}
}
}