76 lines
4.3 KiB
PHP
76 lines
4.3 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Ticket;
|
|
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use Tests\TestCase;
|
|
|
|
class ConvertValidityTimesToUtcTest extends TestCase
|
|
{
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
Schema::create('validity_times', function (Blueprint $table): void {
|
|
$table->id();
|
|
$table->string('type');
|
|
$table->time('start_time')->nullable();
|
|
$table->time('end_time')->nullable();
|
|
$table->dateTime('fixed_starts_at')->nullable();
|
|
$table->dateTime('fixed_expires_at')->nullable();
|
|
});
|
|
DB::table('validity_times')->insert([
|
|
['id' => 1, 'type' => 'fixed_window', 'fixed_starts_at' => '2026-09-21 07:00:00', 'fixed_expires_at' => '2026-09-23 23:59:00'],
|
|
['id' => 2, 'type' => 'fixed_window', 'fixed_starts_at' => null, 'fixed_expires_at' => '2026-09-24 23:59:00'],
|
|
]);
|
|
DB::table('validity_times')->insert(['id' => 3, 'type' => 'time_window', 'start_time' => '07:00:00', 'end_time' => '10:00:00']);
|
|
}
|
|
|
|
public function test_all_round_trip_restores_exact_values_across_dates_and_nulls(): void
|
|
{
|
|
$before = DB::table('validity_times')->orderBy('id')->get()->toJson();
|
|
$this->artisan('tickets:convert-validity-times-to-utc', ['--all' => true, '--apply' => true])->assertSuccessful();
|
|
$this->assertDatabaseHas('validity_times', ['id' => 1, 'fixed_starts_at' => '2026-09-21 10:00:00', 'fixed_expires_at' => '2026-09-24 02:59:00']);
|
|
$this->artisan('tickets:convert-validity-times-to-utc', ['--all' => true, '--reverse' => true, '--apply' => true])->assertSuccessful();
|
|
$this->assertSame($before, DB::table('validity_times')->orderBy('id')->get()->toJson());
|
|
}
|
|
|
|
public function test_reverse_preview_shows_local_result_without_writing(): void
|
|
{
|
|
$before = DB::table('validity_times')->orderBy('id')->get()->toJson();
|
|
$this->artisan('tickets:convert-validity-times-to-utc', ['--all' => true, '--reverse' => true])
|
|
->expectsOutputToContain('UTC -> America/Argentina/Buenos_Aires')
|
|
->expectsTable(['ID', 'Inicio original', 'Fin original', 'Inicio resultante', 'Fin resultante'], [
|
|
[1, '2026-09-21 07:00:00', '2026-09-23 23:59:00', '2026-09-21 04:00:00', '2026-09-23 20:59:00'],
|
|
[2, null, '2026-09-24 23:59:00', null, '2026-09-24 20:59:00'],
|
|
])->assertSuccessful();
|
|
$this->assertSame($before, DB::table('validity_times')->orderBy('id')->get()->toJson());
|
|
}
|
|
|
|
public function test_reverse_by_ids_uses_selected_timezone_and_preserves_other_rows(): void
|
|
{
|
|
$this->artisan('tickets:convert-validity-times-to-utc', ['--ids' => '1', '--reverse' => true, '--source-timezone' => 'Asia/Tokyo', '--apply' => true])->assertSuccessful();
|
|
$this->assertDatabaseHas('validity_times', ['id' => 1, 'fixed_starts_at' => '2026-09-21 16:00:00', 'fixed_expires_at' => '2026-09-24 08:59:00']);
|
|
$this->assertDatabaseHas('validity_times', ['id' => 2, 'fixed_expires_at' => '2026-09-24 23:59:00']);
|
|
$this->assertDatabaseHas('validity_times', ['id' => 3, 'start_time' => '07:00:00']);
|
|
}
|
|
|
|
public function test_invalid_scope_fails_without_changes(): void
|
|
{
|
|
$before = DB::table('validity_times')->orderBy('id')->get()->toJson();
|
|
foreach ([[], ['--ids' => '1', '--all' => true], ['--ids' => '1,999'], ['--ids' => '1,3'], ['--ids' => 'bad'], ['--all' => true, '--source-timezone' => 'bad']] as $options) {
|
|
$this->artisan('tickets:convert-validity-times-to-utc', $options + ['--reverse' => true, '--apply' => true])->assertFailed();
|
|
}
|
|
$this->assertSame($before, DB::table('validity_times')->orderBy('id')->get()->toJson());
|
|
}
|
|
|
|
public function test_reverse_failure_rolls_back_entire_operation(): void
|
|
{
|
|
DB::statement("CREATE TRIGGER fail_second BEFORE UPDATE ON validity_times WHEN OLD.id = 2 BEGIN SELECT RAISE(ABORT, 'test failure'); END");
|
|
$before = DB::table('validity_times')->orderBy('id')->get()->toJson();
|
|
$this->artisan('tickets:convert-validity-times-to-utc', ['--all' => true, '--reverse' => true, '--apply' => true])->assertFailed();
|
|
$this->assertSame($before, DB::table('validity_times')->orderBy('id')->get()->toJson());
|
|
}
|
|
}
|