tests: gemeinsamer Testhelfer, DB_URL-Guard vor RefreshDatabase, Larastan voller Scope ohne Meldung (Umsetzungsaudit 29.09.: Q-01, Q-02; 02.10.: O02)

- makeCustomerForShowPhase8a() nach tests/Pest.php — die Suite läuft damit
  auch parallel (--parallel) und dateiweise grün
- phpunit.xml leert DB_URL, der Guard in TestCase::setUpTraits() bricht bei
  gesetzter url ab, bevor RefreshDatabase eine Verbindung öffnet
- HasUniqueSlug ohne widersprüchliche @var-Annotation

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Kevin Adametz 2026-10-02 13:09:00 +00:00
parent 80536ab998
commit 3dbc3fa673
6 changed files with 74 additions and 34 deletions

View file

@ -86,10 +86,9 @@ trait HasUniqueSlug
*/
protected function slugExists(string $slug, array $scope): bool
{
/** @var Model $self */
$self = $this;
$query = $self::query()
$query = static::query()
->withoutGlobalScopes()
->where($this->slugColumn(), $slug);

View file

@ -31,6 +31,9 @@
<env name="DB_DATABASE" value=":memory:" force="true"/>
<server name="DB_CONNECTION" value="sqlite" force="true"/>
<server name="DB_DATABASE" value=":memory:" force="true"/>
<!-- DB_URL würde Treiber und Datenbank beim Verbindungsaufbau überschreiben (Audit 02.10.2026, O02). -->
<env name="DB_URL" value="" force="true"/>
<server name="DB_URL" value="" force="true"/>
<env name="MAIL_MAILER" value="array"/>
<env name="PULSE_ENABLED" value="false"/>
<env name="QUEUE_CONNECTION" value="sync"/>

View file

@ -4,9 +4,6 @@ use App\Enums\Portal;
use App\Enums\PressReleaseClassification;
use App\Enums\PressReleaseContentTier;
use App\Enums\PressReleaseStatus;
use App\Models\Category;
use App\Models\Company;
use App\Models\Contact;
use App\Models\PressRelease;
use App\Models\User;
use Database\Seeders\RolesAndPermissionsSeeder;
@ -18,35 +15,6 @@ beforeEach(function (): void {
$this->seed(RolesAndPermissionsSeeder::class);
});
/**
* @return array{customer: User, company: Company, contact: Contact, category: Category, pr: PressRelease}
*/
function makeCustomerForShowPhase8a(array $prAttributes = []): array
{
$customer = User::factory()->create(['is_active' => true]);
$customer->assignRole('customer');
$company = Company::factory()->presseecho()->create();
$customer->companies()->attach($company->id, ['role' => 'owner']);
$contact = Contact::factory()->for($company)->create([
'portal' => $company->portal->value,
]);
$category = Category::factory()->create();
$pr = PressRelease::factory()->create(array_merge([
'user_id' => $customer->id,
'company_id' => $company->id,
'category_id' => $category->id,
'portal' => $company->portal->value,
'status' => 'draft',
], $prAttributes));
$pr->contacts()->sync([$contact->id]);
return compact('customer', 'company', 'contact', 'category', 'pr');
}
function makeAdminForShowPhase8a(): User
{
$admin = User::factory()->create(['is_active' => true]);

View file

@ -33,3 +33,24 @@ test('the database guard rejects anything but sqlite in memory', function () {
config()->set('database.default', 'sqlite');
}
});
test('the database guard rejects a url override on the sqlite connection', function () {
/** @var TestCase $this */
$guard = fn () => $this->assertTestDatabaseIsInMemorySqlite();
// Laravel löst `url` erst beim Verbindungsaufbau auf und überschreibt
// damit Treiber und Datenbank — die rohen sqlite/:memory:-Werte allein
// beweisen nichts (Umsetzungsaudit 02.10.2026, O02). Nur Config, keine
// Verbindung: der Guard muss vorher abbrechen.
try {
config()->set('database.connections.sqlite.url', 'mysql://root:secret@127.0.0.1:3306/pressekonto');
expect($guard)->toThrow(RuntimeException::class, 'DB_URL');
} finally {
config()->set('database.connections.sqlite.url', null);
}
});
test('phpunit neutralises DB_URL for the whole run', function () {
expect(config('database.connections.sqlite.url'))->toBeEmpty();
});

View file

@ -1,6 +1,10 @@
<?php
use App\Models\Category;
use App\Models\Company;
use App\Models\Contact;
use App\Models\Plan;
use App\Models\PressRelease;
use App\Models\User;
use App\Services\CurrentPortalContext;
use App\Support\WebPageCache;
@ -86,3 +90,38 @@ function webPageCacheKey(string $key): string
{
return 'web.g'.WebPageCache::generation().'.'.substr($key, strlen('web.'));
}
/**
* Kunde (Rolle customer, Owner einer Presseecho-Firma) mit Kontakt, Kategorie
* und einer Entwurfs-PM. Gemeinsam genutzt von den Phase-8a-/8i-Tests und dem
* Widerrufsverzicht-Test — deshalb hier und nicht in einer einzelnen Testdatei,
* sonst scheitern die Nutzer im Einzel- oder Parallellauf.
*
* @param array<string, mixed> $prAttributes
* @return array{customer: User, company: Company, contact: Contact, category: Category, pr: PressRelease}
*/
function makeCustomerForShowPhase8a(array $prAttributes = []): array
{
$customer = User::factory()->create(['is_active' => true]);
$customer->assignRole('customer');
$company = Company::factory()->presseecho()->create();
$customer->companies()->attach($company->id, ['role' => 'owner']);
$contact = Contact::factory()->for($company)->create([
'portal' => $company->portal->value,
]);
$category = Category::factory()->create();
$pr = PressRelease::factory()->create(array_merge([
'user_id' => $customer->id,
'company_id' => $company->id,
'category_id' => $category->id,
'portal' => $company->portal->value,
'status' => 'draft',
], $prAttributes));
$pr->contacts()->sync([$contact->id]);
return compact('customer', 'company', 'contact', 'category', 'pr');
}

View file

@ -44,6 +44,16 @@ abstract class TestCase extends BaseTestCase
$default = (string) DB::getDefaultConnection();
$connection = (array) config('database.connections.'.$default, []);
// Eine gesetzte `url` überschreibt Treiber und Datenbank erst beim
// Verbindungsaufbau — dann wären die geprüften Rohwerte wertlos
// (Umsetzungsaudit 02.10.2026, O02).
if (! empty($connection['url'])) {
throw new RuntimeException(sprintf(
'Tests laufen mit DB_URL-Override auf „%s“ — die Verbindung würde nicht SQLite :memory: sein. DB_URL für Tests leeren.',
$default,
));
}
if ($default !== 'sqlite' || ($connection['driver'] ?? null) !== 'sqlite' || ($connection['database'] ?? null) !== ':memory:') {
throw new RuntimeException(sprintf(
'Tests laufen gegen „%s“ (%s) statt SQLite :memory: — vermutlich liegt ein Config-Cache. `php artisan config:clear` und erneut starten.',