Add catalog module for service units, services and work types
Phase 1, item 3: the hierarchy customer jobs, tasks and time entries will refer to. - New Catalog module: service unit > optional service > work type. Names are unique per level (case-insensitive); items are never deleted, only deactivated, and foreign keys restrict deletes. - Pohjatiedot page (/catalog, admins and management): add, rename, reorder with move up/down buttons (keyboard-friendly, focus kept and announced) and an "in use" checkbox per row. Work types can move to another service of the same unit. - A migration inserts the starting catalog from the specification when the tables are empty, so every installation has it. - CatalogQueries gives other modules the active service units and the work types of chosen units plus the shared ones. - Spec and CLAUDE.md updated. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
use App\Models\User;
|
||||
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\ServiceUnitModel;
|
||||
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\WorkTypeModel;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
beforeEach(function () {
|
||||
$this->actingAs(User::factory()->management()->create());
|
||||
});
|
||||
|
||||
it('has no accessibility issues on the catalog pages', function (string $page, string $device, string $colorScheme) {
|
||||
$media = ServiceUnitModel::query()->where('name', 'Mediatuotanto')->sole();
|
||||
WorkTypeModel::query()->where('name', 'animointi')->update(['is_active' => false]);
|
||||
$workType = WorkTypeModel::query()->where('name', 'videokuvaus')->sole();
|
||||
|
||||
$url = match ($page) {
|
||||
'catalog' => route('catalog.index'),
|
||||
'new service unit' => route('catalog.service-units.create'),
|
||||
'new service' => route('catalog.services.create', $media->id),
|
||||
'edit work type' => route('catalog.work-types.edit', $workType->id),
|
||||
};
|
||||
|
||||
$visit = visit($url);
|
||||
$visit = $device === 'mobile' ? $visit->on()->mobile() : $visit->on()->desktop();
|
||||
$visit = $colorScheme === 'dark' ? $visit->inDarkMode() : $visit->inLightMode();
|
||||
|
||||
$visit->assertNoJavaScriptErrors()
|
||||
->assertNoAccessibilityIssues(level: 2);
|
||||
})->with(['catalog', 'new service unit', 'new service', 'edit work type'])->with([
|
||||
'desktop, light' => ['desktop', 'light'],
|
||||
'desktop, dark' => ['desktop', 'dark'],
|
||||
'mobile, light' => ['mobile', 'light'],
|
||||
'mobile, dark' => ['mobile', 'dark'],
|
||||
]);
|
||||
|
||||
it('moves a row with the keyboard, keeping focus and announcing it', function () {
|
||||
$marketing = ServiceUnitModel::query()->where('name', 'Markkinointi')->sole();
|
||||
$button = '#move-up-service_unit-'.$marketing->id;
|
||||
|
||||
visit(route('catalog.index'))
|
||||
->keys($button, 'Enter')
|
||||
->assertSee('Markkinointi siirretty ylöspäin.')
|
||||
->assertScript('document.activeElement.id', 'move-up-service_unit-'.$marketing->id)
|
||||
->assertScript('document.querySelector("section h2").textContent.trim()', 'Markkinointi');
|
||||
});
|
||||
|
||||
it('takes a row out of use with the keyboard', function () {
|
||||
$workType = WorkTypeModel::query()->where('name', 'painotyö')->sole();
|
||||
|
||||
visit(route('catalog.index'))
|
||||
->keys('#active-work_type-'.$workType->id, 'Space')
|
||||
->assertSee('painotyö poistettu käytöstä.')
|
||||
->assertSee('(Ei käytössä)')
|
||||
->assertNoAccessibilityIssues(level: 2);
|
||||
});
|
||||
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Fakes;
|
||||
|
||||
use App\Modules\Catalog\Domain\Contracts\ServiceRepository;
|
||||
use App\Modules\Catalog\Domain\Entities\CatalogItem;
|
||||
use App\Modules\Catalog\Domain\Entities\Service;
|
||||
use App\Modules\Catalog\Domain\Enums\MoveDirection;
|
||||
|
||||
class InMemoryServiceRepository implements ServiceRepository
|
||||
{
|
||||
/** @var array<int, Service> */
|
||||
public array $items = [];
|
||||
|
||||
private int $nextId = 1;
|
||||
|
||||
public function findById(int $id): ?Service
|
||||
{
|
||||
return isset($this->items[$id]) ? clone $this->items[$id] : null;
|
||||
}
|
||||
|
||||
public function save(Service $service): Service
|
||||
{
|
||||
$id = $service->id() ?? $this->nextId++;
|
||||
$this->items[$id] = Service::reconstitute($id, $service->serviceUnitId(), $service->name(), $service->isActive(), $service->sortOrder());
|
||||
|
||||
return clone $this->items[$id];
|
||||
}
|
||||
|
||||
public function isNameTaken(int $serviceUnitId, string $name, ?int $exceptId = null): bool
|
||||
{
|
||||
return self::nameTakenIn($this->siblings($serviceUnitId), $name, $exceptId);
|
||||
}
|
||||
|
||||
public function nextSortOrder(int $serviceUnitId): int
|
||||
{
|
||||
return self::nextIn($this->siblings($serviceUnitId));
|
||||
}
|
||||
|
||||
public function neighbour(Service $service, MoveDirection $direction): ?Service
|
||||
{
|
||||
return self::neighbourIn($this->siblings($service->serviceUnitId()), $service, $direction);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, Service>
|
||||
*/
|
||||
private function siblings(int $serviceUnitId): array
|
||||
{
|
||||
return array_filter($this->items, fn (Service $service): bool => $service->serviceUnitId() === $serviceUnitId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @template T of CatalogItem
|
||||
*
|
||||
* @param array<int, T> $siblings
|
||||
* @return T|null
|
||||
*/
|
||||
private static function neighbourIn(array $siblings, CatalogItem $item, MoveDirection $direction): ?CatalogItem
|
||||
{
|
||||
$candidates = array_filter($siblings, fn (CatalogItem $sibling): bool => $direction === MoveDirection::Up
|
||||
? $sibling->sortOrder() < $item->sortOrder()
|
||||
: $sibling->sortOrder() > $item->sortOrder());
|
||||
|
||||
usort($candidates, fn (CatalogItem $a, CatalogItem $b): int => $direction === MoveDirection::Up
|
||||
? $b->sortOrder() <=> $a->sortOrder()
|
||||
: $a->sortOrder() <=> $b->sortOrder());
|
||||
|
||||
return isset($candidates[0]) ? clone $candidates[0] : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, CatalogItem> $siblings
|
||||
*/
|
||||
private static function nameTakenIn(array $siblings, string $name, ?int $exceptId): bool
|
||||
{
|
||||
foreach ($siblings as $id => $sibling) {
|
||||
if ($id !== $exceptId && mb_strtolower($sibling->name()->value) === mb_strtolower($name)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, CatalogItem> $siblings
|
||||
*/
|
||||
private static function nextIn(array $siblings): int
|
||||
{
|
||||
return array_reduce($siblings, fn (int $max, CatalogItem $item): int => max($max, $item->sortOrder()), 0) + 1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Fakes;
|
||||
|
||||
use App\Modules\Catalog\Domain\Contracts\ServiceUnitRepository;
|
||||
use App\Modules\Catalog\Domain\Entities\CatalogItem;
|
||||
use App\Modules\Catalog\Domain\Entities\ServiceUnit;
|
||||
use App\Modules\Catalog\Domain\Enums\MoveDirection;
|
||||
|
||||
class InMemoryServiceUnitRepository implements ServiceUnitRepository
|
||||
{
|
||||
/** @var array<int, ServiceUnit> */
|
||||
public array $items = [];
|
||||
|
||||
private int $nextId = 1;
|
||||
|
||||
public function findById(int $id): ?ServiceUnit
|
||||
{
|
||||
return isset($this->items[$id]) ? clone $this->items[$id] : null;
|
||||
}
|
||||
|
||||
public function save(ServiceUnit $serviceUnit): ServiceUnit
|
||||
{
|
||||
$id = $serviceUnit->id() ?? $this->nextId++;
|
||||
$this->items[$id] = ServiceUnit::reconstitute($id, $serviceUnit->name(), $serviceUnit->isShared(), $serviceUnit->isActive(), $serviceUnit->sortOrder());
|
||||
|
||||
return clone $this->items[$id];
|
||||
}
|
||||
|
||||
public function isNameTaken(string $name, ?int $exceptId = null): bool
|
||||
{
|
||||
return self::nameTakenIn($this->items, $name, $exceptId);
|
||||
}
|
||||
|
||||
public function nextSortOrder(): int
|
||||
{
|
||||
return self::nextIn($this->items);
|
||||
}
|
||||
|
||||
public function neighbour(ServiceUnit $serviceUnit, MoveDirection $direction): ?ServiceUnit
|
||||
{
|
||||
return self::neighbourIn($this->items, $serviceUnit, $direction);
|
||||
}
|
||||
|
||||
/**
|
||||
* @template T of CatalogItem
|
||||
*
|
||||
* @param array<int, T> $siblings
|
||||
* @return T|null
|
||||
*/
|
||||
private static function neighbourIn(array $siblings, CatalogItem $item, MoveDirection $direction): ?CatalogItem
|
||||
{
|
||||
$candidates = array_filter($siblings, fn (CatalogItem $sibling): bool => $direction === MoveDirection::Up
|
||||
? $sibling->sortOrder() < $item->sortOrder()
|
||||
: $sibling->sortOrder() > $item->sortOrder());
|
||||
|
||||
usort($candidates, fn (CatalogItem $a, CatalogItem $b): int => $direction === MoveDirection::Up
|
||||
? $b->sortOrder() <=> $a->sortOrder()
|
||||
: $a->sortOrder() <=> $b->sortOrder());
|
||||
|
||||
return isset($candidates[0]) ? clone $candidates[0] : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, CatalogItem> $siblings
|
||||
*/
|
||||
private static function nameTakenIn(array $siblings, string $name, ?int $exceptId): bool
|
||||
{
|
||||
foreach ($siblings as $id => $sibling) {
|
||||
if ($id !== $exceptId && mb_strtolower($sibling->name()->value) === mb_strtolower($name)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, CatalogItem> $siblings
|
||||
*/
|
||||
private static function nextIn(array $siblings): int
|
||||
{
|
||||
return array_reduce($siblings, fn (int $max, CatalogItem $item): int => max($max, $item->sortOrder()), 0) + 1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Fakes;
|
||||
|
||||
use App\Modules\Catalog\Domain\Contracts\WorkTypeRepository;
|
||||
use App\Modules\Catalog\Domain\Entities\CatalogItem;
|
||||
use App\Modules\Catalog\Domain\Entities\WorkType;
|
||||
use App\Modules\Catalog\Domain\Enums\MoveDirection;
|
||||
|
||||
class InMemoryWorkTypeRepository implements WorkTypeRepository
|
||||
{
|
||||
/** @var array<int, WorkType> */
|
||||
public array $items = [];
|
||||
|
||||
private int $nextId = 1;
|
||||
|
||||
public function findById(int $id): ?WorkType
|
||||
{
|
||||
return isset($this->items[$id]) ? clone $this->items[$id] : null;
|
||||
}
|
||||
|
||||
public function save(WorkType $workType): WorkType
|
||||
{
|
||||
$id = $workType->id() ?? $this->nextId++;
|
||||
$this->items[$id] = WorkType::reconstitute($id, $workType->serviceUnitId(), $workType->serviceId(), $workType->name(), $workType->isActive(), $workType->sortOrder());
|
||||
|
||||
return clone $this->items[$id];
|
||||
}
|
||||
|
||||
public function isNameTaken(int $serviceUnitId, ?int $serviceId, string $name, ?int $exceptId = null): bool
|
||||
{
|
||||
return self::nameTakenIn($this->siblings($serviceUnitId, $serviceId), $name, $exceptId);
|
||||
}
|
||||
|
||||
public function nextSortOrder(int $serviceUnitId, ?int $serviceId): int
|
||||
{
|
||||
return self::nextIn($this->siblings($serviceUnitId, $serviceId));
|
||||
}
|
||||
|
||||
public function neighbour(WorkType $workType, MoveDirection $direction): ?WorkType
|
||||
{
|
||||
return self::neighbourIn($this->siblings($workType->serviceUnitId(), $workType->serviceId()), $workType, $direction);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, WorkType>
|
||||
*/
|
||||
private function siblings(int $serviceUnitId, ?int $serviceId): array
|
||||
{
|
||||
return array_filter($this->items, fn (WorkType $workType): bool => $workType->serviceUnitId() === $serviceUnitId && $workType->serviceId() === $serviceId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @template T of CatalogItem
|
||||
*
|
||||
* @param array<int, T> $siblings
|
||||
* @return T|null
|
||||
*/
|
||||
private static function neighbourIn(array $siblings, CatalogItem $item, MoveDirection $direction): ?CatalogItem
|
||||
{
|
||||
$candidates = array_filter($siblings, fn (CatalogItem $sibling): bool => $direction === MoveDirection::Up
|
||||
? $sibling->sortOrder() < $item->sortOrder()
|
||||
: $sibling->sortOrder() > $item->sortOrder());
|
||||
|
||||
usort($candidates, fn (CatalogItem $a, CatalogItem $b): int => $direction === MoveDirection::Up
|
||||
? $b->sortOrder() <=> $a->sortOrder()
|
||||
: $a->sortOrder() <=> $b->sortOrder());
|
||||
|
||||
return isset($candidates[0]) ? clone $candidates[0] : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, CatalogItem> $siblings
|
||||
*/
|
||||
private static function nameTakenIn(array $siblings, string $name, ?int $exceptId): bool
|
||||
{
|
||||
foreach ($siblings as $id => $sibling) {
|
||||
if ($id !== $exceptId && mb_strtolower($sibling->name()->value) === mb_strtolower($name)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, CatalogItem> $siblings
|
||||
*/
|
||||
private static function nextIn(array $siblings): int
|
||||
{
|
||||
return array_reduce($siblings, fn (int $max, CatalogItem $item): int => max($max, $item->sortOrder()), 0) + 1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
<?php
|
||||
|
||||
use App\Models\User;
|
||||
use App\Modules\Catalog\Application\Contracts\CatalogQueries;
|
||||
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\ServiceModel;
|
||||
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\ServiceUnitModel;
|
||||
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\WorkTypeModel;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Livewire\Features\SupportLockedProperties\CannotUpdateLockedPropertyException;
|
||||
use Livewire\Livewire;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
beforeEach(function () {
|
||||
$this->actingAs(User::factory()->management()->create());
|
||||
$this->unit = fn (string $name) => ServiceUnitModel::query()->where('name', $name)->sole();
|
||||
});
|
||||
|
||||
it('has the starting catalog from the specification', function () {
|
||||
$tree = app(CatalogQueries::class)->tree();
|
||||
$media = $tree[0];
|
||||
|
||||
expect(array_map(fn ($node) => $node->serviceUnit->name, $tree))->toBe([
|
||||
'Mediatuotanto', 'Markkinointi', 'Painotuotanto', 'Ylläpitopalvelut', 'Verkkosivut', 'Ohjelmistot', 'Kuluttajapalvelut', 'Kaikille yhteiset',
|
||||
])
|
||||
->and(array_map(fn ($node) => $node->service->name, $media->services))->toBe(['Videotuotanto', 'Valokuvaus', 'Äänituotanto'])
|
||||
->and(array_map(fn ($workType) => $workType->name, $media->services[0]->workTypes))->toBe(['videokuvaus', 'kuvausassistentti', 'editointi', 'animointi', '3D-mallinnus'])
|
||||
->and(end($tree)->serviceUnit->isShared)->toBeTrue()
|
||||
->and(WorkTypeModel::query()->count())->toBe(29);
|
||||
});
|
||||
|
||||
it('offers the work types of the chosen units and the shared ones, but not inactive ones', function () {
|
||||
$marketing = ($this->unit)('Markkinointi');
|
||||
WorkTypeModel::query()->where('name', 'kampanjatyö')->update(['is_active' => false]);
|
||||
|
||||
$nodes = app(CatalogQueries::class)->activeWorkTypesFor([$marketing->id]);
|
||||
|
||||
expect(array_map(fn ($node) => $node->serviceUnit->name, $nodes))->toBe(['Markkinointi', 'Kaikille yhteiset'])
|
||||
->and(array_map(fn ($workType) => $workType->name, $nodes[0]->workTypes))->toBe(['graafinen työ', 'julkaisutyö'])
|
||||
->and(array_map(fn ($unit) => $unit->name, app(CatalogQueries::class)->activeServiceUnits()))->not->toContain('Kaikille yhteiset');
|
||||
});
|
||||
|
||||
it('shows the catalog page to admins and management only', function (string $role, int $status) {
|
||||
$this->actingAs(User::factory()->{$role}()->create())->get(route('catalog.index'))->assertStatus($status);
|
||||
})->with([
|
||||
'admin' => ['admin', 200],
|
||||
'management' => ['management', 200],
|
||||
'employee' => ['employee', 403],
|
||||
'intern' => ['intern', 403],
|
||||
'subcontractor' => ['subcontractor', 403],
|
||||
]);
|
||||
|
||||
it('shows the menu link only to those who may manage the catalog', function () {
|
||||
$this->get(route('customers.index'))->assertSee('Pohjatiedot');
|
||||
$this->actingAs(User::factory()->create())->get(route('customers.index'))->assertDontSee('Pohjatiedot');
|
||||
});
|
||||
|
||||
it('adds a service unit, a service and a work type', function () {
|
||||
Livewire::test('catalog::service-unit-form')
|
||||
->set('form.name', 'Tapahtumat')
|
||||
->call('save')
|
||||
->assertHasNoErrors()
|
||||
->assertRedirect(route('catalog.index'));
|
||||
|
||||
$unit = ($this->unit)('Tapahtumat');
|
||||
|
||||
Livewire::test('catalog::service-form', ['serviceUnit' => $unit->id])
|
||||
->set('form.name', 'Messut')
|
||||
->call('save')
|
||||
->assertHasNoErrors();
|
||||
|
||||
$service = ServiceModel::query()->where('name', 'Messut')->sole();
|
||||
|
||||
Livewire::test('catalog::work-type-form', ['serviceUnit' => $unit->id])
|
||||
->set('form.name', 'osastosuunnittelu')
|
||||
->set('form.service_id', (string) $service->id)
|
||||
->call('save')
|
||||
->assertHasNoErrors();
|
||||
|
||||
expect(WorkTypeModel::query()->where('name', 'osastosuunnittelu')->sole()->service_id)->toBe($service->id);
|
||||
});
|
||||
|
||||
it('shows a taken name as a Finnish field error', function () {
|
||||
Livewire::test('catalog::service-unit-form')
|
||||
->set('form.name', 'markkinointi')
|
||||
->call('save')
|
||||
->assertHasErrors(['form.name'])
|
||||
->assertSee('Samalla tasolla on jo tämänniminen rivi.');
|
||||
});
|
||||
|
||||
it('moves and deactivates rows on the catalog page', function () {
|
||||
$marketing = ($this->unit)('Markkinointi');
|
||||
|
||||
Livewire::test('catalog::index')
|
||||
->call('move', 'service_unit', $marketing->id, 'up')
|
||||
->assertSee('Markkinointi siirretty ylöspäin.')
|
||||
->call('move', 'service_unit', $marketing->id, 'up')
|
||||
->assertSee('Markkinointi on jo ensimmäisenä.')
|
||||
->call('toggleActive', 'service_unit', $marketing->id)
|
||||
->assertSee('Markkinointi poistettu käytöstä.')
|
||||
->set('showInactive', false)
|
||||
->assertDontSee('graafinen työ');
|
||||
|
||||
expect($marketing->fresh()->sort_order)->toBe(1)
|
||||
->and($marketing->fresh()->is_active)->toBeFalse();
|
||||
});
|
||||
|
||||
it('returns 404 for unknown items and does not add services to the shared unit', function () {
|
||||
$shared = ($this->unit)('Kaikille yhteiset');
|
||||
|
||||
$this->get(route('catalog.service-units.edit', 999))->assertNotFound();
|
||||
$this->get(route('catalog.work-types.edit', 999))->assertNotFound();
|
||||
$this->get(route('catalog.services.create', $shared->id))->assertNotFound();
|
||||
});
|
||||
|
||||
it('does not let the edited item be changed in the browser', function () {
|
||||
Livewire::test('catalog::work-type-form', ['serviceUnit' => ($this->unit)('Markkinointi')->id])
|
||||
->set('workTypeId', 1);
|
||||
})->throws(CannotUpdateLockedPropertyException::class);
|
||||
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
|
||||
use App\Modules\Catalog\Application\UseCases\AddService;
|
||||
use App\Modules\Catalog\Application\UseCases\AddServiceUnit;
|
||||
use App\Modules\Catalog\Application\UseCases\AddWorkType;
|
||||
use App\Modules\Catalog\Application\UseCases\MoveCatalogItem;
|
||||
use App\Modules\Catalog\Application\UseCases\RenameCatalogItem;
|
||||
use App\Modules\Catalog\Application\UseCases\SetCatalogItemActive;
|
||||
use App\Modules\Catalog\Application\UseCases\UpdateWorkType;
|
||||
use App\Modules\Catalog\Domain\Enums\CatalogLevel;
|
||||
use App\Modules\Catalog\Domain\Enums\MoveDirection;
|
||||
use App\Modules\Catalog\Domain\Exceptions\CatalogItemNotFound;
|
||||
use App\Modules\Catalog\Domain\Exceptions\CatalogNameTaken;
|
||||
use App\Modules\Catalog\Domain\Exceptions\ServiceNotInUnit;
|
||||
use Tests\Fakes\FakeTransactionManager;
|
||||
use Tests\Fakes\InMemoryServiceRepository;
|
||||
use Tests\Fakes\InMemoryServiceUnitRepository;
|
||||
use Tests\Fakes\InMemoryWorkTypeRepository;
|
||||
|
||||
beforeEach(function () {
|
||||
$this->units = new InMemoryServiceUnitRepository;
|
||||
$this->services = new InMemoryServiceRepository;
|
||||
$this->workTypes = new InMemoryWorkTypeRepository;
|
||||
$transactions = new FakeTransactionManager;
|
||||
|
||||
$this->addUnit = new AddServiceUnit($this->units, $transactions);
|
||||
$this->addService = new AddService($this->units, $this->services, $transactions);
|
||||
$this->addWorkType = new AddWorkType($this->units, $this->services, $this->workTypes, $transactions);
|
||||
$this->rename = new RenameCatalogItem($this->units, $this->services, $transactions);
|
||||
$this->updateWorkType = new UpdateWorkType($this->units, $this->services, $this->workTypes, $transactions);
|
||||
$this->setActive = new SetCatalogItemActive($this->units, $this->services, $this->workTypes, $transactions);
|
||||
$this->move = new MoveCatalogItem($this->units, $this->services, $this->workTypes, $transactions);
|
||||
|
||||
$this->media = $this->addUnit->execute('Mediatuotanto');
|
||||
$this->marketing = $this->addUnit->execute('Markkinointi');
|
||||
$this->video = $this->addService->execute($this->media, 'Videotuotanto');
|
||||
$this->photo = $this->addService->execute($this->media, 'Valokuvaus');
|
||||
});
|
||||
|
||||
it('adds items last among their siblings', function () {
|
||||
expect($this->units->findById($this->marketing)?->sortOrder())->toBe(2)
|
||||
->and($this->services->findById($this->photo)?->sortOrder())->toBe(2);
|
||||
});
|
||||
|
||||
it('rejects a name already used on the same level, ignoring case', function () {
|
||||
$this->addWorkType->execute($this->media, $this->video, 'editointi');
|
||||
|
||||
$this->addWorkType->execute($this->media, $this->video, 'Editointi');
|
||||
})->throws(CatalogNameTaken::class);
|
||||
|
||||
it('allows the same work type name under another service or unit', function () {
|
||||
$this->addWorkType->execute($this->media, $this->video, 'editointi');
|
||||
$this->addWorkType->execute($this->media, $this->photo, 'editointi');
|
||||
$this->addWorkType->execute($this->marketing, null, 'editointi');
|
||||
|
||||
expect($this->workTypes->items)->toHaveCount(3);
|
||||
});
|
||||
|
||||
it('rejects a service of another unit', function () {
|
||||
$this->addWorkType->execute($this->marketing, $this->video, 'editointi');
|
||||
})->throws(ServiceNotInUnit::class);
|
||||
|
||||
it('renames and rejects a taken name', function () {
|
||||
$this->rename->execute(CatalogLevel::Service, $this->video, 'Video');
|
||||
|
||||
expect($this->services->findById($this->video)?->name()->value)->toBe('Video')
|
||||
->and(fn () => $this->rename->execute(CatalogLevel::ServiceUnit, $this->media, 'markkinointi'))->toThrow(CatalogNameTaken::class);
|
||||
});
|
||||
|
||||
it('moves a work type to another service, last there', function () {
|
||||
$this->addWorkType->execute($this->media, $this->photo, 'kuvaus');
|
||||
$editing = $this->addWorkType->execute($this->media, $this->video, 'editointi');
|
||||
|
||||
$this->updateWorkType->execute($editing, $this->photo, 'editointi');
|
||||
|
||||
$workType = $this->workTypes->findById($editing);
|
||||
expect($workType?->serviceId())->toBe($this->photo)
|
||||
->and($workType?->sortOrder())->toBe(2);
|
||||
});
|
||||
|
||||
it('moves items up and down, and does nothing at either end', function () {
|
||||
expect($this->move->execute(CatalogLevel::ServiceUnit, $this->marketing, MoveDirection::Up))->toBeTrue()
|
||||
->and($this->units->findById($this->marketing)?->sortOrder())->toBe(1)
|
||||
->and($this->units->findById($this->media)?->sortOrder())->toBe(2)
|
||||
->and($this->move->execute(CatalogLevel::ServiceUnit, $this->marketing, MoveDirection::Up))->toBeFalse()
|
||||
->and($this->move->execute(CatalogLevel::Service, $this->photo, MoveDirection::Down))->toBeFalse();
|
||||
});
|
||||
|
||||
it('takes items out of use and back', function () {
|
||||
$this->setActive->execute(CatalogLevel::Service, $this->video, false);
|
||||
expect($this->services->findById($this->video)?->isActive())->toBeFalse();
|
||||
|
||||
$this->setActive->execute(CatalogLevel::Service, $this->video, true);
|
||||
expect($this->services->findById($this->video)?->isActive())->toBeTrue();
|
||||
});
|
||||
|
||||
it('fails for an unknown item', function () {
|
||||
$this->setActive->execute(CatalogLevel::WorkType, 999, false);
|
||||
})->throws(CatalogItemNotFound::class);
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
use App\Modules\Catalog\Domain\Entities\Service;
|
||||
use App\Modules\Catalog\Domain\Entities\ServiceUnit;
|
||||
use App\Modules\Catalog\Domain\Entities\WorkType;
|
||||
use App\Modules\Catalog\Domain\Exceptions\InvalidCatalogName;
|
||||
use App\Modules\Catalog\Domain\Exceptions\ServiceNotInUnit;
|
||||
use App\Modules\Catalog\Domain\Exceptions\SharedUnitHasNoServices;
|
||||
use App\Modules\Catalog\Domain\ValueObjects\CatalogName;
|
||||
|
||||
it('normalises a catalog name and rejects an empty or too long one', function () {
|
||||
expect((new CatalogName(' 3D mallinnus '))->value)->toBe('3D mallinnus')
|
||||
->and(fn () => new CatalogName(' '))->toThrow(InvalidCatalogName::class)
|
||||
->and(fn () => new CatalogName(str_repeat('a', 101)))->toThrow(InvalidCatalogName::class);
|
||||
});
|
||||
|
||||
it('gives the shared unit no services', function () {
|
||||
$shared = ServiceUnit::reconstitute(1, new CatalogName('Kaikille yhteiset'), true, true, 1);
|
||||
|
||||
Service::create($shared, new CatalogName('Palvelu'), 1);
|
||||
})->throws(SharedUnitHasNoServices::class);
|
||||
|
||||
it('puts a work type only under a service of its own unit', function () {
|
||||
$media = ServiceUnit::reconstitute(1, new CatalogName('Mediatuotanto'), false, true, 1);
|
||||
$marketing = ServiceUnit::reconstitute(2, new CatalogName('Markkinointi'), false, true, 2);
|
||||
$video = Service::reconstitute(10, 1, new CatalogName('Videotuotanto'), true, 1);
|
||||
|
||||
expect(WorkType::create($media, $video, new CatalogName('editointi'), 1)->serviceId())->toBe(10)
|
||||
->and(fn () => WorkType::create($marketing, $video, new CatalogName('editointi'), 1))->toThrow(ServiceNotInUnit::class);
|
||||
});
|
||||
|
||||
it('deactivates, activates and swaps order', function () {
|
||||
$first = ServiceUnit::reconstitute(1, new CatalogName('A'), false, true, 1);
|
||||
$second = ServiceUnit::reconstitute(2, new CatalogName('B'), false, true, 2);
|
||||
|
||||
$first->deactivate();
|
||||
$first->swapOrderWith($second);
|
||||
|
||||
expect($first->isActive())->toBeFalse()
|
||||
->and($first->sortOrder())->toBe(2)
|
||||
->and($second->sortOrder())->toBe(1);
|
||||
|
||||
$first->activate();
|
||||
expect($first->isActive())->toBeTrue();
|
||||
});
|
||||
Reference in New Issue
Block a user