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; } }