adrema/tests/Feature/Initialize/InitializeTest.php

235 lines
7.2 KiB
PHP
Raw Normal View History

2021-11-17 22:32:52 +01:00
<?php
namespace Tests\Feature\Initialize;
use App\Activity;
use App\Country;
2021-11-18 01:54:27 +01:00
use App\Course;
2021-11-17 22:32:52 +01:00
use App\Gender;
2021-11-18 01:54:27 +01:00
use App\Member\Member;
2021-11-17 22:32:52 +01:00
use App\Nationality;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Facades\Http;
use Tests\TestCase;
use Zoomyboy\LaravelNami\Backend\FakeBackend;
class InitializeTest extends TestCase
{
use RefreshDatabase;
2021-11-17 23:41:04 +01:00
public function initializeProvider(callable $callback = null): void
2021-11-17 22:32:52 +01:00
{
2021-11-17 23:41:04 +01:00
$backend = app(FakeBackend::class)
2021-11-17 22:32:52 +01:00
->fakeLogin('123', [])
->addSearch(123, ['entries_vorname' => '::firstname::', 'entries_nachname' => '::lastname::', 'entries_gruppierungId' => 1000])
->fakeNationalities([['name' => 'deutsch', 'id' => 291]])
->fakeFees(1000, [['name' => 'Family', 'id' => 300]])
->fakeConfessions([['name' => 'Konf', 'id' => 301]])
->fakeCountries([['name' => 'Germany', 'id' => 302]])
->fakeGenders([['name' => 'Male', 'id' => 303]])
->fakeRegions([['name' => 'nrw', 'id' => 304]])
2021-11-18 01:54:27 +01:00
->fakeCourses([['name' => '1a', 'id' => 506]])
2021-11-17 23:41:04 +01:00
->fakeActivities(1000, [['name' => 'leiter', 'id' => 305]]);
if (!$callback) {
$backend->fakeMember([
2021-11-17 22:32:52 +01:00
'vorname' => '::firstname::',
'nachname' => '::lastname::',
'beitragsartId' => 300,
'geburtsDatum' => '2014-07-11 00:00:00',
'gruppierungId' => 1000,
'geschlechtId' => 303,
'id' => 411,
'eintrittsdatum' => '2020-11-17 00:00:00',
'geschlechtId' => 303,
'landId' => 302,
'staatsangehoerigkeitId' => 291,
'zeitschriftenversand' => true,
'strasse' => '::street',
'plz' => '12345',
'ort' => '::location::',
2021-11-18 00:09:37 +01:00
'gruppierung' => '::group::',
2021-11-17 22:32:52 +01:00
'version' => 40,
]);
2021-11-17 23:41:04 +01:00
} else {
$callback($backend);
}
$backend->fakeSubactivities([
305 => [['name' => 'wö', 'id' => 306]]
]);
}
2021-11-18 01:54:27 +01:00
public function testItInitializesAll(): void
2021-11-17 23:41:04 +01:00
{
$this->withoutExceptionHandling();
$this->initializeProvider();
2021-11-17 22:32:52 +01:00
$this->post('/login', [
'mglnr' => 123,
'password' => 'secret',
]);
$this->post('/initialize');
$this->assertDatabaseHas('regions', [
'name' => 'nrw',
'nami_id' => 304
]);
$this->assertDatabaseHas('genders', [
'name' => 'Male',
'nami_id' => 303
]);
$this->assertDatabaseHas('nationalities', [
'name' => 'deutsch',
'nami_id' => 291
]);
$this->assertDatabaseHas('fees', [
'name' => 'Family',
'nami_id' => 300
]);
$this->assertDatabaseHas('confessions', [
'name' => 'Konf',
'nami_id' => 301
]);
$this->assertDatabaseHas('countries', [
'name' => 'Germany',
'nami_id' => 302
]);
$this->assertDatabaseHas('activities', [
'name' => 'Leiter',
'nami_id' => 305
]);
2021-11-18 01:54:27 +01:00
$this->assertDatabaseHas('courses', [
'name' => '1a',
'nami_id' => 506
]);
2021-11-18 00:09:37 +01:00
$this->assertDatabaseHas('groups', ['nami_id' => 1000, 'name' => '::group::']);
2021-11-17 22:32:52 +01:00
$this->assertDatabaseHas('members', [
'nami_id' => 411,
'gender_id' => Gender::nami(303)->id,
'country_id' => Country::nami(302)->id,
'nationality_id' => Nationality::nami(291)->id,
'send_newspaper' => 1,
'address' => '::street',
'zip' => '12345',
'location' => '::location::',
'version' => 40,
]);
$this->assertEquals([306], Activity::where('nami_id', 305)->firstOrFail()->subactivities()->pluck('nami_id')->toArray());
2021-11-17 22:32:52 +01:00
2021-11-18 01:54:27 +01:00
Http::assertSentCount(15);
}
public function testSyncCoursesOfMember(): void
{
$this->withoutExceptionHandling();
$this->initializeProvider(function($backend) {
$backend->fakeMembers([
2021-11-18 02:01:55 +01:00
$this->member(['courses' => [ ['entries_baustein' => '1a', 'entries_id' => 788, 'entries_veranstalter' => 'KJA', 'entries_vstgName' => 'eventname', 'entries_vstgTag' => '2021-11-12 00:00:00'] ]])
2021-11-18 01:54:27 +01:00
]);
});
$this->post('/login', [
'mglnr' => 123,
'password' => 'secret',
]);
$this->post('/initialize');
$this->assertDatabaseHas('course_member', [
'member_id' => Member::where('firstname', '::firstname::')->firstOrFail()->id,
'course_id' => Course::where('name', '1a')->firstOrFail()->id,
'event_name' => 'eventname',
'completed_at' => '2021-11-12',
'organizer' => 'KJA',
'nami_id' => 788,
]);
2021-11-17 22:32:52 +01:00
}
2021-11-17 23:41:04 +01:00
public function testItDoesntGetMembersWithNoJoinedAtDate(): void
{
$this->withoutExceptionHandling();
$this->initializeProvider(function($backend) {
$backend->fakeMembers([$this->member(['eintrittsdatum' => null])]);
});
$this->post('/login', [
'mglnr' => 123,
'password' => 'secret',
]);
$this->post('/initialize');
$this->assertDatabaseCount('members', 0);
}
2021-11-17 23:41:04 +01:00
/**
* @return array<int, array<int, int>>
*/
public function pageProvider(): array
{
return [
[99],
[100],
[101],
[199],
[200],
[201],
];
}
/**
* @dataProvider pageProvider
*/
public function testItInitializesPages(int $num): void
2021-11-17 23:41:04 +01:00
{
$this->withoutExceptionHandling();
$this->initializeProvider(function($backend) use ($num) {
$members = collect([]);
foreach (range(1, $num) as $i) {
$members->push($this->member(['id' => $i]));
2021-11-17 23:41:04 +01:00
}
$backend->fakeMembers($members->toArray());
});
$this->post('/login', [
'mglnr' => 123,
'password' => 'secret',
]);
$this->post('/initialize');
$this->assertDatabaseCount('members', $num);
}
/**
* @param array<string, mixed> $overwrites
* @return array<string, mixed>
*/
private function member(array $overwrites): array
{
return array_merge([
'vorname' => '::firstname::',
'nachname' => '::lastname::',
'beitragsartId' => 300,
'geburtsDatum' => '2014-07-11 00:00:00',
'gruppierungId' => 1000,
'geschlechtId' => 303,
'id' => 116,
'eintrittsdatum' => '2020-11-17 00:00:00',
'geschlechtId' => 303,
'landId' => 302,
'staatsangehoerigkeitId' => 291,
'zeitschriftenversand' => true,
'strasse' => '::street',
'plz' => '12345',
'ort' => '::location::',
'version' => 40,
'gruppierung' => '::group::',
], $overwrites);
}
2021-11-17 22:32:52 +01:00
}