feat: exclusion groups for additional services
This commit is contained in:
@@ -0,0 +1,128 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Service;
|
||||
|
||||
use App\Entity\Groups\AdditionalService;
|
||||
use App\Service\AdditionalServiceExclusionResolver;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class AdditionalServiceExclusionResolverTest extends TestCase
|
||||
{
|
||||
private const int BASEMENT = 1;
|
||||
private const int FIRST_FLOOR = 2;
|
||||
private const int SECOND_FLOOR = 3;
|
||||
private const int FULL = 4;
|
||||
private const int BREAKFAST = 5;
|
||||
|
||||
public function testSelectingTheExclusiveServiceClearsTheOtherGroupMembers(): void
|
||||
{
|
||||
$resolved = $this->resolve(
|
||||
[self::BASEMENT, self::FIRST_FLOOR, self::FULL],
|
||||
[self::BASEMENT, self::FIRST_FLOOR],
|
||||
);
|
||||
|
||||
self::assertSame([self::FULL], array_values($resolved));
|
||||
}
|
||||
|
||||
public function testSelectingANonExclusiveServiceClearsOnlyTheExclusiveOne(): void
|
||||
{
|
||||
$resolved = $this->resolve(
|
||||
[self::FULL, self::BASEMENT],
|
||||
[self::FULL],
|
||||
);
|
||||
|
||||
self::assertSame([self::BASEMENT], array_values($resolved));
|
||||
}
|
||||
|
||||
public function testNonExclusiveGroupMembersCanBeCombined(): void
|
||||
{
|
||||
$resolved = $this->resolve(
|
||||
[self::BASEMENT, self::FIRST_FLOOR, self::SECOND_FLOOR],
|
||||
[self::BASEMENT],
|
||||
);
|
||||
|
||||
self::assertSame(
|
||||
[self::BASEMENT, self::FIRST_FLOOR, self::SECOND_FLOOR],
|
||||
array_values($resolved),
|
||||
);
|
||||
}
|
||||
|
||||
public function testServicesWithoutAnExclusionGroupAreNeverCleared(): void
|
||||
{
|
||||
$resolved = $this->resolve(
|
||||
[self::BREAKFAST, self::BASEMENT, self::FULL],
|
||||
[self::BREAKFAST, self::BASEMENT],
|
||||
);
|
||||
|
||||
self::assertSame([self::BREAKFAST, self::FULL], array_values($resolved));
|
||||
}
|
||||
|
||||
public function testUnchangedSelectionIsLeftAlone(): void
|
||||
{
|
||||
$resolved = $this->resolve(
|
||||
[self::BASEMENT, self::FULL],
|
||||
[self::BASEMENT, self::FULL],
|
||||
);
|
||||
|
||||
self::assertSame([self::BASEMENT, self::FULL], array_values($resolved));
|
||||
}
|
||||
|
||||
public function testSubmittedArrayKeysArePreserved(): void
|
||||
{
|
||||
$resolver = new AdditionalServiceExclusionResolver();
|
||||
|
||||
$resolved = $resolver->resolve(
|
||||
$this->services(),
|
||||
['Reinigung' => self::FULL, 'Verpflegung' => self::BREAKFAST, 0 => self::BASEMENT],
|
||||
[self::BASEMENT],
|
||||
);
|
||||
|
||||
self::assertSame(
|
||||
['Reinigung' => self::FULL, 'Verpflegung' => self::BREAKFAST],
|
||||
$resolved,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<int> $submittedIds
|
||||
* @param list<int> $previousIds
|
||||
*
|
||||
* @return array<array-key, int>
|
||||
*/
|
||||
private function resolve(array $submittedIds, array $previousIds): array
|
||||
{
|
||||
return (new AdditionalServiceExclusionResolver())->resolve(
|
||||
$this->services(),
|
||||
$submittedIds,
|
||||
$previousIds,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return AdditionalService[]
|
||||
*/
|
||||
private function services(): array
|
||||
{
|
||||
return [
|
||||
$this->service(self::BASEMENT, 'Reinigung', false),
|
||||
$this->service(self::FIRST_FLOOR, 'Reinigung', false),
|
||||
$this->service(self::SECOND_FLOOR, 'Reinigung', false),
|
||||
$this->service(self::FULL, 'Reinigung', true),
|
||||
$this->service(self::BREAKFAST, null, false),
|
||||
];
|
||||
}
|
||||
|
||||
private function service(int $id, ?string $exclusionGroup, bool $isExclusive): AdditionalService
|
||||
{
|
||||
$service = new AdditionalService();
|
||||
$service->setExclusionGroup($exclusionGroup);
|
||||
$service->setIsExclusive($isExclusive);
|
||||
|
||||
$property = new \ReflectionProperty(AdditionalService::class, 'id');
|
||||
$property->setValue($service, $id);
|
||||
|
||||
return $service;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user