presseportale/tests/Feature/LegacyInvoiceArchiveCommandTest.php
Kevin Adametz 5b8bdf4182
Some checks are pending
linter / quality (push) Waiting to run
tests / ci (push) Waiting to run
12-05-2026 Frontend dev
2026-05-12 18:32:33 +02:00

249 lines
8.7 KiB
PHP

<?php
use App\Models\LegacyImportMap;
use App\Models\LegacyInvoice;
use App\Models\User;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Tests\TestCase;
test('legacy invoice archive imports full invoice payload and user mapping', function () {
/** @var TestCase $this */
configureLegacyInvoiceConnection();
$user = User::factory()->create();
LegacyImportMap::query()->create([
'legacy_portal' => 'presseecho',
'legacy_table' => 'sf_guard_user',
'legacy_id' => 501,
'target_table' => 'users',
'target_id' => $user->id,
'imported_at' => now(),
]);
insertLegacyInvoice([
'id' => 1001,
'user_id' => 501,
'user_payment_id' => 901,
'billing_address_id' => 701,
'number' => 'PE-1001',
'status' => 'paid',
'amount' => 119.00,
'invoice_date' => '2024-01-10',
'due_date' => '2024-01-24',
'payment_method' => 'SPK_Berlin',
'pay_date' => '2024-01-12',
]);
$this->artisan('legacy:archive-invoices', [
'--portal' => 'presseecho',
'--no-report' => true,
])
->assertSuccessful()
->expectsOutputToContain('[presseecho] Quelle: 1 | ohne User-Mapping: 0 | PDF-Payload: 1');
$invoice = LegacyInvoice::query()->where('legacy_id', 1001)->firstOrFail();
expect($invoice->user_id)->toBe($user->id)
->and($invoice->status)->toBe('paid')
->and($invoice->amount_cents)->toBe(11900)
->and(data_get($invoice->raw_snapshot, 'number'))->toBe('PE-1001')
->and(data_get($invoice->pdf_payload, 'billing_address.name'))->toBe('Muster GmbH')
->and(data_get($invoice->pdf_payload, 'user_payment.status'))->toBe('finished')
->and(data_get($invoice->pdf_payload, 'payment_option_translation.name'))->toBe('Legacy Pressepaket');
});
test('legacy invoice archive keeps unmapped invoices and reports them', function () {
/** @var TestCase $this */
configureLegacyInvoiceConnection();
insertLegacyInvoice([
'id' => 1002,
'user_id' => 999,
'user_payment_id' => 901,
'billing_address_id' => 701,
'number' => 'PE-1002',
'status' => 'open',
'amount' => 49.00,
'invoice_date' => '2024-02-10',
'due_date' => '2024-02-24',
'payment_method' => 'Invoice',
'pay_date' => '0000-00-00',
]);
$this->artisan('legacy:archive-invoices', [
'--portal' => 'presseecho',
'--no-report' => true,
])
->assertSuccessful()
->expectsOutputToContain('[presseecho] Quelle: 1 | ohne User-Mapping: 1 | PDF-Payload: 1');
$invoice = LegacyInvoice::query()->where('legacy_id', 1002)->firstOrFail();
expect($invoice->user_id)->toBeNull()
->and($invoice->legacy_user_id)->toBe(999)
->and($invoice->status)->toBe('open')
->and($invoice->paid_at)->toBeNull();
});
function configureLegacyInvoiceConnection(): void
{
Config::set('database.connections.mysql_presseecho', [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
'foreign_key_constraints' => false,
]);
DB::purge('mysql_presseecho');
Schema::connection('mysql_presseecho')->create('invoice', function (Blueprint $table): void {
$table->integer('id')->primary();
$table->integer('user_id');
$table->integer('user_payment_id');
$table->integer('billing_address_id');
$table->string('number');
$table->string('status');
$table->integer('reminder_count')->nullable();
$table->date('next_reminder_date')->nullable();
$table->decimal('amount', 10, 2);
$table->boolean('is_netto')->default(false);
$table->boolean('is_media')->default(true);
$table->date('invoice_date');
$table->date('due_date');
$table->date('service_period_begin_date')->nullable();
$table->date('service_period_end_date')->nullable();
$table->string('payment_method')->nullable();
$table->date('pay_date')->nullable();
$table->dateTime('created_at');
$table->dateTime('updated_at');
});
Schema::connection('mysql_presseecho')->create('invoice_billing_address', function (Blueprint $table): void {
$table->integer('id')->primary();
$table->integer('salutation_id')->nullable();
$table->string('title')->nullable();
$table->string('name');
$table->text('address')->nullable();
$table->string('address1')->nullable();
$table->string('address2')->nullable();
$table->string('postal_code')->nullable();
$table->string('city')->nullable();
$table->integer('country_id')->nullable();
$table->string('country_name')->nullable();
$table->dateTime('created_at');
$table->dateTime('updated_at');
});
Schema::connection('mysql_presseecho')->create('user_payment', function (Blueprint $table): void {
$table->integer('id')->primary();
$table->integer('user_payment_option_id');
$table->decimal('amount', 10, 2);
$table->string('status');
$table->dateTime('created_at');
$table->dateTime('updated_at');
});
Schema::connection('mysql_presseecho')->create('user_payment_option', function (Blueprint $table): void {
$table->integer('id')->primary();
$table->integer('user_id');
$table->integer('payment_option_id');
$table->integer('coupon_id')->nullable();
$table->string('status');
$table->date('valid_until_date')->nullable();
$table->date('next_due_date')->nullable();
$table->dateTime('created_at');
$table->dateTime('updated_at');
});
Schema::connection('mysql_presseecho')->create('payment_option', function (Blueprint $table): void {
$table->integer('id')->primary();
$table->string('article_number');
$table->string('type');
$table->decimal('price', 10, 2);
$table->boolean('is_hidden')->default(false);
$table->string('event_name_prefix')->nullable();
$table->boolean('activate_on_first_payment')->default(false);
});
Schema::connection('mysql_presseecho')->create('payment_option_translation', function (Blueprint $table): void {
$table->integer('id');
$table->string('name');
$table->text('description')->nullable();
$table->string('lang', 5);
});
DB::connection('mysql_presseecho')->table('invoice_billing_address')->insert([
'id' => 701,
'salutation_id' => 1,
'title' => null,
'name' => 'Muster GmbH',
'address' => 'Musterstrasse 1',
'address1' => null,
'address2' => null,
'postal_code' => '10115',
'city' => 'Berlin',
'country_id' => 177,
'country_name' => 'Deutschland',
'created_at' => '2024-01-01 00:00:00',
'updated_at' => '2024-01-01 00:00:00',
]);
DB::connection('mysql_presseecho')->table('user_payment')->insert([
'id' => 901,
'user_payment_option_id' => 801,
'amount' => 119.00,
'status' => 'finished',
'created_at' => '2024-01-01 00:00:00',
'updated_at' => '2024-01-01 00:00:00',
]);
DB::connection('mysql_presseecho')->table('user_payment_option')->insert([
'id' => 801,
'user_id' => 501,
'payment_option_id' => 601,
'coupon_id' => null,
'status' => 'finished',
'valid_until_date' => '2024-12-31',
'next_due_date' => null,
'created_at' => '2024-01-01 00:00:00',
'updated_at' => '2024-01-01 00:00:00',
]);
DB::connection('mysql_presseecho')->table('payment_option')->insert([
'id' => 601,
'article_number' => 'PK-LEGACY',
'type' => 'recurring',
'price' => 119.00,
'is_hidden' => false,
'event_name_prefix' => 'paymentoption.legacy',
'activate_on_first_payment' => true,
]);
DB::connection('mysql_presseecho')->table('payment_option_translation')->insert([
'id' => 601,
'name' => 'Legacy Pressepaket',
'description' => null,
'lang' => 'de',
]);
}
/**
* @param array<string, mixed> $overrides
*/
function insertLegacyInvoice(array $overrides): void
{
DB::connection('mysql_presseecho')->table('invoice')->insert(array_merge([
'reminder_count' => null,
'next_reminder_date' => null,
'is_netto' => false,
'is_media' => true,
'service_period_begin_date' => '2024-01-01',
'service_period_end_date' => '2024-12-31',
'created_at' => '2024-01-01 00:00:00',
'updated_at' => '2024-01-01 00:00:00',
], $overrides));
}