Files
Toiminnanohjaus/tests/Feature/Modules/Machines/MachinesTest.php
2026-11-26 16:43:55 +02:00

144 lines
7.4 KiB
PHP

<?php
use App\Models\User;
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\ServiceUnitModel;
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\WorkTypeModel;
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\CustomerJobModel;
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\TaskModel;
use App\Modules\Machines\Infrastructure\Persistence\Eloquent\MachineCounterModel;
use App\Modules\Machines\Infrastructure\Persistence\Eloquent\MachineModel;
use App\Modules\Machines\Infrastructure\Persistence\Eloquent\MachineQueueEntryModel;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Livewire\Livewire;
uses(RefreshDatabase::class);
beforeEach(function () {
$this->travelTo(now()->setTimezone(config('app.business_timezone'))->setTime(10, 0));
$this->today = now(config('app.business_timezone'))->toDateString();
$this->manager = User::factory()->management()->create(['name' => 'Maija Johto']);
$this->employee = User::factory()->employee()->create(['name' => 'Eero Työntekijä']);
$this->print = ServiceUnitModel::query()->where('name', 'Painotuotanto')->sole();
$this->print->update(['uses_stock' => true]);
$this->printing = WorkTypeModel::query()->where('name', 'painotyö')->sole();
$this->printer = MachineModel::factory()->inUnits([$this->print->id])->create(['name' => 'Suurkuvatulostin']);
$this->cutter = MachineModel::factory()->inUnits([$this->print->id])->create(['name' => 'Tarraleikkuri', 'type' => 'cutter']);
$this->job = CustomerJobModel::factory()->create(['name' => 'Messut', 'status' => 'order', 'priority' => 'normal', 'instructions' => 'Kiiltävä laminointi']);
DB::table('customer_job_service_unit')->where('customer_job_id', $this->job->id)->update(['service_unit_id' => $this->print->id]);
$this->urgentJob = CustomerJobModel::factory()->create(['name' => 'Kiire', 'status' => 'order', 'priority' => 'urgent']);
DB::table('customer_job_service_unit')->where('customer_job_id', $this->urgentJob->id)->update(['service_unit_id' => $this->print->id]);
$this->banner = TaskModel::factory()->create(['customer_job_id' => $this->job->id, 'work_type_id' => $this->printing->id, 'title' => 'Banderolli']);
$this->poster = TaskModel::factory()->create(['customer_job_id' => $this->urgentJob->id, 'work_type_id' => $this->printing->id, 'title' => 'Juliste']);
});
function taskMachine(object $test, TaskModel $task): mixed
{
return Livewire::test('machines::task-machine', ['jobId' => $task->customer_job_id, 'taskId' => $task->id, 'taskTitle' => $task->title]);
}
it('adds a machine with the suggested counters of its type in units that use stock', function () {
$this->actingAs($this->manager);
$video = ServiceUnitModel::query()->where('name', 'Mediatuotanto')->sole();
Livewire::test('machines::form')
->assertSet('counters', fn (array $counters) => array_column($counters, 'name') === ['Tulosteet', 'Tulostettu pinta-ala'])
->set('name', 'Valokuvatulostin')
->set('serviceUnitIds', [(string) $this->print->id, (string) $video->id])
->call('save')
->assertHasNoErrors();
$machine = MachineModel::query()->where('name', 'Valokuvatulostin')->sole();
expect(DB::table('machine_service_units')->where('machine_id', $machine->id)->pluck('service_unit_id')->all())->toBe([$this->print->id])
->and(MachineCounterModel::query()->where('machine_id', $machine->id)->pluck('unit')->map->value->all())->toBe(['piece', 'square_metre']);
Livewire::test('machines::form')->set('name', 'Toinen')->set('serviceUnitIds', [])->call('save')->assertHasErrors('serviceUnitIds');
Livewire::test('machines::form')->set('name', 'suurkuvatulostin')->set('serviceUnitIds', [(string) $this->print->id])->call('save')->assertHasErrors('name');
$this->actingAs($this->employee);
$this->get(route('machines.create'))->assertForbidden();
$this->get(route('machines.index'))->assertOk()->assertSee('Suurkuvatulostin');
});
it('moves a task to a machine for a day and orders the day by priority', function () {
$this->actingAs($this->employee);
taskMachine($this, $this->banner)
->assertSee(__('machines.task.not_on_machine'))
->call('startEdit')
->assertSet('day', $this->today)
->set('machineId', (string) $this->printer->id)
->call('save')
->assertHasNoErrors()
->assertSee('Suurkuvatulostin');
taskMachine($this, $this->poster)->call('startEdit')->set('machineId', (string) $this->printer->id)->call('save');
Livewire::test('machines::show', ['machine' => $this->printer->id])
->assertSeeInOrder(['Juliste', 'Banderolli'])
->assertSee('Kiiltävä laminointi')
->assertSee('2 riviä.');
$banner = MachineQueueEntryModel::query()->where('task_id', $this->banner->id)->sole();
Livewire::test('machines::show', ['machine' => $this->printer->id])
->call('move', $banner->id, 'up')
->assertSet('message', 'Banderolli siirretty ylöspäin.')
->assertSeeInOrder(['Banderolli', 'Juliste'])
->call('move', $banner->id, 'up')
->assertSet('message', 'Banderolli on jo ensimmäisenä.');
});
it('moves a row to another day and machine, shows it late afterwards, and takes it off', function () {
$this->actingAs($this->employee);
$tomorrow = now(config('app.business_timezone'))->addDay()->toDateString();
taskMachine($this, $this->banner)->call('startEdit')->set('machineId', (string) $this->printer->id)->set('day', $tomorrow)->call('save');
Livewire::test('machines::show', ['machine' => $this->printer->id])
->assertDontSee('Banderolli')
->call('showDay', 'next')
->assertSee('Banderolli');
$entry = MachineQueueEntryModel::query()->sole();
$this->travel(2)->days();
Livewire::test('machines::show', ['machine' => $this->printer->id])
->assertSee('Banderolli')
->assertSee('Myöhässä (ajoitettu '.now()->subDay()->format('j.n.').')')
->call('startEdit', $entry->id)
->set('editMachineId', (string) $this->cutter->id)
->call('reschedule')
->assertHasNoErrors();
expect($entry->fresh()->machine_id)->toBe($this->cutter->id)
->and(substr($entry->fresh()->planned_on, 0, 10))->toBe(now(config('app.business_timezone'))->toDateString());
Livewire::test('machines::show', ['machine' => $this->cutter->id])->call('remove', $entry->id)->assertSee(__('machines.task.removed'));
expect(MachineQueueEntryModel::query()->count())->toBe(0);
});
it('takes a task that is marked done off the work list', function () {
$this->actingAs($this->employee);
taskMachine($this, $this->banner)->call('startEdit')->set('machineId', (string) $this->printer->id)->call('save');
Livewire::test('jobs::show', ['job' => $this->job->id])->call('changeTaskStatus', $this->banner->id, 'done');
expect(MachineQueueEntryModel::query()->sole()->completed_at)->not->toBeNull();
Livewire::test('machines::show', ['machine' => $this->printer->id])->assertDontSee('Banderolli');
});
it('does not offer machines for tasks of units without stock', function () {
$this->actingAs($this->employee);
$graphic = WorkTypeModel::query()->where('name', 'graafinen työ')->sole();
$task = TaskModel::factory()->create(['customer_job_id' => $this->job->id, 'work_type_id' => $graphic->id, 'title' => 'Taitto']);
taskMachine($this, $task)->assertDontSee(__('machines.task.title'))->call('startEdit')->assertForbidden();
});