116 lines
3.5 KiB
PHP
116 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Tests\Validator;
|
|
|
|
use App\Entity\Bucket;
|
|
use App\Entity\Scenario;
|
|
use App\Enum\BucketAllocationType;
|
|
use App\Enum\BucketType;
|
|
use App\Validator\BufferOnlyForFixedLimit;
|
|
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
|
use Symfony\Component\Validator\ConstraintViolationListInterface;
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface;
|
|
|
|
final class BufferOnlyForFixedLimitTest extends KernelTestCase
|
|
{
|
|
private ValidatorInterface $validator;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
self::bootKernel();
|
|
$this->validator = self::getContainer()->get(ValidatorInterface::class);
|
|
}
|
|
|
|
public function testFixedLimitBucketWithNonZeroBufferIsValid(): void
|
|
{
|
|
$bucket = $this->makeBucket(BucketType::NEED, BucketAllocationType::FIXED_LIMIT, 50000, '1.50');
|
|
|
|
$violations = $this->validator->validate($bucket);
|
|
|
|
$matching = $this->violationsAt($violations, 'bufferMultiplier', BufferOnlyForFixedLimit::class);
|
|
|
|
self::assertCount(
|
|
0,
|
|
$matching,
|
|
'FIXED_LIMIT bucket with a non-zero buffer must not raise BufferOnlyForFixedLimit.',
|
|
);
|
|
}
|
|
|
|
public function testUnlimitedBucketWithNonZeroBufferIsInvalid(): void
|
|
{
|
|
$bucket = $this->makeBucket(BucketType::OVERFLOW, BucketAllocationType::UNLIMITED, null, '2.00');
|
|
|
|
$violations = $this->validator->validate($bucket);
|
|
|
|
$matching = $this->violationsAt($violations, 'bufferMultiplier', BufferOnlyForFixedLimit::class);
|
|
|
|
self::assertNotCount(
|
|
0,
|
|
$matching,
|
|
'UNLIMITED bucket with a non-zero buffer must raise a violation on bufferMultiplier.',
|
|
);
|
|
}
|
|
|
|
public function testNegativeBufferIsInvalid(): void
|
|
{
|
|
$bucket = $this->makeBucket(BucketType::NEED, BucketAllocationType::FIXED_LIMIT, 50000, '-1.00');
|
|
|
|
$violations = $this->validator->validate($bucket);
|
|
|
|
$matching = $this->violationsAt($violations, 'bufferMultiplier');
|
|
|
|
self::assertNotCount(
|
|
0,
|
|
$matching,
|
|
'A negative bufferMultiplier must raise a violation on bufferMultiplier (GreaterThanOrEqual(0)).',
|
|
);
|
|
}
|
|
|
|
private function makeBucket(
|
|
BucketType $type,
|
|
BucketAllocationType $allocationType,
|
|
?int $allocationValue,
|
|
string $bufferMultiplier,
|
|
): Bucket {
|
|
$scenario = new Scenario();
|
|
$scenario->setName('Household Budget');
|
|
|
|
$bucket = new Bucket();
|
|
$bucket->setScenario($scenario);
|
|
$bucket->setType($type);
|
|
$bucket->setName('Test Bucket');
|
|
$bucket->setPriority(1);
|
|
$bucket->setSortOrder(0);
|
|
$bucket->setAllocationType($allocationType);
|
|
$bucket->setAllocationValue($allocationValue);
|
|
$bucket->setStartingAmount(0);
|
|
$bucket->setBufferMultiplier($bufferMultiplier);
|
|
|
|
return $bucket;
|
|
}
|
|
|
|
/**
|
|
* @return list<\Symfony\Component\Validator\ConstraintViolationInterface>
|
|
*/
|
|
private function violationsAt(
|
|
ConstraintViolationListInterface $violations,
|
|
string $propertyPath,
|
|
?string $constraintClass = null,
|
|
): array {
|
|
$matching = [];
|
|
|
|
foreach ($violations as $violation) {
|
|
if ($violation->getPropertyPath() !== $propertyPath) {
|
|
continue;
|
|
}
|
|
|
|
if (null !== $constraintClass && !$violation->getConstraint() instanceof $constraintClass) {
|
|
continue;
|
|
}
|
|
|
|
$matching[] = $violation;
|
|
}
|
|
|
|
return $matching;
|
|
}
|
|
}
|