Add Membership StoreTest
This commit is contained in:
parent
8d4985a4be
commit
b1b85f61b0
app/Membership/Requests
database/factories
tests/Feature/Membership
|
@ -0,0 +1,55 @@
|
|||
<?php
|
||||
|
||||
namespace App\Membership\Requests;
|
||||
|
||||
use App\Activity;
|
||||
use App\Member\Member;
|
||||
use App\Setting\NamiSettings;
|
||||
use App\Subactivity;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Zoomyboy\LaravelNami\Data\Membership;
|
||||
|
||||
class StoreRequest extends FormRequest
|
||||
{
|
||||
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function authorize()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
//
|
||||
];
|
||||
}
|
||||
|
||||
public function persist(Member $member, NamiSettings $settings): void
|
||||
{
|
||||
$from = now()->startOfDay();
|
||||
$namiId = $settings->login()->putMembership($member->nami_id, Membership::fromArray([
|
||||
'startsAt' => $from,
|
||||
'groupId' => $member->group->nami_id,
|
||||
'activityId' => Activity::find($this->input('activity_id'))->nami_id,
|
||||
'subactivityId' => optional(Subactivity::find($this->input('subactivity_id')))->nami_id,
|
||||
]));
|
||||
|
||||
$member->memberships()->create([
|
||||
...$this->input(),
|
||||
...['nami_id' => $namiId, 'group_id' => $member->group->id, 'from' => $from],
|
||||
]);
|
||||
|
||||
$member->syncVersion();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use App\Activity;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
||||
/**
|
||||
* @extends Factory<Activity>
|
||||
*/
|
||||
class ActivityFactory extends Factory
|
||||
{
|
||||
|
||||
protected $model = Activity::class;
|
||||
|
||||
/**
|
||||
* Define the model's default state.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function definition()
|
||||
{
|
||||
return [
|
||||
'name' => $this->faker->words(5, true),
|
||||
'nami_id' => $this->faker->randomNumber(),
|
||||
];
|
||||
}
|
||||
|
||||
public function inNami(int $namiId): self
|
||||
{
|
||||
return $this->state(['nami_id' => $namiId]);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use App\Subactivity;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
||||
/**
|
||||
* @extends Factory<Subactivity>
|
||||
*/
|
||||
class SubactivityFactory extends Factory
|
||||
{
|
||||
|
||||
protected $model = Subactivity::class;
|
||||
|
||||
/**
|
||||
* Define the model's default state.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function definition()
|
||||
{
|
||||
return [
|
||||
'name' => $this->faker->words(5, true),
|
||||
'nami_id' => $this->faker->randomNumber(),
|
||||
];
|
||||
}
|
||||
|
||||
public function inNami(int $namiId): self
|
||||
{
|
||||
return $this->state(['nami_id' => $namiId]);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,59 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Feature\Membership;
|
||||
|
||||
use App\Activity;
|
||||
use App\Group;
|
||||
use App\Member\Member;
|
||||
use App\Subactivity;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use Illuminate\Foundation\Testing\WithFaker;
|
||||
use Tests\TestCase;
|
||||
use Zoomyboy\LaravelNami\Fakes\MemberFake;
|
||||
use Zoomyboy\LaravelNami\Fakes\MembershipFake;
|
||||
|
||||
class StoreTest extends TestCase
|
||||
{
|
||||
|
||||
use DatabaseTransactions;
|
||||
|
||||
public function testItCanCreateAMembership(): void
|
||||
{
|
||||
Carbon::setTestNow(Carbon::parse('2022-02-03 03:00:00'));
|
||||
$this->withoutExceptionHandling()->login()->loginNami();
|
||||
app(MembershipFake::class)->createsSuccessfully(6, 133);
|
||||
app(MemberFake::class)->shows(1400, 6, ['version' => 1506]);
|
||||
$member = Member::factory()
|
||||
->defaults()
|
||||
->for(Group::factory()->inNami(1400))
|
||||
->inNami(6)
|
||||
->createOne();
|
||||
$activity = Activity::factory()
|
||||
->inNami(1)
|
||||
->hasAttached(Subactivity::factory()->inNami(2))
|
||||
->createOne();
|
||||
|
||||
$this->post("/member/{$member->id}/membership", [
|
||||
'activity_id' => $activity->id,
|
||||
'subactivity_id' => $activity->subactivities->first()->id,
|
||||
]);
|
||||
|
||||
$this->assertEquals(1506, $member->fresh()->version);
|
||||
$this->assertDatabaseHas('memberships', [
|
||||
'member_id' => $member->id,
|
||||
'activity_id' => $activity->id,
|
||||
'subactivity_id' => $activity->subactivities->first()->id,
|
||||
'nami_id' => 133,
|
||||
]);
|
||||
app(MembershipFake::class)->assertCreated(6, [
|
||||
'untergliederungId' => 2,
|
||||
'taetigkeitId' => 1,
|
||||
'gruppierungId' => 1400,
|
||||
'aktivVon' => '2022-02-03T00:00:00',
|
||||
'aktivBis' => null,
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue