From a50a43b9ab6efbfda7773283c0928121593c12ba Mon Sep 17 00:00:00 2001 From: philipp lang Date: Wed, 17 Nov 2021 22:32:52 +0100 Subject: [PATCH] Fake initialize members --- app/Country.php | 4 +- app/Gender.php | 4 + app/Initialize/InitializeActivities.php | 8 +- app/Initialize/InitializeFees.php | 2 +- app/Initialize/InitializeJob.php | 6 +- app/Initialize/InitializeMembers.php | 14 +-- app/Nami/InitializeService.php | 17 ---- app/Nationality.php | 4 +- packages/laravel-nami | 2 +- phpstan.neon | 2 +- tests/Feature/Initialize/InitializeTest.php | 104 ++++++++++++++++++++ tests/Feature/LoginTest.php | 19 +++- 12 files changed, 146 insertions(+), 40 deletions(-) delete mode 100644 app/Nami/InitializeService.php create mode 100644 tests/Feature/Initialize/InitializeTest.php diff --git a/app/Country.php b/app/Country.php index 4590ef7e..8598def9 100644 --- a/app/Country.php +++ b/app/Country.php @@ -2,12 +2,14 @@ namespace App; -use Illuminate\Database\Eloquent\Model; +use App\Nami\HasNamiField; use Illuminate\Database\Eloquent\Factories\HasFactory; +use Illuminate\Database\Eloquent\Model; class Country extends Model { use HasFactory; + use HasNamiField; public $fillable = ['name', 'nami_id']; diff --git a/app/Gender.php b/app/Gender.php index 7b2c25eb..be09e6b7 100644 --- a/app/Gender.php +++ b/app/Gender.php @@ -2,9 +2,13 @@ namespace App; +use App\Nami\HasNamiField; use Illuminate\Database\Eloquent\Model; class Gender extends Model { + + use HasNamiField; + public $fillable = ['name', 'nami_id']; } diff --git a/app/Initialize/InitializeActivities.php b/app/Initialize/InitializeActivities.php index 4bf2d3f7..5a0fecdb 100644 --- a/app/Initialize/InitializeActivities.php +++ b/app/Initialize/InitializeActivities.php @@ -23,17 +23,17 @@ class InitializeActivities { public function handle() { $this->bar->task('Synchronisiere Tätigkeiten', function() { - $this->api->group(auth()->user()->getNamiGroupId())->activities()->each(function($activity) { + $this->api->activities(auth()->user()->getNamiGroupId())->each(function($activity) { $activity = \App\Activity::create([ 'nami_id' => $activity->id, 'name' => $activity->name, - 'is_try' => in_array($group->name, $this->tries), - 'is_member' => in_array($group->name, $this->members), + 'is_try' => in_array($activity->name, $this->tries), + 'is_member' => in_array($activity->name, $this->members), ]); $groups = []; - $this->api->subactivitiesOf($activity->id)->each(function($group) use ($activity, &$groups) { + $this->api->subactivitiesOf($activity->nami_id)->each(function($group) use ($activity, &$groups) { $group = \App\Subactivity::updateOrCreate(['nami_id' => $group->id], [ 'nami_id' => $group->id, 'name' => $group->name, diff --git a/app/Initialize/InitializeFees.php b/app/Initialize/InitializeFees.php index dd598ca1..18bd058c 100644 --- a/app/Initialize/InitializeFees.php +++ b/app/Initialize/InitializeFees.php @@ -14,7 +14,7 @@ class InitializeFees { public function handle() { $this->bar->task('Synchronisiere Beiträge', function() { - $this->api->group(auth()->user()->getNamiGroupId())->fees()->each(function($fee) { + $this->api->feesOf(auth()->user()->getNamiGroupId())->each(function($fee) { \App\Fee::create(['nami_id' => $fee->id, 'name' => $fee->name]) ->subscriptions()->create([ 'name' => $fee->name, diff --git a/app/Initialize/InitializeJob.php b/app/Initialize/InitializeJob.php index 9500bc19..f6d6594f 100644 --- a/app/Initialize/InitializeJob.php +++ b/app/Initialize/InitializeJob.php @@ -3,12 +3,12 @@ namespace App\Initialize; use App\Member; -use Illuminate\Bus\Queueable; -use Illuminate\Queue\SerializesModels; use Aweos\Agnoster\Progress\HasProgress; -use Illuminate\Queue\InteractsWithQueue; +use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; +use Illuminate\Queue\InteractsWithQueue; +use Illuminate\Queue\SerializesModels; use Zoomyboy\LaravelNami\NamiUser; class InitializeJob implements ShouldQueue diff --git a/app/Initialize/InitializeMembers.php b/app/Initialize/InitializeMembers.php index fc851144..ae9fb9c5 100644 --- a/app/Initialize/InitializeMembers.php +++ b/app/Initialize/InitializeMembers.php @@ -2,15 +2,16 @@ namespace App\Initialize; -use App\Gender; +use App\Activity; use App\Confession; use App\Country; -use App\Member\Member; -use App\Region; -use App\Nationality; use App\Fee; +use App\Gender; use App\Group; -use App\Activity; +use App\Member\Member; +use App\Nationality; +use App\Region; +use Zoomyboy\LaravelNami\Member as NamiMember; class InitializeMembers { @@ -35,7 +36,8 @@ class InitializeMembers { $allMembers = collect([]); $this->bar->task('Synchronisiere Mitglieder', function() { - $this->api->group(auth()->user()->getNamiGroupId())->members()->each(function($member) { + $this->api->search([])->each(function($member) { + $member = NamiMember::fromNami($this->api->member($member->group_id, $member->id)); $m = Member::create([ 'firstname' => $member->firstname, 'lastname' => $member->lastname, diff --git a/app/Nami/InitializeService.php b/app/Nami/InitializeService.php deleted file mode 100644 index 9c2e7d89..00000000 --- a/app/Nami/InitializeService.php +++ /dev/null @@ -1,17 +0,0 @@ -api = $api; - } - - public function handle() { - dd(auth()->user()->getNamiService()->fees()); - } - -} diff --git a/app/Nationality.php b/app/Nationality.php index c4cee070..d4b46d85 100644 --- a/app/Nationality.php +++ b/app/Nationality.php @@ -2,12 +2,14 @@ namespace App; -use Illuminate\Database\Eloquent\Model; +use App\Nami\HasNamiField; use Illuminate\Database\Eloquent\Factories\HasFactory; +use Illuminate\Database\Eloquent\Model; class Nationality extends Model { use HasFactory; + use HasNamiField; public $fillable = ['name','nami_id']; } diff --git a/packages/laravel-nami b/packages/laravel-nami index 07595f68..a39c8513 160000 --- a/packages/laravel-nami +++ b/packages/laravel-nami @@ -1 +1 @@ -Subproject commit 07595f681d282e6dc06bd57adbefdf04d6b8c1f5 +Subproject commit a39c8513c9c1ac65f94e1010a45124054da5d0b0 diff --git a/phpstan.neon b/phpstan.neon index c6ef9db8..178cb565 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -7,7 +7,7 @@ parameters: - app # The level 8 is the highest level - level: 6 + level: 8 ignoreErrors: - '#Unsafe usage of new static#' diff --git a/tests/Feature/Initialize/InitializeTest.php b/tests/Feature/Initialize/InitializeTest.php new file mode 100644 index 00000000..0398bce8 --- /dev/null +++ b/tests/Feature/Initialize/InitializeTest.php @@ -0,0 +1,104 @@ +withoutExceptionHandling(); + app(FakeBackend::class) + ->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]]) + ->fakeActivities(1000, [['name' => 'leiter', 'id' => 305]]) + ->fakeMember([ + '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::', + 'version' => 40, + ]) + ->fakeSubactivities([ + 305 => [['name' => 'wö', 'id' => 306]] + ]); + $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 + ]); + $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::firstWhere('nami_id', 305)->subactivities()->pluck('nami_id')->toArray()); + + Http::assertSentCount(13); + } +} diff --git a/tests/Feature/LoginTest.php b/tests/Feature/LoginTest.php index de398844..1ed46b9e 100644 --- a/tests/Feature/LoginTest.php +++ b/tests/Feature/LoginTest.php @@ -19,7 +19,9 @@ class LoginTest extends TestCase public function testItCanLoginWithANamiAccount() { $this->withoutExceptionHandling(); - app('nami.backend')->fakeLogin(123, [], 'cookie-123'); + app('nami.backend') + ->fakeLogin(123, [], 'cookie-123') + ->addSearch(123, ['entries_vorname' => '::firstname::', 'entries_nachname' => '::lastname::', 'entries_gruppierungId' => 1000]); $this->post('/login', [ 'mglnr' => 123, @@ -29,6 +31,9 @@ class LoginTest extends TestCase $key = session()->get('auth_key'); $cache = Cache::get("namiauth-{$key}"); $this->assertEquals('secret', data_get($cache, 'credentials.password')); + $this->assertEquals('::firstname::', data_get($cache, 'firstname')); + $this->assertEquals('::lastname::', data_get($cache, 'lastname')); + $this->assertEquals(1000, data_get($cache, 'group_id')); $this->assertEquals(123, data_get($cache, 'credentials.mglnr')); $this->assertTrue(auth()->check()); } @@ -36,7 +41,9 @@ class LoginTest extends TestCase public function testItDoesntLoginTwoTimes() { $this->withoutExceptionHandling(); - app('nami.backend')->fakeLogin(123, [], 'cookie-123'); + app('nami.backend') + ->fakeLogin(123, [], 'cookie-123') + ->addSearch(123, ['entries_vorname' => '::firstname::', 'entries_nachname' => '::lastname::', 'entries_gruppierungId' => 1000]); $this->post('/login', [ 'mglnr' => 123, @@ -50,13 +57,15 @@ class LoginTest extends TestCase $this->assertTrue(auth()->check()); - Http::assertSentCount(2); + Http::assertSentCount(4); } public function testItResolvesTheLoginFromTheCache() { $this->withoutExceptionHandling(); - app('nami.backend')->fakeLogin(123, [], 'cookie-123'); + app('nami.backend') + ->fakeLogin(123, [], 'cookie-123') + ->addSearch(123, ['entries_vorname' => '::firstname::', 'entries_nachname' => '::lastname::', 'entries_gruppierungId' => 1000]); $this->post('/login', [ 'mglnr' => 123, @@ -70,7 +79,7 @@ class LoginTest extends TestCase $this->assertTrue(auth()->check()); - Http::assertSentCount(2); + Http::assertSentCount(3); } public function testItThrowsExceptionWhenLoginFailed()