buckets/tests/Validator/BufferOnlyForFixedLimitValidatorTest.php

102 lines
3.4 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 App\Validator\BufferOnlyForFixedLimitValidator;
use Symfony\Component\Validator\ConstraintValidatorInterface;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
/**
* @extends ConstraintValidatorTestCase<BufferOnlyForFixedLimitValidator>
*/
final class BufferOnlyForFixedLimitValidatorTest extends ConstraintValidatorTestCase
{
protected function createValidator(): ConstraintValidatorInterface
{
return new BufferOnlyForFixedLimitValidator();
}
public function testFixedLimitWithNonZeroBufferIsValid(): void
{
$bucket = $this->makeBucket(BucketAllocationType::FIXED_LIMIT, '1.50');
$this->validator->validate($bucket, new BufferOnlyForFixedLimit());
$this->assertNoViolation();
}
public function testFixedLimitWithZeroBufferIsValid(): void
{
$bucket = $this->makeBucket(BucketAllocationType::FIXED_LIMIT, '0.00');
$this->validator->validate($bucket, new BufferOnlyForFixedLimit());
$this->assertNoViolation();
}
public function testPercentageWithNonZeroBufferIsInvalid(): void
{
$bucket = $this->makeBucket(BucketAllocationType::PERCENTAGE, '1.50');
$this->validator->validate($bucket, new BufferOnlyForFixedLimit());
$this->buildViolation('The buffer multiplier can only be set for fixed-limit buckets.')
->atPath('property.path.bufferMultiplier')
->setCode(BufferOnlyForFixedLimit::BUFFER_NOT_ALLOWED_ERROR)
->assertRaised();
}
public function testUnlimitedWithNonZeroBufferIsInvalid(): void
{
$bucket = $this->makeBucket(BucketAllocationType::UNLIMITED, '2.00');
$this->validator->validate($bucket, new BufferOnlyForFixedLimit());
$this->buildViolation('The buffer multiplier can only be set for fixed-limit buckets.')
->atPath('property.path.bufferMultiplier')
->setCode(BufferOnlyForFixedLimit::BUFFER_NOT_ALLOWED_ERROR)
->assertRaised();
}
public function testPercentageWithZeroBufferIsValid(): void
{
$bucket = $this->makeBucket(BucketAllocationType::PERCENTAGE, '0.00');
$this->validator->validate($bucket, new BufferOnlyForFixedLimit());
$this->assertNoViolation();
}
public function testPercentageWithZeroBufferWithoutDecimalsIsValid(): void
{
$bucket = $this->makeBucket(BucketAllocationType::PERCENTAGE, '0');
$this->validator->validate($bucket, new BufferOnlyForFixedLimit());
$this->assertNoViolation();
}
private function makeBucket(BucketAllocationType $allocationType, string $bufferMultiplier): Bucket
{
$scenario = new Scenario();
$scenario->setName('Household Budget');
$bucket = new Bucket();
$bucket->setScenario($scenario);
$bucket->setType(BucketType::NEED);
$bucket->setName('Test Bucket');
$bucket->setPriority(1);
$bucket->setSortOrder(0);
$bucket->setAllocationType($allocationType);
$bucket->setAllocationValue(BucketAllocationType::FIXED_LIMIT === $allocationType ? 50000 : null);
$bucket->setStartingAmount(0);
$bucket->setBufferMultiplier($bufferMultiplier);
return $bucket;
}
}