adrema/tests/Feature/Member/UpdateTest.php

152 lines
5.5 KiB
PHP
Raw Normal View History

2022-02-11 01:24:09 +01:00
<?php
namespace Tests\Feature\Member;
use App\Confession;
use App\Country;
use App\Fee;
use App\Group;
use App\Member\Member;
use App\Nationality;
use App\Payment\Subscription;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Http;
use Tests\TestCase;
use Zoomyboy\LaravelNami\Backend\FakeBackend;
class UpdateTest extends TestCase
{
use DatabaseTransactions;
public function testItRedirectsToMemberOverview(): void
{
2022-02-19 18:08:29 +01:00
$this->withoutExceptionHandling()->login()->loginNami();
2022-02-11 01:24:09 +01:00
$member = $this->member();
$this->fakeRequest();
$response = $this
->from("/member/{$member->id}")
->patch("/member/{$member->id}", array_merge($member->getAttributes(), ['has_nami' => true]));
$response->assertRedirect('/member');
}
public function testItHasPutRequest(): void
{
2022-02-19 18:08:29 +01:00
$this->withoutExceptionHandling()->login()->loginNami();
2022-02-11 01:24:09 +01:00
$member = $this->member();
$this->fakeRequest();
$response = $this
->patch("/member/{$member->id}", array_merge($member->getAttributes(), ['has_nami' => true, 'firstname' => '::firstname::']));
2022-03-11 20:19:17 +01:00
Http::assertSent(fn ($request) => 'PUT' === $request->method()
&& '::firstname::' === $request['vorname']
2022-02-11 01:24:09 +01:00
);
}
public function testItMergesExistingData(): void
{
2022-02-19 18:08:29 +01:00
$this->withoutExceptionHandling()->login()->loginNami();
2022-02-11 01:24:09 +01:00
$member = $this->member();
$this->fakeRequest();
$response = $this
->from("/member/{$member->id}")
->patch("/member/{$member->id}", array_merge($member->getAttributes(), ['has_nami' => true, 'firstname' => '::firstname::']));
2022-03-11 20:19:17 +01:00
Http::assertSent(fn ($request) => 'PUT' === $request->method()
&& '{"a":"b"}' === $request['kontoverbindung']
&& 'missingvalue' === $request['missingkey']
&& '::firstname::' === $request['vorname']
2022-02-11 01:24:09 +01:00
);
}
public function testItChecksVersion(): void
{
$this->login()->loginNami();
$member = $this->member();
$member->update(['version' => 43]);
$this->fakeRequest();
$response = $this
->from("/member/{$member->id}")
->patch("/member/{$member->id}", array_merge($member->getAttributes(), ['has_nami' => true, 'firstname' => '::firstname::']));
$response->assertRedirect("/member/{$member->id}/edit?conflict=1");
}
2022-02-11 01:24:09 +01:00
public function testItUpdatesVersion(): void
{
2022-02-19 18:08:29 +01:00
$this->withoutExceptionHandling()->login()->loginNami();
2022-02-11 01:24:09 +01:00
$member = $this->member();
$this->fakeRequest();
$response = $this
->from("/member/{$member->id}")
->patch("/member/{$member->id}", array_merge($member->getAttributes(), ['has_nami' => true]));
$this->assertEquals(44, $member->fresh()->version);
}
2022-03-15 16:32:05 +01:00
public function testItUpdatesCriminalRecord(): void
{
$this->withoutExceptionHandling()->login()->loginNami();
$member = $this->member();
$this->fakeRequest();
$response = $this
->from("/member/{$member->id}")
2022-04-28 23:20:41 +02:00
->patch("/member/{$member->id}", array_merge($member->getAttributes(), [
'ps_at' => '2021-02-01',
'more_ps_at' => '2021-02-02',
'has_svk' => true,
'has_vk' => true,
'efz' => '2021-02-03',
'without_education_at' => '2021-02-04',
'without_efz_at' => '2021-02-05',
'has_nami' => true,
'multiply_pv' => true,
'multiply_more_pv' => true,
]));
$this->assertEquals('2021-02-01', $member->fresh()->ps_at);
$this->assertEquals('2021-02-02', $member->fresh()->more_ps_at);
$this->assertTrue($member->fresh()->has_svk);
$this->assertTrue($member->fresh()->has_vk);
$this->assertTrue($member->fresh()->multiply_pv);
$this->assertTrue($member->fresh()->multiply_more_pv);
2022-03-15 16:32:05 +01:00
$this->assertEquals('2021-02-03', $member->fresh()->efz);
2022-04-28 23:20:41 +02:00
$this->assertEquals('2021-02-04', $member->fresh()->without_education_at);
$this->assertEquals('2021-02-05', $member->fresh()->without_efz_at);
2022-03-15 16:32:05 +01:00
}
2022-02-11 01:24:09 +01:00
private function member(): Member
{
return Member::factory()
->for(Group::factory()->state(['nami_id' => 10]))
->for(Confession::factory())
->for(Nationality::factory())
->for(Subscription::factory()->for(Fee::factory()))
->for(Country::factory())
2022-03-11 20:19:17 +01:00
->create(['nami_id' => 135]);
2022-02-11 01:24:09 +01:00
}
private function fakeRequest(): void
{
Http::fake(function ($request) {
2022-03-11 20:19:17 +01:00
if ($request->url() === app(FakeBackend::class)->singleMemberUrl(10, 135) && 'GET' === $request->method()) {
2022-02-11 01:56:02 +01:00
return Http::response('{ "success": true, "data": {"missingkey": "missingvalue", "kontoverbindung": {"a": "b"} } }', 200);
2022-02-11 01:24:09 +01:00
}
if ($request->url() === app(FakeBackend::class)->singleMemberUrl(10, 135) && 'PUT' === $request->method() && 43 === $request['version']) {
return Http::response('{ "success": false, "message": "Update nicht möglich. Der Datensatz wurde zwischenzeitlich verändert." }', 200);
}
2022-03-11 20:19:17 +01:00
if ($request->url() === app(FakeBackend::class)->singleMemberUrl(10, 135) && 'PUT' === $request->method()) {
2022-02-11 01:24:09 +01:00
return Http::response('{ "success": true, "data": { "version": 44 } }', 200);
}
});
}
}