Files
laraundfelix/tests/Feature/OfferActivationParallelTest.php
T
LarsandCursor 7820bececb
Deploy Test / test (push) Failing after 10s
Deploy Test / deploy (push) Skipped
Paket 1: eine aktive Angebotsfassung und Bestellbezeichnung
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-16 06:54:43 +01:00

107 lines
4.0 KiB
PHP

<?php
namespace Tests\Feature;
use App\Domain\Catalog\ArticleService;
use App\Domain\Catalog\ContentService;
use App\Domain\Catalog\LicenseArticle;
use App\Domain\Catalog\Offer;
use App\Domain\Catalog\OfferService;
use App\Domain\Catalog\OfferStatus;
use App\Domain\Catalog\OfferVersion;
use App\Domain\Catalog\RightsProfileVersion;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Process;
use Illuminate\Support\Str;
use Tests\TestCase;
class OfferActivationParallelTest extends TestCase
{
private ?string $databasePath = null;
protected function setUp(): void
{
parent::setUp();
$this->databasePath = sys_get_temp_dir().DIRECTORY_SEPARATOR.'luf_parallel_'.Str::uuid()->toString().'.sqlite';
touch($this->databasePath);
config(['database.connections.sqlite.database' => $this->databasePath]);
DB::purge('sqlite');
DB::reconnect('sqlite');
Artisan::call('migrate', ['--force' => true]);
}
protected function tearDown(): void
{
if ($this->databasePath !== null && is_file($this->databasePath)) {
@unlink($this->databasePath);
}
config(['database.connections.sqlite.database' => ':memory:']);
DB::purge('sqlite');
parent::tearDown();
}
public function test_overlapping_activations_leave_exactly_one_active_version(): void
{
[$article, $rights, $offer] = $this->offerWithArticle();
$baseEnv = [
'APP_ENV' => 'testing',
'APP_KEY' => config('app.key'),
'DB_CONNECTION' => 'sqlite',
'DB_DATABASE' => $this->databasePath,
'CACHE_STORE' => 'array',
'QUEUE_CONNECTION' => 'sync',
'SESSION_DRIVER' => 'array',
];
$worker = base_path('tests/Support/offer_activate_worker.php');
$results = Process::pool(function ($pool) use ($baseEnv, $worker, $offer, $article, $rights): void {
$pool->as('a')
->path(base_path())
->env($baseEnv)
->timeout(120)
->command([PHP_BINARY, $worker, $this->databasePath, (string) $offer->id, (string) $article->id, (string) $rights->id, '1000', '80']);
$pool->as('b')
->path(base_path())
->env($baseEnv)
->timeout(120)
->command([PHP_BINARY, $worker, $this->databasePath, (string) $offer->id, (string) $article->id, (string) $rights->id, '2000', '0']);
})->start()->wait();
$this->assertTrue($results['a']->successful(), $results['a']->errorOutput());
$this->assertTrue($results['b']->successful(), $results['b']->errorOutput());
$active = OfferVersion::query()->where('offer_id', $offer->id)->where('status', OfferStatus::Aktiv)->get();
$this->assertCount(1, $active);
$versions = OfferVersion::query()->where('offer_id', $offer->id)->orderBy('number')->get();
$this->assertCount(2, $versions);
$this->assertSame(OfferStatus::Beendet, $versions->first()->status);
$this->assertContains($versions->last()->price_cents, [1000, 2000]);
$this->assertNotSame($versions->first()->price_cents, $versions->last()->price_cents);
}
/**
* @return array{0: LicenseArticle, 1: RightsProfileVersion, 2: Offer}
*/
private function offerWithArticle(): array
{
$content = app(ContentService::class);
$version = $content->publishElementVersion($content->createElement('text', 'text', 'Basis'), 'Basis');
$article = app(ArticleService::class)->publishRelease(
app(ArticleService::class)->create('parallel-basis'),
$version,
'Paralleltest.',
)->article->fresh();
$rights = app(OfferService::class)->createRightsProfile('Lesen', 'ehrenamtlich', ['text' => ['ansehen']]);
$offer = app(OfferService::class)->create('Parallelangebot');
return [$article, $rights, $offer];
}
}